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 if conv_defs is not provided or from_layer does not meet the
        size requirement.
    """

        if not self._conv_defs:
            raise ValueError('Must provide backbone conv defs.')

        if len(self._from_layer) != 2:
            raise ValueError('SSD input feature names are not provided.')

        preprocessed_inputs = shape_utils.check_min_image_dim(
            33, preprocessed_inputs)

        feature_map_layout = {
            'from_layer':
            [self._from_layer[0], self._from_layer[1], '', '', '', ''],
            'layer_depth': [-1, -1, 512, 256, 256, 128],
            'use_depthwise':
            self._use_depthwise,
            'use_explicit_padding':
            self._use_explicit_padding,
        }

        with tf.variable_scope(self._scope_name,
                               reuse=self._reuse_weights) as scope:
            with slim.arg_scope(
                mobilenet_v3.training_scope(is_training=None, bn_decay=0.9997)), \
                slim.arg_scope(
                    [mobilenet.depth_multiplier], min_depth=self._min_depth):
                with (slim.arg_scope(self._conv_hyperparams_fn())
                      if self._override_base_feature_extractor_hyperparams else
                      context_manager.IdentityContextManager()):
                    _, image_features = mobilenet_v3.mobilenet_base(
                        ops.pad_to_multiple(preprocessed_inputs,
                                            self._pad_to_multiple),
                        conv_defs=self._conv_defs,
                        final_endpoint=self._from_layer[1],
                        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.multi_resolution_feature_maps(
                        feature_map_layout=feature_map_layout,
                        depth_multiplier=self._depth_multiplier,
                        min_depth=self._min_depth,
                        insert_1x1_conv=True,
                        image_features=image_features)

        return list(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]
    """
        preprocessed_inputs = shape_utils.check_min_image_dim(
            33, preprocessed_inputs)

        feature_map_layout = {
            'from_layer':
            ['layer_15/expansion_output', 'layer_19', '', '', '', ''],
            'layer_depth': [-1, -1, 512, 256, 256, 128],
            'use_depthwise': self._use_depthwise,
            'use_explicit_padding': self._use_explicit_padding,
        }

        if self._num_layers == 7:
            feature_map_layout['from_layer'] += ['']
            feature_map_layout['layer_depth'] += [64]

        with tf.variable_scope('MobilenetV3',
                               reuse=self._reuse_weights) as scope:
            with slim.arg_scope(
                mobilenet_v3.training_scope(is_training=None, bn_decay=0.9997)), \
                slim.arg_scope(
                    [mobilenet.depth_multiplier], min_depth=self._min_depth):
                with (slim.arg_scope(self._conv_hyperparams_fn())
                      if self._override_base_feature_extractor_hyperparams else
                      context_manager.IdentityContextManager()):
                    _, image_features = mobilenet_v3.mobilenet_base(
                        ops.pad_to_multiple(preprocessed_inputs,
                                            self._pad_to_multiple),
                        final_endpoint='layer_19',
                        depth_multiplier=self._depth_multiplier,
                        use_explicit_padding=self._use_explicit_padding,
                        squeeze_excitation=self._squeeze_excitation,
                        scope=scope)
                with slim.arg_scope(self._conv_hyperparams_fn()):
                    feature_maps = feature_map_generators.multi_resolution_feature_maps(
                        feature_map_layout=feature_map_layout,
                        depth_multiplier=self._depth_multiplier,
                        min_depth=self._min_depth,
                        insert_1x1_conv=True,
                        image_features=image_features)

        return feature_maps.values()
    def _extract_box_classifier_features(self, proposal_feature_maps, scope):
        """Extracts second stage box classifier features.

    Args:
      proposal_feature_maps: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, crop_height, crop_width, depth]
        representing the feature map cropped to each proposal.
      scope: A scope name (unused).

    Returns:
      proposal_classifier_features: A 4-D float tensor with shape
        [batch_size * self.max_num_proposals, height, width, depth]
        representing box classifier features for each proposal.
    """
        net = proposal_feature_maps

        conv_depth = 1024
        if self._skip_last_stride:
            conv_depth_ratio = float(
                self._conv_depth_ratio_in_percentage) / 100.0
            conv_depth = int(float(conv_depth) * conv_depth_ratio)

        depth = lambda d: max(int(d * 1.0), 16)
        with tf.variable_scope('MobilenetV3', reuse=self._reuse_weights):
            with slim.arg_scope(
                    mobilenet_v3.training_scope(
                        is_training=None, weight_decay=self._weight_decay)):
                with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
                                    padding='SAME'):
                    net = slim.separable_conv2d(net,
                                                depth(conv_depth), [3, 3],
                                                depth_multiplier=1,
                                                stride=2,
                                                scope='Conv2d_12_pointwise')
                    return slim.separable_conv2d(net,
                                                 depth(conv_depth), [3, 3],
                                                 depth_multiplier=1,
                                                 stride=1,
                                                 scope='Conv2d_13_pointwise')
    def _extract_proposal_features(self, preprocessed_inputs, scope):
        """Extracts first stage RPN features.

    Args:
      preprocessed_inputs: A [batch, height, width, channels] float32 tensor
        representing a batch of images.
      scope: A scope name.

    Returns:
      rpn_feature_map: A tensor with shape [batch, height, width, depth]
      activations: A dictionary mapping feature extractor tensor names to
        tensors

    Raises:
      InvalidArgumentError: If the spatial size of `preprocessed_inputs`
        (height or width) is less than 33.
      ValueError: If the created network is missing the required activation.
    """

        preprocessed_inputs = shape_utils.check_min_image_dim(
            33, preprocessed_inputs)

        with tf.variable_scope('MobilenetV3',
                               reuse=self._reuse_weights) as scope:
            with slim.arg_scope(
                mobilenet_v3.training_scope(is_training=None, bn_decay=0.9997)), \
                slim.arg_scope(
                    [mobilenet.depth_multiplier], min_depth=self._min_depth):
                _, activations = mobilenet_v3.mobilenet_base(
                    preprocessed_inputs,
                    conv_defs=mobilenet_v3.V3_LARGE_DETECTION,
                    final_endpoint='layer_17',
                    depth_multiplier=self._depth_multiplier,
                    scope=scope)

        return activations['layer_17'], activations
Beispiel #5
0
tf.reset_default_graph()

# For simplicity we just decode jpeg inside tensorflow.
# But one can provide any input obviously.
file_input = tf.placeholder(tf.string, ())

image = tf.image.decode_jpeg(tf.read_file(file_input))

images = tf.expand_dims(image, 0)
images = tf.cast(images, tf.float32) / 128. - 1
images.set_shape((None, None, None, 3))
images = tf.image.resize_images(images, (224, 224))

# 测试MobileNetV2,MobileNetV3
# Note: arg_scope is optional for inference.
with tf.contrib.slim.arg_scope(mobilenet_v3.training_scope(is_training=False)):
    logits, endpoints = mobilenet_v3.mobilenet(images)

# 测试MobileNetV1
#with tf.contrib.slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=False)):
#logits, endpoints = mobilenet_v1.mobilenet_v1(images)
# Restore using exponential moving average since it produces (1.5-2%) higher
# accuracy
ema = tf.train.ExponentialMovingAverage(0.999)
vars = ema.variables_to_restore()

saver = tf.train.Saver(vars)

from datasets import imagenet
import os
import time
    (nets.MobileNet100v3largemini, (224, 224, 3),
     'mobilenet_100_v3_large_mini', mobilenet_v3.large_minimalistic, 1.0,
     'v3-large-minimalistic_224_1.0_float/ema/model-342500'),
    (nets.MobileNet100v3smallmini, (224, 224, 3),
     'mobilenet_100_v3_small_mini', mobilenet_v3.small_minimalistic, 1.0,
     'v3-small-minimalistic_224_1.0_float/ema/model-498000'),
]

for (net, shape, model_name, net_slim, alpha, checkpoint) in models_list:

    with tf.Graph().as_default():

        inputs = tf.compat.v1.placeholder(tf.float32, [None] + list(shape))

        with tf.contrib.slim.arg_scope(
                mobilenet_v3.training_scope(is_training=False)):
            logits, endpoints = net_slim(inputs, depth_multiplier=alpha)

        saver = tf.compat.v1.train.Saver()

        weights_tfslim = tf.compat.v1.get_collection(
            tf.compat.v1.GraphKeys.GLOBAL_VARIABLES)

        model = net(inputs, scope='a')

        img = nets.utils.load_img('/home/taehoonlee/tensornets/cat.png',
                                  target_size=int(shape[0] * 8 / 7),
                                  crop_size=shape[0])

        with tf.compat.v1.Session() as sess: