Skip to main content

Tensorflow Downsampling

Guangzhou, China

Using Representation Learning to downsample images.

Build the Autoencoder

# build autoencoder model
autoencoder = tf.keras.models.Sequential()

# build the encoder CNN
autoencoder.add(tf.keras.layers.Conv2D(64, (3,3), strides=1, padding="same", input_shape=(32, 32, 3)))
autoencoder.add(tf.keras.layers.BatchNormalization())
autoencoder.add(tf.keras.layers.Activation('relu'))
autoencoder.add(tf.keras.layers.AveragePooling2D((2,2), padding="same"))

autoencoder.add(tf.keras.layers.Conv2D(32, (3,3), strides=1, padding="same"))
autoencoder.add(tf.keras.layers.BatchNormalization())
autoencoder.add(tf.keras.layers.Activation('relu'))

# representation layer
autoencoder.add(tf.keras.layers.AveragePooling2D((2,2), padding="same"))

# build the decoder CNN
autoencoder.add(tf.keras.layers.BatchNormalization())
autoencoder.add(tf.keras.layers.Activation('relu'))
autoencoder.add(tf.keras.layers.UpSampling2D((2, 2)))

autoencoder.add(tf.keras.layers.Conv2D(64, (3,3), strides=1, padding="same"))
autoencoder.add(tf.keras.layers.BatchNormalization())
autoencoder.add(tf.keras.layers.Activation('relu'))
autoencoder.add(tf.keras.layers.UpSampling2D((2, 2)))

autoencoder.add(tf.keras.layers.Conv2D(3, (3,3), strides=1, activation='sigmoid', padding="same"))
# compile model
autoencoder.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(lr=0.01))
autoencoder.summary()

Train the Autoencoder

Before I used Representation Learning to remove digital noise from image files. There we needed to compare the generated image from our CNN layers with a noise-free version of the image as a performance metric. For downsampling we need to compare the de-compressed image to the original image - the model first compresses features (encoding) and then de-compresses them (decoding) to try to match the original input:

# fit model to dataset
autoencoder.fit(X_train,
X_train,
epochs=20,
batch_size=200,
validation_data=(X_test, X_test))

Evaluation

And we end up with a lightly compressed version of the input images:

# test training
# take 15 images from test set and predict compressed image
predicted = autoencoder.predict(X_test[:10].reshape(-1, 32, 32, 3))
# plot input vs output
fig, axes = plt.subplots(nrows=2, ncols=10, sharex=True, sharey=True, figsize=(20,4))
for images, row in zip([X_test[:10], predicted], axes):
for img, ax in zip(images, row):
ax.imshow(img.reshape((32, 32, 3)))
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

plt.show()

Tensorflow Transfer Learning