示例#1
0
  def testOutputStride8BuildAndCheckAllEndPointsUptoConv2d_13(self):
    global numbers
    numbers += 1
    print('testOutputStride8BuildAndCheckAllEndPointsUptoConv2d_13: ',numbers)
    batch_size = 5
    height, width = 224, 224
    output_stride = 8

    inputs = tf.random_uniform((batch_size, height, width, 3))
    with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
                        normalizer_fn=slim.batch_norm):
      _, end_points = mobilenet_v1.mobilenet_v1_base(
          inputs, output_stride=output_stride,
          final_endpoint='Conv2d_13_pointwise')
      _, explicit_padding_end_points = mobilenet_v1.mobilenet_v1_base(
          inputs, output_stride=output_stride,
          final_endpoint='Conv2d_13_pointwise', use_explicit_padding=True)
    endpoints_shapes = {'Conv2d_0': [batch_size, 112, 112, 32],
                        'Conv2d_1_depthwise': [batch_size, 112, 112, 32],
                        'Conv2d_1_pointwise': [batch_size, 112, 112, 64],
                        'Conv2d_2_depthwise': [batch_size, 56, 56, 64],
                        'Conv2d_2_pointwise': [batch_size, 56, 56, 128],
                        'Conv2d_3_depthwise': [batch_size, 56, 56, 128],
                        'Conv2d_3_pointwise': [batch_size, 56, 56, 128],
                        'Conv2d_4_depthwise': [batch_size, 28, 28, 128],
                        'Conv2d_4_pointwise': [batch_size, 28, 28, 256],
                        'Conv2d_5_depthwise': [batch_size, 28, 28, 256],
                        'Conv2d_5_pointwise': [batch_size, 28, 28, 256],
                        'Conv2d_6_depthwise': [batch_size, 28, 28, 256],
                        'Conv2d_6_pointwise': [batch_size, 28, 28, 512],
                        'Conv2d_7_depthwise': [batch_size, 28, 28, 512],
                        'Conv2d_7_pointwise': [batch_size, 28, 28, 512],
                        'Conv2d_8_depthwise': [batch_size, 28, 28, 512],
                        'Conv2d_8_pointwise': [batch_size, 28, 28, 512],
                        'Conv2d_9_depthwise': [batch_size, 28, 28, 512],
                        'Conv2d_9_pointwise': [batch_size, 28, 28, 512],
                        'Conv2d_10_depthwise': [batch_size, 28, 28, 512],
                        'Conv2d_10_pointwise': [batch_size, 28, 28, 512],
                        'Conv2d_11_depthwise': [batch_size, 28, 28, 512],
                        'Conv2d_11_pointwise': [batch_size, 28, 28, 512],
                        'Conv2d_12_depthwise': [batch_size, 28, 28, 512],
                        'Conv2d_12_pointwise': [batch_size, 28, 28, 1024],
                        'Conv2d_13_depthwise': [batch_size, 28, 28, 1024],
                        'Conv2d_13_pointwise': [batch_size, 28, 28, 1024]}
    self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
    for endpoint_name, expected_shape in endpoints_shapes.items():
      self.assertTrue(endpoint_name in end_points)
      self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                           expected_shape)
    self.assertItemsEqual(endpoints_shapes.keys(),
                          explicit_padding_end_points.keys())
    for endpoint_name, expected_shape in endpoints_shapes.items():
      self.assertTrue(endpoint_name in explicit_padding_end_points)
      self.assertListEqual(
          explicit_padding_end_points[endpoint_name].get_shape().as_list(),
          expected_shape)
示例#2
0
  def testBuildAndCheckAllEndPointsApproximateFaceNet(self):
    global numbers
    numbers += 1
    print('testBuildAndCheckAllEndPointsApproximateFaceNet: ',numbers)
    batch_size = 5
    height, width = 128, 128

    inputs = tf.random_uniform((batch_size, height, width, 3))
    with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
                        normalizer_fn=slim.batch_norm):
      _, end_points = mobilenet_v1.mobilenet_v1_base(
          inputs, final_endpoint='Conv2d_13_pointwise', depth_multiplier=0.75)
      _, explicit_padding_end_points = mobilenet_v1.mobilenet_v1_base(
          inputs, final_endpoint='Conv2d_13_pointwise', depth_multiplier=0.75,
          use_explicit_padding=True)
    # For the Conv2d_0 layer FaceNet has depth=16
    endpoints_shapes = {'Conv2d_0': [batch_size, 64, 64, 24],
                        'Conv2d_1_depthwise': [batch_size, 64, 64, 24],
                        'Conv2d_1_pointwise': [batch_size, 64, 64, 48],
                        'Conv2d_2_depthwise': [batch_size, 32, 32, 48],
                        'Conv2d_2_pointwise': [batch_size, 32, 32, 96],
                        'Conv2d_3_depthwise': [batch_size, 32, 32, 96],
                        'Conv2d_3_pointwise': [batch_size, 32, 32, 96],
                        'Conv2d_4_depthwise': [batch_size, 16, 16, 96],
                        'Conv2d_4_pointwise': [batch_size, 16, 16, 192],
                        'Conv2d_5_depthwise': [batch_size, 16, 16, 192],
                        'Conv2d_5_pointwise': [batch_size, 16, 16, 192],
                        'Conv2d_6_depthwise': [batch_size, 8, 8, 192],
                        'Conv2d_6_pointwise': [batch_size, 8, 8, 384],
                        'Conv2d_7_depthwise': [batch_size, 8, 8, 384],
                        'Conv2d_7_pointwise': [batch_size, 8, 8, 384],
                        'Conv2d_8_depthwise': [batch_size, 8, 8, 384],
                        'Conv2d_8_pointwise': [batch_size, 8, 8, 384],
                        'Conv2d_9_depthwise': [batch_size, 8, 8, 384],
                        'Conv2d_9_pointwise': [batch_size, 8, 8, 384],
                        'Conv2d_10_depthwise': [batch_size, 8, 8, 384],
                        'Conv2d_10_pointwise': [batch_size, 8, 8, 384],
                        'Conv2d_11_depthwise': [batch_size, 8, 8, 384],
                        'Conv2d_11_pointwise': [batch_size, 8, 8, 384],
                        'Conv2d_12_depthwise': [batch_size, 4, 4, 384],
                        'Conv2d_12_pointwise': [batch_size, 4, 4, 768],
                        'Conv2d_13_depthwise': [batch_size, 4, 4, 768],
                        'Conv2d_13_pointwise': [batch_size, 4, 4, 768]}
    self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
    for endpoint_name, expected_shape in endpoints_shapes.items():
      self.assertTrue(endpoint_name in end_points)
      self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                           expected_shape)
    self.assertItemsEqual(endpoints_shapes.keys(),
                          explicit_padding_end_points.keys())
    for endpoint_name, expected_shape in endpoints_shapes.items():
      self.assertTrue(endpoint_name in explicit_padding_end_points)
      self.assertListEqual(
          explicit_padding_end_points[endpoint_name].get_shape().as_list(),
          expected_shape)
示例#3
0
 def testModelHasExpectedNumberOfParameters(self):
   global numbers
   numbers += 1
   print('testModelHasExpectedNumberOfParameters: ',numbers)
   batch_size = 5
   height, width = 224, 224
   inputs = tf.random_uniform((batch_size, height, width, 3))
   with slim.arg_scope([slim.conv2d, slim.separable_conv2d],
                       normalizer_fn=slim.batch_norm):
     mobilenet_v1.mobilenet_v1_base(inputs)
     total_params, _ = slim.model_analyzer.analyze_vars(
         slim.get_model_variables())
     self.assertAlmostEqual(3217920, total_params)
示例#4
0
def mobilenet(inputs,
              num_classes=1000,
              dropout_keep_prob=0.999,
              is_training=True,
              min_depth=8,
              depth_multiplier=1.0,
              conv_defs=None,
              spatial_squeeze=True,
              reuse=None,
              scope='MobilenetV1',
              global_pool=False):
    input_shape = inputs.get_shape().as_list()
    if len(input_shape) != 4:
        raise ValueError('Invalid input tensor rank, expected 4, was: %d' %
                         len(input_shape))

    with tf.variable_scope(scope, 'MobilenetV1', [inputs],
                           reuse=reuse) as scope:
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
            nets = mobilenet_v1.mobilenet_v1_base(
                inputs,
                scope=scope,
                min_depth=min_depth,
                depth_multiplier=depth_multiplier,
                conv_defs=conv_defs)
            net = nets[0]

    return net
示例#5
0
 def testBuildOnlyUptoFinalEndpoint(self):
   global numbers
   numbers += 1
   print('testBuildOnlyUptoFinalEndpoint: ',numbers)
   batch_size = 5
   height, width = 224, 224
   endpoints = ['Conv2d_0',
                'Conv2d_1_depthwise', 'Conv2d_1_pointwise',
                'Conv2d_2_depthwise', 'Conv2d_2_pointwise',
                'Conv2d_3_depthwise', 'Conv2d_3_pointwise',
                'Conv2d_4_depthwise', 'Conv2d_4_pointwise',
                'Conv2d_5_depthwise', 'Conv2d_5_pointwise',
                'Conv2d_6_depthwise', 'Conv2d_6_pointwise',
                'Conv2d_7_depthwise', 'Conv2d_7_pointwise',
                'Conv2d_8_depthwise', 'Conv2d_8_pointwise',
                'Conv2d_9_depthwise', 'Conv2d_9_pointwise',
                'Conv2d_10_depthwise', 'Conv2d_10_pointwise',
                'Conv2d_11_depthwise', 'Conv2d_11_pointwise',
                'Conv2d_12_depthwise', 'Conv2d_12_pointwise',
                'Conv2d_13_depthwise', 'Conv2d_13_pointwise']
   for index, endpoint in enumerate(endpoints):
     with tf.Graph().as_default():
       inputs = tf.random_uniform((batch_size, height, width, 3))
       out_tensor, end_points = mobilenet_v1.mobilenet_v1_base(
           inputs, final_endpoint=endpoint)
       self.assertTrue(out_tensor.op.name.startswith(
           'MobilenetV1/' + endpoint))
       self.assertItemsEqual(endpoints[:index+1], end_points.keys())
示例#6
0
  def testBuildBaseNetwork(self):
    global numbers
    numbers += 1
    print('testBuildBaseNetwork: ',numbers)
    batch_size = 5
    height, width = 224, 224

    inputs = tf.random_uniform((batch_size, height, width, 3))
    net, end_points = mobilenet_v1.mobilenet_v1_base(inputs)
    self.assertTrue(net.op.name.startswith('MobilenetV1/Conv2d_13'))
    self.assertListEqual(net.get_shape().as_list(),
                         [batch_size, 7, 7, 1024])
    expected_endpoints = ['Conv2d_0',
                          'Conv2d_1_depthwise', 'Conv2d_1_pointwise',
                          'Conv2d_2_depthwise', 'Conv2d_2_pointwise',
                          'Conv2d_3_depthwise', 'Conv2d_3_pointwise',
                          'Conv2d_4_depthwise', 'Conv2d_4_pointwise',
                          'Conv2d_5_depthwise', 'Conv2d_5_pointwise',
                          'Conv2d_6_depthwise', 'Conv2d_6_pointwise',
                          'Conv2d_7_depthwise', 'Conv2d_7_pointwise',
                          'Conv2d_8_depthwise', 'Conv2d_8_pointwise',
                          'Conv2d_9_depthwise', 'Conv2d_9_pointwise',
                          'Conv2d_10_depthwise', 'Conv2d_10_pointwise',
                          'Conv2d_11_depthwise', 'Conv2d_11_pointwise',
                          'Conv2d_12_depthwise', 'Conv2d_12_pointwise',
                          'Conv2d_13_depthwise', 'Conv2d_13_pointwise']
    self.assertItemsEqual(end_points.keys(), expected_endpoints)
示例#7
0
 def branch(name, inputs):
     with tf.variable_scope(name):
         output, endpoints = mn.mobilenet_v1_base(inputs, \
             depth_multiplier=model_util.MOBILENET_DEP_MULTI)
         #kernel = mn.reduced_kernel_size_for_small_input(left_output, KERNEL_SIZE)
         #pool_l = slim.avg_pool2d(left_output, kernel, padding='VALID')
         flat = tf.contrib.layers.flatten(output)
         return inputs, flat
示例#8
0
 def branch(name):
     with tf.variable_scope(name):
         shape = (None, model_util.IMAGE_SIZE, model_util.IMAGE_SIZE,
                  model_util.IMAGE_CHANNELS)
         inputs = tf.placeholder(tf.float32, shape)
         output, endpoints = mn.mobilenet_v1_base(inputs, \
             depth_multiplier=model_util.MOBILENET_DEP_MULTI)
         #kernel = mn.reduced_kernel_size_for_small_input(left_output, KERNEL_SIZE)
         #pool_l = slim.avg_pool2d(left_output, kernel, padding='VALID')
         flat = tf.contrib.layers.flatten(output)
         norm = tf.nn.l2_normalize(flat, 1, name="Norm")
         return inputs, norm
示例#9
0
def load_mobilenet_and_continue(name,
                                graph_filename,
                                layer_cutoff,
                                end_num=13):
    inputs, output = load_mobilenet(name, graph_filename, layer_cutoff)
    conv_defs = mn._CONV_DEFS[layer_cutoff + 1:end_num + 1]
    mn_output, _ = mn.mobilenet_v1_base(output,
                                        final_endpoint='Conv2d_%i_pointwise' %
                                        (end_num - layer_cutoff - 1),
                                        min_depth=MOBILENET_MIN_DEPTH,
                                        depth_multiplier=MOBILENET_DEP_MULTI,
                                        conv_defs=conv_defs,
                                        output_stride=None,
                                        scope=None)
    return inputs, mn_output
示例#10
0
  def testBuildCustomNetworkUsingConvDefs(self):
    global numbers
    numbers += 1
    print('testBuildCustomNetworkUsingConvDefs: ',numbers)
    batch_size = 5
    height, width = 224, 224
    conv_defs = [
        mobilenet_v1.Conv(kernel=[3, 3], stride=2, depth=32),
        mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=64),
        mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=2, depth=128),
        mobilenet_v1.DepthSepConv(kernel=[3, 3], stride=1, depth=512)
    ]

    inputs = tf.random_uniform((batch_size, height, width, 3))
    net, end_points = mobilenet_v1.mobilenet_v1_base(
        inputs, final_endpoint='Conv2d_3_pointwise', conv_defs=conv_defs)
    self.assertTrue(net.op.name.startswith('MobilenetV1/Conv2d_3'))
    self.assertListEqual(net.get_shape().as_list(),
                         [batch_size, 56, 56, 512])
    expected_endpoints = ['Conv2d_0',
                          'Conv2d_1_depthwise', 'Conv2d_1_pointwise',
                          'Conv2d_2_depthwise', 'Conv2d_2_pointwise',
                          'Conv2d_3_depthwise', 'Conv2d_3_pointwise']
    self.assertItemsEqual(end_points.keys(), expected_endpoints)
def ssd_net(inputs,
            num_classes=SSDNet.default_params.num_classes,
            feat_layers=SSDNet.default_params.feat_layers,
            anchor_sizes=SSDNet.default_params.anchor_sizes,
            anchor_ratios=SSDNet.default_params.anchor_ratios,
            normalizations=SSDNet.default_params.normalizations,
            is_training=True,
            dropout_keep_prob=0.5,
            prediction_fn=slim.softmax,
            reuse=None,
            scope='ssd_300_mobilenetv1'):
    """SSD net definition.
    """
    # if data_format == 'NCHW':
    #     inputs = tf.transpose(inputs, perm=(0, 3, 1, 2))

    # End_points collect relevant activations for external use.
    end_points = {}

    min_depth = 32 
    depth_multiplier = 1.0
    with tf.variable_scope(scope, 'ssd_300_mobilenetv1', [inputs], reuse=reuse):
        input_shape = inputs.get_shape().as_list()
        if len(input_shape) != 4:
          raise ValueError('Invalid input tensor rank, expected 4, was: %d' %
                     len(input_shape))

        with slim.arg_scope(mobilenet_v1.mobilenet_v1_arg_scope(is_training=is_training)):	
          with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training):
            net, end_points = mobilenet_v1.mobilenet_v1_base(inputs, scope='MobilenetV1',
                                          min_depth=min_depth,
                                          depth_multiplier=depth_multiplier,
                                          conv_defs=None)
        '''
        # Additional SSD blocks.
        # Block 6: let's dilate the hell out of it!
        end_point = 'block13'
        with tf.variable_scope(end_point)
          net = slim.conv2d(net, 1024, [3, 3], rate=6, scope='atrous_conv')
          end_points['block13_atrous'] = net
          net = tf.layers.dropout(net, rate=dropout_keep_prob, training=is_training)
          # Block 7: 1x1 conv. Because the f**k.
          net = slim.conv2d(net, 1024, [1, 1], scope='conv1x1')
          end_points['block13'] = net
          net = tf.layers.dropout(net, rate=dropout_keep_prob, training=is_training)
        '''

        # Block 14/15/16/17: 1x1 and 3x3 convolutions stride 2 (except lasts).
        end_point = 'block14'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 256, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = custom_layers.pad2d(net, pad=(1, 1))
            net = slim.conv2d(net, 512, [3, 3], biases_initializer=None, trainable=is_training, stride=2, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        end_point = 'block15'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 128, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = custom_layers.pad2d(net, pad=(1, 1))
            net = slim.conv2d(net, 256, [3, 3], biases_initializer=None, trainable=is_training, stride=2, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        end_point = 'block16'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 128, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = slim.conv2d(net, 256, [3, 3], biases_initializer=None, trainable=is_training, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        '''
        end_point = 'block17'
        with tf.variable_scope(end_point):
            net = slim.conv2d(net, 64, [1, 1], biases_initializer=None, trainable=is_training, scope='conv1x1')
            net = slim.conv2d(net, 128, [3, 3], biases_initializer=None, trainable=is_training, scope='conv3x3', padding='VALID')
        end_points[end_point] = net
        '''

        # Prediction and localisations layers.
        predictions = []
        logits = []
        localisations = []
        

        for i, layer in enumerate(feat_layers):
            with tf.variable_scope(layer + '_box'):
                p, l = ssd_multibox_layer(end_points[layer],
                                          num_classes,
                                          anchor_sizes[i],
                                          anchor_ratios[i],
                                          normalizations[i],
										  is_training=is_training)
            predictions.append(prediction_fn(p))
            logits.append(p)
            localisations.append(l)
#        end_points['logits'] = logits
#        end_points['predictions'] = predictions
#        end_points['localisations'] = localisations

        return predictions, localisations, logits, end_points
示例#12
0
def osmn_lite(inputs,
              model_params,
              visual_modulator_params=None,
              scope='osmn',
              is_training=False):
    """Defines the OSMN
    Args:
    inputs: Tensorflow placeholder that contains the input image, visual guide, and spatial guide
    model_params: paramters related to the model structure
    visual_modulator_params: if None it will generate new visual modulation parameters using guide image, otherwise
            it will reuse the current paramters.
    scope: Scope name for the network
    is_training: training or testing 
    Returns:
    net: Output Tensor of the network
    end_points: Dictionary with all Tensors of the network
    """
    guide_im_size = tf.shape(inputs[0])
    im_size = model_params.im_size
    batch_size = inputs[1].get_shape().as_list()[0]
    use_visual_modulator = model_params.use_visual_modulator
    use_spatial_modulator = model_params.use_spatial_modulator
    train_seg = model_params.train_seg
    n_modulator_param = 1024 + 512 + 256 + 128
    mod_layer_ids = [3, 5, 11, 13]
    output_stride = 32
    batch_norm_params = {
        'decay': 0.99,
        'scale': True,
        'epsilon': 0.001,
        'updates_collections': None,
        'is_training': not model_params.fix_bn and is_training
    }
    if use_visual_modulator and visual_modulator_params == None:
        visual_modulator_params = visual_modulator_lite(
            inputs[0], model_params, scope=scope, is_training=is_training)
    with tf.variable_scope(scope, [inputs]) as sc, slim.arg_scope(
            mobilenet_v1.mobilenet_v1_arg_scope(
                is_training=is_training)) as arg_sc:
        end_points_collection = sc.name + '_end_points'

        # index to mark the current position of the modulation params
        visual_mod_id = 0
        with tf.variable_scope('modulator_sp'):
            with slim.arg_scope(
                [slim.conv2d],
                    activation_fn=tf.nn.relu,
                    normalizer_fn=slim.batch_norm,
                    normalizer_params=batch_norm_params,
                    padding='SAME',
                    outputs_collections=end_points_collection) as bn_arg_sc:
                if not use_spatial_modulator:
                    sp_mod_params = None
                else:
                    ds_mask = slim.avg_pool2d(inputs[1], [4, 4],
                                              stride=4,
                                              scope='pool4')
                    conv3_att = slim.conv2d(ds_mask,
                                            128, [1, 1],
                                            scope='conv3')
                    ds_mask = slim.avg_pool2d(ds_mask, [2, 2], scope='pool8')
                    conv5_att = slim.conv2d(ds_mask,
                                            256, [1, 1],
                                            scope='conv5')
                    ds_mask = slim.avg_pool2d(ds_mask, [2, 2], scope='pool16')
                    conv11_att = slim.conv2d(ds_mask,
                                             512, [1, 1],
                                             scope='conv11')
                    ds_mask = slim.avg_pool2d(ds_mask, [2, 2], scope='pool32')
                    conv13_att = slim.conv2d(ds_mask,
                                             1024, [1, 1],
                                             scope='conv13')
                    sp_mod_params = [
                        conv3_att, conv5_att, conv11_att, conv13_att
                    ]

        # Collect outputs of all intermediate layers.
        net, end_points = mobilenet_v1.mobilenet_v1_base(
            inputs[2],
            output_stride=output_stride,
            vis_mod_params=visual_modulator_params,
            sp_mod_params=sp_mod_params,
            mod_layer_ids=mod_layer_ids,
            scope='seg')

        with slim.arg_scope([slim.conv2d],
                            activation_fn=None,
                            normalizer_fn=None):
            net_2 = end_points['Conv2d_3_pointwise']
            net_3 = end_points['Conv2d_5_pointwise']
            net_4 = end_points['Conv2d_11_pointwise']
            net_5 = end_points['Conv2d_13_pointwise']
            side_2 = slim.conv2d(net_2, 16, [3, 3], scope='conv3_16')
            side_3 = slim.conv2d(net_3, 16, [3, 3], scope='conv5_16')
            side_4 = slim.conv2d(net_4, 16, [3, 3], scope='conv11_16')
            side_5 = slim.conv2d(net_5, 16, [3, 3], scope='conv13_16')
            up_size = [im_size[1] / 2, im_size[0] / 2]
            side_2_f = tf.image.resize_bilinear(side_2, up_size)
            side_3_f = tf.image.resize_bilinear(side_3, up_size)
            side_4_f = tf.image.resize_bilinear(side_4, up_size)
            side_5_f = tf.image.resize_bilinear(side_5, up_size)

            net = tf.concat([side_2_f, side_3_f, side_4_f, side_5_f], axis=3)
            net = slim.conv2d(net, 1, [1, 1], scope='score')
            net = tf.image.resize_bilinear(net, [im_size[1], im_size[0]])
        #net = slim.conv2d_transpose(net, 1, output_stride * 2, output_stride, normalizer_fn=None, padding="SAME", scope='score-up')
        end_points = slim.utils.convert_collection_to_dict(
            end_points_collection)
        return net, end_points