user13410977
user13410977

Reputation:

What is the correct way to upsample a [32x32x6] layer in a CNN

I have a CNN that produces a [32x32] image with 6 channels, but I need to upsample it to 256x256. I'm doing:

def upsample(filters, size):
  initializer = tf.random_normal_initializer(0., 0.02)

  result = tf.keras.Sequential()
  result.add(tf.keras.layers.Conv2DTranspose(filters, size, strides=2,
                                             padding='same',
                                             kernel_initializer=initializer,
                                             use_bias=False))
  return result

Then I pass the layer like this:

up_stack = [
upsample(6, 3),  # x2
upsample(6, 3),  # x2
upsample(6, 3)   # x2
]

for up in up_stack:
  finalLayer = up(finalLayer)

But this setup produces inaccurate results. Is there anything I'm doing wrong?

Upvotes: 0

Views: 1257

Answers (2)

pratsbhatt
pratsbhatt

Reputation: 1538

If you wish to use the up-convolution, I will suggest doing the following to achieve what you want. Following code works, just change the filter based on your need.

import tensorflow as tf
from tensorflow.keras import layers

# in = 32x32 out 256x256

inputs = layers.Input(shape=(32, 32, 6))

deconc01 = layers.Conv2DTranspose(256, kernel_size=2, strides=(2, 2), activation='relu')(inputs)
deconc02 = layers.Conv2DTranspose(256, kernel_size=2, strides=(2, 2), activation='relu')(deconc01)
outputs = layers.Conv2DTranspose(256, kernel_size=2, strides=(2, 2), activation='relu')(deconc02)

model = tf.keras.Model(inputs=inputs, outputs=outputs, name="up-conv")

Model: "up-conv"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 32, 32, 6)]       0         
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 64, 64, 256)       6400      
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 128, 128, 256)     262400    
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 256, 256, 256)     262400    
=================================================================
Total params: 531,200
Trainable params: 531,200
Non-trainable params: 0
_________________________________________________________________

Upvotes: 1

Abhishek Verma
Abhishek Verma

Reputation: 1729

Your other option would be to use tf.keras.layers.UpSampling2D for your purpose, but that doesn't learn a kernel to upsample (it uses bilinear upsampling).

So, your approach is correct. But, you have used kernel_size as 3x3.

It should be 2x2 and if you are not satisfied with the results, you should increase the number of filters from [32, 256].

Upvotes: 1

Related Questions