コード例 #1
1
 def testBlockSizeNotDivisibleBoth(self):
   # The block size does not divide neither width or height.
   x_np = [[[[1], [2]], [[3], [4]]]]
   paddings = np.zeros((2, 2), dtype=np.int32)
   block_size = 3
   with self.assertRaises(IndexError):
     _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #2
0
    def bottleneck(l, ch_out, stride, preact, rate=1):
        ch_in = l.get_shape().as_list()[-1]
        if preact == 'both_preact':
            l = tf.nn.relu(l, name='preact-relu')
        bottom_in = l
        with arg_scope(
            [layers.convolution2d],
                stride=1,
                padding='SAME',
                activation_fn=tf.nn.relu,
                normalizer_fn=layers.batch_norm,
                normalizer_params=bn_params,
                weights_initializer=init_func,
                weights_regularizer=layers.l2_regularizer(weight_decay)):

            l = layers.convolution2d(l,
                                     ch_out,
                                     kernel_size=1,
                                     stride=stride,
                                     scope='conv1')

            if rate > 1:
                l = tf.space_to_batch(l, paddings=PADDINGS, block_size=rate)
            l = layers.convolution2d(l, ch_out, kernel_size=3, scope='conv2')
            l = layers.convolution2d(l,
                                     ch_out * 4,
                                     kernel_size=1,
                                     activation_fn=None,
                                     scope='conv3')
            if rate > 1:
                l = tf.batch_to_space(l, crops=CROPS, block_size=rate)
            return l + shortcut(bottom_in, ch_in, ch_out * 4, stride)
コード例 #3
0
 def testBlockSizeNotDivisibleHeight(self):
     # The block size divides height but not width.
     x_np = [[[[1], [2]], [[3], [4]], [[5], [6]]]]
     paddings = np.zeros((2, 2), dtype=np.int32)
     block_size = 3
     with self.assertRaises(IndexError):
         _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #4
0
 def testBlockSizeNotDivisibleBoth(self):
     # The block size does not divide neither width or height.
     x_np = [[[[1], [2]], [[3], [4]]]]
     paddings = np.zeros((2, 2), dtype=np.int32)
     block_size = 3
     with self.assertRaises(IndexError):
         _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #5
0
 def vol3d_decoder(self, x, name='Vol3D_Decoder'):
     with argscope([Conv3DTranspose],
                   kernel_shape=3,
                   padding='SAME',
                   nl=tf.nn.leaky_relu):
         x = tf_2tanh(x)
         # x = tf.reshape(x, [128, 8, 8, 8, 3])
         x = tf.space_to_batch(x,
                               paddings=[[0, 0], [0, 0]],
                               block_size=32,
                               name='s2b')
         x = tf.reshape(x, [-1, 8, 8, 8, 3])
         x = tf.transpose(x, [4, 1, 2, 3, 0])  # #here 3x8x8x8x128
         x = (
             LinearWrap(x).Conv3DTranspose(
                 'conv5b', 64, strides=2,
                 padding='SAME')  #here 3x16x16x16x64
             .Conv3DTranspose('conv4b', 32, strides=2,
                              padding='SAME')  #here 3x32x32x32x32
             .Conv3DTranspose('conv3b', 16, strides=2,
                              padding='SAME')  #here 3x64x64x64x16
             .Conv3DTranspose('conv2b', 8, strides=2,
                              padding='SAME')  #here 3x128x128x128x8
             .Conv3DTranspose('conv1b',
                              1,
                              strides=2,
                              padding='SAME',
                              nl=tf.tanh)  #here 3x256x256x256x1
             ())
         x = tf_2imag(x)
         x = tf.transpose(x, [4, 1, 2, 3, 0])  # here 1x256x256x256x3
         x = tf.squeeze(x)
         return x
コード例 #6
0
 def testInputWrongDimMissingBatch(self):
   # The input is missing the first dimension ("batch")
   x_np = [[[1], [2]], [[3], [4]]]
   paddings = np.zeros((2, 2), dtype=np.int32)
   block_size = 2
   with self.assertRaises(ValueError):
     _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #7
0
 def testBlockSizeNotDivisibleWidth(self):
     # The block size divides width but not height.
     x_np = [[[[1], [2], [3]], [[3], [4], [7]]]]
     paddings = np.zeros((2, 2), dtype=np.int32)
     block_size = 3
     with self.assertRaises(ValueError):
         _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #8
0
def space_to_batch_execution():
    # about batch
    # https://stackoverflow.com/questions/41175401/what-is-a-batch-in-tensorflow
    with tf.Graph().as_default(), tf.Session():
        a = tf.constant([[[[1], [2]], [[3], [4]]]])
        print(a.get_shape())
        # from a.get_shape() possible to get data for such things as 4-D with shape [batch, height, width, depth].
        print("##########")
        # paddings in general change indexes by step equal padding/block_size
        # add additional zeros
        # block_size: Non-overlapping blocks of size block_size x block size in
        # the height and width dimensions are rearranged into the batch dimension at each location.
        # (so should save indexes of element from old to new?)
        # use to understand how much parts will be created from initial data
        stb = tf.space_to_batch(a, paddings=[[0, 0], [0, 0]], block_size=2)
        print(stb.get_shape())
        print(stb.eval())
        print("##########")
        # customize out put
        # divides "spatial" dimensions [1, ..., M] of the input into a grid of blocks of shape block_shape
        stbn = tf.space_to_batch_nd(a,
                                    block_shape=[1, 2],
                                    paddings=[[0, 0], [0, 0]])
        print(stbn.get_shape())
        print(stbn.eval())
コード例 #9
0
 def testBlockSizeNotDivisibleHeight(self):
   # The block size divides height but not width.
   x_np = [[[[1], [2]], [[3], [4]], [[5], [6]]]]
   paddings = np.zeros((2, 2), dtype=np.int32)
   block_size = 3
   with self.assertRaises(IndexError):
     _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #10
0
def conv_layers_2(x, bs, max_seq_len, im_dim):
    #x = tf.cast(x, tf.float32)
    wid, hei, chan = im_dim[0], im_dim[1], im_dim[2]
    try:
        x = tf.reshape(x, [bs * max_seq_len, wid, hei, chan])
    except:
        print('image dimensions not compatible')
        raise
    rate = 3
    rem_wid, rem_hei = rate * 2 - (wid % (rate * 2)) , rate * 2 - (hei % (rate * 2))
    pad = tf.constant([[np.floor(rem_hei / 2), np.ceil(rem_hei / 2)], [np.floor(rem_wid / 2), np.ceil(rem_wid / 2)]])
    pad = tf.cast(pad, tf.int32)
    filters1 = tf.Variable(tf.random_normal([3,3,3,5]), dtype=tf.float32)
    filters2 = tf.Variable(tf.random_normal([3,3,5,5]), dtype=tf.float32)
    filters3 = tf.Variable(tf.random_normal([3,3,5,5]), dtype=tf.float32)
    net = space_to_batch(x,paddings=pad,block_size=rate)
    net = tf.nn.conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME", name="dil_conv_1")
    # print "dil_conv_1"
    # print net.get_shape()
    # net = tf.nn.conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME", name="dil_conv_2")
    # print "dil_conv_2"
    # print net.get_shape()
    net = tf.nn.conv2d(net, filters3, strides=[1, 1, 1, 1], padding="SAME", name="dil_conv_3")
    # print "dil_conv_3"
    # print net.get_shape()
    net = batch_to_space(net, crops=pad, block_size=rate)
    # print "final_output"
    # print net.get_shape()
    output = tf.layers.flatten(net)
    # print "output"
    # print output.get_shape()
    return output
コード例 #11
0
ファイル: dilated.py プロジェクト: sjakati98/pixel_link
def _decomposed_dilated_conv2d(x,
                               kernel_size,
                               num_o,
                               dilation_factor,
                               name,
                               biased=False):
    """
	Decomposed dilated conv2d without BN or relu.
	"""
    # padding so that the input dims are multiples of dilation_factor
    H = tf.shape(x)[1]
    W = tf.shape(x)[2]
    pad_bottom = (dilation_factor -
                  H % dilation_factor) if H % dilation_factor != 0 else 0
    pad_right = (dilation_factor -
                 W % dilation_factor) if W % dilation_factor != 0 else 0
    pad = [[0, pad_bottom], [0, pad_right]]
    # decomposition to smaller-sized feature maps
    # [N,H,W,C] -> [N*d*d, H/d, W/d, C]
    o = tf.space_to_batch(x, paddings=pad, block_size=dilation_factor)
    # perform regular conv2d
    num_x = x.shape[3].value
    with tf.variable_scope(name) as scope:
        w = tf.get_variable('weights',
                            shape=[kernel_size, kernel_size, num_x, num_o])
        s = [1, 1, 1, 1]
        o = tf.nn.conv2d(o, w, s, padding='SAME')
        if biased:
            b = tf.get_variable('biases', shape=[num_o])
            o = tf.nn.bias_add(o, b)
    o = tf.batch_to_space(o, crops=pad, block_size=dilation_factor)
    return o
コード例 #12
0
 def testBlockSizeNotDivisibleWidth(self):
   # The block size divides width but not height.
   x_np = [[[[1], [2], [3]], [[3], [4], [7]]]]
   paddings = np.zeros((2, 2), dtype=np.int32)
   block_size = 3
   with self.assertRaises(ValueError):
     _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #13
0
 def testInputWrongDimMissingBatch(self):
     # The input is missing the first dimension ("batch")
     x_np = [[[1], [2]], [[3], [4]]]
     paddings = np.zeros((2, 2), dtype=np.int32)
     block_size = 2
     with self.assertRaises(ValueError):
         _ = tf.space_to_batch(x_np, paddings, block_size)
コード例 #14
0
def dense_block(net, size, growth, name, is_training=False, first=False,
                split=False, rate=1):
  with tf.variable_scope(name):
    for i in range(size):
      x = net
      #net, first_relu = layer(net, k, 'layer'+str(i), is_training, first=first)
      net = layer(net, growth, 'layer'+str(i), is_training, first=first)
      net = tf.concat([x, net], maps_dim)
      if first:
        first = False
      if split and i == (size // 2) - 1:
        split_out = net
        print('Split shape = ', net)
        if rate == 1:
          net = pool_func(net, 2, stride=2, padding='SAME', data_format=data_format)
        else:
          paddings, crops = tf.required_space_to_batch_paddings(image_size(net),
              [rate,rate])
          net = tf.space_to_batch(net, paddings=paddings, block_size=rate)
  if split and rate > 1:
    net = tf.batch_to_space(net, crops=crops, block_size=rate)
  print('Dense block out: ', net)
  if split:
    return net, split_out
  return net
コード例 #15
0
    def __init__(self, type, size, skip, name, stateful=False):
        super(DRNNLayer, self).__init__()

        types = {'gru': kl.GRU, 'lstm': kl.LSTM}
        assert type in types

        self.skip_size = skip

        self.explode = kl.Lambda(
            lambda x: tf.space_to_batch(x, [skip], paddings=[[0, 0]]),
            name='space_to_batch_%s' % name,
        )

        self.rnn = types[type](
            size,
            activation='tanh',
            return_sequences=True,
            stateful=stateful,
            name='rnn_%s_%s' % (type, name),
        )

        self.implode = kl.Lambda(
            lambda x: tf.batch_to_space(x, [skip], crops=[[0, 0]]),
            name='batch_to_space_%s' % name,
        )
コード例 #16
0
 def testSpaceToDepthTranspose(self):
     x = np.arange(5 * 10 * 16 * 7, dtype=np.float32).reshape([5, 10, 16, 7])
     block_size = 2
     paddings = np.zeros((2, 2), dtype=np.int32)
     y1 = tf.space_to_batch(x, paddings, block_size=block_size)
     y2 = tf.transpose(tf.space_to_depth(tf.transpose(x, [3, 1, 2, 0]), block_size=block_size), [3, 1, 2, 0])
     with self.test_session():
         self.assertAllEqual(y1.eval(), y2.eval())
コード例 #17
0
 def testBlockSizeOne(self):
   # The block size is 1. The block size needs to be > 1.
   x_np = [[[[1], [2]], [[3], [4]]]]
   paddings = np.zeros((2, 2), dtype=np.int32)
   block_size = 1
   with self.assertRaises(ValueError):
     out_tf = tf.space_to_batch(x_np, paddings, block_size)
     out_tf.eval()
コード例 #18
0
ファイル: LLayer.py プロジェクト: anivegesana/MSDNet
def Space2Batch(bottom, paddings, block_size=2, name=''):
    with tf.variable_scope(name):
        top = tf.space_to_batch(bottom,
                                paddings=paddings,
                                block_size=block_size,
                                name=name)
        print(bottom.name, '->', top.name)
        return top
コード例 #19
0
 def testBlockSizeLarger(self):
     # The block size is too large for this input.
     x_np = [[[[1], [2]], [[3], [4]]]]
     paddings = np.zeros((2, 2), dtype=np.int32)
     block_size = 10
     with self.assertRaises(IndexError):
         out_tf = tf.space_to_batch(x_np, paddings, block_size)
         out_tf.eval()
コード例 #20
0
 def testBlockSizeOne(self):
     # The block size is 1. The block size needs to be > 1.
     x_np = [[[[1], [2]], [[3], [4]]]]
     paddings = np.zeros((2, 2), dtype=np.int32)
     block_size = 1
     with self.assertRaises(ValueError):
         out_tf = tf.space_to_batch(x_np, paddings, block_size)
         out_tf.eval()
コード例 #21
0
 def testBlockSizeLarger(self):
   # The block size is too large for this input.
   x_np = [[[[1], [2]], [[3], [4]]]]
   paddings = np.zeros((2, 2), dtype=np.int32)
   block_size = 10
   with self.assertRaises(IndexError):
     out_tf = tf.space_to_batch(x_np, paddings, block_size)
     out_tf.eval()
コード例 #22
0
ファイル: nn_test.py プロジェクト: yaroslavvb/imperative
  def testAtrousSequence(self):
    """Tests optimization of sequence of atrous convolutions.

    Verifies that a sequence of `atrous_conv2d` operations with identical `rate`
    parameters, 'SAME' `padding`, and `filters` with odd heights/ widths:

        net = atrous_conv2d(net, filters1, rate, padding="SAME")
        net = atrous_conv2d(net, filters2, rate, padding="SAME")
        ...
        net = atrous_conv2d(net, filtersK, rate, padding="SAME")

    is equivalent to:

        pad = ...  # padding so that the input dims are multiples of rate
        net = space_to_batch(net, paddings=pad, block_size=rate)
        net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME")
        net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME")
        ...
        net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME")
        net = batch_to_space(net, crops=pad, block_size=rate)
    """
    padding = "SAME"  # The padding needs to be "SAME"
    np.random.seed(1)  # Make it reproducible.

    default_graph_controller = env.g.as_default()
    default_graph_controller.__enter__()

    with self.test_session():
      # Input: [batch, height, width, input_depth]
#      for height in range(15, 17):
      for height in range(15, 16):
#        for width in range(15, 17):
        for width in range(15, 16):
          x_shape = [3, height, width, 2]
          x = np.random.random_sample(x_shape).astype(np.float32)

          for kernel in [1]:  # The kernel size needs to be odd.
#          for kernel in [1, 3, 5]:  # The kernel size needs to be odd.
            # Filter: [kernel_height, kernel_width, input_depth, output_depth]
            f_shape = [kernel, kernel, 2, 2]
            f = 1e-2 * np.random.random_sample(f_shape).astype(np.float32)

            for rate in range(2, 3):
#            for rate in range(2, 4):
              # y1: three atrous_conv2d in a row.
              y1 = tf.nn.atrous_conv2d(x, f, rate, padding=padding)
              y1 = tf.nn.atrous_conv2d(y1, f, rate, padding=padding)
              y1 = tf.nn.atrous_conv2d(y1, f, rate, padding=padding)
              # y2: space_to_batch, three conv2d in a row, batch_to_space
              pad_bottom = 0 if height % rate == 0 else rate - height % rate
              pad_right = 0 if width % rate == 0 else rate - width % rate
              pad = [[0, pad_bottom], [0, pad_right]]
              y2 = tf.space_to_batch(x, paddings=pad, block_size=rate)
              y2 = tf.nn.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
              y2 = tf.nn.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
              y2 = tf.nn.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
              y2 = tf.batch_to_space(y2, crops=pad, block_size=rate)
              self.assertAllClose(y1.eval(), y2.eval(), rtol=1e-2, atol=1e-2)
コード例 #23
0
 def _testPad(self, inputs, paddings, block_size, outputs):
     for use_gpu in [False, True]:
         with self.test_session(use_gpu=use_gpu):
             # outputs = space_to_batch(inputs)
             x_tf = tf.space_to_batch(tf.to_float(inputs), paddings, block_size=block_size)
             self.assertAllEqual(x_tf.eval(), outputs)
             # inputs = batch_to_space(outputs)
             x_tf = tf.batch_to_space(tf.to_float(outputs), paddings, block_size=block_size)
             self.assertAllEqual(x_tf.eval(), inputs)
コード例 #24
0
 def testSpaceToDepthTranspose(self):
     x = np.arange(5 * 10 * 16 * 7,
                   dtype=np.float32).reshape([5, 10, 16, 7])
     block_size = 2
     paddings = np.zeros((2, 2), dtype=np.int32)
     y1 = tf.space_to_batch(x, paddings, block_size=block_size)
     y2 = tf.transpose(
         tf.space_to_depth(tf.transpose(x, [3, 1, 2, 0]),
                           block_size=block_size), [3, 1, 2, 0])
     with self.test_session():
         self.assertAllEqual(y1.eval(), y2.eval())
コード例 #25
0
    def _checkGrad(self, x, paddings, block_size):
        assert 4 == x.ndim
        with self.test_session():
            tf_x = tf.convert_to_tensor(x)
            tf_y = tf.space_to_batch(tf_x, paddings, block_size)
            epsilon = 1e-5
            ((x_jacob_t, x_jacob_n)) = tf.test.compute_gradient(
                tf_x, x.shape, tf_y, tf_y.get_shape().as_list(), x_init_value=x, delta=epsilon
            )

        self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=epsilon)
コード例 #26
0
 def _testPad(self, inputs, paddings, block_size, outputs):
     with self.test_session():
         # outputs = space_to_batch(inputs)
         x_tf = tf.space_to_batch(tf.to_float(inputs),
                                  paddings,
                                  block_size=block_size)
         self.assertAllEqual(x_tf.eval(), outputs)
         # inputs = batch_to_space(outputs)
         x_tf = tf.batch_to_space(tf.to_float(outputs),
                                  paddings,
                                  block_size=block_size)
         self.assertAllEqual(x_tf.eval(), inputs)
コード例 #27
0
  def testAtrousSequence(self):
    """Tests optimization of sequence of atrous convolutions.

    Verifies that a sequence of `atrous_conv2d` operations with identical `rate`
    parameters, 'SAME' `padding`, and `filters` with odd heights/ widths:

        net = atrous_conv2d(net, filters1, rate, padding="SAME")
        net = atrous_conv2d(net, filters2, rate, padding="SAME")
        ...
        net = atrous_conv2d(net, filtersK, rate, padding="SAME")

    is equivalent to:

        pad = ...  # padding so that the input dims are multiples of rate
        net = space_to_batch(net, paddings=pad, block_size=rate)
        net = conv2d(net, filters1, strides=[1, 1, 1, 1], padding="SAME")
        net = conv2d(net, filters2, strides=[1, 1, 1, 1], padding="SAME")
        ...
        net = conv2d(net, filtersK, strides=[1, 1, 1, 1], padding="SAME")
        net = batch_to_space(net, crops=pad, block_size=rate)
    """
    padding = "SAME"  # The padding needs to be "SAME"
    np.random.seed(1)  # Make it reproducible.

    with self.test_session():
      # Input: [batch, height, width, input_depth]
      for height in range(15, 17):
        for width in range(15, 17):
          x_shape = [3, height, width, 2]
          x = np.random.random_sample(x_shape).astype(np.float32)

          for kernel in [1, 3, 5]:  # The kernel size needs to be odd.
            # Filter: [kernel_height, kernel_width, input_depth, output_depth]
            f_shape = [kernel, kernel, 2, 2]
            f = 1e-2 * np.random.random_sample(f_shape).astype(np.float32)

            for rate in range(2, 4):
              # y1: three atrous_conv2d in a row.
              y1 = tf.nn.atrous_conv2d(x, f, rate, padding=padding)
              y1 = tf.nn.atrous_conv2d(y1, f, rate, padding=padding)
              y1 = tf.nn.atrous_conv2d(y1, f, rate, padding=padding)
              # y2: space_to_batch, three conv2d in a row, batch_to_space
              pad_bottom = 0 if height % rate == 0 else rate - height % rate
              pad_right = 0 if width % rate == 0 else rate - width % rate
              pad = [[0, pad_bottom], [0, pad_right]]
              y2 = tf.space_to_batch(x, paddings=pad, block_size=rate)
              y2 = tf.nn.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
              y2 = tf.nn.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
              y2 = tf.nn.conv2d(y2, f, strides=[1, 1, 1, 1], padding=padding)
              y2 = tf.batch_to_space(y2, crops=pad, block_size=rate)
              self.assertAllClose(y1.eval(), y2.eval(), rtol=1e-2, atol=1e-2)
コード例 #28
0
 def layer(l, layername, features, count, stride, rate=1, first=False):
     with tf.variable_scope(layername):
         with tf.variable_scope('block0'):
             l = bottleneck(l, features, stride,
                            'no_preact' if first else 'both_preact', rate)
         if rate > 1:
             l = tf.space_to_batch(l, paddings=PADDINGS, block_size=rate)
         for i in range(1, count):
             with tf.variable_scope('block{}'.format(i)):
                 l = bottleneck(l, features, 1, 'both_preact')
         if rate > 1:
             assert stride == 1
             l = tf.batch_to_space(l, crops=CROPS, block_size=rate)
         return l
コード例 #29
0
    def _checkGrad(self, x, paddings, block_size):
        assert 4 == x.ndim
        with self.test_session():
            tf_x = tf.convert_to_tensor(x)
            tf_y = tf.space_to_batch(tf_x, paddings, block_size)
            epsilon = 1e-5
            ((x_jacob_t, x_jacob_n)) = tf.test.compute_gradient(
                tf_x,
                x.shape,
                tf_y,
                tf_y.get_shape().as_list(),
                x_init_value=x,
                delta=epsilon)

        self.assertAllClose(x_jacob_t, x_jacob_n, rtol=1e-2, atol=epsilon)
コード例 #30
0
    def create_space_to_batch_net(self, in_shape, pads_value,
                                  block_shape_value, out_shape, ir_version):
        """
            Tensorflow net               IR net

            Input->SpaceToBatch        =>      Input->SpaceToBatch

        """

        #
        #   Create Tensorflow model
        #

        import tensorflow as tf

        tf.compat.v1.reset_default_graph()

        # Create the graph and model
        with tf.compat.v1.Session() as sess:
            x = tf.compat.v1.placeholder(tf.float32, in_shape, 'Input')
            pads = tf.constant(pads_value)
            block_shape = tf.constant(block_shape_value)
            tf.space_to_batch(x, block_shape, pads, name='Operation')

            tf.compat.v1.global_variables_initializer()
            tf_net = sess.graph_def

        #
        #   Create reference IR net
        #   Please, specify 'type': 'Input' for input node
        #   Moreover, do not forget to validate ALL layer attributes!!!
        #

        ref_net = None

        return tf_net, ref_net
コード例 #31
0
def DilatedConv_Decompose(x, k, num_out, factor, name, biased=False):
    H = tf.shape(x)[1]
    W = tf.shape(x)[2]
    pad_bottom = (factor - H % factor) if H % factor != 0 else 0
    pad_right = (factor - W % factor) if W % factor != 0 else 0
    pad = [[0, pad_bottom], [0, pad_right]]
    output = tf.space_to_batch(x, paddings=pad, block_size=factor)
    num_input = x.shape[3].value
    with tf.variable_scope(name) as scope:
        w = tf.get_variable('weights', shape=[k, k, num_input, num_out])
        s = [1, 1, 1, 1]
        output = tf.nn.conv2d(output, w, s, padding='SAME')
        if biased:
            bias = tf.get_variable('biases', shape=[num_out])
            output = tf.nn.bias_add(output, bias)
    output = tf.batch_to_space(output, crops=pad, block_size=factor)
    return output
コード例 #32
0
def atrous_conv2d(value, filters, rate, name):
    """ Returns the result of a convolution with holes from value and filters.
    Do not use the tensorflow implementation because of issues with shape definition
    of the result. The semantic is the same.
    It uses only the "VALID" padding.

    Warning: this implementation is PGNet specific. It's used only to define the last
    convolutional layer and therefore depends on pgnet constants
    """

    pad_top = 0
    pad_bottom = 0
    pad_left = 0
    pad_right = 0

    in_height = value.get_shape()[1].value + pad_top + pad_bottom
    in_width = value.get_shape()[2].value + pad_left + pad_right

    # More padding so that rate divides the height and width of the input.
    pad_bottom_extra = (rate - in_height % rate) % rate
    pad_right_extra = (rate - in_width % rate) % rate

    # The paddings argument to space_to_batch includes both padding components.
    space_to_batch_pad = ((pad_top, pad_bottom + pad_bottom_extra),
                          (pad_left, pad_right + pad_right_extra))

    value = tf.space_to_batch(input=value,
                              paddings=space_to_batch_pad,
                              block_size=rate)

    value = tf.nn.conv2d(input=value,
                         filter=filters,
                         strides=(1, LAST_CONV_OUTPUT_STRIDE,
                                  LAST_CONV_OUTPUT_STRIDE, 1),
                         padding="VALID",
                         name=name)

    # The crops argument to batch_to_space is just the extra padding component.
    batch_to_space_crop = ((0, pad_bottom_extra), (0, pad_right_extra))

    value = tf.batch_to_space(input=value,
                              crops=batch_to_space_crop,
                              block_size=rate)

    return value
コード例 #33
0
ファイル: model.py プロジェクト: galeone/pgnet
def atrous_conv2d(value, filters, rate, name):
    """ Returns the result of a convolution with holes from value and filters.
    Do not use the tensorflow implementation because of issues with shape definition
    of the result. The semantic is the same.
    It uses only the "VALID" padding.

    Warning: this implementation is PGNet specific. It's used only to define the last
    convolutional layer and therefore depends on pgnet constants
    """

    pad_top = 0
    pad_bottom = 0
    pad_left = 0
    pad_right = 0

    in_height = value.get_shape()[1].value + pad_top + pad_bottom
    in_width = value.get_shape()[2].value + pad_left + pad_right

    # More padding so that rate divides the height and width of the input.
    pad_bottom_extra = (rate - in_height % rate) % rate
    pad_right_extra = (rate - in_width % rate) % rate

    # The paddings argument to space_to_batch includes both padding components.
    space_to_batch_pad = ((pad_top, pad_bottom + pad_bottom_extra),
                          (pad_left, pad_right + pad_right_extra))

    value = tf.space_to_batch(
        input=value, paddings=space_to_batch_pad, block_size=rate)

    value = tf.nn.conv2d(
        input=value,
        filter=filters,
        strides=(1, LAST_CONV_OUTPUT_STRIDE, LAST_CONV_OUTPUT_STRIDE, 1),
        padding="VALID",
        name=name)

    # The crops argument to batch_to_space is just the extra padding component.
    batch_to_space_crop = ((0, pad_bottom_extra), (0, pad_right_extra))

    value = tf.batch_to_space(
        input=value, crops=batch_to_space_crop, block_size=rate)

    return value
コード例 #34
0
def _smoothed_dilated_conv2d_GI(x,
                                kernel_size,
                                num_o,
                                dilation_factor,
                                name,
                                top_scope,
                                biased=False):
    """
    Smoothed dilated conv2d via the Group Interaction (GI) layer without BN or relu.
    """
    # padding so that the input dims are multiples of dilation_factor
    H = tf.shape(x)[1]
    W = tf.shape(x)[2]
    pad_bottom = (dilation_factor -
                  H % dilation_factor) if H % dilation_factor != 0 else 0
    pad_right = (dilation_factor -
                 W % dilation_factor) if W % dilation_factor != 0 else 0
    pad = [[0, pad_bottom], [0, pad_right]]
    # decomposition to smaller-sized feature maps
    # [N,H,W,C] -> [N*d*d, H/d, W/d, C]
    o = tf.space_to_batch(x, paddings=pad, block_size=dilation_factor)
    # perform regular conv2d
    num_x = x.shape[3].value
    with tf.variable_scope(name) as scope:
        w = tf.get_variable('weights',
                            shape=[kernel_size, kernel_size, num_x, num_o])
        s = [1, 1, 1, 1]
        o = tf.nn.conv2d(o, w, s, padding='SAME')
        fix_w = tf.Variable(tf.eye(dilation_factor * dilation_factor),
                            name='fix_w')
        l = tf.split(o, dilation_factor * dilation_factor, axis=0)
        os = []
        for i in six.moves.range(0, dilation_factor * dilation_factor):
            os.append(fix_w[0, i] * l[i])
            for j in six.moves.range(1, dilation_factor * dilation_factor):
                os[i] += fix_w[j, i] * l[j]
        o = tf.concat(os, axis=0)
        if biased:
            b = tf.get_variable('biases', shape=[num_o])
            o = tf.nn.bias_add(o, b)
    o = tf.batch_to_space(o, crops=pad, block_size=dilation_factor)
    return o
コード例 #35
0
 def vol3d_decoder(self, x, name='Vol3D_Decoder'):
     with argscope([Conv3DTranspose],
                   kernel_shape=4,
                   padding='SAME',
                   nl=tf.nn.elu):
         # x = x - VGG19_MEAN_TENSOR
         # x = BNLReLU(x)
         x = tf_2tanh(x)
         # x = x/255.0
         x = tf.space_to_batch(x,
                               paddings=[[0, 0], [0, 0]],
                               block_size=64,
                               name='s2b')
         x = tf.reshape(x, [-1, 4, 4, 4, 3])
         x = tf.transpose(x, [4, 1, 2, 3, 0])  # #
         x = (
             LinearWrap(x).Conv3DTranspose('conv6a',
                                           256,
                                           strides=2,
                                           padding='SAME')  #
             .Conv3DTranspose('conv5a', 128, strides=2, padding='SAME')  #
             .Conv3DTranspose('conv4a', 64, strides=2, padding='SAME')  #
             .Conv3DTranspose('conv3a', 32, strides=2, padding='SAME')  #
             .Conv3DTranspose('conv2a', 16, strides=2, padding='SAME')  #
             .Conv3DTranspose('conv1a',
                              1,
                              strides=2,
                              padding='SAME',
                              use_bias=True,
                              nl=tf.tanh)  #
             ())
         x = tf.transpose(x, [4, 1, 2, 3, 0])  #
         x = tf.squeeze(x)
         # x = x*255.0
         x = tf_2imag(x)
         # x = x + VGG19_MEAN_TENSOR
         return x
コード例 #36
0
 def space_to_batch(*args, **kwargs):
   return tf.space_to_batch(*args, **kwargs)
コード例 #37
0
 def testUnknownShape(self):
     t = tf.space_to_batch(tf.placeholder(tf.float32), tf.placeholder(tf.int32), block_size=4)
     self.assertEqual(4, t.get_shape().ndims)
コード例 #38
0
 def testUnknownShape(self):
     t = tf.space_to_batch(tf.placeholder(tf.float32),
                           tf.placeholder(tf.int32),
                           block_size=4)
     self.assertEqual(4, t.get_shape().ndims)
コード例 #39
0
def _clahe(image: TensorLike, clip_limit: Number,
           tile_grid_size: Union[List[int], Tuple[int]],
           gpu_optimized: bool) -> tf.Tensor:
    """Implements CLAHE as tf ops"""
    original_2d_shape = (tf.shape(image)[0], tf.shape(image)[1])
    original_dtype = image.dtype

    # Need image in int32 format for later gather_nd ops
    image = tf.cast(image, tf.int32)

    tile_shape = tf.truediv(original_2d_shape, tile_grid_size)
    tile_shape = tf.cast(tf.math.ceil(tile_shape), tf.int32)

    # Reflection-pad image
    pad_y = 0
    pad_x = 0

    if original_2d_shape[0] % tile_shape[0] != 0:
        pad_y = tile_shape[0] - (original_2d_shape[0] % tile_shape[0])

    if original_2d_shape[1] % tile_shape[1] != 0:
        pad_x = tile_shape[1] - (original_2d_shape[1] % tile_shape[1])

    image_padded = tf.pad(image, [[0, pad_y], [0, pad_x], [0, 0]], "REFLECT")

    all_tiles = tf.space_to_batch(
        input=tf.expand_dims(image_padded, axis=0),
        block_shape=tile_shape,
        paddings=[[0, 0], [0, 0]],
    )

    # Compute per-tile histogram
    if gpu_optimized:
        hists = tf.math.reduce_sum(tf.one_hot(all_tiles,
                                              depth=256,
                                              on_value=1,
                                              off_value=0,
                                              axis=0),
                                   axis=1)
    else:
        single_dimension_tiles = tf.reshape(
            all_tiles,
            (
                tile_shape[0] * tile_shape[1],
                tile_grid_size[0] * tile_grid_size[1] * tf.shape(image)[-1],
            ),
        )

        single_dimension_tiles = tf.transpose(single_dimension_tiles)
        hists = tf.math.bincount(single_dimension_tiles,
                                 minlength=256,
                                 maxlength=256,
                                 axis=-1)

        hists = tf.transpose(hists)
        hists = tf.reshape(
            hists,
            (256, tile_grid_size[0], tile_grid_size[1], tf.shape(image)[-1]))

    if clip_limit > 0:
        clip_limit_actual = tf.cast(
            clip_limit * ((tile_shape[0] * tile_shape[1]) / 256), tf.int32)
        clipped_hists = tf.clip_by_value(hists,
                                         clip_value_min=0,
                                         clip_value_max=clip_limit_actual)
        clipped_px_count = tf.math.reduce_sum(hists - clipped_hists, axis=0)
        clipped_hists = tf.cast(clipped_hists, tf.float32)
        clipped_px_count = tf.cast(clipped_px_count, tf.float32)
        clipped_hists = clipped_hists + tf.math.truediv(clipped_px_count, 256)
    else:
        clipped_hists = tf.cast(hists, tf.float32)

    cdf = tf.math.cumsum(clipped_hists, axis=0)
    cdf_min = tf.math.reduce_min(cdf, axis=0)

    numerator = cdf - cdf_min
    denominator = tf.cast(tile_shape[0] * tile_shape[1], tf.float32) - cdf_min

    cdf_normalized = tf.round(
        tf.math.divide_no_nan(numerator, denominator) * (255))
    cdf_normalized = tf.cast(cdf_normalized, tf.int32)

    # Reflection-pad the cdf functions so that we don't have to explicitly deal with corners/edges
    cdf_padded = tf.pad(cdf_normalized, [[0, 0], [1, 1], [1, 1], [0, 0]],
                        mode="SYMMETRIC")

    coords = tf.stack(
        tf.meshgrid(
            tf.range(tf.shape(image_padded)[0]),
            tf.range(tf.shape(image_padded)[1]),
            tf.range(tf.shape(image_padded)[2]),
            indexing="ij",
        ))

    y_coords = coords[0, :, :]
    x_coords = coords[1, :, :]
    z_coords = coords[2, :, :]

    half_tile_shape = tf.math.floordiv(tile_shape, 2)

    nw_y_component = tf.math.floordiv(y_coords - half_tile_shape[0],
                                      tile_shape[0])
    nw_x_component = tf.math.floordiv(x_coords - half_tile_shape[1],
                                      tile_shape[1])

    # Need to correct negative values because negative-indexing for gather_nd ops
    # not supported on all processors (cdf is padded to account for this)
    nw_y_component = nw_y_component + 1
    nw_x_component = nw_x_component + 1

    ne_y_component = nw_y_component
    ne_x_component = nw_x_component + 1

    sw_y_component = nw_y_component + 1
    sw_x_component = nw_x_component

    se_y_component = sw_y_component
    se_x_component = sw_x_component + 1

    def cdf_transform(x_comp, y_comp):
        gatherable = tf.stack([image_padded, y_comp, x_comp, z_coords],
                              axis=-1)
        return tf.cast(tf.gather_nd(cdf_padded, gatherable), tf.float32)

    nw_transformed = cdf_transform(nw_x_component, nw_y_component)
    ne_transformed = cdf_transform(ne_x_component, ne_y_component)
    sw_transformed = cdf_transform(sw_x_component, sw_y_component)
    se_transformed = cdf_transform(se_x_component, se_y_component)

    a = (y_coords - half_tile_shape[0]) % tile_shape[0]
    a = tf.cast(tf.math.truediv(a, tile_shape[0]), tf.float32)
    b = (x_coords - half_tile_shape[1]) % tile_shape[1]
    b = tf.cast(tf.math.truediv(b, tile_shape[1]), tf.float32)

    # Interpolate
    interpolated = (
        a * (b * se_transformed +
             (1 - b) * sw_transformed)) + (1 - a) * (b * ne_transformed +
                                                     (1 - b) * nw_transformed)

    # Return image to original size and dtype
    interpolated = interpolated[0:original_2d_shape[0],
                                0:original_2d_shape[1], :]
    interpolated = tf.cast(tf.round(interpolated), original_dtype)

    return interpolated
コード例 #40
0
def build(inputs):
    weight_decay = 5e-4
    bn_params = {
        # Decay for the moving averages.
        'decay': 0.999,
        'center': True,
        'scale': True,
        # epsilon to prevent 0s in variance.
        'epsilon': 0.001,
        # None to force the updates
        'updates_collections': None,
        'is_training': False,
    }
    with tf.contrib.framework.arg_scope(
        [layers.convolution2d],
            kernel_size=3,
            stride=1,
            padding='SAME',
            rate=1,
            activation_fn=tf.nn.relu,
            # normalizer_fn=layers.batch_norm, normalizer_params=bn_params,
            # weights_initializer=layers.variance_scaling_initializer(),
            normalizer_fn=None,
            weights_initializer=None,
            weights_regularizer=layers.l2_regularizer(weight_decay)):
        import pdb
        #pdb.set_trace()
        net = layers.convolution2d(inputs, 64, scope='conv1_1')
        net = layers.convolution2d(net, 64, scope='conv1_2')
        net = layers.max_pool2d(net, 2, 2, scope='pool1')
        net = layers.convolution2d(net, 128, scope='conv2_1')
        net = layers.convolution2d(net, 128, scope='conv2_2')
        net = layers.max_pool2d(net, 2, 2, scope='pool2')
        net = layers.convolution2d(net, 256, scope='conv3_1')
        net = layers.convolution2d(net, 256, scope='conv3_2')
        net = layers.convolution2d(net, 256, scope='conv3_3')

        paddings = [[0, 0], [0, 0]]
        crops = [[0, 0], [0, 0]]

        block_size = 2
        net = tf.space_to_batch(net, paddings=paddings, block_size=block_size)
        net = layers.convolution2d(net, 512, scope='conv4_1')
        net = layers.convolution2d(net, 512, scope='conv4_2')
        net = layers.convolution2d(net, 512, scope='conv4_3')
        net = tf.batch_to_space(net, crops=crops, block_size=block_size)

        block_size = 4
        net = tf.space_to_batch(net, paddings=paddings, block_size=block_size)
        net = layers.convolution2d(net, 512, scope='conv5_1')
        net = layers.convolution2d(net, 512, scope='conv5_2')
        net = layers.convolution2d(net, 512, scope='conv5_3')
        net = tf.batch_to_space(net, crops=crops, block_size=block_size)

    with tf.contrib.framework.arg_scope(
        [layers.convolution2d],
            stride=1,
            padding='SAME',
            weights_initializer=layers.variance_scaling_initializer(),
            activation_fn=tf.nn.relu,
            normalizer_fn=layers.batch_norm,
            normalizer_params=bn_params,
            weights_regularizer=layers.l2_regularizer(1e-3)):
        net = layers.convolution2d(net,
                                   512,
                                   kernel_size=3,
                                   scope='conv6_1',
                                   rate=4)

    logits = layers.convolution2d(net,
                                  19,
                                  1,
                                  padding='SAME',
                                  activation_fn=None,
                                  scope='unary_2',
                                  rate=2)
    print('logits', logits.get_shape())
    #logits=tf.image.resize_bilinear(logits,[256,512],name='resize_score')

    return logits
コード例 #41
0
    def build(self, rgb, train=False, num_classes=20, random_init_fc8=False,
              debug=False, use_dilated=False):
        """
        Build the VGG model using loaded weights
        Parameters
        ----------
        rgb: image batch tensor
            Image in rgb shap. Scaled to Intervall [0, 255]
        train: bool
            Whether to build train or inference graph
        num_classes: int
            How many classes should be predicted (by fc8)
        random_init_fc8 : bool
            Whether to initialize fc8 layer randomly.
            Finetuning is required in this case.
        debug: bool
            Whether to print additional Debug Information.
        """
        # Convert RGB to BGR

        with tf.name_scope('Processing'):

            red, green, blue = tf.split(rgb, 3, 3)
            # assert red.get_shape().as_list()[1:] == [224, 224, 1]
            # assert green.get_shape().as_list()[1:] == [224, 224, 1]
            # assert blue.get_shape().as_list()[1:] == [224, 224, 1]
            bgr = tf.concat([
                blue - VGG_MEAN[0],
                green - VGG_MEAN[1],
                red - VGG_MEAN[2],
            ], 3)

            if debug:
                bgr = tf.Print(bgr, [tf.shape(bgr)],
                               message='Shape of input image: ',
                               summarize=4, first_n=1)

        self.conv1_1 = self._conv_layer(bgr, "conv1_1")
        self.conv1_2 = self._conv_layer(self.conv1_1, "conv1_2")
        self.pool1 = self._max_pool(self.conv1_2, 'pool1', debug)

        self.conv2_1 = self._conv_layer(self.pool1, "conv2_1")
        self.conv2_2 = self._conv_layer(self.conv2_1, "conv2_2")
        self.pool2 = self._max_pool(self.conv2_2, 'pool2', debug)

        self.conv3_1 = self._conv_layer(self.pool2, "conv3_1")
        self.conv3_2 = self._conv_layer(self.conv3_1, "conv3_2")
        self.conv3_3 = self._conv_layer(self.conv3_2, "conv3_3")
        self.pool3 = self._max_pool(self.conv3_3, 'pool3', debug)

        self.conv4_1 = self._conv_layer(self.pool3, "conv4_1")
        self.conv4_2 = self._conv_layer(self.conv4_1, "conv4_2")
        self.conv4_3 = self._conv_layer(self.conv4_2, "conv4_3")

        if use_dilated:
            pad = [[0, 0], [0, 0]]
            self.pool4 = tf.nn.max_pool(self.conv4_3, ksize=[1, 2, 2, 1],
                                        strides=[1, 1, 1, 1],
                                        padding='SAME', name='pool4')
            self.pool4 = tf.space_to_batch(self.pool4,
                                           paddings=pad, block_size=2)
        else:
            self.pool4 = self._max_pool(self.conv4_3, 'pool4', debug)

        self.conv5_1 = self._conv_layer(self.pool4, "conv5_1")
        self.conv5_2 = self._conv_layer(self.conv5_1, "conv5_2")
        self.conv5_3 = self._conv_layer(self.conv5_2, "conv5_3")
        if use_dilated:
            pad = [[0, 0], [0, 0]]
            self.pool5 = tf.nn.max_pool(self.conv5_3, ksize=[1, 2, 2, 1],
                                        strides=[1, 1, 1, 1],
                                        padding='SAME', name='pool5')
            self.pool5 = tf.space_to_batch(self.pool5,
                                           paddings=pad, block_size=2)
        else:
            self.pool5 = self._max_pool(self.conv5_3, 'pool5', debug)

        self.fc6 = self._fc_layer(self.pool5, "fc6")

        if train:
            self.fc6 = tf.nn.dropout(self.fc6, 0.5)

        self.fc7 = self._fc_layer(self.fc6, "fc7")
        if train:
            self.fc7 = tf.nn.dropout(self.fc7, 0.5)

        if use_dilated:
            self.pool5 = tf.batch_to_space(self.pool5, crops=pad, block_size=2)
            self.pool5 = tf.batch_to_space(self.pool5, crops=pad, block_size=2)
            self.fc7 = tf.batch_to_space(self.fc7, crops=pad, block_size=2)
            self.fc7 = tf.batch_to_space(self.fc7, crops=pad, block_size=2)
            return

        if random_init_fc8:
            self.score_fr = self._score_layer(self.fc7, "score_fr",
                                              num_classes)
        else:
            self.score_fr = self._fc_layer(self.fc7, "score_fr",
                                           num_classes=num_classes,
                                           relu=False)

        self.pred = tf.argmax(self.score_fr, dimension=3)

        self.upscore2 = self._upscore_layer(self.score_fr,
                                            shape=tf.shape(self.pool4),
                                            num_classes=num_classes,
                                            debug=debug, name='upscore2',
                                            ksize=4, stride=2)
        self.score_pool4 = self._score_layer(self.pool4, "score_pool4",
                                             num_classes=num_classes)
        self.fuse_pool4 = tf.add(self.upscore2, self.score_pool4)

        self.upscore4 = self._upscore_layer(self.fuse_pool4,
                                            shape=tf.shape(self.pool3),
                                            num_classes=num_classes,
                                            debug=debug, name='upscore4',
                                            ksize=4, stride=2)
        self.score_pool3 = self._score_layer(self.pool3, "score_pool3",
                                             num_classes=num_classes)
        self.fuse_pool3 = tf.add(self.upscore4, self.score_pool3)

        self.upscore32 = self._upscore_layer(self.fuse_pool3,
                                             shape=tf.shape(bgr),
                                             num_classes=num_classes,
                                             debug=debug, name='upscore32',
                                             ksize=16, stride=8)

        self.pred_up = tf.argmax(self.upscore32, dimension=3)
コード例 #42
0
ファイル: FCN8s.py プロジェクト: wb-finalking/Matting
    def build(self, rgb, train=False, num_classes=3, random_init_fc8=False,
              debug=False, use_dilated=False):
        """
        Build the VGG model using loaded weights
        Parameters
        ----------
        rgb: image batch tensor
            Image in rgb shap. Scaled to Intervall [0, 255]
        train: bool
            Whether to build train or inference graph
        num_classes: int
            How many classes should be predicted (by fc8)
        random_init_fc8 : bool
            Whether to initialize fc8 layer randomly.
            Finetuning is required in this case.
        debug: bool
            Whether to print additional Debug Information.
        """
        # Convert RGB to BGR

        # with tf.name_scope('Processing'):
        #
        #     red, green, blue = tf.split(rgb, 3, 3)
        #     # assert red.get_shape().as_list()[1:] == [224, 224, 1]
        #     # assert green.get_shape().as_list()[1:] == [224, 224, 1]
        #     # assert blue.get_shape().as_list()[1:] == [224, 224, 1]
        #     bgr = tf.concat([
        #         blue - VGG_MEAN[0],
        #         green - VGG_MEAN[1],
        #         red - VGG_MEAN[2],
        #     ], 3)
        #
        #     if debug:
        #         bgr = tf.Print(bgr, [tf.shape(bgr)],
        #                        message='Shape of input image: ',
        #                        summarize=4, first_n=1)
        bgr = rgb
        self.conv1_1 = self._conv_layer(bgr, "conv1_1")
        self.conv1_2 = self._conv_layer(self.conv1_1, "conv1_2")
        self.pool1 = self._max_pool(self.conv1_2, 'pool1', debug)

        self.conv2_1 = self._conv_layer(self.pool1, "conv2_1")
        self.conv2_2 = self._conv_layer(self.conv2_1, "conv2_2")
        self.pool2 = self._max_pool(self.conv2_2, 'pool2', debug)

        self.conv3_1 = self._conv_layer(self.pool2, "conv3_1")
        self.conv3_2 = self._conv_layer(self.conv3_1, "conv3_2")
        self.conv3_3 = self._conv_layer(self.conv3_2, "conv3_3")
        self.pool3 = self._max_pool(self.conv3_3, 'pool3', debug)

        self.conv4_1 = self._conv_layer(self.pool3, "conv4_1")
        self.conv4_2 = self._conv_layer(self.conv4_1, "conv4_2")
        self.conv4_3 = self._conv_layer(self.conv4_2, "conv4_3")

        if use_dilated:
            pad = [[0, 0], [0, 0]]
            self.pool4 = tf.nn.max_pool(self.conv4_3, ksize=[1, 2, 2, 1],
                                        strides=[1, 1, 1, 1],
                                        padding='SAME', name='pool4')
            self.pool4 = tf.space_to_batch(self.pool4,
                                           paddings=pad, block_size=2)
        else:
            self.pool4 = self._max_pool(self.conv4_3, 'pool4', debug)

        self.conv5_1 = self._conv_layer(self.pool4, "conv5_1")
        self.conv5_2 = self._conv_layer(self.conv5_1, "conv5_2")
        self.conv5_3 = self._conv_layer(self.conv5_2, "conv5_3")
        if use_dilated:
            pad = [[0, 0], [0, 0]]
            self.pool5 = tf.nn.max_pool(self.conv5_3, ksize=[1, 2, 2, 1],
                                        strides=[1, 1, 1, 1],
                                        padding='SAME', name='pool5')
            self.pool5 = tf.space_to_batch(self.pool5,
                                           paddings=pad, block_size=2)
        else:
            self.pool5 = self._max_pool(self.conv5_3, 'pool5', debug)

        self.fc6 = self._fc_layer(self.pool5, "fc6")


        if train:
            self.fc6 = tf.nn.dropout(self.fc6, 0.5)
        self.fc7 = self._fc_layer(self.fc6, "fc7")
        if train:
            self.fc7 = tf.nn.dropout(self.fc7, 0.5)

        if use_dilated:
            self.pool5 = tf.batch_to_space(self.pool5, crops=pad, block_size=2)
            self.pool5 = tf.batch_to_space(self.pool5, crops=pad, block_size=2)
            self.fc7 = tf.batch_to_space(self.fc7, crops=pad, block_size=2)
            self.fc7 = tf.batch_to_space(self.fc7, crops=pad, block_size=2)
            return

        if random_init_fc8:
            self.score_fr = self._score_layer(self.fc7, "score_fr",
                                              num_classes)
        else:
            self.score_fr = self._fc_layer(self.fc7, "score_fr",
                                           num_classes=num_classes,
                                           relu=False)

        self.pred = tf.argmax(self.score_fr, dimension=3)

        self.upscore2 = self._upscore_layer(self.score_fr,
                                            shape=tf.shape(self.pool4),
                                            num_classes=num_classes,
                                            debug=debug, name='upscore2',
                                            ksize=4, stride=2)
        self.score_pool4 = self._score_layer(self.pool4, "score_pool4",
                                             num_classes=num_classes)
        self.fuse_pool4 = tf.add(self.upscore2, self.score_pool4)

        self.upscore4 = self._upscore_layer(self.fuse_pool4,
                                            shape=tf.shape(self.pool3),
                                            num_classes=num_classes,
                                            debug=debug, name='upscore4',
                                            ksize=4, stride=2)
        self.score_pool3 = self._score_layer(self.pool3, "score_pool3",
                                             num_classes=num_classes)
        self.fuse_pool3 = tf.add(self.upscore4, self.score_pool3)

        self.upscore32 = self._upscore_layer(self.fuse_pool3,
                                             shape=tf.shape(bgr),
                                             num_classes=num_classes,
                                             debug=debug, name='upscore32',
                                             ksize=16, stride=8)

        self.pred_up = tf.argmax(self.upscore32, dimension=3)

        self.softmax = tf.nn.softmax(self.upscore32, dim=3)
def build(inputs, labels, weights, is_training=True, needs_vgg=False):
    if needs_vgg:
        vgg_layers, vgg_layer_names = read_vgg_init(FLAGS.vgg_init_dir)

    weight_decay = 5e-4
    bn_params = {
        # Decay for the moving averages.
        'decay': 0.999,
        'center': True,
        'scale': True,
        # epsilon to prevent 0s in variance.
        'epsilon': 0.001,
        # None to force the updates
        'updates_collections': None,
        'is_training': is_training,
    }
    with tf.contrib.framework.arg_scope(
        [layers.convolution2d],
            kernel_size=3,
            stride=1,
            padding='SAME',
            rate=1,
            activation_fn=tf.nn.relu,
            # normalizer_fn=layers.batch_norm, normalizer_params=bn_params,
            # weights_initializer=layers.variance_scaling_initializer(),
            normalizer_fn=None,
            weights_initializer=None,
            weights_regularizer=layers.l2_regularizer(weight_decay)):
        net = layers.convolution2d(inputs, 64, scope='conv1_1')
        net = layers.convolution2d(net, 64, scope='conv1_2')
        net = layers.max_pool2d(net, 2, 2, scope='pool1')
        net = layers.convolution2d(net, 128, scope='conv2_1')
        net = layers.convolution2d(net, 128, scope='conv2_2')
        net = layers.max_pool2d(net, 2, 2, scope='pool2')
        net = layers.convolution2d(net, 256, scope='conv3_1')
        net = layers.convolution2d(net, 256, scope='conv3_2')
        net = layers.convolution2d(net, 256, scope='conv3_3')

        paddings = [[0, 0], [0, 0]]
        crops = [[0, 0], [0, 0]]

        block_size = 2
        net = tf.space_to_batch(net, paddings=paddings, block_size=block_size)
        net = layers.convolution2d(net, 512, scope='conv4_1')
        net = layers.convolution2d(net, 512, scope='conv4_2')
        net = layers.convolution2d(net, 512, scope='conv4_3')
        net = tf.batch_to_space(net, crops=crops, block_size=block_size)

        block_size = 4
        net = tf.space_to_batch(net, paddings=paddings, block_size=block_size)
        net = layers.convolution2d(net, 512, scope='conv5_1')
        net = layers.convolution2d(net, 512, scope='conv5_2')
        net = layers.convolution2d(net, 512, scope='conv5_3')
        net = tf.batch_to_space(net, crops=crops, block_size=block_size)

    with tf.contrib.framework.arg_scope(
        [layers.convolution2d],
            stride=1,
            padding='SAME',
            weights_initializer=layers.variance_scaling_initializer(),
            activation_fn=tf.nn.relu,
            normalizer_fn=layers.batch_norm,
            normalizer_params=bn_params,
            weights_regularizer=layers.l2_regularizer(FLAGS.weight_decay)):
        net = layers.convolution2d(
            net, 512, kernel_size=3, scope='conv6_1', rate=4)

    logits = layers.convolution2d(
        net,
        FLAGS.num_classes,
        1,
        padding='SAME',
        activation_fn=None,
        scope='unary_2',
        rate=2)
    print('logits', logits.get_shape())

    logits = tf.image.resize_bilinear(
        logits, [FLAGS.img_height, FLAGS.img_width], name='resize_score')

    loss = get_loss(logits, labels, weights, is_training=is_training)

    if is_training and needs_vgg:
        init_op, init_feed = create_init_op(vgg_layers)
        return logits, loss, init_op, init_feed

    return logits, loss
コード例 #44
0
def tf_space_to_batch(inputs,
                      paddings=[[0, 0], [0, 0]],
                      block_shape=2,
                      name=None):
    return tf.space_to_batch(inputs, paddings, block_shape, name)