| import numpy as np |
| import random |
| import glob |
| import sys |
| import os |
| from PIL import Image |
| import matplotlib.pyplot as plt |
| from tensorflow.keras.losses import mean_squared_error |
| from tensorflow.keras.preprocessing.image import ImageDataGenerator |
| from tensorflow.keras.models import load_model |
| from pathlib import Path |
|
|
| ''' |
| Evaluates the trained model on a new dataset |
| Uses Mean Square Error as evaluation metric |
| ''' |
|
|
| |
| EVAL_PATH = Path(sys.argv[1]).resolve() |
| BATCH_SIZE = 1 |
| TEST_NUM = len(glob.glob(str(EVAL_PATH))) |
|
|
|
|
| |
| def normalize_batch(imgs): |
| 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 |
|
|
|
|
| |
| test_imgen = ImageDataGenerator(rescale=1. / 255) |
|
|
|
|
| |
| def custom_loss_1(secret, secret_pred): |
| secret_mse = mean_squared_error(secret, secret_pred) |
| return secret_mse |
|
|
|
|
| |
| def custom_loss_2(cover, cover_pred): |
| cover_mse = mean_squared_error(cover, cover_pred) |
| return cover_mse |
|
|
|
|
| |
| model = load_model(sys.argv[2], custom_objects={'custom_loss_1': custom_loss_1, 'custom_loss_2': custom_loss_2}) |
|
|
|
|
| |
| def generate_generator_multiple(generator, direc): |
| genX1 = generator.flow_from_directory(direc, target_size=(224, 224), batch_size=12, shuffle=True, seed=3, |
| class_mode=None) |
| genX2 = generator.flow_from_directory(direc, target_size=(224, 224), batch_size=12, shuffle=True, seed=8, |
| class_mode=None) |
|
|
| while True: |
| X1i = normalize_batch(genX1.next()) |
| X2i = normalize_batch(genX2.next()) |
|
|
| yield ({'secret': X1i, 'cover': X2i}, |
| {'hide_conv_f': X2i, 'revl_conv_f': X1i}) |
|
|
|
|
| |
| testgenerator = generate_generator_multiple(test_imgen, direc=str(EVAL_PATH.parent)) |
|
|
| |
| score = model.evaluate(testgenerator, steps=TEST_NUM / BATCH_SIZE, verbose=0) |
|
|
| |
| print('Mean square error:', score[0]) |
|
|
| ''' |
| Test the model on a random pair of images (test) |
| Plots the input and output for verification |
| ''' |
|
|
|
|
| |
| def predict(source, cover): |
| |
| secret = np.array(source / 255.0) |
| cover = np.array(cover / 255.0) |
|
|
| |
| coverout, secretout = model.predict( |
| [normalize_batch(np.reshape(secret, (1, 224, 224, 3))), normalize_batch(np.reshape(cover, (1, 224, 224, 3)))]) |
|
|
| |
| 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) |
|
|
| |
| fig_out, ax_out = plt.subplots(1, 2, figsize=(10, 10)) |
| fig_out.suptitle('Outputs') |
| ax_out[0].title.set_text("Secret output") |
| ax_out[0].imshow(secretout) |
| ax_out[1].title.set_text("Cover output") |
| ax_out[1].imshow(coverout) |
| print("Prediction Completed!") |
|
|
|
|
| |
| sec, cov = random.sample(os.listdir(str(EVAL_PATH)), k=2) |
|
|
| source = np.array(Image.open(str(EVAL_PATH) + '/' + sec)) |
| cover = np.array(Image.open(str(EVAL_PATH) + '/' + cov)) |
|
|
| |
| fig_in, ax_in = plt.subplots(1, 2, figsize=(10, 10)) |
| fig_in.suptitle('Inputs') |
| ax_in[0].title.set_text("Secret input") |
| ax_in[0].imshow(source) |
| ax_in[1].title.set_text("Cover input") |
| ax_in[1].imshow(cover) |
|
|
| |
| predict(source, cover) |
|
|
| |
|
|