import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.utils import plot_model
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Dense, Input, Conv2D, Flatten
from tensorflow.keras.layers import Reshape, Conv2DTranspose
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ReduceLROnPlateau, ModelCheckpoint
def rgb2gray(rgb):
"""convert RGB images to gray_scale.
formula > gray_scale = 0.299*red + 0.587*green + 0.114*blue
"""
return np.dot(rgb[...,:3],[0.299,0.587,0.114])
(x_train,y_train),(x_test,y_test) = cifar10.load_data()
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 3s 0us/step
img_rows = x_train.shape[1]
img_cols = x_train.shape[2]
channles = x_train.shape[3] #rgb
print(img_rows,img_cols,channles)
32 32 3
image_dir = 'saved_images'
save_dir = os.path.join(os.getcwd(),image_dir)
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
images = x_test[:25] #from the test
images = images.reshape((5,5,img_rows,img_cols,channles))
images = np.vstack([np.hstack(i) for i in images]) #3D stacking of the values
plt.figure()
plt.axis('off')
plt.title("Test images")
plt.imshow(images,interpolation='none')
plt.show()
x_train_gray = rgb2gray(x_train)
x_test_gray = rgb2gray(x_test)
images = x_test_gray[:25] #from the test
images = images.reshape((5,5,img_rows,img_cols))
images = np.vstack([np.hstack(i) for i in images]) #3D stacking of the values
plt.figure()
plt.axis('off')
plt.title("Test images")
plt.imshow(images,interpolation='none')
plt.show()
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
x_train_gray = x_train_gray.astype('float32') / 255
x_test_gray = x_test_gray.astype('float32') / 255
x_train = x_train.reshape(x_train.shape[0],img_rows,img_cols,channles)
x_test = x_test.reshape(x_test.shape[0],img_rows,img_cols,channles)
print(x_train.shape)
print(x_test.shape)
(50000, 32, 32, 3)
(10000, 32, 32, 3)
x_train_gray = x_train_gray.reshape(x_train_gray.shape[0],img_rows,img_cols,1)
x_test_gray = x_test_gray.reshape(x_test_gray.shape[0],img_rows,img_cols,1)
print(x_train_gray.shape)
print(x_test_gray.shape)
(50000, 32, 32, 1)
(10000, 32, 32, 1)
input_shape = (img_rows,img_cols,1)
batch_size = 32
latent_dim = 256
layer_filters = [64,128,256]
inputs = Input(shape=input_shape,name='encoder_input')
x = inputs
for filters in layer_filters:
x = Conv2D(filters=filters,
kernel_size = 3,
strides=2,
activation='relu',
padding='same')(x)
shape = K.int_shape(x)
# generating latent vector
x = Flatten()(x)
latent = Dense(latent_dim,name='latent_vector')(x)
encoder = Model(inputs,latent,name='encoder')
encoder.summary()
Model: "encoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
encoder_input (InputLayer) [(None, 32, 32, 1)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 16, 16, 64) 640
_________________________________________________________________
conv2d_1 (Conv2D) (None, 8, 8, 128) 73856
_________________________________________________________________
conv2d_2 (Conv2D) (None, 4, 4, 256) 295168
_________________________________________________________________
flatten (Flatten) (None, 4096) 0
_________________________________________________________________
latent_vector (Dense) (None, 256) 1048832
=================================================================
Total params: 1,418,496
Trainable params: 1,418,496
Non-trainable params: 0
_________________________________________________________________
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)
for filters in layer_filters[::-1] :
x = Conv2DTranspose(filters=filters,
kernel_size = 3,
strides=2,
activation='relu',
padding='same')(x)
outputs = Conv2DTranspose(filters=channles,
kernel_size=3,
activation = 'sigmoid',
padding='same',
name='decoder_output')(x)
decoder = Model(latent_inputs,outputs,name='decoder')
decoder.summary()
Model: "decoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
decoder_input (InputLayer) [(None, 256)] 0
_________________________________________________________________
dense (Dense) (None, 4096) 1052672
_________________________________________________________________
reshape (Reshape) (None, 4, 4, 256) 0
_________________________________________________________________
conv2d_transpose (Conv2DTran (None, 8, 8, 256) 590080
_________________________________________________________________
conv2d_transpose_1 (Conv2DTr (None, 16, 16, 128) 295040
_________________________________________________________________
conv2d_transpose_2 (Conv2DTr (None, 32, 32, 64) 73792
_________________________________________________________________
decoder_output (Conv2DTransp (None, 32, 32, 3) 1731
=================================================================
Total params: 2,013,315
Trainable params: 2,013,315
Non-trainable params: 0
_________________________________________________________________
autoencoder = Model(inputs,decoder(encoder(inputs)),
name='autoencoder')
autoencoder.summary()
Model: "autoencoder"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
encoder_input (InputLayer) [(None, 32, 32, 1)] 0
_________________________________________________________________
encoder (Functional) (None, 256) 1418496
_________________________________________________________________
decoder (Functional) (None, 32, 32, 3) 2013315
=================================================================
Total params: 3,431,811
Trainable params: 3,431,811
Non-trainable params: 0
_________________________________________________________________
save_dir = os.path.join(os.getcwd(),'saved_models')
model_name = 'Colorzier_Autoencoder_Model.{epoch:03d}.h5'
if not os.path.isdir(save_dir):
os.makedirs(save_dir)
filepath = os.path.join(save_dir,model_name)
lr_reducer = ReduceLROnPlateau(factor=np.sqrt(0.1),
cooldown=0,
patience=3,
verbose=1,
min_lr=0.5e-6)
checkpoint = ModelCheckpoint(filepath=filepath,
monitor = 'val_loss',
verbose=1,
save_best_only=True)
autoencoder.compile(loss='mse',optimizer='adam',metrics='accuracy')
callbacks = [lr_reducer,checkpoint]
autoencoder.fit(x_train_gray,
x_train,
validation_data=(x_test_gray,x_test),
epochs=10,
batch_size=128,
callbacks=callbacks)
Epoch 1/10
251/391 [==================>...........] - ETA: 2:26 - loss: 0.0372 - accuracy: 0.4570
x_decode = autoencoder.predict(x_test_gray)
imgs = x_decode[:25]
imgs = imgs.reshape((5, 5, img_rows, img_cols, channles))
imgs = np.vstack([np.hstack(i) for i in imgs])
plt.figure()
plt.axis('off')
plt.title('Colorized test images (Predicted)')
plt.imshow(imgs, interpolation='none')
plt.savefig('%s/colorized.png' % image_dir)
plt.show()
images = x_test[:25] #from the test
images = images.reshape((5,5,img_rows,img_cols,channles))
images = np.vstack([np.hstack(i) for i in images]) #3D stacking of the values
plt.figure()
plt.axis('off')
plt.title("Test images")
plt.imshow(images,interpolation='none')
plt.show()