def test_get_model_for_search_with_v3_model(self, dropout_rate): model_spec = mobile_search_space_v3.get_search_space_spec( mobile_search_space_v3.MOBILENET_V3_LARGE) model = mobile_classifier_factory.get_model_for_search( model_spec, dropout_rate=dropout_rate) train_inputs = tf.ones([1, 224, 224, 3]) model.build(train_inputs.shape) train_output, _ = model.apply(train_inputs, training=True) train_regularization_loss = model.regularization_loss() trainable_variables = model.trainable_variables() self.assertEqual(train_output.shape.as_list(), [1, 1001]) self.assertEqual(train_regularization_loss.shape.as_list(), []) self.assertEqual(train_regularization_loss.dtype, train_inputs.dtype) valid_inputs = tf.ones([2, 224, 224, 3]) valid_output, _ = model.apply(valid_inputs, training=False) valid_regularization_loss = model.regularization_loss() self.assertEqual(valid_output.shape.as_list(), [2, 1001]) self.assertEqual(valid_regularization_loss.shape.as_list(), []) self.assertEqual(valid_regularization_loss.dtype, valid_inputs.dtype) # No trainable variables should be created during the validation pass. self.assertEqual( trainable_variables, model.trainable_variables())
def test_get_standalone_model_v3(self): ssd = mobile_search_space_v3.MOBILENET_V3_LARGE model_spec = mobile_search_space_v3.get_search_space_spec(ssd) model = mobile_classifier_factory.get_standalone_model(model_spec) inputs = tf.ones([2, 224, 224, 3]) model.build(inputs.shape) outputs, _ = model.apply(inputs, training=False) self.assertEqual(outputs.shape, [2, 1001])
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 get_model_spec(ssd, filters=None, op_indices=None, indices=None, filters_multipliers=1.0, path_dropout_rate=0.0, training=None): """Get a model spec namedtuple for the given search space definition. Args: ssd: Search space definition to use. filters: List of filter sizes to use. Required for V2 search spaces. op_indices: List of integers specifying the operations to select. indices: List of integers specifying the values to use for all the operations in the search space. If specified, this will override op_indices and filters. filters_multipliers: Single value or a list of possible values, used to scale up/down the number of filters in each layer of the network. path_dropout_rate: Rate of path dropout to use during stand-alone training. training: Boolean. Only needs to be specified if path_dropout_rate > 0. Returns: A basic_specs.ConvTowerSpec namedtuple. """ if ssd in mobile_search_space_v3.ALL_SSDS: model_spec = mobile_search_space_v3.get_search_space_spec(ssd) else: raise ValueError('Unsupported SSD: {}'.format(ssd)) if indices: genotype = indices else: genotype = dict() if op_indices: genotype[basic_specs.OP_TAG] = op_indices if filters and ssd in mobile_search_space_v3.ALL_SSDS: genotype[basic_specs.FILTERS_TAG] = filters model_spec = search_space_utils.prune_model_spec( model_spec, genotype, prune_filters_by_value=True, path_dropout_rate=path_dropout_rate, training=training) model_spec = search_space_utils.scale_conv_tower_spec( model_spec, filters_multipliers) return model_spec
def estimate_cost(indices, ssd): """Estimate the cost of a given architecture based on its indices. Args: indices: List of integers encoding an architecture in the search space. ssd: The name of the search space definition to use for the cost model. Returns: The estimated cost for the specified network architecture. """ with tf.Graph().as_default(): model_spec = mobile_search_space_v3.get_search_space_spec(ssd) model_spec = _with_constant_masks(indices, model_spec) features = coupled_tf_features(model_spec) cost = cost_model_lib.estimate_cost(features, ssd) with tf.Session() as sess: return float(sess.run(cost))
def test_get_search_space_spec(self, ssd): spec = mobile_search_space_v3.get_search_space_spec(ssd) self.assertIsInstance(spec, basic_specs.ConvTowerSpec)
def test_coupled_tf_features_with_mobile_model_v3(self, ssd): model_spec = mobile_search_space_v3.get_search_space_spec(ssd) model_spec = schema.map_oneofs(_assign_random_mask, model_spec) features = mobile_cost_model.coupled_tf_features(model_spec) self.assertEqual(features.dtype, tf.float32) self.assertEqual(features.shape.rank, 1)