NOTE: This Use Case is not purposed for resource constrained devices.
Improving Image Resolution with Autoencoders¶
Credit: AITS Cainvas Community
Autoencoders can be used for increasing Image Resolution and this has proved to be effective when we want extract information from the feeds of Surveillance Cameras and autoencoders can also be used for Noise Reduction and they can serve as part of various IIoT Applications.
Downloading the Dataset¶
Data has been taken from the Imagenet dataset.
In [1]:
!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/data.zip"
!unzip -qo data.zip
!rm data.zip
Building the Autoencoder¶
In [2]:
# Importing necessary Libraries
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, Conv2DTranspose, UpSampling2D, add
from tensorflow.keras.models import Model
from tensorflow.keras import regularizers
# Building the encoder
input_img = Input(shape=(256, 256, 3))
l1 = Conv2D(64, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(input_img)
l2 = Conv2D(64, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l1)
l3 = MaxPooling2D(padding='same')(l2)
l3 = Dropout(0.3)(l3)
l4 = Conv2D(128, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l3)
l5 = Conv2D(128, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l4)
l6 = MaxPooling2D(padding='same')(l5)
l7 = Conv2D(256, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l6)
In [3]:
# Building the Decoder
l8 = UpSampling2D()(l7)
l9 = Conv2D(128, (3, 3), padding='same', activation='relu',
activity_regularizer=regularizers.l1(10e-10))(l8)
l10 = Conv2D(128, (3, 3), padding='same', activation='relu',
activity_regularizer=regularizers.l1(10e-10))(l9)
l11 = add([l5, l10])
l12 = UpSampling2D()(l11)
l13 = Conv2D(64, (3, 3), padding='same', activation='relu',
activity_regularizer=regularizers.l1(10e-10))(l12)
l14 = Conv2D(64, (3, 3), padding='same', activation='relu',
activity_regularizer=regularizers.l1(10e-10))(l13)
l15 = add([l14, l2])
decoded = Conv2D(3, (3, 3), padding='same', activation='relu', activity_regularizer=regularizers.l1(10e-10))(l15)
# Create our network
autoencoder = Model(input_img, decoded)
In [4]:
autoencoder.summary()
Defining the Callbacks and Compiling the Model¶
In [5]:
from tensorflow.keras.callbacks import ModelCheckpoint
autoencoder.compile(optimizer='adam', loss='mean_squared_error')
checkpointer = ModelCheckpoint(filepath = "model1.h5", verbose = 2, save_best_only = True)
Reading the images from Directory and Creating the Dataset and Defining Training Routine¶
In [6]:
import os
import re
from scipy import ndimage, misc
from skimage.transform import resize, rescale
from matplotlib import pyplot
import numpy as np
def train_batches(just_load_dataset=False):
# Number of images to have at the same time in a batch
batches = 256
# Number if images in the current batch (grows over time and then resets for each batch)
batch = 0
# For printing purpose
batch_nb = 0
max_batches = -1
# Number of epochs
ep = 4
images = []
x_train_high = []
x_train_low = []
x_train_high2 = [] # Resulting high res dataset
x_train_low2 = [] # Resulting low res dataset
for root, dirnames, filenames in os.walk("data"):
for filename in filenames:
if re.search("\.(jpg|jpeg|JPEG|png|bmp|tiff)$", filename):
filepath = os.path.join(root, filename)
image = pyplot.imread(filepath)
if len(image.shape) > 2:
# Resize the image so that every image is the same size
image_resized = resize(image, (256, 256))
x_train_high.append(image_resized) # Add this image to the high res dataset
x_train_low.append(rescale(rescale(image_resized, 0.5, multichannel=True), 2.0, multichannel=True)) # Creating the blurred images
batch += 1
if batch == batches:
x_train_high2 = np.array(x_train_high)
x_train_low2 = np.array(x_train_low)
if just_load_dataset:
return x_train_high2, x_train_low2
batch_nb += 1
print('Training batch', batch_nb)
autoencoder.fit(x_train_low2, x_train_high2,
epochs=ep,
batch_size=10,
shuffle=True,
validation_split=0.15,
callbacks = [checkpointer] )
x_train_high = []
x_train_low = []
batch = 0
return x_train_high2, x_train_low2
Training the model¶
In [7]:
x_train_n, x_train_down = train_batches(just_load_dataset=False)
Loading The saved Model¶
In [8]:
import tensorflow as tf
new_model1 = tf.keras.models.load_model('model1.h5')
new_model1.summary()
Visualizing the results¶
In [9]:
# We clip the output so that it doesn't produce weird colors
sr1 = np.clip(new_model1.predict(x_train_down), 0.0, 1.0)
image_index = 210
In [10]:
import matplotlib.pyplot as plt
plt.figure(figsize=(128, 128))
i = 1
ax = plt.subplot(10, 10, i)
plt.title("Blurred Image")
plt.imshow(x_train_down[image_index])
i += 1
ax = plt.subplot(10, 10, i)
plt.title("Original Image")
plt.imshow(x_train_n[image_index])
i += 1
ax = plt.subplot(10, 10, i)
plt.title("Recovered Image")
plt.imshow(sr1[image_index])
plt.show()
Compiling the Model using DeepCC¶
In [ ]:
!deepCC model1.h5