-1
$\begingroup$

I am going throught GAN for image generation and I am using this article for reference. The author is creating a generator model which does this. enter image description here

and the generator model code is

self.G = Sequential()
dropout = 0.4
depth = 64+64+64+64
dim = 7

# In: 100
# Out: dim x dim x depth
self.G.add(Dense(dim*dim*depth, input_dim=100))
self.G.add(BatchNormalization(momentum=0.9))
self.G.add(Activation('relu'))
self.G.add(Reshape((dim, dim, depth)))
self.G.add(Dropout(dropout))


# In: dim x dim x depth
# Out: 2*dim x 2*dim x depth/2
self.G.add(UpSampling2D())
self.G.add(Conv2DTranspose(int(depth/2), 5, padding='same'))
self.G.add(BatchNormalization(momentum=0.9))
self.G.add(Activation('relu'))
self.G.add(UpSampling2D())
self.G.add(Conv2DTranspose(int(depth/4), 5, padding='same'))
self.G.add(BatchNormalization(momentum=0.9))
self.G.add(Activation('relu'))
self.G.add(Conv2DTranspose(int(depth/8), 5, padding='same'))
self.G.add(BatchNormalization(momentum=0.9))
self.G.add(Activation('relu'))

# Out: 28 x 28 x 1 grayscale image [0.0,1.0] per pix
self.G.add(Conv2DTranspose(1, 5, padding='same'))
self.G.add(Activation('sigmoid'))
self.G.summary()

I understood most of the codes but I have doubts in these two functions

self.G.add(UpSampling2D())
self.G.add(Conv2DTranspose(int(depth/number), 5, padding='same'))

What is happeing in those layers?

$\endgroup$
1
$\begingroup$

You should either use UpSampling2D or Conv2DTranspose.

Upsampling2D just repeats the input. After Upsampling you should add a normal convolutional layer.

Conv2DTranspose is the backpropagated Conv2D, and there you do not neet any upsampling

For both cases read the documentary. https://arxiv.org/pdf/1603.07285v1.pdf

$\endgroup$
1
$\begingroup$

According to the official Keras MNIST autoencoder example here:

keras/examples/mnist_denoising_autoencoder.py

You use UpSampling2D along with Conv2DTranspose when strides < 2 They claim that it is better to use Conv2DTranspose with strides >= 2

# Build the Decoder Model
latent_inputs = Input(shape=(latent_dim,), name='decoder_input')
x = Dense(shape[1] * shape[2] * shape[3])(latent_inputs)
x = Reshape((shape[1], shape[2], shape[3]))(x)

# Stack of Transposed Conv2D blocks
# Notes:
# 1) Use Batch Normalization before ReLU on deep networks
# 2) Use UpSampling2D as alternative to strides>1
# - faster but not as good as strides>1
for filters in layer_filters[::-1]:
    x = Conv2DTranspose(filters=filters,
                        kernel_size=kernel_size,
                        strides=2,
                        activation='relu',
                        padding='same')(x)

x = Conv2DTranspose(filters=1,
                    kernel_size=kernel_size,
                    padding='same')(x)

outputs = Activation('sigmoid', name='decoder_output')(x)

# Instantiate Decoder Model
decoder = Model(latent_inputs, outputs, name='decoder')
decoder.summary()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.