%%html
<style>
@import url('https://fonts.googleapis.com/css?family=Orbitron|Roboto');
body {background-color: aliceblue;}
a {color: #4876ff; font-family: 'Roboto';}
h1 {color: #348ABD; font-family: 'Orbitron'; text-shadow: 4px 4px 4px #ccc;}
h2, h3 {color: slategray; font-family: 'Roboto'; text-shadow: 4px 4px 4px #ccc;}
h4 {color: #348ABD; font-family: 'Orbitron';}
span {text-shadow: 4px 4px 4px #ccc;}
div.output_prompt, div.output_area pre {color: slategray;}
div.input_prompt, div.output_subarea {color: #4876ff;}
div.output_stderr pre {background-color: aliceblue;}
div.output_stderr {background-color: slategrey;}
</style>
<script>
code_show = true;
function code_display() {
if (code_show) {
$('div.input').each(function(id) {
if (id == 0 || $(this).html().indexOf('hide_code') > -1) {$(this).hide();}
});
$('div.output_prompt').css('opacity', 0);
} else {
$('div.input').each(function(id) {$(this).show();});
$('div.output_prompt').css('opacity', 1);
};
code_show = !code_show;
}
$(document).ready(code_display);
</script>
<form action="javascript: code_display()">
<input style="color: #348ABD; background: aliceblue; opacity: 0.8;" \
type="submit" value="Click to display or hide code cells">
</form>
hide_code = ''
import numpy as np
import pandas as pd
import math
import tensorflow as tf
from sklearn.model_selection import train_test_split
from keras.utils import to_categorical
import h5py
import cv2
from keras.models import Sequential, load_model, Model
from keras.layers import Input, UpSampling2D
from keras.layers import Dense, LSTM, GlobalAveragePooling1D, GlobalAveragePooling2D
from keras.layers import Activation, Flatten, Dropout, BatchNormalization
from keras.layers import Conv2D, MaxPooling2D, GlobalMaxPooling2D
import matplotlib.pylab as plt
from matplotlib import cm
%matplotlib inline
hide_code
# Read the h5 file
f = h5py.File('LetterColorImages_23.h5', 'r')
# List all groups
keys = list(f.keys())
keys
hide_code
# Create tensors and targets
backgrounds = np.array(f[keys[0]])
tensors = np.array(f[keys[1]])
targets = np.array(f[keys[2]])
print ('Tensor shape:', tensors.shape)
print ('Target shape', targets.shape)
print ('Background shape:', backgrounds.shape)
hide_code
# Plot letter images
fig, ax = plt.subplots(figsize=(18, 3), nrows=1, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(5):
image = tensors[i*2000]/255
ax[i].imshow(image)
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.gcf()
ax[2].set_title('Examples of letters', fontsize=25);
hide_code
# Normalize the tensors
tensors = tensors.astype('float32')/255
hide_code
# Grayscaled tensors
gray_tensors = np.dot(tensors[...,:3], [0.299, 0.587, 0.114])
gray_tensors = gray_tensors.reshape(-1, 32, 32, 1)
print ('Grayscaled Tensor shape:', gray_tensors.shape)
hide_code
# One-hot encode the targets, started from the zero label
cat_targets = to_categorical(np.array(targets-1), 33)
cat_targets.shape
hide_code
# One-hot encode the background targets
backgrounds = to_categorical(backgrounds-2,2)
backgrounds.shape
hide_code
# Create multi-label targets
back_targets = np.concatenate((cat_targets, backgrounds), axis=1)
back_targets.shape
hide_code
# Split the grayscaled data
x_train, x_test, y_train, y_test = train_test_split(gray_tensors, cat_targets,
test_size = 0.2,
random_state = 1)
n = int(len(x_test)/2)
x_valid, y_valid = x_test[:n], y_test[:n]
x_test, y_test = x_test[n:], y_test[n:]
hide_code
# Split the grayscaled data
x_train2, x_test2, y_train2, y_test2 = train_test_split(gray_tensors, back_targets,
test_size = 0.2,
random_state = 1)
n = int(len(x_test2)/2)
x_valid2, y_valid2 = x_test2[:n], y_test2[:n]
x_test2, y_test2 = x_test2[n:], y_test2[n:]
hide_code
# NN for noise reduction
inputs_ = tf.placeholder(tf.float32, (None, 32, 32, 1), name='inputs')
targets_ = tf.placeholder(tf.float32, (None, 32, 32, 1), name='targets')
### Encoder
conv1 = tf.layers.conv2d(inputs_, 32, (3,3), padding='same', activation=tf.nn.relu)
# Now 32x32x32
maxpool1 = tf.layers.max_pooling2d(conv1, (2,2), (2,2), padding='same')
# Now 16x16x32
conv2 = tf.layers.conv2d(maxpool1, 16, (3,3), padding='same', activation=tf.nn.relu)
# Now 16x16x16
maxpool2 = tf.layers.max_pooling2d(conv2, (2,2), (2,2), padding='same')
# Now 8x8x16
conv3 = tf.layers.conv2d(maxpool2, 8, (3,3), padding='same', activation=tf.nn.relu)
# Now 8x8x8
encoded = tf.layers.max_pooling2d(conv3, (2,2), (2,2), padding='same')
# Now 4x4x8
### Decoder
upsample1 = tf.image.resize_nearest_neighbor(encoded, (8,8))
# Now 8x8x8
conv4 = tf.layers.conv2d(upsample1, 8, (3,3), padding='same', activation=tf.nn.relu)
# Now 8x8x8
upsample2 = tf.image.resize_nearest_neighbor(conv4, (16,16))
# Now 16x16x16
conv5 = tf.layers.conv2d(upsample2, 16, (3,3), padding='same', activation=tf.nn.relu)
# Now 16x16x16
upsample3 = tf.image.resize_nearest_neighbor(conv5, (32,32))
# Now 32x32x16
conv6 = tf.layers.conv2d(upsample3, 32, (3,3), padding='same', activation=tf.nn.relu)
# Now 32x32x32
logits = tf.layers.conv2d(conv6, 1, (3,3), padding='same', activation=None)
# Now 32x32x1
decoded = tf.nn.sigmoid(logits, name='decoded')
loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=targets_, logits=logits)
cost = tf.reduce_mean(loss)
opt = tf.train.AdamOptimizer(0.001).minimize(cost)
hide_code
# Function for creating batches
def get_batches(images, batch_size):
current_index = 0
while current_index + batch_size <= images.shape[0]:
data_batch = images[current_index:current_index + batch_size]
current_index += batch_size
yield data_batch
hide_code
# Define parameters
epochs = 200
batch_size = 64
train_step = 0
hide_code
# Run the tensorflow session
sess = tf.Session()
sess.run(tf.global_variables_initializer())
gray_tensors1 = np.copy(gray_tensors)
for e in range(epochs):
for batch_images in get_batches(gray_tensors1, batch_size):
train_step += 1
batch_cost, _ = sess.run([cost, opt], feed_dict={inputs_: batch_images,
targets_: batch_images})
if train_step % 200 == 0:
print("Epoch: {}/{}...".format(e+1, epochs),
"Training loss: {:.4f}".format(batch_cost))
hide_code
# Display original grayscaled images
fig, ax = plt.subplots(figsize=(18, 3), nrows=1, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(5):
image = gray_tensors[i*2000].reshape(32,32)
ax[i].imshow(image, cmap=cm.bone)
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.gcf()
ax[2].set_title('Examples of grayscaled images', fontsize=25);
hide_code
# Display output grayscaled images
fig, ax = plt.subplots(figsize=(18, 3), nrows=1, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(5):
image = gray_tensors1[i*2000].reshape(32,32)
ax[i].imshow(image, cmap=cm.bone)
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.gcf()
ax[2].set_title('Examples of cleaned images. TensorFlow', fontsize=25);
hide_code
def autoencoder():
inputs = Input(shape=(32, 32, 1))
# Encode
x = Conv2D(32, 5, activation='relu', padding='same')(inputs)
x = MaxPooling2D(padding='same')(x)
x = Conv2D(16, 5, activation='relu', padding='same')(x)
x = MaxPooling2D(padding='same')(x)
x = Conv2D(8, 5, activation='relu', padding='same')(x)
encoded = MaxPooling2D(padding='same')(x)
# Decode
x = Conv2D(8, 5, activation='relu', padding='same')(encoded)
x = UpSampling2D()(x)
x = Conv2D(16, 5, activation='relu', padding='same')(x)
x = UpSampling2D()(x)
x = Conv2D(32, 5, activation='relu', padding='same')(x)
x = UpSampling2D()(x)
decoded = Conv2D(1, 3, activation='sigmoid', padding='same')(x)
# Autoencoder
autoencoder = Model(inputs, decoded)
# Compile
autoencoder.compile(optimizer='nadam', loss='binary_crossentropy')
return autoencoder
autoencoder = autoencoder()
hide_code
# Display autoencoder architecture
autoencoder.summary()
hide_code
# Train autoencoder
autoencoder_history = autoencoder.fit(x_train, x_train,
epochs=200, batch_size=64, verbose=2,
validation_data=(x_valid, x_valid))
hide_code
# Plot training history
plt.figure(figsize=(18, 6))
plt.plot(autoencoder_history.history['loss'][2:], color='slategray', label = 'train')
plt.plot(autoencoder_history.history['val_loss'][2:], color='#4876ff', label = 'valid')
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.title('Loss Function');
hide_code
# Create decoded images
x_test_decoded = autoencoder.predict(x_test)
hide_code
fig, ax = plt.subplots(figsize=(18, 3), nrows=1, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(5):
image = x_test[i*150].reshape(32,32)
ax[i].imshow(image, cmap=cm.bone)
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.gcf()
ax[2].set_title('Examples of grayscaled letters', fontsize=25);
hide_code
fig, ax = plt.subplots(figsize=(18, 3), nrows=1, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(5):
image = x_test_decoded[i*150].reshape(32,32)
ax[i].imshow(image, cmap=cm.bone)
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.gcf()
ax[2].set_title('Examples of cleaned letters. Keras', fontsize=25);