def test_mobilenet_v3_like_search_gradients(self): model_spec = mobile_search_space_v3.mobilenet_v3_like_search() model_spec = schema.map_oneofs(_with_largest_possible_masks, model_spec) model = mobile_model_v3.get_model( model_spec, num_classes=1001, force_stateless_batch_norm=True) inputs = tf.random_normal(shape=[8, 224, 224, 3], dtype=tf.float32) model.build(inputs.shape) logits, unused_endpoints = model.apply(inputs, training=True) labels = tf.one_hot([0]*8, 1001, dtype=tf.float32) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)) variables = model.trainable_variables() grads = tf.gradients(loss, variables) for variable, grad in zip(variables, grads): self.assertIsNotNone( grad, msg='Gradient for {} is None'.format(variable.name)) self.evaluate(tf.global_variables_initializer()) for variable, array in zip(variables, self.evaluate(grads)): self.assertFalse( np.all(np.equal(array, 0)), msg='Gradient for {} is identically zero'.format(variable.name))
def test_search_space_construction(self, ssd): model_spec = mobile_search_space_v3.get_search_space_spec(ssd) model_spec = test_utils.with_random_masks(model_spec) model = mobile_model_v3.get_model( model_spec, num_classes=1001, force_stateless_batch_norm=True) inputs = tf.random_normal(shape=[128, 224, 224, 3]) model.build(inputs.shape) output, unused_endpoints = model.apply(inputs, training=True) self.assertEqual(output.shape, [128, 1001])
def test_dynamic_input_size(self, ssd): model_spec = mobile_search_space_v3.get_search_space_spec(ssd) model_spec = test_utils.with_random_pruning(model_spec) model = mobile_model_v3.get_model( model_spec, num_classes=1001, force_stateless_batch_norm=False) # Input height/width are not known at graph construction time. inputs = tf.placeholder(shape=[128, None, None, 3], dtype=tf.float32) model.build(inputs.shape) output, unused_endpoints = model.apply(inputs, training=True) self.assertEqual(output.shape, [128, 1001])
def test_output_shapes(self): model_spec = mobile_search_space_v3.mobilenet_v3_large() model = mobile_model_v3.get_model(model_spec, num_classes=50) features = tf.ones([8, 224, 224, 3]) model.build(features.shape) logits, endpoints = model.apply(features, training=True) self.assertEqual(logits.shape, tf.TensorShape([8, 50])) self.assertEqual([x.shape for x in endpoints], [ [8, 112, 112, 16], [8, 56, 56, 24], [8, 28, 28, 40], [8, 14, 14, 112], [8, 7, 7, 160], ])
def test_output_shapes_with_variable_kernel_sizes(self): model_spec = mobile_search_space_v3.mobilenet_v3_like_search() model_spec = test_utils.with_random_masks(model_spec) model = mobile_model_v3.get_model( model_spec, num_classes=50, force_stateless_batch_norm=True) features = tf.ones([8, 224, 224, 3]) model.build(features.shape) logits, endpoints = model.apply(features, training=True) self.assertEqual(logits.shape, tf.TensorShape([8, 50])) self.assertLen(endpoints, 5) endpoints[0].shape.assert_is_compatible_with([8, 112, 112, None]) endpoints[1].shape.assert_is_compatible_with([8, 56, 56, None]) endpoints[2].shape.assert_is_compatible_with([8, 28, 28, None]) endpoints[3].shape.assert_is_compatible_with([8, 14, 14, None]) endpoints[4].shape.assert_is_compatible_with([8, 7, 7, None])
def get_standalone_model(model_spec, **kwargs): """Create a standalone model based on `model_spec`. Args: model_spec: basic_specs.ConvTowerSpec namedtuple. **kwargs: Additional keyword arguments to pass to the model builder. Returns: An instance of rematlib/layers.Layer. The layer's apply() function will return a tuple `(output, endpoints)`, where `output` is a Tensor and `endpoints` is a list of Tensors for object detection. Raises: ValueError: If `ssd` is not recognized. """ return mobile_model_v3.get_model(model_spec=model_spec, force_stateless_batch_norm=False, **kwargs)
def get_model_for_search(model_spec, **kwargs): """Create a one-shot/weight-shared model based on `model_spec`. NOTE: We will always force the selected algorithm to run with stateless batch normalization. This makes the method suitable for architecture searches but not stand-alone model training. Args: model_spec: basic_specs.ConvTowerSpec namedtuple. **kwargs: Additional keyword arguments to pass to the model builder. Returns: An instance of rematlib/layers.Layer. The layer's apply() function will return a tuple `(output, endpoints)`, where `output` is a Tensor and `endpoints` is a list of Tensors for object detection. """ return mobile_model_v3.get_model(model_spec=model_spec, force_stateless_batch_norm=True, **kwargs)
def test_output_shapes_with_variable_filter_sizes(self): filter_multipliers = (0.5, 1.0, 2.0) model_spec = mobile_search_space_v3.mobilenet_v3_large() model_spec = search_space_utils.scale_conv_tower_spec( model_spec, multipliers=filter_multipliers) model_spec = test_utils.with_random_masks(model_spec) model = mobile_model_v3.get_model(model_spec, num_classes=50) features = tf.ones([8, 224, 224, 3]) model.build(features.shape) logits, endpoints = model.apply(features, training=True) self.assertEqual(logits.shape, tf.TensorShape([8, 50])) self.assertEqual([x.shape for x in endpoints], [ [8, 112, 112, int(16 * max(filter_multipliers))], [8, 56, 56, int(24 * max(filter_multipliers))], [8, 28, 28, int(40 * max(filter_multipliers))], [8, 14, 14, int(112 * max(filter_multipliers))], [8, 7, 7, int(160 * max(filter_multipliers))], ])