image_output_1.png
from keras.datasets import mnist, cifar10

import matplotlib.pyplot as plt

import numpy as np

def preprocess(array1, array2, channel):
    """
    Normalizes/scales [0,1], divinding by the supplied array and reshapes 
    it into the appropriate format.
    """
    if channel == 1:
        ar1 = array1.astype('float32').reshape([-1, 28, 28, 1]) / 255.0
        ar2 = array2.astype('float32').reshape([-1, 28, 28, 1]) / 255.0
    else:
        ar1 = array1.astype('float32').reshape([-1, 32, 32, 3]) / 255
        ar2 = array2.astype('float32').reshape([-1, 32, 32, 3]) / 255
    return (ar1, ar2)

def noise(a1, a2, channel):
    """
    Adds random noise to each image in the supplied array.
    """
    if channel == 1:
        noise_factor = 0.2
        noisy_arr1 = a1 + noise_factor * np.random.normal(0.0, 1.0, size=a1.shape)
        noisy_arr2 = a2 + noise_factor * np.random.normal(0.0, 1.0, size=a2.shape)
    else:
        noi = 0.1
        noisy_arr1 = a1 + noi * np.random.normal(0.0, 1.0, size=a1.shape)
        noisy_arr2 = a2 + noi * np.random.normal(0.0, 1.0, size=a2.shape)
    ab1 = np.clip(noisy_arr1, 0, 1)
    ab2 = np.clip(noisy_arr2, 0, 1)
    return (ab1, ab2)

# Visualization for mnist, cifar10, noisy, denoised/predictions data
def display(rows, cols, a, b, check=False ):
    '''rows: defining no. of rows in figure
      cols: defining no. of colums in figure
      a: train images without noise or noisy_image while test
      prediction
      b: train images with noise or denoised_image based while test 
      prediction
      check: default False for 32*32 cifar10, true for 28*28
      mnist dataset and any predictions
    '''
    # defining a figure 
    f = plt.figure(figsize=(2*cols,2*rows*2)) 
    for i in range(rows):
        for j in range(cols):
            # adding subplot to figure on each iteration
            f.add_subplot(rows*2,cols, (2*i*cols)+(j+1))
            if check:
                plt.imshow(a[i*cols + j].reshape([28,28]),cmap="Blues")
            else:
                plt.imshow(a[i*cols + j])
            plt.axis("off")
        for j in range(cols):
            # adding subplot to figure on each iteration
            f.add_subplot(rows*2,cols,((2*i+1)*cols)+(j+1)) 
            if check:
                plt.imshow(b[i*cols + j].reshape([28,28]),cmap="Blues")
            else:
                plt.imshow(b[i*cols + j])
            plt.axis("off")
            plt.axis("off")
        
    #f.suptitle("Sample Training Data",fontsize=18)
    plt.savefig("ss.png")
    plt.show()

(train_data, _), (test_data, _) = mnist.load_data()
channel = 1
train_data, test_data = preprocess(train_data, test_data, channel)
noisy_train_data, noisy_test_data = noise(train_data, test_data, channel)

(cifar_train, _), (cifar_test, _) = cifar10.load_data()
rows = 2
cols = 3
channel = 3
cifar_train, cifar_test = preprocess(cifar_train, cifar_test, channel)
cifar_train_noise, cifar_test_noise = noise(cifar_train, cifar_test, channel)
display(rows, cols, cifar_train, cifar_train_noise)