You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

  1. # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Test utility functions for manipulating Keras models."""
  16. import tensorflow as tf
  17. from object_detection.utils import model_util
  18. class ExtractSubmodelUtilTest(tf.test.TestCase):
  19. def test_simple_model(self):
  20. inputs = tf.keras.Input(shape=(256,)) # Returns a placeholder tensor
  21. # A layer instance is callable on a tensor, and returns a tensor.
  22. x = tf.keras.layers.Dense(128, activation='relu', name='a')(inputs)
  23. x = tf.keras.layers.Dense(64, activation='relu', name='b')(x)
  24. x = tf.keras.layers.Dense(32, activation='relu', name='c')(x)
  25. x = tf.keras.layers.Dense(16, activation='relu', name='d')(x)
  26. x = tf.keras.layers.Dense(8, activation='relu', name='e')(x)
  27. predictions = tf.keras.layers.Dense(10, activation='softmax')(x)
  28. model = tf.keras.Model(inputs=inputs, outputs=predictions)
  29. new_in = model.get_layer(
  30. name='b').input
  31. new_out = model.get_layer(
  32. name='d').output
  33. new_model = model_util.extract_submodel(
  34. model=model,
  35. inputs=new_in,
  36. outputs=new_out)
  37. batch_size = 3
  38. ones = tf.ones((batch_size, 128))
  39. final_out = new_model(ones)
  40. self.assertAllEqual(final_out.shape, (batch_size, 16))
  41. if __name__ == '__main__':
  42. tf.test.main()