Пример #1
0
  def model(self):

    d, s, m, r = self.model_params

    # Feature Extraction
    size = self.padding + 1
    weights = tf.get_variable('w1', shape=[size, size, 1, d], initializer=tf.variance_scaling_initializer(0.1))
    biases = tf.get_variable('b1', initializer=tf.zeros([d]))
    features = tf.nn.conv2d(self.images, weights, strides=[1,1,1,1], padding='VALID', data_format='NHWC')
    features = tf.nn.bias_add(features, biases, data_format='NHWC')

    # Shrinking
    if self.model_params[1] > 0:
      features = self.prelu(features, 1)
      weights = tf.get_variable('w2', shape=[1, 1, d, s], initializer=tf.variance_scaling_initializer(2))
      biases = tf.get_variable('b2', initializer=tf.zeros([s]))
      features = tf.nn.conv2d(features, weights, strides=[1,1,1,1], padding='SAME', data_format='NHWC')
      features = tf.nn.bias_add(features, biases, data_format='NHWC')
    else:
      s = d

    conv = features
    # Mapping (# mapping layers = m)
    with tf.variable_scope("mapping_block") as scope:
        for ri in range(r):
          for i in range(3, m + 3):
            weights = tf.get_variable('w{}'.format(i), shape=[3, 3, s, s], initializer=tf.variance_scaling_initializer(2))
            biases = tf.get_variable('b{}'.format(i), initializer=tf.zeros([s]))
            if i > 3:
              conv = self.prelu(conv, i)
            conv = tf.nn.conv2d(conv, weights, strides=[1,1,1,1], padding='SAME', data_format='NHWC')
            conv = tf.nn.bias_add(conv, biases, data_format='NHWC')
            if i == m + 2:
              conv = self.prelu(conv, m + 3)
              weights = tf.get_variable('w{}'.format(m + 3), shape=[1, 1, s, s], initializer=tf.variance_scaling_initializer(2))
              biases = tf.get_variable('b{}'.format(m + 3), initializer=tf.zeros([s]))
              conv = tf.nn.conv2d(conv, weights, strides=[1,1,1,1], padding='SAME', data_format='NHWC')
              conv = tf.nn.bias_add(conv, biases, data_format='NHWC')
              conv = tf.add(conv, features)
          scope.reuse_variables()
    conv = self.prelu(conv, 2)

    # Expanding
    if self.model_params[1] > 0:
      expand_weights = tf.get_variable('w{}'.format(m + 4), shape=[1, 1, s, d], initializer=tf.variance_scaling_initializer(2))
      expand_biases = tf.get_variable('b{}'.format(m + 4), initializer=tf.zeros([d]))
      conv = tf.nn.conv2d(conv, expand_weights, strides=[1,1,1,1], padding='SAME', data_format='NHWC')
      conv = tf.nn.bias_add(conv, expand_biases, data_format='NHWC')
      conv = self.prelu(conv, m + 4)

    # Sub-pixel convolution
    size = self.radius * 2 + 1
    deconv_weights = tf.get_variable('deconv_w', shape=[size, size, d, self.scale**2], initializer=tf.variance_scaling_initializer(0.01))
    deconv_biases = tf.get_variable('deconv_b', initializer=tf.zeros([self.scale**2]))
    deconv = tf.nn.conv2d(conv, deconv_weights, strides=[1,1,1,1], padding='SAME', data_format='NHWC')
    deconv = tf.nn.bias_add(deconv, deconv_biases, data_format='NHWC')
    if self.scale > 1:
        deconv = tf.depth_to_space(deconv, self.scale, name='pixel_shuffle', data_format='NHWC')

    return deconv
def SubpixelConv2D(*args, **kwargs):
    kwargs['output_dim'] = 4 * kwargs['output_dim']
    output = lib.ops.conv2d.Conv2D(*args, **kwargs)
    output = tf.transpose(output, [0, 2, 3, 1])
    output = tf.depth_to_space(output, 2)
    output = tf.transpose(output, [0, 3, 1, 2])
    return output
Пример #3
0
 def build_generator(self,input_shapeA,input_shapeB):#unet
     if(self.load(self.config.resource.generator_json_path,self.config.resource.generator_weights_path)):
         return self.model
     else:#init
         print(f'init network parameters')
         inputsA = Input(input_shapeA,name='imageSmall')#None,None,6
         inputsB = Input(input_shapeB,name='imageUp')#None,None,3
         #layer 1
         F_ = Conv2D(filters = 32, kernel_size = (3, 3), strides = (1,1), padding = 'same')(inputsA)#conv1
         F_0 = self.__unet1(F_)#32
         F_1 = self.__RDB(F_0,32,6,32)#RDB1
         F_2 = self.__RDB(F_1,32,6,32)#RDB2
         F_3 = self.__RDB(F_2,32,6,32)#RDB3
         FF = concatenate([F_1, F_2,F_3], axis=3)
         FdLF = Conv2D(filters = 32, kernel_size = (1, 1), strides = (1,1), padding = 'same')(FF)
         FGF = Conv2D(filters = 32, kernel_size = (3, 3), strides = (1,1), padding = 'same')(FdLF)
         FDF = Add()([FGF, F_])
         us = Conv2D(filters = 32*4, kernel_size = (3, 3), strides = (1,1), padding = 'same')(FDF)
         us = Lambda(lambda x: tf.depth_to_space(x,2))(us)#x2(upsample),32
         d3 = Conv2D(filters = 3, kernel_size = (3, 3), strides = (1,1), padding = 'same')(us)
         d3 = Activation('tanh')(d3)
         d3 = Lambda(lambda x: x/2+0.5)(d3)
         combined = concatenate([inputsB, d3], axis=3)#blur-generator,6
         o2 = self.__unet2(combined)
         model = Model(inputs=[inputsA,inputsB], outputs=o2, name='generator')
         return model
Пример #4
0
    def hyper_decode(self, inputs):
        with tf.variable_scope('decoder', reuse=tf.AUTO_REUSE):
            hyper_decoder_rnn_input = tf.layers.conv2d(inputs=inputs, filters=128, kernel_size=[
                3, 3], strides=(1, 1), padding='same', name='hyper_decoder_rnn_input')
            self.hiddens1 = rnn_conv('hyper_decoder_rnn_conv_1',
                                     hyper_decoder_rnn_input, self.hiddens1, 128, [3, 3], (1, 1))
            d_rnn_h1 = tf.depth_to_space(self.hiddens1[0], 2)

            self.hiddens2 = rnn_conv('hyper_decoder_rnn_conv_2',
                                     d_rnn_h1, self.hiddens2, 128, [3, 3], (1, 1))
            d_rnn_h2 = tf.depth_to_space(self.hiddens2[0], 2)

            hyper_output = tf.layers.conv2d(inputs=d_rnn_h2, filters=128, kernel_size=[
                3, 3], strides=(1, 1), padding='same', name='hyper_outputs', activation=None)


        return hyper_output
Пример #5
0
    def decoder(self, x):
        with tf.variable_scope(f"codebook", reuse=True):
            embedding = tf.get_variable("codebook",
                                        shape=[self.n_hid, self.num_tokens],
                                        dtype=tf.float32)

            x = tf.matmul(x, embedding, transpose_b=True)

        if self.bf16:
            x = tf.cast(x, tf.bfloat16)

        with tf.variable_scope("decoder"):
            for block, (stack,
                        channels) in enumerate(reversed(self.convblocks)):
                with tf.variable_scope(f"block_{block}"):
                    for i in range(stack):
                        with tf.variable_scope(f"layer_{i}"):
                            if i == 0:
                                # upsample
                                x = self.conv2dtranspose(x,
                                                         channels, (4, 4),
                                                         (2, 2),
                                                         padding="SAME",
                                                         name=f"conv_upsample")
                            else:
                                # normal residual block

                                def decoder_block(x, channels=channels):
                                    out = self.conv2d(x,
                                                      channels, (3, 3), (1, 1),
                                                      padding="SAME",
                                                      name=f"conv_in")
                                    # out = self.norm(out, name=f"bn_in")
                                    out = self.activation(out, name=f"activ")
                                    out = self.conv2d(out,
                                                      channels, (3, 3), (1, 1),
                                                      padding="SAME",
                                                      name=f"conv_out")
                                    # out = self.norm(out, name=f"bn_out")
                                    return out

                                res_out = recompute_grad(
                                    decoder_block, self.bf16
                                )(x) if self.recompute_grad else decoder_block(
                                    x)

                                x = x + res_out

            x = self.conv2d(x, self.num_ch * self.stack_factor**2, (1, 1),
                            (1, 1))

            if self.bf16:
                x = tf.cast(x, tf.float32)

            if self.stack_factor > 1:
                x = tf.depth_to_space(x, self.stack_factor)

            return x
Пример #6
0
    def model(self):
        d = self.model_params
        m = len(d) + 2

        # Feature Extraction
        size = self.padding + 1
        weights = tf.get_variable(
            'w1',
            shape=[size, size, 1, d[0]],
            initializer=tf.variance_scaling_initializer(0.1))
        biases = tf.get_variable('b1', initializer=tf.zeros([d[0]]))
        conv = tf.nn.conv2d(self.images,
                            weights,
                            strides=[1, 1, 1, 1],
                            padding='VALID',
                            data_format='NHWC')
        conv = tf.nn.bias_add(conv, biases, data_format='NHWC')
        conv = self.prelu(conv, 1)

        # Mapping (# mapping layers = m)
        for i in range(3, m):
            weights = tf.get_variable(
                'w{}'.format(i),
                shape=[3, 3, d[i - 3], d[i - 2]],
                initializer=tf.variance_scaling_initializer(2))
            biases = tf.get_variable('b{}'.format(i),
                                     initializer=tf.zeros([d[i - 2]]))
            conv = tf.nn.conv2d(conv,
                                weights,
                                strides=[1, 1, 1, 1],
                                padding='SAME',
                                data_format='NHWC')
            conv = tf.nn.bias_add(conv, biases, data_format='NHWC')
            conv = self.prelu(conv, i)

        # Sub-pixel convolution
        size = self.radius * 2 + 1
        deconv_weights = tf.get_variable(
            'deconv_w',
            shape=[size, size, d[-1], self.scale**2],
            initializer=tf.variance_scaling_initializer(0.01))
        deconv_biases = tf.get_variable('deconv_b',
                                        initializer=tf.zeros([self.scale**2]))
        deconv = tf.nn.conv2d(conv,
                              deconv_weights,
                              strides=[1, 1, 1, 1],
                              padding='SAME',
                              data_format='NHWC')
        deconv = tf.nn.bias_add(deconv, deconv_biases, data_format='NHWC')
        deconv = tf.depth_to_space(deconv,
                                   self.scale,
                                   name='pixel_shuffle',
                                   data_format='NHWC')

        return deconv
Пример #7
0
    def decode(self, inputs):
        with tf.variable_scope('decoder', reuse=tf.AUTO_REUSE):
            decoder_rnn_input = tf.layers.conv2d(inputs=inputs, filters=128, kernel_size=[
                3, 3], strides=(1, 1), padding='same', name='decoder_rnn_input')
            self.hiddens1 = rnn_conv('decoder_rnn_conv_1',
                                     decoder_rnn_input, self.hiddens1, 512, [2, 2], (1, 1))
            d_rnn_h1 = tf.depth_to_space(self.hiddens1[0], 2)
            self.hiddens2 = rnn_conv('decoder_rnn_conv_2',
                                     d_rnn_h1, self.hiddens2, 512, [3, 3], (1, 1))
            d_rnn_h2 = tf.depth_to_space(self.hiddens2[0], 2)
            self.hiddens3 = rnn_conv('decoder_rnn_conv_3',
                                     d_rnn_h2, self.hiddens3, 256, [3, 3], (1, 1))
            d_rnn_h3 = tf.depth_to_space(self.hiddens3[0], 2)
            self.hiddens4 = rnn_conv('decoder_rnn_conv_4',
                                     d_rnn_h3, self.hiddens4, 128, [3, 3], (1, 1))
            d_rnn_h4 = tf.depth_to_space(self.hiddens4[0], 2)

            output = tf.layers.conv2d(inputs=d_rnn_h4, filters=3, kernel_size=[
                3, 3], strides=(1, 1), padding='same', name='output', activation=tf.nn.tanh)
        return output / 2
Пример #8
0
 def build_pixel_shuffler_layer(self, name, h, scale, input_filters, output_filters, activator=None, depthwise_separable=False):
     with tf.variable_scope(name):
         if (depthwise_separable):
             self.build_depthwise_separable_conv(name + "_CNN", h, self.cnn_size, input_filters, scale * scale * output_filters,
                         use_batch_norm=False,
                         use_bias=True)
         else:
             self.build_conv(name + "_CNN", h, self.cnn_size, input_filters, scale * scale * output_filters,
                         use_batch_norm=False,
                         use_bias=True)
         self.H.append(tf.depth_to_space(self.H[-1], scale))
         self.build_activator(self.H[-1], output_filters, activator, base_name=name)
    def setUp(self):
        super(DepthToSpaceOpHandlerTest, self).setUp()
        # Test a Identity -> DepthToSpace -> Identity chain of ops.
        inputs = tf.zeros([2, 4, 4, 4])
        id1 = tf.identity(inputs)
        dts = tf.depth_to_space(id1, 2)
        tf.identity(dts)

        g = tf.get_default_graph()

        # Declare OpSlice and OpGroup for ops of interest.
        self.id1_op = g.get_operation_by_name('Identity')
        self.id1_op_slice = orm.OpSlice(self.id1_op, orm.Slice(0, 4))
        self.id1_op_group = orm.OpGroup(
            self.id1_op_slice, omit_source_op_slices=[self.id1_op_slice])
        self.id1_op_slice0 = orm.OpSlice(self.id1_op, orm.Slice(0, 1))
        self.id1_op_slice1 = orm.OpSlice(self.id1_op, orm.Slice(1, 1))
        self.id1_op_slice2 = orm.OpSlice(self.id1_op, orm.Slice(2, 1))
        self.id1_op_slice3 = orm.OpSlice(self.id1_op, orm.Slice(3, 1))

        self.dts_op = g.get_operation_by_name('DepthToSpace')
        self.dts_op_slice = orm.OpSlice(self.dts_op, orm.Slice(0, 1))
        self.dts_op_group = orm.OpGroup(
            self.dts_op_slice, omit_source_op_slices=[self.dts_op_slice])

        self.id2_op = g.get_operation_by_name('Identity_1')
        self.id2_op_slice = orm.OpSlice(self.id2_op, orm.Slice(0, 1))
        self.id2_op_group = orm.OpGroup(
            self.id2_op_slice, omit_source_op_slices=[self.id2_op_slice])

        # Create mock OpRegularizerManager with custom mapping of OpSlice and
        # OpGroup.
        self.mock_op_reg_manager = mock.create_autospec(
            orm.OpRegularizerManager)

        self.op_slice_dict = {
            self.id1_op: [self.id1_op_slice],
            self.dts_op: [self.dts_op_slice],
            self.id2_op: [self.id2_op_slice],
        }

        def get_op_slices(op):
            return self.op_slice_dict.get(op)

        def get_op_group(op_slice):
            return self.op_group_dict.get(op_slice)

        self.mock_op_reg_manager.get_op_slices.side_effect = get_op_slices
        self.mock_op_reg_manager.get_op_group.side_effect = get_op_group
        self.mock_op_reg_manager.is_source_op.return_value = False
        self.mock_op_reg_manager.ops = [self.id1_op, self.dts_op, self.id2_op]
Пример #10
0
def decompress_step(source, hparams, first_relu, is_2d, name):
    """Decompression function."""
    with tf.variable_scope(name):
        shape = common_layers.shape_list(source)
        multiplier = 4 if is_2d else 2
        kernel = (1, 1) if is_2d else (1, 1)
        thicker = common_layers.conv_block(source,
                                           hparams.hidden_size * multiplier,
                                           [((1, 1), kernel)],
                                           first_relu=first_relu,
                                           name="decompress_conv")
        if is_2d:
            return tf.depth_to_space(thicker, 2)
        return tf.reshape(thicker,
                          [shape[0], shape[1] * 2, 1, hparams.hidden_size])
Пример #11
0
def demosaic(bayer_images):
    """Bilinearly demosaics a batch of RGGB Bayer images."""
    bayer_images.shape.assert_is_compatible_with((None, None, None, 4))

    # This implementation exploits how edges are aligned when upsampling with
    # tf.image.resize_bilinear().

    with tf.name_scope(None, 'demosaic'):
        shape = tf.shape(bayer_images)
        shape = [shape[1] * 2, shape[2] * 2]

        red = bayer_images[Ellipsis, 0:1]
        red = tf.image.resize_bilinear(red, shape)

        green_red = bayer_images[Ellipsis, 1:2]
        green_red = tf.image.flip_left_right(green_red)
        green_red = tf.image.resize_bilinear(green_red, shape)
        green_red = tf.image.flip_left_right(green_red)
        green_red = tf.space_to_depth(green_red, 2)

        green_blue = bayer_images[Ellipsis, 2:3]
        green_blue = tf.image.flip_up_down(green_blue)
        green_blue = tf.image.resize_bilinear(green_blue, shape)
        green_blue = tf.image.flip_up_down(green_blue)
        green_blue = tf.space_to_depth(green_blue, 2)

        green_at_red = (green_red[Ellipsis, 0] + green_blue[Ellipsis, 0]) / 2
        green_at_green_red = green_red[Ellipsis, 1]
        green_at_green_blue = green_blue[Ellipsis, 2]
        green_at_blue = (green_red[Ellipsis, 3] + green_blue[Ellipsis, 3]) / 2

        green_planes = [
            green_at_red, green_at_green_red, green_at_green_blue,
            green_at_blue
        ]
        green = tf.depth_to_space(tf.stack(green_planes, axis=-1), 2)

        blue = bayer_images[Ellipsis, 3:4]
        blue = tf.image.flip_up_down(tf.image.flip_left_right(blue))
        blue = tf.image.resize_bilinear(blue, shape)
        blue = tf.image.flip_up_down(tf.image.flip_left_right(blue))

        rgb_images = tf.concat([red, green, blue], axis=-1)
        return rgb_images
def UpsampleConv(name,
                 input_dim,
                 output_dim,
                 filter_size,
                 inputs,
                 he_init=True,
                 biases=True):
    output = inputs
    output = tf.concat([output, output, output, output], axis=1)
    output = tf.transpose(output, [0, 2, 3, 1])
    output = tf.depth_to_space(output, 2)
    output = tf.transpose(output, [0, 3, 1, 2])
    output = lib.ops.conv2d.Conv2D(name,
                                   input_dim,
                                   output_dim,
                                   filter_size,
                                   output,
                                   he_init=he_init,
                                   biases=biases)
    return output
def position_sensitive_crop_regions(image,
                                    boxes,
                                    crop_size,
                                    num_spatial_bins,
                                    global_pool):
  """Position-sensitive crop and pool rectangular regions from a feature grid.

  The output crops are split into `spatial_bins_y` vertical bins
  and `spatial_bins_x` horizontal bins. For each intersection of a vertical
  and a horizontal bin the output values are gathered by performing
  `tf.image.crop_and_resize` (bilinear resampling) on a a separate subset of
  channels of the image. This reduces `depth` by a factor of
  `(spatial_bins_y * spatial_bins_x)`.

  When global_pool is True, this function implements a differentiable version
  of position-sensitive RoI pooling used in
  [R-FCN detection system](https://arxiv.org/abs/1605.06409).

  When global_pool is False, this function implements a differentiable version
  of position-sensitive assembling operation used in
  [instance FCN](https://arxiv.org/abs/1603.08678).

  Args:
    image: A `Tensor`. Must be one of the following types: `uint8`, `int8`,
      `int16`, `int32`, `int64`, `half`, `float32`, `float64`.
      A 3-D tensor of shape `[image_height, image_width, depth]`.
      Both `image_height` and `image_width` need to be positive.
    boxes: A `Tensor` of type `float32`.
      A 2-D tensor of shape `[num_boxes, 4]`. Each box is specified in
      normalized coordinates `[y1, x1, y2, x2]`. A normalized coordinate value
      of `y` is mapped to the image coordinate at `y * (image_height - 1)`, so
      as the `[0, 1]` interval of normalized image height is mapped to
      `[0, image_height - 1] in image height coordinates. We do allow y1 > y2,
      in which case the sampled crop is an up-down flipped version of the
      original image. The width dimension is treated similarly.
    crop_size: A list of two integers `[crop_height, crop_width]`. All
      cropped image patches are resized to this size. The aspect ratio of the
      image content is not preserved. Both `crop_height` and `crop_width` need
      to be positive.
    num_spatial_bins: A list of two integers `[spatial_bins_y, spatial_bins_x]`.
      Represents the number of position-sensitive bins in y and x directions.
      Both values should be >= 1. `crop_height` should be divisible by
      `spatial_bins_y`, and similarly for width.
      The number of image channels should be divisible by
      (spatial_bins_y * spatial_bins_x).
      Suggested value from R-FCN paper: [3, 3].
    global_pool: A boolean variable.
      If True, we perform average global pooling on the features assembled from
        the position-sensitive score maps.
      If False, we keep the position-pooled features without global pooling
        over the spatial coordinates.
      Note that using global_pool=True is equivalent to but more efficient than
        running the function with global_pool=False and then performing global
        average pooling.

  Returns:
    position_sensitive_features: A 4-D tensor of shape
      `[num_boxes, K, K, crop_channels]`,
      where `crop_channels = depth / (spatial_bins_y * spatial_bins_x)`,
      where K = 1 when global_pool is True (Average-pooled cropped regions),
      and K = crop_size when global_pool is False.
  Raises:
    ValueError: Raised in four situations:
      `num_spatial_bins` is not >= 1;
      `num_spatial_bins` does not divide `crop_size`;
      `(spatial_bins_y*spatial_bins_x)` does not divide `depth`;
      `bin_crop_size` is not square when global_pool=False due to the
        constraint in function space_to_depth.
  """
  total_bins = 1
  bin_crop_size = []

  for (num_bins, crop_dim) in zip(num_spatial_bins, crop_size):
    if num_bins < 1:
      raise ValueError('num_spatial_bins should be >= 1')

    if crop_dim % num_bins != 0:
      raise ValueError('crop_size should be divisible by num_spatial_bins')

    total_bins *= num_bins
    bin_crop_size.append(crop_dim // num_bins)

  if not global_pool and bin_crop_size[0] != bin_crop_size[1]:
    raise ValueError('Only support square bin crop size for now.')

  ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=1)
  spatial_bins_y, spatial_bins_x = num_spatial_bins

  # Split each box into spatial_bins_y * spatial_bins_x bins.
  position_sensitive_boxes = []
  for bin_y in range(spatial_bins_y):
    step_y = (ymax - ymin) / spatial_bins_y
    for bin_x in range(spatial_bins_x):
      step_x = (xmax - xmin) / spatial_bins_x
      box_coordinates = [ymin + bin_y * step_y,
                         xmin + bin_x * step_x,
                         ymin + (bin_y + 1) * step_y,
                         xmin + (bin_x + 1) * step_x,
                        ]
      position_sensitive_boxes.append(tf.stack(box_coordinates, axis=1))

  image_splits = tf.split(value=image, num_or_size_splits=total_bins, axis=2)

  image_crops = []
  for (split, box) in zip(image_splits, position_sensitive_boxes):
    if split.shape.is_fully_defined() and box.shape.is_fully_defined():
      crop = tf.squeeze(
          matmul_crop_and_resize(
              tf.expand_dims(split, axis=0), tf.expand_dims(box, axis=0),
              bin_crop_size),
          axis=0)
    else:
      crop = tf.image.crop_and_resize(
          tf.expand_dims(split, 0), box,
          tf.zeros(tf.shape(boxes)[0], dtype=tf.int32), bin_crop_size)
    image_crops.append(crop)

  if global_pool:
    # Average over all bins.
    position_sensitive_features = tf.add_n(image_crops) / len(image_crops)
    # Then average over spatial positions within the bins.
    position_sensitive_features = tf.reduce_mean(
        position_sensitive_features, [1, 2], keepdims=True)
  else:
    # Reorder height/width to depth channel.
    block_size = bin_crop_size[0]
    if block_size >= 2:
      image_crops = [tf.space_to_depth(
          crop, block_size=block_size) for crop in image_crops]

    # Pack image_crops so that first dimension is for position-senstive boxes.
    position_sensitive_features = tf.stack(image_crops, axis=0)

    # Unroll the position-sensitive boxes to spatial positions.
    position_sensitive_features = tf.squeeze(
        tf.batch_to_space_nd(position_sensitive_features,
                             block_shape=[1] + num_spatial_bins,
                             crops=tf.zeros((3, 2), dtype=tf.int32)),
        axis=[0])

    # Reorder back the depth channel.
    if block_size >= 2:
      position_sensitive_features = tf.depth_to_space(
          position_sensitive_features, block_size=block_size)

  return position_sensitive_features
def network(input):
    #Xavier initializer
    initializer = tf.initializers.glorot_uniform()
    filter0 = tf.Variable(initializer([3, 3, 4, 32]))
    b0 = tf.Variable(tf.zeros([32]))
    conv1 = tf.nn.conv2d(input, filter0, [1, 1, 1, 1], padding='SAME')
    conv1 = tf.nn.bias_add(conv1, b0)
    conv1 = lrelu(conv1)
    filter1 = tf.Variable(initializer([3, 3, 32, 32]))
    b1 = tf.Variable(tf.zeros([32]))
    conv1 = tf.nn.conv2d(conv1, filter1, [1, 1, 1, 1], padding='SAME')
    conv1 = tf.nn.bias_add(conv1, b1)
    conv1 = lrelu(conv1)
    pool1 = tf.nn.max_pool(conv1, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

    filter2 = tf.Variable(initializer([3, 3, 32, 64]))
    b2 = tf.Variable(tf.zeros([64]))
    conv2 = tf.nn.conv2d(pool1, filter2, [1, 1, 1, 1], padding='SAME')
    conv2 = tf.nn.bias_add(conv2, b2)
    conv2 = lrelu(conv2)
    filter3 = tf.Variable(initializer([3, 3, 64, 64]))
    b3 = tf.Variable(tf.zeros([64]))
    conv2 = tf.nn.conv2d(conv2, filter3, [1, 1, 1, 1], padding='SAME')
    conv2 = tf.nn.bias_add(conv2, b3)
    conv2 = lrelu(conv2)
    pool2 = tf.nn.max_pool(conv2, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

    filter4 = tf.Variable(initializer([3, 3, 64, 128]))
    b4 = tf.Variable(tf.zeros([128]))
    conv3 = tf.nn.conv2d(pool2, filter4, [1, 1, 1, 1], padding='SAME')
    conv3 = tf.nn.bias_add(conv3, b4)
    conv3 = lrelu(conv3)
    filter5 = tf.Variable(initializer([3, 3, 128, 128]))
    b5 = tf.Variable(tf.zeros([128]))
    conv3 = tf.nn.conv2d(conv3, filter5, [1, 1, 1, 1], padding='SAME')
    conv3 = tf.nn.bias_add(conv3, b5)
    conv3 = lrelu(conv3)
    pool3 = tf.nn.max_pool(conv3, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

    filter6 = tf.Variable(initializer([3, 3, 128, 256]))
    b6 = tf.Variable(tf.zeros([256]))
    conv4 = tf.nn.conv2d(pool3, filter6, [1, 1, 1, 1], padding='SAME')
    conv4 = tf.nn.bias_add(conv4, b6)
    conv4 = lrelu(conv4)
    filter7 = tf.Variable(initializer([3, 3, 256, 256]))
    b7 = tf.Variable(tf.zeros([256]))
    conv4 = tf.nn.conv2d(conv4, filter7, [1, 1, 1, 1], padding='SAME')
    conv4 = tf.nn.bias_add(conv4, b7)
    conv4 = lrelu(conv4)
    pool4 = tf.nn.max_pool(conv4, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME')

    filter8 = tf.Variable(initializer([3, 3, 256, 512]))
    b8 = tf.Variable(tf.zeros([512]))
    conv5 = tf.nn.conv2d(pool4, filter8, [1, 1, 1, 1], padding='SAME')
    conv5 = tf.nn.bias_add(conv5, b8)
    conv5 = lrelu(conv5)
    filter9 = tf.Variable(initializer([3, 3, 512, 512]))
    b9 = tf.Variable(tf.zeros([512]))
    conv5 = tf.nn.conv2d(conv5, filter9, [1, 1, 1, 1], padding='SAME')
    conv5 = tf.nn.bias_add(conv5, b9)
    conv5 = lrelu(conv5)

    up6 = upsample_and_concat(conv5, conv4, 256, 512)  #todo:bug
    filter10 = tf.Variable(initializer([3, 3, 512, 256]))
    b10 = tf.Variable(tf.zeros([256]))
    conv6 = tf.nn.conv2d(up6, filter10, [1, 1, 1, 1], padding='SAME')
    conv6 = tf.nn.bias_add(conv6, b10)
    conv6 = lrelu(conv6)
    filter11 = tf.Variable(initializer([3, 3, 256, 256]))
    b11 = tf.Variable(tf.zeros([256]))
    conv6 = tf.nn.conv2d(conv6, filter11, [1, 1, 1, 1], padding='SAME')
    conv6 = tf.nn.bias_add(conv6, b11)
    conv6 = lrelu(conv6)

    up7 = upsample_and_concat(conv6, conv3, 128, 256)
    filter12 = tf.Variable(initializer([3, 3, 256, 128]))
    b12 = tf.Variable(tf.zeros([128]))
    conv7 = tf.nn.conv2d(up7, filter12, [1, 1, 1, 1], padding='SAME')
    conv7 = tf.nn.bias_add(conv7, b12)
    conv7 = lrelu(conv7)
    filter13 = tf.Variable(initializer([3, 3, 128, 128]))
    b13 = tf.Variable(tf.zeros([128]))
    conv7 = tf.nn.conv2d(conv7, filter13, [1, 1, 1, 1], padding='SAME')
    conv7 = tf.nn.bias_add(conv7, b13)
    conv7 = lrelu(conv7)

    up8 = upsample_and_concat(conv7, conv2, 64, 128)
    filter14 = tf.Variable(initializer([3, 3, 128, 64]))
    b14 = tf.Variable(tf.zeros([64]))
    conv8 = tf.nn.conv2d(up8, filter14, [1, 1, 1, 1], padding='SAME')
    conv8 = tf.nn.bias_add(conv8, b14)
    conv8 = lrelu(conv8)
    filter15 = tf.Variable(initializer([3, 3, 64, 64]))
    b15 = tf.Variable(tf.zeros([64]))
    conv8 = tf.nn.conv2d(conv8, filter15, [1, 1, 1, 1], padding='SAME')
    conv8 = tf.nn.bias_add(conv8, b15)
    conv8 = lrelu(conv8)

    up9 = upsample_and_concat(conv8, conv1, 32, 64)
    filter16 = tf.Variable(initializer([3, 3, 64, 32]))
    b16 = tf.Variable(tf.zeros([32]))
    conv9 = tf.nn.conv2d(up9, filter16, [1, 1, 1, 1], padding='SAME')
    conv9 = tf.nn.bias_add(conv9, b16)
    conv9 = lrelu(conv9)
    filter17 = tf.Variable(initializer([3, 3, 32, 32]))
    b17 = tf.Variable(tf.zeros([32]))
    conv9 = tf.nn.conv2d(conv9, filter17, [1, 1, 1, 1], padding='SAME')
    conv9 = tf.nn.bias_add(conv9, b17)
    conv9 = lrelu(conv9)

    filter18 = tf.Variable(initializer([1, 1, 32, 12]))
    b18 = tf.Variable(tf.zeros([12]))
    conv10 = tf.nn.conv2d(conv9, filter18, [1, 1, 1, 1], padding='SAME')
    conv10 = tf.nn.bias_add(conv10, b18)

    out = tf.depth_to_space(conv10, 2)

    return out
Пример #15
0
def network(input):
    conv1 = slim.conv2d(input,
                        32, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv1_1')
    conv1 = slim.conv2d(conv1,
                        32, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv1_2')
    pool1 = slim.max_pool2d(conv1, [2, 2], padding='SAME')

    conv2 = slim.conv2d(pool1,
                        64, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv2_1')
    conv2 = slim.conv2d(conv2,
                        64, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv2_2')
    pool2 = slim.max_pool2d(conv2, [2, 2], padding='SAME')

    conv3 = slim.conv2d(pool2,
                        128, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv3_1')
    conv3 = slim.conv2d(conv3,
                        128, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv3_2')
    pool3 = slim.max_pool2d(conv3, [2, 2], padding='SAME')

    conv4 = slim.conv2d(pool3,
                        256, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv4_1')
    conv4 = slim.conv2d(conv4,
                        256, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv4_2')
    pool4 = slim.max_pool2d(conv4, [2, 2], padding='SAME')

    conv5 = slim.conv2d(pool4,
                        512, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv5_1')
    conv5 = slim.conv2d(conv5,
                        512, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv5_2')

    up6 = upsample_and_concat(conv5, conv4, 256, 512)
    conv6 = slim.conv2d(up6,
                        256, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv6_1')
    conv6 = slim.conv2d(conv6,
                        256, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv6_2')

    up7 = upsample_and_concat(conv6, conv3, 128, 256)
    conv7 = slim.conv2d(up7,
                        128, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv7_1')
    conv7 = slim.conv2d(conv7,
                        128, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv7_2')

    up8 = upsample_and_concat(conv7, conv2, 64, 128)
    conv8 = slim.conv2d(up8,
                        64, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv8_1')
    conv8 = slim.conv2d(conv8,
                        64, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv8_2')

    up9 = upsample_and_concat(conv8, conv1, 32, 64)
    conv9 = slim.conv2d(up9,
                        32, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv9_1')
    conv9 = slim.conv2d(conv9,
                        32, [3, 3],
                        rate=1,
                        activation_fn=lrelu,
                        scope='g_conv9_2')

    conv10 = slim.conv2d(conv9,
                         32, [2, 2],
                         stride=2,
                         rate=1,
                         activation_fn=None,
                         scope='g_conv10_1')
    conv10 = slim.conv2d(conv10,
                         12, [1, 1],
                         rate=1,
                         activation_fn=None,
                         scope='g_conv10_2')
    out = tf.depth_to_space(conv10, 2)
    return out