def extract_features(self, preprocessed_inputs): """Extract features from preprocessed inputs. Args: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i] Raises: ValueError: depth multiplier is not supported. """ if self._depth_multiplier != 1.0: raise ValueError('Depth multiplier not supported.') preprocessed_inputs = shape_utils.check_min_image_dim( 129, preprocessed_inputs) with tf.variable_scope(self._resnet_scope_name, reuse=self._reuse_weights) as scope: with slim.arg_scope(resnet_v1.resnet_arg_scope()): with (slim.arg_scope(self._conv_hyperparams_fn()) if self._override_base_feature_extractor_hyperparams else context_manager.IdentityContextManager()): with slim.arg_scope([resnet_v1.bottleneck], use_bounded_activations=self. _use_bounded_activations): _, activations = self._resnet_base_fn( inputs=ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), num_classes=None, is_training=None, global_pool=False, output_stride=None, store_non_strided_activations=True, scope=scope) with slim.arg_scope(self._conv_hyperparams_fn()): feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=self._base_feature_map_depth, num_layers=self._num_layers, image_features={ 'image_features': self._filter_features(activations)['block3'] }) return feature_maps.values()
def extract_features(self, preprocessed_inputs): """Extract features from preprocessed inputs. Args: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i] Raises: ValueError: depth multiplier is not supported. """ if self._depth_multiplier != 1.0: raise ValueError('Depth multiplier not supported.') preprocessed_inputs = shape_utils.check_min_image_dim( 129, preprocessed_inputs) with tf.variable_scope( self._resnet_scope_name, reuse=self._reuse_weights) as scope: with slim.arg_scope(resnet_v1.resnet_arg_scope()): with (slim.arg_scope(self._conv_hyperparams_fn()) if self._override_base_feature_extractor_hyperparams else context_manager.IdentityContextManager()): with slim.arg_scope( [resnet_v1.bottleneck], use_bounded_activations=self._use_bounded_activations): _, activations = self._resnet_base_fn( inputs=ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), num_classes=None, is_training=None, global_pool=False, output_stride=None, store_non_strided_activations=True, scope=scope) with slim.arg_scope(self._conv_hyperparams_fn()): feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=self._base_feature_map_depth, num_layers=self._num_layers, image_features={ 'image_features': self._filter_features(activations)['block3'] }) return feature_maps.values()
def test_get_expected_variable_names(self, replace_pool_with_conv): image_features = { 'image_features': tf.random_uniform([4, 19, 19, 1024]) } feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=1024, num_layers=6, image_features=image_features, replace_pool_with_conv=replace_pool_with_conv) expected_pool_variables = set([ 'Base_Conv2d_1x1_1024/weights', 'Base_Conv2d_1x1_1024/biases', ]) expected_conv_variables = set([ 'Base_Conv2d_1x1_1024/weights', 'Base_Conv2d_1x1_1024/biases', 'Conv2d_0_3x3_s2_1024/weights', 'Conv2d_0_3x3_s2_1024/biases', 'Conv2d_1_3x3_s2_1024/weights', 'Conv2d_1_3x3_s2_1024/biases', 'Conv2d_2_3x3_s2_1024/weights', 'Conv2d_2_3x3_s2_1024/biases', 'Conv2d_3_3x3_s2_1024/weights', 'Conv2d_3_3x3_s2_1024/biases', 'Conv2d_4_3x3_s2_1024/weights', 'Conv2d_4_3x3_s2_1024/biases', ]) init_op = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init_op) sess.run(feature_maps) actual_variable_set = set( [var.op.name for var in tf.trainable_variables()]) if replace_pool_with_conv: self.assertSetEqual(expected_conv_variables, actual_variable_set) else: self.assertSetEqual(expected_pool_variables, actual_variable_set)
def test_get_expected_feature_map_shapes(self, replace_pool_with_conv): image_features = { 'image_features': tf.random_uniform([4, 19, 19, 1024]) } feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=1024, num_layers=6, image_features=image_features, replace_pool_with_conv=replace_pool_with_conv) expected_pool_feature_map_shapes = { 'Base_Conv2d_1x1_1024': (4, 19, 19, 1024), 'MaxPool2d_0_2x2': (4, 10, 10, 1024), 'MaxPool2d_1_2x2': (4, 5, 5, 1024), 'MaxPool2d_2_2x2': (4, 3, 3, 1024), 'MaxPool2d_3_2x2': (4, 2, 2, 1024), 'MaxPool2d_4_2x2': (4, 1, 1, 1024), } expected_conv_feature_map_shapes = { 'Base_Conv2d_1x1_1024': (4, 19, 19, 1024), 'Conv2d_0_3x3_s2_1024': (4, 10, 10, 1024), 'Conv2d_1_3x3_s2_1024': (4, 5, 5, 1024), 'Conv2d_2_3x3_s2_1024': (4, 3, 3, 1024), 'Conv2d_3_3x3_s2_1024': (4, 2, 2, 1024), 'Conv2d_4_3x3_s2_1024': (4, 1, 1, 1024), } init_op = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init_op) out_feature_maps = sess.run(feature_maps) out_feature_map_shapes = { key: value.shape for key, value in out_feature_maps.items() } if replace_pool_with_conv: self.assertDictEqual(expected_conv_feature_map_shapes, out_feature_map_shapes) else: self.assertDictEqual(expected_pool_feature_map_shapes, out_feature_map_shapes)
def test_get_expected_variable_names(self, replace_pool_with_conv): image_features = { 'image_features': tf.random_uniform([4, 19, 19, 1024]) } feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=1024, num_layers=6, image_features=image_features, replace_pool_with_conv=replace_pool_with_conv) expected_pool_variables = set([ 'Base_Conv2d_1x1_1024/weights', 'Base_Conv2d_1x1_1024/biases', ]) expected_conv_variables = set([ 'Base_Conv2d_1x1_1024/weights', 'Base_Conv2d_1x1_1024/biases', 'Conv2d_0_3x3_s2_1024/weights', 'Conv2d_0_3x3_s2_1024/biases', 'Conv2d_1_3x3_s2_1024/weights', 'Conv2d_1_3x3_s2_1024/biases', 'Conv2d_2_3x3_s2_1024/weights', 'Conv2d_2_3x3_s2_1024/biases', 'Conv2d_3_3x3_s2_1024/weights', 'Conv2d_3_3x3_s2_1024/biases', 'Conv2d_4_3x3_s2_1024/weights', 'Conv2d_4_3x3_s2_1024/biases', ]) init_op = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init_op) sess.run(feature_maps) actual_variable_set = set( [var.op.name for var in tf.trainable_variables()]) if replace_pool_with_conv: self.assertSetEqual(expected_conv_variables, actual_variable_set) else: self.assertSetEqual(expected_pool_variables, actual_variable_set)
def test_get_expected_feature_map_shapes(self, replace_pool_with_conv): image_features = { 'image_features': tf.random_uniform([4, 19, 19, 1024]) } feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=1024, num_layers=6, image_features=image_features, replace_pool_with_conv=replace_pool_with_conv) expected_pool_feature_map_shapes = { 'Base_Conv2d_1x1_1024': (4, 19, 19, 1024), 'MaxPool2d_0_2x2': (4, 10, 10, 1024), 'MaxPool2d_1_2x2': (4, 5, 5, 1024), 'MaxPool2d_2_2x2': (4, 3, 3, 1024), 'MaxPool2d_3_2x2': (4, 2, 2, 1024), 'MaxPool2d_4_2x2': (4, 1, 1, 1024), } expected_conv_feature_map_shapes = { 'Base_Conv2d_1x1_1024': (4, 19, 19, 1024), 'Conv2d_0_3x3_s2_1024': (4, 10, 10, 1024), 'Conv2d_1_3x3_s2_1024': (4, 5, 5, 1024), 'Conv2d_2_3x3_s2_1024': (4, 3, 3, 1024), 'Conv2d_3_3x3_s2_1024': (4, 2, 2, 1024), 'Conv2d_4_3x3_s2_1024': (4, 1, 1, 1024), } init_op = tf.global_variables_initializer() with self.test_session() as sess: sess.run(init_op) out_feature_maps = sess.run(feature_maps) out_feature_map_shapes = {key: value.shape for key, value in out_feature_maps.items()} if replace_pool_with_conv: self.assertDictEqual(expected_conv_feature_map_shapes, out_feature_map_shapes) else: self.assertDictEqual(expected_pool_feature_map_shapes, out_feature_map_shapes)
def extract_features(self, preprocessed_inputs): """Extract features from preprocessed inputs. Args: preprocessed_inputs: a [batch, height, width, channels] float tensor representing a batch of images. Returns: feature_maps: a list of tensors where the ith tensor has shape [batch, height_i, width_i, depth_i] """ preprocessed_inputs = shape_utils.check_min_image_dim( 33, preprocessed_inputs) with tf.variable_scope('MobilenetV1', reuse=self._reuse_weights) as scope: with slim.arg_scope( mobilenet_v1.mobilenet_v1_arg_scope( is_training=None, regularize_depthwise=True)): with (slim.arg_scope(self._conv_hyperparams_fn()) if self._override_base_feature_extractor_hyperparams else context_manager.IdentityContextManager()): _, image_features = mobilenet_v1.mobilenet_v1_base( ops.pad_to_multiple(preprocessed_inputs, self._pad_to_multiple), final_endpoint='Conv2d_13_pointwise', min_depth=self._min_depth, depth_multiplier=self._depth_multiplier, use_explicit_padding=self._use_explicit_padding, scope=scope) with slim.arg_scope(self._conv_hyperparams_fn()): feature_maps = feature_map_generators.pooling_pyramid_feature_maps( base_feature_map_depth=0, num_layers=6, image_features={ 'image_features': image_features['Conv2d_11_pointwise'] }) return list(feature_maps.values())