Python tensorflow.depth_to_space() Examples

The following are 30 code examples of tensorflow.depth_to_space(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: dufvsr.py    From PFNL with MIT License 6 votes vote down vote up
def forward(self, x, is_train):  
        # shape of x: [B,T_in,H,W,C]

        # Generate filters and residual
        # Fx: [B,1,H,W,1*5*5,R*R]
        # Rx: [B,1,H,W,3*R*R]
        with tf.variable_scope('G',reuse=tf.AUTO_REUSE) as scope:
            Fx, Rx = FR_52L(x, is_train) 

            x_c = []
            for c in range(3):
                t = DynFilter3D(x[:,self.num_frames//2:self.num_frames//2+1,:,:,c], Fx[:,0,:,:,:,:], [1,5,5]) # [B,H,W,R*R]
                t = tf.depth_to_space(t, self.scale) # [B,H*R,W*R,1]
                x_c += [t]
            x = tf.concat(x_c, axis=3)   # [B,H*R,W*R,3]
            x = tf.expand_dims(x, axis=1)

            Rx = depth_to_space_3D(Rx, self.scale)   # [B,1,H*R,W*R,3]
            x += Rx
            
            return x 
Example #2
Source File: modules.py    From GAN-Voice-Conversion with MIT License 6 votes vote down vote up
def upsample2d_block(
        inputs,
        filters,
        kernel_size,
        strides,
        shuffle_size=2,
        name_prefix='upsample2d_block_'):
    h1 = conv2d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, activation=None,
                      name=name_prefix + 'h1_conv')
    h1_shuffle = tf.depth_to_space(input=h1, block_size=2, name='h1_shuffle')
    h1_norm = instance_norm_layer(inputs=h1_shuffle, activation_fn=None, name=name_prefix + 'h1_norm')

    h1_gates = conv2d_layer(inputs=inputs, filters=filters, kernel_size=kernel_size, strides=strides, activation=None,
                            name=name_prefix + 'h1_gates')
    h1_shuffle_gates = tf.depth_to_space(input=h1_gates, block_size=2, name='h1_shuffle_gates')
    h1_norm_gates = instance_norm_layer(inputs=h1_shuffle_gates, activation_fn=None, name=name_prefix + 'h1_norm_gates')

    h1_glu = gated_linear_layer(inputs=h1_norm, gates=h1_norm_gates, name=name_prefix + 'h1_glu')

    return h1_glu 
Example #3
Source File: tensorflow_backend.py    From keras-contrib with MIT License 6 votes vote down vote up
def depth_to_space(input, scale, data_format=None):
    """ Uses phase shift algorithm to convert channels/depth for spatial resolution.

    # Arguments
        input: Input tensor
        scale: n `int` that is `>= 2`. The size of the spatial block.
        data_format: 'channels_first' or 'channels_last'.
            Whether to use Theano or TensorFlow dimension
            ordering in inputs/kernels/ouputs.

    # Returns
        TODO (PR welcome): Filling this section.
    """
    if data_format is None:
        data_format = K.image_data_format()
    data_format = data_format.lower()
    input = _preprocess_conv2d_input(input, data_format)
    out = tf.depth_to_space(input, scale)
    out = _postprocess_conv2d_output(out, data_format)
    return out 
Example #4
Source File: gqn_vae.py    From tf-gqn with Apache License 2.0 6 votes vote down vote up
def vae_simple_decoder(z, scope="VAESimpleDecoder"):
  def _upsample_conv2d(net, factor, filters, **kwargs):
    net = tf.layers.conv2d(net, filters=factor*factor*filters, **kwargs)
    net = tf.depth_to_space(net, block_size=factor)

    return net

  with tf.variable_scope(scope):
    endpoints = {}

    net = z  # shape (b, 1, 1, c)
    net = _upsample_conv2d(
      net, kernel_size=3, filters=128, factor=16, activation=tf.nn.relu,
      padding="SAME")  # shape out: (b, 16, 16, 128)
    net = _upsample_conv2d(
      net, kernel_size=3, filters=512, factor=2, activation=tf.nn.relu,
      padding="SAME")  # shape out: (b, 32, 32, 512)
    net = _upsample_conv2d(
      net, kernel_size=3, filters=512, factor=2, activation=tf.nn.relu,
      padding="SAME")  # shape out: (b, 64, 64, 512)
    net = tf.layers.conv2d(net, kernel_size=3, filters=3, padding="SAME")

    return net, endpoints 
Example #5
Source File: FFDNet.py    From VideoSuperResolution with MIT License 6 votes vote down vote up
def build_graph(self):
    super(FFDNet, self).build_graph()  # build inputs placeholder
    with tf.variable_scope(self.name):
      # build layers
      inputs = self.inputs_preproc[-1] / 255
      if self.training:
        sigma = tf.random_uniform((), maxval=self.sigma / 255)
        inputs += tf.random_normal(tf.shape(inputs)) * sigma
      else:
        sigma = self.sigma / 255
      inputs = tf.space_to_depth(inputs, block_size=self.space_down)
      noise_map = tf.ones_like(inputs)[..., 0:1] * sigma
      x = tf.concat([inputs, noise_map], axis=-1)
      x = self.relu_conv2d(x, 64, 3)
      for i in range(1, self.layers - 1):
        x = self.bn_relu_conv2d(x, 64, 3, use_bias=False)
      # the last layer w/o BN and ReLU
      x = self.conv2d(x, self.channel * self.space_down ** 2, 3)
      denoised = tf.depth_to_space(x, block_size=self.space_down)
      self.outputs.append(denoised * 255) 
Example #6
Source File: mru.py    From SketchySceneColorization with MIT License 6 votes vote down vote up
def upsample_conv(inputs, num_outputs, kernel_size, sn, activation_fn=None,
                  normalizer_fn=None, normalizer_params=None,
                  weights_regularizer=None,
                  weights_initializer=ly.xavier_initializer_conv2d(),
                  biases_initializer=tf.zeros_initializer(),
                  data_format='NCHW'):
    output = inputs
    output = tf.concat([output, output, output, output], axis=1 if data_format == 'NCHW' else 3)
    if data_format == 'NCHW':
        output = tf.transpose(output, [0, 2, 3, 1])
    output = tf.depth_to_space(output, 2)
    if data_format == 'NCHW':
        output = tf.transpose(output, [0, 3, 1, 2])
    output = conv2d(output, num_outputs, kernel_size, sn=sn, activation_fn=activation_fn,
                    normalizer_fn=normalizer_fn, normalizer_params=normalizer_params,
                    weights_regularizer=weights_regularizer, weights_initializer=weights_initializer,
                    biases_initializer=biases_initializer,
                    data_format=data_format)
    return output 
Example #7
Source File: celeba64_3bit_official.py    From flowpp with MIT License 5 votes vote down vote up
def inverse(self, y, **kwargs):
        return tf.depth_to_space(y, self.block_size), None 
Example #8
Source File: gan_64x64.py    From improved_wgan_training with MIT License 5 votes vote down vote up
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 
Example #9
Source File: test_tf_converter.py    From tf-coreml with Apache License 2.0 5 votes vote down vote up
def test_depth_to_space(self):
    self._test_reorganize_data(tf.depth_to_space, [1, 1, 1, 4]) 
Example #10
Source File: imagenet32_official.py    From flowpp with MIT License 5 votes vote down vote up
def inverse(self, y, **kwargs):
        return tf.depth_to_space(y, self.block_size), None 
Example #11
Source File: flows.py    From flowpp with MIT License 5 votes vote down vote up
def inverse(self, y, **kwargs):
        return tf.depth_to_space(y, self.block_size), None 
Example #12
Source File: utils.py    From PFNL with MIT License 5 votes vote down vote up
def depth_to_space_3D(x, block_size):
    ds_x = tf.shape(x)
    x = tf.reshape(x, [ds_x[0]*ds_x[1], ds_x[2], ds_x[3], ds_x[4]])
    
    y = tf.depth_to_space(x, block_size)
    
    ds_y = tf.shape(y)
    x = tf.reshape(y, [ds_x[0], ds_x[1], ds_y[1], ds_y[2], ds_y[3]])
    return x 
Example #13
Source File: celeba128_5bit_official.py    From flowpp with MIT License 5 votes vote down vote up
def inverse(self, y, **kwargs):
        return tf.depth_to_space(y, self.block_size), None 
Example #14
Source File: celeba64_5bit_official.py    From flowpp with MIT License 5 votes vote down vote up
def inverse(self, y, **kwargs):
        return tf.depth_to_space(y, self.block_size), None 
Example #15
Source File: nn_extra_nvp_conditional.py    From bruno with MIT License 5 votes vote down vote up
def backward(self, y, z, y_label=None):
        ys = int_shape(y)
        assert ys[3] % 4 == 0
        x = tf.depth_to_space(y, 2)

        if z is not None:
            z = tf.depth_to_space(z, 2)

        return x, z 
Example #16
Source File: nn_extra_nvp.py    From bruno with MIT License 5 votes vote down vote up
def backward(self, y, z, sum_log_det_jacobian):
        ys = int_shape(y)
        assert ys[3] % 4 == 0
        x = tf.depth_to_space(y, 2)

        if z is not None:
            z = tf.depth_to_space(z, 2)

        return x, z, sum_log_det_jacobian 
Example #17
Source File: wgangp_64x64.py    From f-AnoGAN with MIT License 5 votes vote down vote up
def UpsampleConv(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True):
    output = inputs
    output = tf.concat([output, output, output, output], 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 
Example #18
Source File: layers.py    From faceswap with GNU General Public License v3.0 5 votes vote down vote up
def _depth_to_space(cls, ipt, scale, data_format=None):
        """ Uses phase shift algorithm to convert channels/depth for spatial resolution """
        if data_format is None:
            data_format = K.image_data_format()
        data_format = data_format.lower()
        ipt = cls._preprocess_conv2d_input(ipt, data_format)
        out = tf.depth_to_space(ipt, scale)
        out = cls._postprocess_conv2d_output(out, data_format)
        return out 
Example #19
Source File: gan_SR.py    From improved_wgan_training with MIT License 5 votes vote down vote up
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 
Example #20
Source File: utils.py    From VSR-DUF-Reimplement with Apache License 2.0 5 votes vote down vote up
def depth_to_space_3D(x, block_size):
    ds_x = tf.shape(x)
    x = tf.reshape(x, [ds_x[0]*ds_x[1], ds_x[2], ds_x[3], ds_x[4]])
    
    y = tf.depth_to_space(x, block_size)
    
    ds_y = tf.shape(y)
    x = tf.reshape(y, [ds_x[0], ds_x[1], ds_y[1], ds_y[2], ds_y[3]])
    return x 
Example #21
Source File: wgan_gp.py    From Disentangled-Person-Image-Generation with MIT License 5 votes vote down vote up
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 
Example #22
Source File: wgan_gp.py    From Pose-Guided-Person-Image-Generation with MIT License 5 votes vote down vote up
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 
Example #23
Source File: tensorflow_backend.py    From keras-onnx with MIT License 5 votes vote down vote up
def depth_to_space(input, scale, data_format=None):
    ''' Uses phase shift algorithm to convert channels/depth for spatial resolution '''
    if data_format is None:
        data_format = image_data_format()

    if data_format == 'channels_first':
        data_format = 'NCHW'
    else:
        data_format = 'NHWC'

    data_format = data_format.lower()
    out = tf.depth_to_space(input, scale, data_format=data_format)
    return out 
Example #24
Source File: mru.py    From SketchySceneColorization with MIT License 5 votes vote down vote up
def upsample(input, data_format):
    assert data_format == 'NCHW'
    output = tf.concat([input, input, input, input], 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])
    return output 
Example #25
Source File: resnet_block.py    From Robust-Conditional-GAN with MIT License 5 votes vote down vote up
def UpsampleConv(inputs, output_dim, filter_size=3, stride=1, name=None,
                 spectral_normed=False, update_collection=None, inputs_norm=False,
                 he_init=True, biases=True):
    output = inputs
    output = tf.concat([output, output, output, output], axis=3)
    output = tf.depth_to_space(output, 2)
    # w, h = inputs.shape.as_list()[1], inputs.shape.as_list()[2]
    # output = tf.image.resize_images(inputs, [w * 2, h * 2])
    output = lib.ops.conv2d.Conv2D(output, output.shape.as_list()[-1], output_dim, filter_size, stride, name,
                                   spectral_normed=spectral_normed,
                                   update_collection=update_collection,
                                   inputs_norm=inputs_norm,
                                   he_init=he_init, biases=biases)

    return output 
Example #26
Source File: gan_resnet.py    From Robust-Conditional-GAN with MIT License 5 votes vote down vote up
def UpsampleConv(inputs, output_dim, filter_size=3, stride=1, name=None,
                 spectral_normed=False, update_collection=None, inputs_norm=False,
                 he_init=True, biases=True):
    output = inputs
    output = tf.concat([output, output, output, output], axis=3)
    output = tf.depth_to_space(output, 2)
    # w, h = inputs.shape.as_list()[1], inputs.shape.as_list()[2]
    # output = tf.image.resize_images(inputs, [w * 2, h * 2])
    output = lib.ops.conv2d.Conv2D(output, output.shape.as_list()[-1], output_dim, filter_size, stride, name,
                                   spectral_normed=spectral_normed,
                                   update_collection=update_collection,
                                   he_init=he_init, biases=biases)

    return output 
Example #27
Source File: gan_cifar_resnet.py    From ambient-gan with MIT License 5 votes vote down vote up
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 
Example #28
Source File: layers.py    From ArtGAN with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def subpixel(inp, nfm, upscale=2, name='subpixel'):
    # assert inp.get_shape().as_list()[1] % upscale == 0
    output = conv2d(inp, nout=nfm * (upscale ** 2), kernel=1, name=name, print_struct=False)
    output = tf.transpose(output, [0, 2, 3, 1])
    output = tf.depth_to_space(output, upscale)
    output = tf.transpose(output, [0, 3, 1, 2])
    print name + ': ' + str(output.get_shape().as_list())
    return output 
Example #29
Source File: eusr.py    From tf-perceptual-eusr with Apache License 2.0 5 votes vote down vote up
def _enhanced_upscaling_module(self, x, scale):
    if (scale & (scale - 1)) != 0:
      raise NotImplementedError
    
    for module_index in range(int(math.log(scale, 2))):
      with tf.variable_scope('m%d' % (module_index)):
        x_list = []
        for pixel_index in range(4):
          with tf.variable_scope('px%d' % (pixel_index)):
            x = self._residual_module(x, num_features=self.num_conv_features, num_blocks=self.num_upscale_blocks)
            x_list.append(x)
        x = tf.concat(x_list, axis=3)
        x = tf.depth_to_space(x, 2)
    
    return x 
Example #30
Source File: gan_ops.py    From logo-gen with MIT License 5 votes vote down vote up
def ScaledUpsampleConv(name, input_dim, output_dim, filter_size, inputs, he_init=True, biases=True):
    output = inputs
    output = lib.ops.concat.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, gain=0.5)
    return output