Esempio n. 1
0
 def testReuseArgScope(self):
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     key_op = _key_op(func1)
     current_scope = {key_op: func1_kwargs.copy()}
     with self.cached_session():
         with arg_scope([func1], a=1, b=None, c=[1]) as scope1:
             pass
         with arg_scope(scope1) as scope:
             self.assertDictEqual(scope, current_scope)
Esempio n. 2
0
def overfeat_arg_scope(weight_decay=0.0005):
  with arg_scope(
      [layers.conv2d, layers_lib.fully_connected],
      activation_fn=nn_ops.relu,
      weights_regularizer=regularizers.l2_regularizer(weight_decay),
      biases_initializer=init_ops.zeros_initializer()):
    with arg_scope([layers.conv2d], padding='SAME'):
      with arg_scope([layers_lib.max_pool2d], padding='VALID') as arg_sc:
        return arg_sc
Esempio n. 3
0
 def testClearArgScope(self):
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     key_op = _key_op(func1)
     func1_scope = {key_op: func1_kwargs.copy()}
     with self.cached_session():
         with arg_scope([func1], a=1, b=None, c=[1]) as sc1:
             self.assertEqual(sc1, func1_scope)
             with arg_scope({}) as sc2:
                 self.assertEqual(sc2, {})
             with arg_scope([]) as current_arg_scope:
                 self.assertEqual(current_arg_scope, func1_scope)
Esempio n. 4
0
    def testNestedArgScopeObjectCreatedOutsideScopeOverridesArgScope(self):
        def get_scope_object():
            with arg_scope([func1], a=1, b=None, c=[1]) as sc:
                return sc

        scope_object = get_scope_object()
        with arg_scope([func1], b=2, d=10):
            with arg_scope(scope_object):
                args, kwargs = func1(0)
                self.assertTupleEqual(args, (0, ))
                self.assertDictEqual(kwargs, {'a': 1, 'b': None, 'c': [1]})
Esempio n. 5
0
 def testCurrentArgScopeNested(self):
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     func2_kwargs = {'b': 2, 'd': [2]}
     key = _key_op
     current_scope = {
         key(func1): func1_kwargs.copy(),
         key(func2): func2_kwargs.copy()
     }
     with self.cached_session():
         with arg_scope([func1], a=1, b=None, c=[1]):
             with arg_scope([func2], b=2, d=[2]) as scope:
                 self.assertDictEqual(scope, current_scope)
Esempio n. 6
0
 def testNestedArgScope(self):
     func1_args = (0, )
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     with arg_scope([func1], a=1, b=None, c=[1]):
         args, kwargs = func1(0)
         self.assertTupleEqual(args, func1_args)
         self.assertDictEqual(kwargs, func1_kwargs)
         func1_kwargs['b'] = 2
         with arg_scope([func1], b=2):
             args, kwargs = func1(0)
             self.assertTupleEqual(args, func1_args)
             self.assertDictEqual(kwargs, func1_kwargs)
Esempio n. 7
0
 def testPartiallySharedArgScope(self):
     func1_args = (0, )
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     func2_args = (1, )
     func2_kwargs = {'a': 1, 'b': None, 'd': [2]}
     with arg_scope([func1, func2], a=1, b=None):
         with arg_scope([func1], c=[1]):
             with arg_scope([func2], d=[2]):
                 args, kwargs = func1(0)
                 self.assertTupleEqual(args, func1_args)
                 self.assertDictEqual(kwargs, func1_kwargs)
                 args, kwargs = func2(1)
                 self.assertTupleEqual(args, func2_args)
                 self.assertDictEqual(kwargs, func2_kwargs)
Esempio n. 8
0
def inception_v3_arg_scope(weight_decay=0.00004,
                           batch_norm_var_collection='moving_vars',
                           batch_norm_decay=0.9997,
                           batch_norm_epsilon=0.001,
                           updates_collections=ops.GraphKeys.UPDATE_OPS,
                           use_fused_batchnorm=True):
    """Defines the default InceptionV3 arg scope.

  Args:
    weight_decay: The weight decay to use for regularizing the model.
    batch_norm_var_collection: The name of the collection for the batch norm
      variables.
    batch_norm_decay: Decay for batch norm moving average
    batch_norm_epsilon: Small float added to variance to avoid division by zero
    updates_collections: Collections for the update ops of the layer
    use_fused_batchnorm: Enable fused batchnorm.

  Returns:
    An `arg_scope` to use for the inception v3 model.
  """
    batch_norm_params = {
        # Decay for the moving averages.
        'decay': batch_norm_decay,
        # epsilon to prevent 0s in variance.
        'epsilon': batch_norm_epsilon,
        # collection containing update_ops.
        'updates_collections': updates_collections,
        # Use fused batch norm if possible.
        'fused': use_fused_batchnorm,
        # collection containing the moving mean and moving variance.
        'variables_collections': {
            'beta': None,
            'gamma': None,
            'moving_mean': [batch_norm_var_collection],
            'moving_variance': [batch_norm_var_collection],
        }
    }

    # Set weight_decay for weights in Conv and FC layers.
    with arg_scope(
        [layers.conv2d, layers_lib.fully_connected],
            weights_regularizer=regularizers.l2_regularizer(weight_decay)):
        with arg_scope([layers.conv2d],
                       weights_initializer=initializers.
                       variance_scaling_initializer(),
                       activation_fn=nn_ops.relu,
                       normalizer_fn=layers_lib.batch_norm,
                       normalizer_params=batch_norm_params) as sc:
            return sc
Esempio n. 9
0
    def testArgScopeObjectCreatedWithinScopeInheritsArgScope(self):
        def get_scope_object():
            with arg_scope([func1], a=1, b=None, c=[1]) as sc:
                return sc

        with arg_scope([func1], b=2, d=10):
            with arg_scope(get_scope_object()):
                args, kwargs = func1(0)
                self.assertTupleEqual(args, (0, ))
                self.assertDictEqual(kwargs, {
                    'a': 1,
                    'b': None,
                    'c': [1],
                    'd': 10
                })
Esempio n. 10
0
def inception_v1_arg_scope(weight_decay=0.00004,
                           use_batch_norm=True,
                           batch_norm_var_collection='moving_vars'):
    """Defines the default InceptionV1 arg scope.

  Note: Althougth the original paper didn't use batch_norm we found it useful.

  Args:
    weight_decay: The weight decay to use for regularizing the model.
    use_batch_norm: "If `True`, batch_norm is applied after each convolution.
    batch_norm_var_collection: The name of the collection for the batch norm
      variables.

  Returns:
    An `arg_scope` to use for the inception v3 model.
  """
    batch_norm_params = {
        # Decay for the moving averages.
        'decay': 0.9997,
        # epsilon to prevent 0s in variance.
        'epsilon': 0.001,
        # collection containing update_ops.
        'updates_collections': ops.GraphKeys.UPDATE_OPS,
        # collection containing the moving mean and moving variance.
        'variables_collections': {
            'beta': None,
            'gamma': None,
            'moving_mean': [batch_norm_var_collection],
            'moving_variance': [batch_norm_var_collection],
        }
    }
    if use_batch_norm:
        normalizer_fn = layers_lib.batch_norm
        normalizer_params = batch_norm_params
    else:
        normalizer_fn = None
        normalizer_params = {}
    # Set weight_decay for weights in Conv and FC layers.
    with arg_scope(
        [layers.conv2d, layers_lib.fully_connected],
            weights_regularizer=regularizers.l2_regularizer(weight_decay)):
        with arg_scope([layers.conv2d],
                       weights_initializer=initializers.
                       variance_scaling_initializer(),
                       activation_fn=nn_ops.relu,
                       normalizer_fn=normalizer_fn,
                       normalizer_params=normalizer_params) as sc:
            return sc
Esempio n. 11
0
    def testNonDecorated(self):
        def my_func(t, a=None):
            return (t, a)

        with self.assertRaises(ValueError):
            with arg_scope([my_func], a=1):
                pass
Esempio n. 12
0
 def testEndPointsV2(self):
   """Test the end points of a tiny v2 bottleneck network."""
   blocks = [
       resnet_v2.resnet_v2_block(
           'block1', base_depth=1, num_units=2, stride=2),
       resnet_v2.resnet_v2_block(
           'block2', base_depth=2, num_units=2, stride=1),
   ]
   inputs = create_test_input(2, 32, 16, 3)
   with arg_scope(resnet_utils.resnet_arg_scope()):
     _, end_points = self._resnet_plain(inputs, blocks, scope='tiny')
   expected = [
       'tiny/block1/unit_1/bottleneck_v2/shortcut',
       'tiny/block1/unit_1/bottleneck_v2/conv1',
       'tiny/block1/unit_1/bottleneck_v2/conv2',
       'tiny/block1/unit_1/bottleneck_v2/conv3',
       'tiny/block1/unit_2/bottleneck_v2/conv1',
       'tiny/block1/unit_2/bottleneck_v2/conv2',
       'tiny/block1/unit_2/bottleneck_v2/conv3',
       'tiny/block2/unit_1/bottleneck_v2/shortcut',
       'tiny/block2/unit_1/bottleneck_v2/conv1',
       'tiny/block2/unit_1/bottleneck_v2/conv2',
       'tiny/block2/unit_1/bottleneck_v2/conv3',
       'tiny/block2/unit_2/bottleneck_v2/conv1',
       'tiny/block2/unit_2/bottleneck_v2/conv2',
       'tiny/block2/unit_2/bottleneck_v2/conv3'
   ]
   self.assertItemsEqual(expected, end_points)
Esempio n. 13
0
 def _resnet_plain(self, inputs, blocks, output_stride=None, scope=None):
   """A plain ResNet without extra layers before or after the ResNet blocks."""
   with variable_scope.variable_scope(scope, values=[inputs]):
     with arg_scope([layers.conv2d], outputs_collections='end_points'):
       net = resnet_utils.stack_blocks_dense(inputs, blocks, output_stride)
       end_points = utils.convert_collection_to_dict('end_points')
       return net, end_points
Esempio n. 14
0
 def testAtrousFullyConvolutionalValues(self):
   """Verify dense feature extraction with atrous convolution."""
   nominal_stride = 32
   for output_stride in [4, 8, 16, 32, None]:
     with arg_scope(resnet_utils.resnet_arg_scope()):
       with ops.Graph().as_default():
         with self.cached_session() as sess:
           random_seed.set_random_seed(0)
           inputs = create_test_input(2, 81, 81, 3)
           # Dense feature extraction followed by subsampling.
           output, _ = self._resnet_small(
               inputs,
               None,
               is_training=False,
               global_pool=False,
               output_stride=output_stride)
           if output_stride is None:
             factor = 1
           else:
             factor = nominal_stride // output_stride
           output = resnet_utils.subsample(output, factor)
           # Make the two networks use the same weights.
           variable_scope.get_variable_scope().reuse_variables()
           # Feature extraction at the nominal network rate.
           expected, _ = self._resnet_small(
               inputs, None, is_training=False, global_pool=False)
           sess.run(variables.global_variables_initializer())
           self.assertAllClose(
               output.eval(), expected.eval(), atol=1e-4, rtol=1e-4)
Esempio n. 15
0
def vgg_arg_scope(weight_decay=0.0005):
    """Defines the VGG arg scope.

  Args:
    weight_decay: The l2 regularization coefficient.

  Returns:
    An arg_scope.
  """
    with arg_scope(
        [layers.conv2d, layers_lib.fully_connected],
            activation_fn=nn_ops.relu,
            weights_regularizer=regularizers.l2_regularizer(weight_decay),
            biases_initializer=init_ops.zeros_initializer()):
        with arg_scope([layers.conv2d], padding='SAME') as arg_sc:
            return arg_sc
Esempio n. 16
0
 def testOverwriteArgScope(self):
     func1_args = (0, )
     func1_kwargs = {'a': 1, 'b': 2, 'c': [1]}
     with arg_scope([func1], a=1, b=None, c=[1]):
         args, kwargs = func1(0, b=2)
         self.assertTupleEqual(args, func1_args)
         self.assertDictEqual(kwargs, func1_kwargs)
Esempio n. 17
0
    def testAtrousValuesBottleneck(self):
        """Verify the values of dense feature extraction by atrous convolution.

    Make sure that dense feature extraction by stack_blocks_dense() followed by
    subsampling gives identical results to feature extraction at the nominal
    network output stride using the simple self._stack_blocks_nondense() above.
    """
        block = resnet_v1.resnet_v1_block
        blocks = [
            block('block1', base_depth=1, num_units=2, stride=2),
            block('block2', base_depth=2, num_units=2, stride=2),
            block('block3', base_depth=4, num_units=2, stride=2),
            block('block4', base_depth=8, num_units=2, stride=1),
        ]
        nominal_stride = 8

        # Test both odd and even input dimensions.
        height = 30
        width = 31
        with arg_scope(resnet_utils.resnet_arg_scope()):
            with arg_scope([layers.batch_norm], is_training=False):
                for output_stride in [1, 2, 4, 8, None]:
                    with ops.Graph().as_default():
                        with self.cached_session() as sess:
                            random_seed.set_random_seed(0)
                            inputs = create_test_input(1, height, width, 3)
                            # Dense feature extraction followed by subsampling.
                            output = resnet_utils.stack_blocks_dense(
                                inputs, blocks, output_stride)
                            if output_stride is None:
                                factor = 1
                            else:
                                factor = nominal_stride // output_stride

                            output = resnet_utils.subsample(output, factor)
                            # Make the two networks use the same weights.
                            variable_scope.get_variable_scope(
                            ).reuse_variables()
                            # Feature extraction at the nominal network rate.
                            expected = self._stack_blocks_nondense(
                                inputs, blocks)
                            sess.run(variables.global_variables_initializer())
                            output, expected = sess.run([output, expected])
                            self.assertAllClose(output,
                                                expected,
                                                atol=1e-4,
                                                rtol=1e-4)
Esempio n. 18
0
def resnet_arg_scope(weight_decay=0.0001,
                     batch_norm_decay=0.997,
                     batch_norm_epsilon=1e-5,
                     batch_norm_scale=True):
    """Defines the default ResNet arg scope.

  TODO(gpapan): The batch-normalization related default values above are
    appropriate for use in conjunction with the reference ResNet models
    released at https://github.com/KaimingHe/deep-residual-networks. When
    training ResNets from scratch, they might need to be tuned.

  Args:
    weight_decay: The weight decay to use for regularizing the model.
    batch_norm_decay: The moving average decay when estimating layer activation
      statistics in batch normalization.
    batch_norm_epsilon: Small constant to prevent division by zero when
      normalizing activations by their variance in batch normalization.
    batch_norm_scale: If True, uses an explicit `gamma` multiplier to scale the
      activations in the batch normalization layer.

  Returns:
    An `arg_scope` to use for the resnet models.
  """
    batch_norm_params = {
        'decay': batch_norm_decay,
        'epsilon': batch_norm_epsilon,
        'scale': batch_norm_scale,
        'updates_collections': ops.GraphKeys.UPDATE_OPS,
    }

    with arg_scope(
        [layers_lib.conv2d],
            weights_regularizer=regularizers.l2_regularizer(weight_decay),
            weights_initializer=initializers.variance_scaling_initializer(),
            activation_fn=nn_ops.relu,
            normalizer_fn=layers.batch_norm,
            normalizer_params=batch_norm_params):
        with arg_scope([layers.batch_norm], **batch_norm_params):
            # The following implies padding='SAME' for pool1, which makes feature
            # alignment easier for dense prediction tasks. This is also used in
            # https://github.com/facebook/fb.resnet.torch. However the accompanying
            # code of 'Deep Residual Learning for Image Recognition' uses
            # padding='VALID' for pool1. You can switch to that choice by setting
            # tf.contrib.framework.arg_scope([tf.contrib.layers.max_pool2d], padding='VALID').
            with arg_scope([layers.max_pool2d], padding='SAME') as arg_sc:
                return arg_sc
Esempio n. 19
0
 def testSimpleArgScopeWithTuple(self):
     func1_args = (0, )
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     with self.cached_session():
         with arg_scope((func1, ), a=1, b=None, c=[1]):
             args, kwargs = func1(0)
             self.assertTupleEqual(args, func1_args)
             self.assertDictEqual(kwargs, func1_kwargs)
Esempio n. 20
0
 def testModelHasExpectedNumberOfParameters(self):
   batch_size = 5
   height, width = 224, 224
   inputs = random_ops.random_uniform((batch_size, height, width, 3))
   with arg_scope(inception_v2.inception_v2_arg_scope()):
     inception_v2.inception_v2_base(inputs)
   total_params, _ = model_analyzer.analyze_vars(
       variables_lib.get_model_variables())
   self.assertAlmostEqual(10173112, total_params)
Esempio n. 21
0
 def testSharedArgScopeTuple(self):
     func1_args = (0, )
     func1_kwargs = {'a': 1, 'b': None, 'c': [1]}
     with arg_scope((func1, func2), a=1, b=None, c=[1]):
         args, kwargs = func1(0)
         self.assertTupleEqual(args, func1_args)
         self.assertDictEqual(kwargs, func1_kwargs)
         args, kwargs = func2(0)
         self.assertTupleEqual(args, func1_args)
         self.assertDictEqual(kwargs, func1_kwargs)
Esempio n. 22
0
 def testClassificationEndPoints(self):
   global_pool = True
   num_classes = 10
   inputs = create_test_input(2, 224, 224, 3)
   with arg_scope(resnet_utils.resnet_arg_scope()):
     logits, end_points = self._resnet_small(
         inputs, num_classes, global_pool=global_pool, scope='resnet')
   self.assertTrue(logits.op.name.startswith('resnet/logits'))
   self.assertListEqual(logits.get_shape().as_list(), [2, 1, 1, num_classes])
   self.assertTrue('predictions' in end_points)
   self.assertListEqual(end_points['predictions'].get_shape().as_list(),
                        [2, 1, 1, num_classes])
Esempio n. 23
0
def inception_v2_arg_scope(weight_decay=0.00004,
                           batch_norm_var_collection='moving_vars'):
    """Defines the default InceptionV2 arg scope.

  Args:
    weight_decay: The weight decay to use for regularizing the model.
    batch_norm_var_collection: The name of the collection for the batch norm
      variables.

  Returns:
    An `arg_scope` to use for the inception v3 model.
  """
    batch_norm_params = {
        # Decay for the moving averages.
        'decay': 0.9997,
        # epsilon to prevent 0s in variance.
        'epsilon': 0.001,
        # collection containing update_ops.
        'updates_collections': ops.GraphKeys.UPDATE_OPS,
        # collection containing the moving mean and moving variance.
        'variables_collections': {
            'beta': None,
            'gamma': None,
            'moving_mean': [batch_norm_var_collection],
            'moving_variance': [batch_norm_var_collection],
        }
    }

    # Set weight_decay for weights in Conv and FC layers.
    with arg_scope(
        [layers.conv2d, layers_lib.fully_connected],
            weights_regularizer=regularizers.l2_regularizer(weight_decay)):
        with arg_scope([layers.conv2d],
                       weights_initializer=initializers.
                       variance_scaling_initializer(),
                       activation_fn=nn_ops.relu,
                       normalizer_fn=layers_lib.batch_norm,
                       normalizer_params=batch_norm_params) as sc:
            return sc
Esempio n. 24
0
 def testFullyConvolutionalUnknownHeightWidth(self):
   batch = 2
   height, width = 65, 65
   global_pool = False
   inputs = create_test_input(batch, None, None, 3)
   with arg_scope(resnet_utils.resnet_arg_scope()):
     output, _ = self._resnet_small(inputs, None, global_pool=global_pool)
   self.assertListEqual(output.get_shape().as_list(), [batch, None, None, 32])
   images = create_test_input(batch, height, width, 3)
   with self.cached_session() as sess:
     sess.run(variables.global_variables_initializer())
     output = sess.run(output, {inputs: images.eval()})
     self.assertEqual(output.shape, (batch, 3, 3, 32))
Esempio n. 25
0
 def testClassificationShapes(self):
   global_pool = True
   num_classes = 10
   inputs = create_test_input(2, 224, 224, 3)
   with arg_scope(resnet_utils.resnet_arg_scope()):
     _, end_points = self._resnet_small(
         inputs, num_classes, global_pool=global_pool, scope='resnet')
     endpoint_to_shape = {
         'resnet/block1': [2, 28, 28, 4],
         'resnet/block2': [2, 14, 14, 8],
         'resnet/block3': [2, 7, 7, 16],
         'resnet/block4': [2, 7, 7, 32]
     }
     for endpoint in endpoint_to_shape:
       shape = endpoint_to_shape[endpoint]
       self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape)
Esempio n. 26
0
 def testFullyConvolutionalEndpointShapes(self):
   global_pool = False
   num_classes = 10
   inputs = create_test_input(2, 321, 321, 3)
   with arg_scope(resnet_utils.resnet_arg_scope()):
     _, end_points = self._resnet_small(
         inputs, num_classes, global_pool=global_pool, scope='resnet')
     endpoint_to_shape = {
         'resnet/block1': [2, 41, 41, 4],
         'resnet/block2': [2, 21, 21, 8],
         'resnet/block3': [2, 11, 11, 16],
         'resnet/block4': [2, 11, 11, 32]
     }
     for endpoint in endpoint_to_shape:
       shape = endpoint_to_shape[endpoint]
       self.assertListEqual(end_points[endpoint].get_shape().as_list(), shape)
def _recomputing_grad_fn(compute_fn, original_args, original_vars,
                         output_grads, grad_fn_variables, use_data_dep,
                         tupleize_grads, arg_scope, var_scope,
                         has_is_recompute_kwarg):
    """Grad fn for recompute_grad."""
    variables = grad_fn_variables or []

    # Identity ops around the inputs ensures correct gradient graph-walking.
    inputs = [array_ops.identity(x) for x in list(original_args)]

    # Recompute outputs
    # Use a control dependency to ensure that the recompute is not eliminated by
    # CSE and that it happens on the backwards pass.
    ctrl_dep_grads = [g for g in output_grads if g is not None]
    with framework_ops.control_dependencies(ctrl_dep_grads):
        if use_data_dep:
            inputs = _force_data_dependency(output_grads, inputs)
        # Re-enter scopes
        with contrib_framework_ops.arg_scope(arg_scope):
            with variable_scope.variable_scope(var_scope, reuse=True):
                # Re-call the function and ensure that the touched variables are the
                # same as in the first call.
                with backprop.GradientTape() as tape:
                    fn_kwargs = {}
                    if has_is_recompute_kwarg:
                        fn_kwargs["is_recomputing"] = True
                    outputs = compute_fn(*inputs, **fn_kwargs)
                recompute_vars = set(tape.watched_variables())
                if original_vars != recompute_vars:
                    raise ValueError(_WRONG_VARS_ERR)

    if not isinstance(outputs, (list, tuple)):
        outputs = [outputs]
    outputs = list(outputs)

    # Compute gradients
    grads = gradients_impl.gradients(outputs, inputs + variables, output_grads)

    if tupleize_grads:
        if use_data_dep:
            grads = _tuple_with_data_dep(grads)
        else:
            grads = control_flow_ops.tuple(grads)

    grad_inputs = grads[:len(inputs)]
    grad_vars = grads[len(inputs):]
    return grad_inputs, grad_vars
Esempio n. 28
0
 def testUnknownBatchSize(self):
   batch = 2
   height, width = 65, 65
   global_pool = True
   num_classes = 10
   inputs = create_test_input(None, height, width, 3)
   with arg_scope(resnet_utils.resnet_arg_scope()):
     logits, _ = self._resnet_small(
         inputs, num_classes, global_pool=global_pool, scope='resnet')
   self.assertTrue(logits.op.name.startswith('resnet/logits'))
   self.assertListEqual(logits.get_shape().as_list(),
                        [None, 1, 1, num_classes])
   images = create_test_input(batch, height, width, 3)
   with self.cached_session() as sess:
     sess.run(variables.global_variables_initializer())
     output = sess.run(logits, {inputs: images.eval()})
     self.assertEqual(output.shape, (batch, 1, 1, num_classes))
Esempio n. 29
0
  def test_conv_layer(self):
    with compat.forward_compatibility_horizon(2019, 6, 7):
      g = ops.Graph()
      with g.as_default():
        inputs = array_ops.placeholder(dtypes.float32, shape=[8, 5, 5, 3])

      with contrib_ops.arg_scope([layers.batch_norm],
                                 fused=True,
                                 is_training=True,
                                 trainable=True):
        return layers.convolution(
            inputs,
            num_outputs=16,
            kernel_size=3,
            stride=1,
            padding='VALID',
            activation_fn=nn_ops.relu,
            normalizer_fn=layers.batch_norm,
            normalizer_params={},
            weights_initializer=initializers.xavier_initializer(),
            weights_regularizer=None,
            biases_initializer=init_ops.zeros_initializer(),
            biases_regularizer=None,
            reuse=None,
            trainable=True,
            scope=None)

      inputs_pattern = graph_matcher.OpTypePattern('*', name='inputs')
      relu_pattern = graph_matcher.OpTypePattern(
          'Relu',
          name='relu',
          inputs=[
              graph_matcher.OpTypePattern(
                  'FusedBatchNormV3',
                  inputs=[
                      graph_matcher.OpTypePattern(
                          'Conv2D', inputs=[inputs_pattern, '*']), '*', '*',
                      '*', '*'
                  ])
          ])
      matcher = graph_matcher.GraphMatcher(relu_pattern)
      match_results = list(matcher.match_graph(g))
      self.assertEqual(1, len(match_results))
      match_result = match_results[0]
      self.assertEqual(match_result.get_tensor(inputs_pattern), inputs)
      self.assertEqual(match_result.get_tensor('inputs'), inputs)
Esempio n. 30
0
def overfeat(inputs,
             num_classes=1000,
             is_training=True,
             dropout_keep_prob=0.5,
             spatial_squeeze=True,
             scope='overfeat'):
  """Contains the model definition for the OverFeat network.

  The definition for the network was obtained from:
    OverFeat: Integrated Recognition, Localization and Detection using
    Convolutional Networks
    Pierre Sermanet, David Eigen, Xiang Zhang, Michael Mathieu, Rob Fergus and
    Yann LeCun, 2014
    http://arxiv.org/abs/1312.6229

  Note: All the fully_connected layers have been transformed to conv2d layers.
        To use in classification mode, resize input to 231x231. To use in fully
        convolutional mode, set spatial_squeeze to false.

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    num_classes: number of predicted classes.
    is_training: whether or not the model is being trained.
    dropout_keep_prob: the probability that activations are kept in the dropout
      layers during training.
    spatial_squeeze: whether or not should squeeze the spatial dimensions of the
      outputs. Useful to remove unnecessary dimensions for classification.
    scope: Optional scope for the variables.

  Returns:
    the last op containing the log predictions and end_points dict.

  """
  with variable_scope.variable_scope(scope, 'overfeat', [inputs]) as sc:
    end_points_collection = sc.name + '_end_points'
    # Collect outputs for conv2d, fully_connected and max_pool2d
    with arg_scope(
        [layers.conv2d, layers_lib.fully_connected, layers_lib.max_pool2d],
        outputs_collections=end_points_collection):
      net = layers.conv2d(
          inputs, 64, [11, 11], 4, padding='VALID', scope='conv1')
      net = layers_lib.max_pool2d(net, [2, 2], scope='pool1')
      net = layers.conv2d(net, 256, [5, 5], padding='VALID', scope='conv2')
      net = layers_lib.max_pool2d(net, [2, 2], scope='pool2')
      net = layers.conv2d(net, 512, [3, 3], scope='conv3')
      net = layers.conv2d(net, 1024, [3, 3], scope='conv4')
      net = layers.conv2d(net, 1024, [3, 3], scope='conv5')
      net = layers_lib.max_pool2d(net, [2, 2], scope='pool5')
      with arg_scope(
          [layers.conv2d],
          weights_initializer=trunc_normal(0.005),
          biases_initializer=init_ops.constant_initializer(0.1)):
        # Use conv2d instead of fully_connected layers.
        net = layers.conv2d(net, 3072, [6, 6], padding='VALID', scope='fc6')
        net = layers_lib.dropout(
            net, dropout_keep_prob, is_training=is_training, scope='dropout6')
        net = layers.conv2d(net, 4096, [1, 1], scope='fc7')
        net = layers_lib.dropout(
            net, dropout_keep_prob, is_training=is_training, scope='dropout7')
        net = layers.conv2d(
            net,
            num_classes, [1, 1],
            activation_fn=None,
            normalizer_fn=None,
            biases_initializer=init_ops.zeros_initializer(),
            scope='fc8')
      # Convert end_points_collection into a end_point dict.
      end_points = utils.convert_collection_to_dict(end_points_collection)
      if spatial_squeeze:
        net = array_ops.squeeze(net, [1, 2], name='fc8/squeezed')
        end_points[sc.name + '/fc8'] = net
      return net, end_points