コード例 #1
0
 def testCreateConvNoRegularizers(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     conv2d_ws.conv2d(images, 32, [3, 3])
     self.assertEqual(
         tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES), [])
コード例 #2
0
ファイル: conv2d_ws_test.py プロジェクト: Matija-KK/models
 def testInvalidShape(self):
     with self.cached_session():
         images_3d = tf.random.uniform((5, 6, 7, 9, 3), seed=1)
         with self.assertRaisesRegexp(
                 ValueError,
                 'Convolution expects input with rank 4, got 5'):
             conv2d_ws.conv2d(images_3d, 32, 3)
コード例 #3
0
 def testNonReuseVars(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     conv2d_ws.conv2d(images, 32, [3, 3])
     self.assertEqual(len(contrib_framework.get_variables()), 2)
     conv2d_ws.conv2d(images, 32, [3, 3])
     self.assertEqual(len(contrib_framework.get_variables()), 4)
コード例 #4
0
 def testCreateConvCreatesWeightsAndBiasesVarsWithRateTwo(self):
   height, width = 7, 9
   images = tf.random_uniform((5, height, width, 3), seed=1)
   with self.cached_session():
     self.assertFalse(contrib_framework.get_variables('conv1/weights'))
     self.assertFalse(contrib_framework.get_variables('conv1/biases'))
     conv2d_ws.conv2d(images, 32, [3, 3], rate=2, scope='conv1')
     self.assertTrue(contrib_framework.get_variables('conv1/weights'))
     self.assertTrue(contrib_framework.get_variables('conv1/biases'))
コード例 #5
0
  def testFullyConvWithCustomGetter(self):
    height, width = 7, 9
    with self.cached_session():
      called = [0]

      def custom_getter(getter, *args, **kwargs):
        called[0] += 1
        return getter(*args, **kwargs)

      with tf.variable_scope('test', custom_getter=custom_getter):
        images = tf.random_uniform((5, height, width, 32), seed=1)
        conv2d_ws.conv2d(images, 64, images.get_shape()[1:3])
      self.assertEqual(called[0], 2)  # Custom getter called twice.
コード例 #6
0
 def testCreateConvWithWD(self):
   height, width = 7, 9
   weight_decay = 0.01
   with self.cached_session() as sess:
     images = tf.random_uniform((5, height, width, 3), seed=1)
     regularizer = contrib_layers.l2_regularizer(weight_decay)
     conv2d_ws.conv2d(images, 32, [3, 3], weights_regularizer=regularizer)
     l2_loss = tf.nn.l2_loss(
         contrib_framework.get_variables_by_name('weights')[0])
     wd = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)[0]
     self.assertEqual(wd.op.name, 'Conv/kernel/Regularizer/l2_regularizer')
     sess.run(tf.global_variables_initializer())
     self.assertAlmostEqual(sess.run(wd), weight_decay * l2_loss.eval())
コード例 #7
0
 def testReuseConvWithBatchNorm(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 32), seed=1)
     with contrib_framework.arg_scope([conv2d_ws.conv2d],
                                      normalizer_fn=contrib_layers.batch_norm,
                                      normalizer_params={'decay': 0.9}):
       net = conv2d_ws.conv2d(images, 32, [3, 3], scope='Conv')
       net = conv2d_ws.conv2d(net, 32, [3, 3], scope='Conv', reuse=True)
     self.assertEqual(len(contrib_framework.get_variables()), 4)
     self.assertEqual(
         len(contrib_framework.get_variables('Conv/BatchNorm')), 3)
     self.assertEqual(
         len(contrib_framework.get_variables('Conv_1/BatchNorm')), 0)
コード例 #8
0
 def testReuseConvWithWD(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     weight_decay = contrib_layers.l2_regularizer(0.01)
     with contrib_framework.arg_scope([conv2d_ws.conv2d],
                                      weights_regularizer=weight_decay):
       conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1')
       self.assertEqual(len(contrib_framework.get_variables()), 2)
       self.assertEqual(
           len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
       conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1', reuse=True)
       self.assertEqual(len(contrib_framework.get_variables()), 2)
       self.assertEqual(
           len(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)), 1)
コード例 #9
0
 def testCreateConvWithTensorShape(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     output = conv2d_ws.conv2d(images, 32, images.get_shape()[1:3])
     self.assertEqual(output.op.name, 'Conv/Relu')
     self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32])
コード例 #10
0
 def testCreateConvWithStride(self):
   height, width = 6, 8
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     output = conv2d_ws.conv2d(images, 32, [3, 3], stride=2)
     self.assertEqual(output.op.name, 'Conv/Relu')
     self.assertListEqual(output.get_shape().as_list(),
                          [5, height / 2, width / 2, 32])
コード例 #11
0
 def testCreateHorizontalConv(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 4), seed=1)
     output = conv2d_ws.conv2d(images, 32, [1, 3])
     self.assertEqual(output.op.name, 'Conv/Relu')
     self.assertListEqual(output.get_shape().as_list(), [5, height, width, 32])
     weights = contrib_framework.get_variables_by_name('weights')[0]
     self.assertListEqual(weights.get_shape().as_list(), [1, 3, 4, 32])
コード例 #12
0
 def testCreateConvWithCollection(self):
   height, width = 7, 9
   images = tf.random_uniform((5, height, width, 3), seed=1)
   with tf.name_scope('fe'):
     conv = conv2d_ws.conv2d(
         images, 32, [3, 3], outputs_collections='outputs', scope='Conv')
   output_collected = tf.get_collection('outputs')[0]
   self.assertEqual(output_collected.aliases, ['Conv'])
   self.assertEqual(output_collected, conv)
コード例 #13
0
def lite_bottleneck(inputs,
                    depth,
                    stride,
                    unit_rate=1,
                    rate=1,
                    outputs_collections=None,
                    scope=None):
    """Bottleneck residual unit variant with BN after convolutions.

  This is the original residual unit proposed in [1]. See Fig. 1(a) of [2] for
  its definition. Note that we use here the bottleneck variant which has an
  extra bottleneck layer.

  When putting together two consecutive ResNet blocks that use this unit, one
  should use stride = 2 in the last unit of the first block.

  Args:
    inputs: A tensor of size [batch, height, width, channels].
    depth: The depth of the ResNet unit output.
    stride: The ResNet unit's stride. Determines the amount of downsampling of
      the units output compared to its input.
    unit_rate: An integer, unit rate for atrous convolution.
    rate: An integer, rate for atrous convolution.
    outputs_collections: Collection to add the ResNet unit output.
    scope: Optional variable_scope.

  Returns:
    The ResNet unit's output.
  """
    with tf.variable_scope(scope, 'lite_bottleneck_v1', [inputs]) as sc:
        depth_in = slim.utils.last_dimension(inputs.get_shape(), min_rank=4)
        if depth == depth_in:
            shortcut = resnet_utils.subsample(inputs, stride, 'shortcut')
        else:
            shortcut = conv2d_ws.conv2d(inputs,
                                        depth, [1, 1],
                                        stride=stride,
                                        activation_fn=None,
                                        scope='shortcut')

        residual = conv2d_ws.conv2d_same(inputs,
                                         depth,
                                         3,
                                         1,
                                         rate=rate * unit_rate,
                                         scope='conv1')
        with slim.arg_scope([conv2d_ws.conv2d], activation_fn=None):
            residual = conv2d_ws.conv2d_same(residual,
                                             depth,
                                             3,
                                             stride,
                                             rate=rate * unit_rate,
                                             scope='conv2')
        output = tf.nn.relu(shortcut + residual)

        return slim.utils.collect_named_outputs(outputs_collections, sc.name,
                                                output)
コード例 #14
0
 def testCreateFullyConv(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 32), seed=1)
     output = conv2d_ws.conv2d(
         images, 64, images.get_shape()[1:3], padding='VALID')
     self.assertEqual(output.op.name, 'Conv/Relu')
     self.assertListEqual(output.get_shape().as_list(), [5, 1, 1, 64])
     biases = contrib_framework.get_variables_by_name('biases')[0]
     self.assertListEqual(biases.get_shape().as_list(), [64])
コード例 #15
0
 def testCreateConvNCHW(self):
   height, width = 7, 9
   with self.cached_session():
     images = np.random.uniform(size=(5, 4, height, width)).astype(np.float32)
     output = conv2d_ws.conv2d(images, 32, [3, 3], data_format='NCHW')
     self.assertEqual(output.op.name, 'Conv/Relu')
     self.assertListEqual(output.get_shape().as_list(), [5, 32, height, width])
     weights = contrib_framework.get_variables_by_name('weights')[0]
     self.assertListEqual(weights.get_shape().as_list(), [3, 3, 4, 32])
     biases = contrib_framework.get_variables_by_name('biases')[0]
     self.assertListEqual(biases.get_shape().as_list(), [32])
コード例 #16
0
  def testWithScope(self):
    num_filters = 32
    input_size = [5, 9, 11, 3]
    expected_size = [5, 5, 7, num_filters]

    images = tf.random_uniform(input_size, seed=1)
    output = conv2d_ws.conv2d(
        images, num_filters, [3, 3], rate=2, padding='VALID', scope='conv7')
    with self.cached_session() as sess:
      sess.run(tf.global_variables_initializer())
      self.assertEqual(output.op.name, 'conv7/Relu')
      self.assertListEqual(list(output.eval().shape), expected_size)
コード例 #17
0
  def testOutputSizeWithRateTwoThreeValidPadding(self):
    num_filters = 32
    input_size = [5, 10, 12, 3]
    expected_size = [5, 6, 6, num_filters]

    images = tf.random_uniform(input_size, seed=1)
    output = conv2d_ws.conv2d(
        images, num_filters, [3, 3], rate=[2, 3], padding='VALID')
    self.assertListEqual(list(output.get_shape().as_list()), expected_size)
    with self.cached_session() as sess:
      sess.run(tf.global_variables_initializer())
      self.assertEqual(output.op.name, 'Conv/Relu')
      self.assertListEqual(list(output.eval().shape), expected_size)
コード例 #18
0
  def testDynamicOutputSizeWithRateTwoValidPadding(self):
    num_filters = 32
    input_size = [5, 9, 11, 3]
    expected_size = [None, None, None, num_filters]
    expected_size_dynamic = [5, 5, 7, num_filters]

    with self.cached_session():
      images = tf.placeholder(np.float32, [None, None, None, input_size[3]])
      output = conv2d_ws.conv2d(
          images, num_filters, [3, 3], rate=2, padding='VALID')
      tf.global_variables_initializer().run()
      self.assertEqual(output.op.name, 'Conv/Relu')
      self.assertListEqual(output.get_shape().as_list(), expected_size)
      eval_output = output.eval({images: np.zeros(input_size, np.float32)})
      self.assertListEqual(list(eval_output.shape), expected_size_dynamic)
コード例 #19
0
  def testDynamicOutputSizeWithRateOneValidPaddingNCHW(self):
    if tf.test.is_gpu_available(cuda_only=True):
      num_filters = 32
      input_size = [5, 3, 9, 11]
      expected_size = [None, num_filters, None, None]
      expected_size_dynamic = [5, num_filters, 7, 9]

      with self.session(use_gpu=True):
        images = tf.placeholder(np.float32, [None, input_size[1], None, None])
        output = conv2d_ws.conv2d(
            images,
            num_filters, [3, 3],
            rate=1,
            padding='VALID',
            data_format='NCHW')
        tf.global_variables_initializer().run()
        self.assertEqual(output.op.name, 'Conv/Relu')
        self.assertListEqual(output.get_shape().as_list(), expected_size)
        eval_output = output.eval({images: np.zeros(input_size, np.float32)})
        self.assertListEqual(list(eval_output.shape), expected_size_dynamic)
コード例 #20
0
 def testInvalidDataFormat(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     with self.assertRaisesRegexp(ValueError, 'data_format'):
       conv2d_ws.conv2d(images, 32, 3, data_format='CHWN')
コード例 #21
0
 def testCreateConvWithoutActivation(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     output = conv2d_ws.conv2d(images, 32, [3, 3], activation_fn=None)
     self.assertEqual(output.op.name, 'Conv/BiasAdd')
コード例 #22
0
 def testCreateConvWithScope(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     output = conv2d_ws.conv2d(images, 32, [3, 3], scope='conv1')
     self.assertEqual(output.op.name, 'conv1/Relu')
コード例 #23
0
def resnet_v1_beta(inputs,
                   blocks,
                   num_classes=None,
                   is_training=None,
                   global_pool=True,
                   output_stride=None,
                   root_block_fn=None,
                   reuse=None,
                   scope=None,
                   sync_batch_norm_method='None'):
    """Generator for v1 ResNet models (beta variant).

  This function generates a family of modified ResNet v1 models. In particular,
  the first original 7x7 convolution is replaced with three 3x3 convolutions.
  See the resnet_v1_*() methods for specific model instantiations, obtained by
  selecting different block instantiations that produce ResNets of various
  depths.

  The code is modified from slim/nets/resnet_v1.py, and please refer to it for
  more details.

  Args:
    inputs: A tensor of size [batch, height_in, width_in, channels].
    blocks: A list of length equal to the number of ResNet blocks. Each element
      is a resnet_utils.Block object describing the units in the block.
    num_classes: Number of predicted classes for classification tasks. If None
      we return the features before the logit layer.
    is_training: Enable/disable is_training for batch normalization.
    global_pool: If True, we perform global average pooling before computing the
      logits. Set to True for image classification, False for dense prediction.
    output_stride: If None, then the output will be computed at the nominal
      network stride. If output_stride is not None, it specifies the requested
      ratio of input to output spatial resolution.
    root_block_fn: The function consisting of convolution operations applied to
      the root input. If root_block_fn is None, use the original setting of
      RseNet-v1, which is simply one convolution with 7x7 kernel and stride=2.
    reuse: whether or not the network and its variables should be reused. To be
      able to reuse 'scope' must be given.
    scope: Optional variable_scope.
    sync_batch_norm_method: String, sync batchnorm method.

  Returns:
    net: A rank-4 tensor of size [batch, height_out, width_out, channels_out].
      If global_pool is False, then height_out and width_out are reduced by a
      factor of output_stride compared to the respective height_in and width_in,
      else both height_out and width_out equal one. If num_classes is None, then
      net is the output of the last ResNet block, potentially after global
      average pooling. If num_classes is not None, net contains the pre-softmax
      activations.
    end_points: A dictionary from components of the network to the corresponding
      activation.

  Raises:
    ValueError: If the target output_stride is not valid.
  """
    if root_block_fn is None:
        root_block_fn = functools.partial(conv2d_ws.conv2d_same,
                                          num_outputs=64,
                                          kernel_size=7,
                                          stride=2,
                                          scope='conv1')
    batch_norm = utils.get_batch_norm_fn(sync_batch_norm_method)
    with tf.variable_scope(scope, 'resnet_v1', [inputs], reuse=reuse) as sc:
        end_points_collection = sc.original_name_scope + '_end_points'
        with slim.arg_scope([
                conv2d_ws.conv2d, bottleneck, lite_bottleneck,
                resnet_utils.stack_blocks_dense
        ],
                            outputs_collections=end_points_collection):
            if is_training is not None:
                arg_scope = slim.arg_scope([batch_norm],
                                           is_training=is_training)
            else:
                arg_scope = slim.arg_scope([])
            with arg_scope:
                net = inputs
                if output_stride is not None:
                    if output_stride % 4 != 0:
                        raise ValueError(
                            'The output_stride needs to be a multiple of 4.')
                    output_stride //= 4
                net = root_block_fn(net)
                net = slim.max_pool2d(net,
                                      3,
                                      stride=2,
                                      padding='SAME',
                                      scope='pool1')
                net = resnet_utils.stack_blocks_dense(net, blocks,
                                                      output_stride)

                if global_pool:
                    # Global average pooling.
                    net = tf.reduce_mean(net, [1, 2],
                                         name='pool5',
                                         keepdims=True)
                if num_classes is not None:
                    net = conv2d_ws.conv2d(net,
                                           num_classes, [1, 1],
                                           activation_fn=None,
                                           normalizer_fn=None,
                                           scope='logits',
                                           use_weight_standardization=False)
                # Convert end_points_collection into a dictionary of end_points.
                end_points = slim.utils.convert_collection_to_dict(
                    end_points_collection)
                if num_classes is not None:
                    end_points['predictions'] = slim.softmax(
                        net, scope='predictions')
                return net, end_points
コード例 #24
0
 def testCreateConvValid(self):
   height, width = 7, 9
   with self.cached_session():
     images = tf.random_uniform((5, height, width, 3), seed=1)
     output = conv2d_ws.conv2d(images, 32, [3, 3], padding='VALID')
     self.assertListEqual(output.get_shape().as_list(), [5, 5, 7, 32])