This notebook will work only if you already have the trained model or you have executed training module first. The loading module cell will load all the necessary libararies and modules.
Loading Modules¶
In [1]:
import os, skimage, pickle
from utils import psnr
import numpy as np
import tf_psnr
import SRDataGenerator
import matplotlib.pyplot as plt
from tensorflow import keras
from matplotlib.image import imsave
Loading Test Data¶
Here the test data is loaded separately and all the necessary preprocessing steps such as normalization and reshaping are applied in this block.
In [2]:
imageDirPath = './img_data'
def ReshapeData(noisyImages, goodImages, IMG_RES, IMG_RES1):
noisyImages, goodImages= np.array(noisyImages), np.array(goodImages)
noisyImages = np.reshape(noisyImages, (noisyImages.shape[0],) + IMG_RES)
goodImages = np.reshape(goodImages, (goodImages.shape[0],) + (1,) +IMG_RES1 )
return noisyImages, goodImages
def LoadTestData(imageDirPath):
testxPath= os.path.join(imageDirPath,'trainx')
testyPath= os.path.join(imageDirPath,'trainy')
testXpath= SRDataGenerator.ExtractPath(testxPath)
testYpath= SRDataGenerator.ExtractPath(testyPath)
testDataX= SRDataGenerator.LoadImages(testXpath)
testDataY= SRDataGenerator.LoadImages(testYpath)
testx=SRDataGenerator.ImageNormalization(testDataX)
testy=SRDataGenerator.ImageNormalization(testDataY)
return testx[:], testy[:]
testx,testy= LoadTestData(imageDirPath)
IMG_RES = tuple(testx[0].shape[0:3])
IMG_RES1 = tuple(testy[0].shape[0:2])
testx, testy= ReshapeData(testx, testy, IMG_RES, IMG_RES1)
Load Pretrained Model¶
The function below will load the already trained model and also plot the summary of the model.
In [3]:
def LoadModel(modelFile):
model = keras.models.load_model(modelFile, custom_objects ={'psnr':psnr})
return model
modelFile='SR_REDSIM_25-07-2020_07-42-model'
model=LoadModel(modelFile)
model.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, 15, 512, 512 0
__________________________________________________________________________________________________
conv2d_1 (Conv2D) (None, 64, 512, 512) 8704 input_1[0][0]
__________________________________________________________________________________________________
conv2d_2 (Conv2D) (None, 64, 512, 512) 36928 conv2d_1[0][0]
__________________________________________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 512, 512) 36928 conv2d_2[0][0]
__________________________________________________________________________________________________
conv2d_4 (Conv2D) (None, 64, 512, 512) 36928 conv2d_3[0][0]
__________________________________________________________________________________________________
conv2d_5 (Conv2D) (None, 64, 512, 512) 36928 conv2d_4[0][0]
__________________________________________________________________________________________________
conv2d_6 (Conv2D) (None, 64, 512, 512) 36928 conv2d_5[0][0]
__________________________________________________________________________________________________
conv2d_7 (Conv2D) (None, 64, 512, 512) 36928 conv2d_6[0][0]
__________________________________________________________________________________________________
conv2d_8 (Conv2D) (None, 64, 512, 512) 36928 conv2d_7[0][0]
__________________________________________________________________________________________________
conv2d_9 (Conv2D) (None, 64, 512, 512) 36928 conv2d_8[0][0]
__________________________________________________________________________________________________
conv2d_10 (Conv2D) (None, 64, 512, 512) 36928 conv2d_9[0][0]
__________________________________________________________________________________________________
conv2d_11 (Conv2D) (None, 64, 512, 512) 36928 conv2d_10[0][0]
__________________________________________________________________________________________________
conv2d_12 (Conv2D) (None, 64, 512, 512) 36928 conv2d_11[0][0]
__________________________________________________________________________________________________
conv2d_13 (Conv2D) (None, 64, 512, 512) 36928 conv2d_12[0][0]
__________________________________________________________________________________________________
conv2d_14 (Conv2D) (None, 64, 512, 512) 36928 conv2d_13[0][0]
__________________________________________________________________________________________________
conv2d_15 (Conv2D) (None, 64, 512, 512) 36928 conv2d_14[0][0]
__________________________________________________________________________________________________
conv2d_16 (Conv2D) (None, 64, 512, 512) 36928 conv2d_15[0][0]
__________________________________________________________________________________________________
conv2d_17 (Conv2D) (None, 64, 512, 512) 36928 conv2d_16[0][0]
__________________________________________________________________________________________________
conv2d_18 (Conv2D) (None, 64, 512, 512) 36928 conv2d_17[0][0]
__________________________________________________________________________________________________
conv2d_19 (Conv2D) (None, 64, 512, 512) 36928 conv2d_18[0][0]
__________________________________________________________________________________________________
conv2d_20 (Conv2D) (None, 64, 512, 512) 36928 conv2d_19[0][0]
__________________________________________________________________________________________________
conv2d_21 (Conv2D) (None, 64, 512, 512) 36928 conv2d_20[0][0]
__________________________________________________________________________________________________
conv2d_transpose_1 (Conv2DTrans (None, 64, 512, 512) 36928 conv2d_21[0][0]
__________________________________________________________________________________________________
add_1 (Add) (None, 64, 512, 512) 0 conv2d_transpose_1[0][0]
conv2d_20[0][0]
__________________________________________________________________________________________________
activation_1 (Activation) (None, 64, 512, 512) 0 add_1[0][0]
__________________________________________________________________________________________________
conv2d_transpose_2 (Conv2DTrans (None, 64, 512, 512) 36928 activation_1[0][0]
__________________________________________________________________________________________________
conv2d_transpose_3 (Conv2DTrans (None, 64, 512, 512) 36928 conv2d_transpose_2[0][0]
__________________________________________________________________________________________________
add_2 (Add) (None, 64, 512, 512) 0 conv2d_transpose_3[0][0]
conv2d_18[0][0]
__________________________________________________________________________________________________
activation_2 (Activation) (None, 64, 512, 512) 0 add_2[0][0]
__________________________________________________________________________________________________
conv2d_transpose_4 (Conv2DTrans (None, 64, 512, 512) 36928 activation_2[0][0]
__________________________________________________________________________________________________
conv2d_transpose_5 (Conv2DTrans (None, 64, 512, 512) 36928 conv2d_transpose_4[0][0]
__________________________________________________________________________________________________
add_3 (Add) (None, 64, 512, 512) 0 conv2d_transpose_5[0][0]
conv2d_16[0][0]
__________________________________________________________________________________________________
activation_3 (Activation) (None, 64, 512, 512) 0 add_3[0][0]
__________________________________________________________________________________________________
conv2d_transpose_6 (Conv2DTrans (None, 64, 512, 512) 36928 activation_3[0][0]
__________________________________________________________________________________________________
conv2d_transpose_7 (Conv2DTrans (None, 64, 512, 512) 36928 conv2d_transpose_6[0][0]
__________________________________________________________________________________________________
add_4 (Add) (None, 64, 512, 512) 0 conv2d_transpose_7[0][0]
conv2d_14[0][0]
__________________________________________________________________________________________________
activation_4 (Activation) (None, 64, 512, 512) 0 add_4[0][0]
__________________________________________________________________________________________________
conv2d_transpose_8 (Conv2DTrans (None, 64, 512, 512) 36928 activation_4[0][0]
__________________________________________________________________________________________________
conv2d_transpose_9 (Conv2DTrans (None, 64, 512, 512) 36928 conv2d_transpose_8[0][0]
__________________________________________________________________________________________________
add_5 (Add) (None, 64, 512, 512) 0 conv2d_transpose_9[0][0]
conv2d_12[0][0]
__________________________________________________________________________________________________
activation_5 (Activation) (None, 64, 512, 512) 0 add_5[0][0]
__________________________________________________________________________________________________
conv2d_transpose_10 (Conv2DTran (None, 64, 512, 512) 36928 activation_5[0][0]
__________________________________________________________________________________________________
conv2d_transpose_11 (Conv2DTran (None, 64, 512, 512) 36928 conv2d_transpose_10[0][0]
__________________________________________________________________________________________________
add_6 (Add) (None, 64, 512, 512) 0 conv2d_transpose_11[0][0]
conv2d_10[0][0]
__________________________________________________________________________________________________
activation_6 (Activation) (None, 64, 512, 512) 0 add_6[0][0]
__________________________________________________________________________________________________
conv2d_transpose_12 (Conv2DTran (None, 64, 512, 512) 36928 activation_6[0][0]
__________________________________________________________________________________________________
conv2d_transpose_13 (Conv2DTran (None, 64, 512, 512) 36928 conv2d_transpose_12[0][0]
__________________________________________________________________________________________________
add_7 (Add) (None, 64, 512, 512) 0 conv2d_transpose_13[0][0]
conv2d_8[0][0]
__________________________________________________________________________________________________
activation_7 (Activation) (None, 64, 512, 512) 0 add_7[0][0]
__________________________________________________________________________________________________
conv2d_transpose_14 (Conv2DTran (None, 64, 512, 512) 36928 activation_7[0][0]
__________________________________________________________________________________________________
conv2d_transpose_15 (Conv2DTran (None, 64, 512, 512) 36928 conv2d_transpose_14[0][0]
__________________________________________________________________________________________________
add_8 (Add) (None, 64, 512, 512) 0 conv2d_transpose_15[0][0]
conv2d_6[0][0]
__________________________________________________________________________________________________
activation_8 (Activation) (None, 64, 512, 512) 0 add_8[0][0]
__________________________________________________________________________________________________
conv2d_transpose_16 (Conv2DTran (None, 64, 512, 512) 36928 activation_8[0][0]
__________________________________________________________________________________________________
conv2d_transpose_17 (Conv2DTran (None, 64, 512, 512) 36928 conv2d_transpose_16[0][0]
__________________________________________________________________________________________________
add_9 (Add) (None, 64, 512, 512) 0 conv2d_transpose_17[0][0]
conv2d_4[0][0]
__________________________________________________________________________________________________
activation_9 (Activation) (None, 64, 512, 512) 0 add_9[0][0]
__________________________________________________________________________________________________
conv2d_transpose_18 (Conv2DTran (None, 64, 512, 512) 36928 activation_9[0][0]
__________________________________________________________________________________________________
conv2d_transpose_19 (Conv2DTran (None, 64, 512, 512) 36928 conv2d_transpose_18[0][0]
__________________________________________________________________________________________________
add_10 (Add) (None, 64, 512, 512) 0 conv2d_transpose_19[0][0]
conv2d_2[0][0]
__________________________________________________________________________________________________
activation_10 (Activation) (None, 64, 512, 512) 0 add_10[0][0]
__________________________________________________________________________________________________
conv2d_transpose_20 (Conv2DTran (None, 64, 512, 512) 36928 activation_10[0][0]
__________________________________________________________________________________________________
conv2d_transpose_21 (Conv2DTran (None, 64, 512, 512) 36928 conv2d_transpose_20[0][0]
__________________________________________________________________________________________________
conv2d_transpose_22 (Conv2DTran (None, 64, 1024, 102 36928 conv2d_transpose_21[0][0]
__________________________________________________________________________________________________
conv2d_transpose_23 (Conv2DTran (None, 1, 1024, 1024 577 conv2d_transpose_22[0][0]
==================================================================================================
Total params: 1,560,257
Trainable params: 1,560,257
Non-trainable params: 0
__________________________________________________________________________________________________
In [4]:
plt.imshow(testx[0][0].reshape(512,512), cmap='gray')
Out[4]:
<matplotlib.image.AxesImage at 0x7f7cad39a690>
Result and Evaluation¶
Here the stack of test input is propagated into the trained model and the model will predict a high-quality SR-SIM image along with loss value and PSNR metrics.
In [4]:
inImgReshaped = testx[0].reshape((1,) + testx[0].shape)
originalImgReshaped = testy[0].reshape((1,) + testy[0].shape)
propagatedImg = np.array(model.predict(inImgReshaped, batch_size=1))
plt.imshow(propagatedImg.reshape(1024,1024), cmap='gray')
metrics= [model.evaluate( inImgReshaped, originalImgReshaped, batch_size=1)]
print (metrics)
1/1 [==============================] - 9s 9s/sample - loss: 0.0016 - psnr: 27.9446 - mean_squared_error: 0.0016 [[0.0016052557621151209, 27.944557, 0.0016052558]]
Save Results¶
Save the predicted denoised and reconstructed image onto the disk
