Exemplo n.º 1
0
 def testModelHasExpectedNumberOfParameters(self):
     batch_size = 5
     height, width = 224, 224
     inputs = tf.random_uniform((batch_size, height, width, 3))
     with slim.arg_scope(inception.inception_v2_arg_scope()):
         inception.inception_v2_base(inputs)
     total_params, _ = slim.model_analyzer.analyze_vars(slim.get_model_variables())
     self.assertAlmostEqual(10173112, total_params)
Exemplo n.º 2
0
 def testModelHasExpectedNumberOfParameters(self):
     batch_size = 5
     height, width = 224, 224
     inputs = tf.random_uniform((batch_size, height, width, 3))
     with slim.arg_scope(inception.inception_v2_arg_scope()):
         inception.inception_v2_base(inputs)
     total_params, _ = slim.model_analyzer.analyze_vars(
         slim.get_model_variables())
     self.assertAlmostEqual(10173112, total_params)
Exemplo n.º 3
0
    def testBuildAndCheckAllEndPointsUptoMixed5c(self):
        batch_size = 5
        height, width = 224, 224

        inputs = tf.random_uniform((batch_size, height, width, 3))
        _, end_points = inception.inception_v2_base(inputs,
                                                    final_endpoint='Mixed_5c')
        endpoints_shapes = {
            'Mixed_3b': [batch_size, 28, 28, 256],
            'Mixed_3c': [batch_size, 28, 28, 320],
            'Mixed_4a': [batch_size, 14, 14, 576],
            'Mixed_4b': [batch_size, 14, 14, 576],
            'Mixed_4c': [batch_size, 14, 14, 576],
            'Mixed_4d': [batch_size, 14, 14, 576],
            'Mixed_4e': [batch_size, 14, 14, 576],
            'Mixed_5a': [batch_size, 7, 7, 1024],
            'Mixed_5b': [batch_size, 7, 7, 1024],
            'Mixed_5c': [batch_size, 7, 7, 1024],
            'Conv2d_1a_7x7': [batch_size, 112, 112, 64],
            'MaxPool_2a_3x3': [batch_size, 56, 56, 64],
            'Conv2d_2b_1x1': [batch_size, 56, 56, 64],
            'Conv2d_2c_3x3': [batch_size, 56, 56, 192],
            'MaxPool_3a_3x3': [batch_size, 28, 28, 192]
        }
        self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
        for endpoint_name in endpoints_shapes:
            expected_shape = endpoints_shapes[endpoint_name]
            self.assertTrue(endpoint_name in end_points)
            self.assertListEqual(
                end_points[endpoint_name].get_shape().as_list(),
                expected_shape)
Exemplo n.º 4
0
def build_model(model_name, inputs, num_classes, is_training, dropout_keep_prob):
    use_fcn = False
    if model_name.find('fcn') >= 0:
        use_fcn = True
        model_base_name = model_name[0:-4]
    else:
        model_base_name = model_name

    if model_base_name == 'vgg16':
        net = vgg16_base(inputs)
    elif model_base_name == 'inception_v1':
        with slim.arg_scope(inception.inception_v1_arg_scope()):
            net, _ = inception.inception_v1_base(inputs)
    elif model_base_name == 'inception_v2':
        with slim.arg_scope(inception.inception_v2_arg_scope()):
            net, _ = inception.inception_v2_base(inputs)
    elif model_base_name == 'inception_v3':
        with slim.arg_scope(inception.inception_v3_arg_scope()):
            net, _ = inception.inception_v3_base(inputs)
    else:
        raise Exception('model {} is not existed'.format(model_name))

    with tf.variable_scope('not_pretrained'):
        if use_fcn:
            net = fully_convolutional_networks(net, num_classes, is_training, dropout_keep_prob)
        else:
            net = fully_connected_networks(net, num_classes, is_training, dropout_keep_prob)
    return net
Exemplo n.º 5
0
  def testBuildAndCheckAllEndPointsUptoMixed5c(self):
    batch_size = 5
    height, width = 224, 224

    inputs = tf.random_uniform((batch_size, height, width, 3))
    _, end_points = inception.inception_v2_base(inputs,
                                                final_endpoint='Mixed_5c')
    endpoints_shapes = {'Mixed_3b': [batch_size, 28, 28, 256],
                        'Mixed_3c': [batch_size, 28, 28, 320],
                        'Mixed_4a': [batch_size, 14, 14, 576],
                        'Mixed_4b': [batch_size, 14, 14, 576],
                        'Mixed_4c': [batch_size, 14, 14, 576],
                        'Mixed_4d': [batch_size, 14, 14, 576],
                        'Mixed_4e': [batch_size, 14, 14, 576],
                        'Mixed_5a': [batch_size, 7, 7, 1024],
                        'Mixed_5b': [batch_size, 7, 7, 1024],
                        'Mixed_5c': [batch_size, 7, 7, 1024],
                        'Conv2d_1a_7x7': [batch_size, 112, 112, 64],
                        'MaxPool_2a_3x3': [batch_size, 56, 56, 64],
                        'Conv2d_2b_1x1': [batch_size, 56, 56, 64],
                        'Conv2d_2c_3x3': [batch_size, 56, 56, 192],
                        'MaxPool_3a_3x3': [batch_size, 28, 28, 192]}
    self.assertItemsEqual(endpoints_shapes.keys(), end_points.keys())
    for endpoint_name in endpoints_shapes:
      expected_shape = endpoints_shapes[endpoint_name]
      self.assertTrue(endpoint_name in end_points)
      self.assertListEqual(end_points[endpoint_name].get_shape().as_list(),
                           expected_shape)
Exemplo n.º 6
0
 def testBuildOnlyUptoFinalEndpoint(self):
     batch_size = 5
     height, width = 224, 224
     endpoints = [
         "Conv2d_1a_7x7",
         "MaxPool_2a_3x3",
         "Conv2d_2b_1x1",
         "Conv2d_2c_3x3",
         "MaxPool_3a_3x3",
         "Mixed_3b",
         "Mixed_3c",
         "Mixed_4a",
         "Mixed_4b",
         "Mixed_4c",
         "Mixed_4d",
         "Mixed_4e",
         "Mixed_5a",
         "Mixed_5b",
         "Mixed_5c",
     ]
     for index, endpoint in enumerate(endpoints):
         with tf.Graph().as_default():
             inputs = tf.random_uniform((batch_size, height, width, 3))
             out_tensor, end_points = inception.inception_v2_base(inputs, final_endpoint=endpoint)
             self.assertTrue(out_tensor.op.name.startswith("InceptionV2/" + endpoint))
             self.assertItemsEqual(endpoints[: index + 1], end_points)
Exemplo n.º 7
0
    def testBuildBaseNetwork(self):
        batch_size = 5
        height, width = 224, 224

        inputs = tf.random_uniform((batch_size, height, width, 3))
        mixed_5c, end_points = inception.inception_v2_base(inputs)
        self.assertTrue(mixed_5c.op.name.startswith("InceptionV2/Mixed_5c"))
        self.assertListEqual(mixed_5c.get_shape().as_list(), [batch_size, 7, 7, 1024])
        expected_endpoints = [
            "Mixed_3b",
            "Mixed_3c",
            "Mixed_4a",
            "Mixed_4b",
            "Mixed_4c",
            "Mixed_4d",
            "Mixed_4e",
            "Mixed_5a",
            "Mixed_5b",
            "Mixed_5c",
            "Conv2d_1a_7x7",
            "MaxPool_2a_3x3",
            "Conv2d_2b_1x1",
            "Conv2d_2c_3x3",
            "MaxPool_3a_3x3",
        ]
        self.assertItemsEqual(end_points.keys(), expected_endpoints)
Exemplo n.º 8
0
    def build(self,
              inputs,
              input_pixel_size,
              is_training,
              scope='img_inception'):
        """Inception for BEV feature extraction

        Args:
            inputs: a tensor of size [batch_size, height, width, channels].
            input_pixel_size: size of the input (H x W)
            is_training: True for training, False fo validation/testing.
            scope: Optional scope for the variables.

        Returns:
            The net, a rank-4 tensor of size [batch, height_out, width_out,
                channels_out] and end_points dict.
        """

        inception_config = self.config

        with tf.variable_scope(scope, 'img_inception', [inputs]) as scope:
            with slim.arg_scope([slim.batch_norm, slim.dropout],
                                is_training=is_training):

                if inception_config.inception_v == 'inception_v1':
                    with slim.arg_scope(inception.inception_v1_arg_scope()):
                        net, end_points = inception.inception_v1_base(
                            inputs, scope=scope)

                elif inception_config.inception_v == 'inception_v2':
                    with slim.arg_scope(inception.inception_v2_arg_scope()):
                        net, end_points = inception.inception_v2_base(
                            inputs, scope=scope)

                elif inception_config.inception_v == 'inception_v3':
                    with slim.arg_scope(inception.inception_v3_arg_scope()):
                        net, end_points = inception.inception_v3_base(
                            inputs, scope=scope)
                else:
                    raise ValueError('Invalid Inception version {},'.format(
                        inception_config.inception_v))

                with tf.variable_scope('upsampling'):
                    # This feature extractor downsamples the input by a factor
                    # of 32
                    downsampling_factor = 32
                    downsampled_shape = input_pixel_size / downsampling_factor

                    upsampled_shape = downsampled_shape * \
                        inception_config.upsampling_multiplier

                    feature_maps_out = tf.image.resize_bilinear(
                        net, upsampled_shape)

        return feature_maps_out, end_points
Exemplo n.º 9
0
 def testBuildOnlyUptoFinalEndpoint(self):
   batch_size = 5
   height, width = 224, 224
   endpoints = ['Conv2d_1a_7x7', 'MaxPool_2a_3x3', 'Conv2d_2b_1x1',
                'Conv2d_2c_3x3', 'MaxPool_3a_3x3', 'Mixed_3b', 'Mixed_3c',
                'Mixed_4a', 'Mixed_4b', 'Mixed_4c', 'Mixed_4d', 'Mixed_4e',
                'Mixed_5a', 'Mixed_5b', 'Mixed_5c']
   for index, endpoint in enumerate(endpoints):
     with tf.Graph().as_default():
       inputs = tf.random_uniform((batch_size, height, width, 3))
       out_tensor, end_points = inception.inception_v2_base(
           inputs, final_endpoint=endpoint)
       self.assertTrue(out_tensor.op.name.startswith(
           'InceptionV2/' + endpoint))
       self.assertItemsEqual(endpoints[:index+1], end_points)
Exemplo n.º 10
0
  def testBuildBaseNetwork(self):
    batch_size = 5
    height, width = 224, 224

    inputs = tf.random_uniform((batch_size, height, width, 3))
    mixed_5c, end_points = inception.inception_v2_base(inputs)
    self.assertTrue(mixed_5c.op.name.startswith('InceptionV2/Mixed_5c'))
    self.assertListEqual(mixed_5c.get_shape().as_list(),
                         [batch_size, 7, 7, 1024])
    expected_endpoints = ['Mixed_3b', 'Mixed_3c', 'Mixed_4a', 'Mixed_4b',
                          'Mixed_4c', 'Mixed_4d', 'Mixed_4e', 'Mixed_5a',
                          'Mixed_5b', 'Mixed_5c', 'Conv2d_1a_7x7',
                          'MaxPool_2a_3x3', 'Conv2d_2b_1x1', 'Conv2d_2c_3x3',
                          'MaxPool_3a_3x3']
    self.assertItemsEqual(end_points.keys(), expected_endpoints)
Exemplo n.º 11
0
  def testInceptionV2_TotalCost(self):
    conv_params = {
        'activation_fn': tf.nn.relu6,
        'weights_regularizer': contrib_layers.l2_regularizer(0.00004),
        'weights_initializer': tf.random_normal_initializer(stddev=0.03),
        'trainable': True,
        'biases_initializer': tf.constant_initializer(0.0),
        'normalizer_fn': contrib_layers.batch_norm,
        'normalizer_params': {
            'is_training': False,
            'decay': 0.9997,
            'scale': True,
            'epsilon': 0.001,
        }
    }

    tf.reset_default_graph()
    with slim.arg_scope([slim.layers.conv2d, slim.layers.separable_conv2d],
                        **conv_params):
      # Build model.
      image = tf.zeros([1, 224, 224, 3])
      net, _ = inception.inception_v2_base(image)
      logits = slim.layers.fully_connected(
          net,
          1001,
          activation_fn=None,
          scope='logits',
          weights_initializer=tf.random_normal_initializer(stddev=1e-3),
          biases_initializer=tf.constant_initializer(0.0))

    # Instantiate regularizers.
    flop_reg = flop_regularizer.GammaFlopsRegularizer(
        [logits.op], gamma_threshold=0.5)
    p100_reg = latency_regularizer.GammaLatencyRegularizer(
        [logits.op], gamma_threshold=0.5, hardware='P100')
    v100_reg = latency_regularizer.GammaLatencyRegularizer(
        [logits.op], gamma_threshold=0.5, hardware='V100')
    model_size_reg = model_size_regularizer.GammaModelSizeRegularizer(
        [logits.op], gamma_threshold=0.5)

    with self.cached_session():
      tf.global_variables_initializer().run()

    # Verify costs are expected.
    self.assertAllClose(3.86972e+09, flop_reg.get_cost())
    self.assertAllClose(517536.0, p100_reg.get_cost())
    self.assertAllClose(173330.453125, v100_reg.get_cost())
    self.assertAllClose(1.11684e+07, model_size_reg.get_cost())
Exemplo n.º 12
0
  def testInceptionV2(self, hardware):
    image = tf.zeros([1, 224, 224, 3])
    net, _ = inception.inception_v2_base(image)
    g = tf.get_default_graph()
    self.regularizer = latency_regularizer.GammaLatencyRegularizer(
        [net.op], gamma_threshold=0.5, hardware=hardware)

    # Compute-bound convolution.
    op = g.get_operation_by_name(
        'InceptionV2/Mixed_3c/Branch_2/Conv2d_0c_3x3/Conv2D')
    # FLOP cost = 2 * NHWRSCK
    expected_cost = (2 * 28 * 28 * 3 * 3 * 96 * 96
                     / resource_function.PEAK_COMPUTE[hardware])
    self.assertAllClose(expected_cost, self.get_cost([op]))

    # Memory-bound convolution.
    op = g.get_operation_by_name(
        'InceptionV2/Conv2d_1a_7x7/separable_conv2d')
    # Memory cost = input_tensor + weight_tensor + output_tensor
    #             = NHWC + RSCK + NHWK
    # Note that this is a pointwise convolution with kernel 1x1.
    expected_cost = ((112 * 112 * 24 + 24 * 64 + 112 * 112 * 64) * 4
                     / resource_function.MEMORY_BANDWIDTH[hardware])
    self.assertAllClose(expected_cost, self.get_cost([op]))