| import numpy as np |
| import sys |
| from tensorflow.keras.models import Model |
| from tensorflow.keras.utils import plot_model |
| from tensorflow.keras.models import load_model |
| from PIL import Image |
| import matplotlib.pyplot as plt |
| from random import randint |
| import imageio |
| from skimage.util.shape import view_as_blocks |
|
|
| ''' |
| Test the model on sample images (unseen) |
| Plot the input and output images |
| ''' |
|
|
| |
| test_images = np.load(sys.argv[1]) |
|
|
| |
| model = load_model(sys.argv[2], compile=False) |
|
|
|
|
| |
| def normalize_batch(imgs): |
| """ Performs channel-wise z-score normalization """ |
|
|
| return (imgs - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225]) |
|
|
|
|
| |
| def denormalize_batch(imgs, should_clip=True): |
| imgs = (imgs * np.array([0.229, 0.224, 0.225])) + np.array([0.485, 0.456, 0.406]) |
|
|
| if should_clip: |
| imgs = np.clip(imgs, 0, 1) |
| return imgs |
|
|
|
|
| |
| secretin = test_images[np.random.choice(len(test_images), size=4, replace=False)] |
| coverin = test_images[np.random.choice(len(test_images), size=4, replace=False)] |
|
|
| |
| coverout, secretout = model.predict([normalize_batch(secretin), normalize_batch(coverin)]) |
|
|
| |
| coverout = denormalize_batch(coverout) |
| coverout = np.squeeze(coverout) * 255.0 |
| coverout = np.uint8(coverout) |
|
|
| |
| secretout = denormalize_batch(secretout) |
| secretout = np.squeeze(secretout) * 255.0 |
| secretout = np.uint8(secretout) |
|
|
| |
| coverin = np.uint8(np.squeeze(coverin * 255.0)) |
| secretin = np.uint8(np.squeeze(secretin * 255.0)) |
|
|
|
|
| |
| def plot(im, title): |
| fig = plt.figure(figsize=(20, 20)) |
|
|
| for i in range(4): |
| sub = fig.add_subplot(1, 4, i + 1) |
| sub.title.set_text(title + " " + str(i + 1)) |
| sub.imshow(im[i, :, :, :]) |
|
|
|
|
| |
| plot(secretin, "Secret Input") |
| plot(secretout, "Secret Output") |
|
|
| |
| plot(coverin, "Cover Input") |
| plot(coverout, "Cover Output") |
|
|
| |
|
|