<jupyter_start><jupyter_text>**Mounting Google Drive for Dataset**<jupyter_code>from google.colab import drive
drive.mount('/content/drive')<jupyter_output><empty_output><jupyter_text>**Importing Libraries**<jupyter_code>import numpy as np
import h5py
import math

from keras.models import Model
from keras.layers import Input, Activation, BatchNormalization, Conv2D, Conv3D
from keras.layers import Lambda, Concatenate, MaxPooling2D, Maximum, Add
from keras.initializers import RandomNormal
from keras.optimizers import SGD
from keras.losses import MeanSquaredError
from keras.callbacks import Callback,LearningRateScheduler
from keras.utils import plot_model

import keras.backend as K
K.set_image_data_format('channels_last')

import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow

%matplotlib inline<jupyter_output>Using TensorFlow backend.<jupyter_text>**Util functions**<jupyter_code># Load Dataset from Google Drive
def load_train_dataset():
    """
    Load training dataset from Google Drive
    
    Returns:
    clean_image -- Clean image patches
    clean_image -- Hazy image patches
    transmission_value -- Transmission value which convert clean image to 
                          haze image
    """

    file = '/content/drive/My Drive/DL Project/Datasets/TrainData/train_data_patch_16x16.hdf5'
    train_dataset = h5py.File(file, 'r')
    clean_image = np.array(train_dataset['clear_image'][:])
    haze_image = np.array(train_dataset['haze_image'][:])
    transmission_value = np.array(train_dataset['transmission_value'])
    
    return clean_image, haze_image, transmission_value


# Gaussian Weight Initializtion for layers
weight_init = RandomNormal(mean=0.0, stddev=0.001)


# LearningRate Decay function
def lr_schedule(epoch, lr, logs={}):
    """
    Learning Rate Deacy scheduler

    Arguments:
    epoch -- current epoch number
    lr -- current learning rate
    log -- dictionary storing the logs of training

    Returns:
    lr -- learning rate for next epoch
    """
    
    print('learning_rate:',lr)
    logs.update({'lr': lr})
    if epoch in (49,99):
        return lr*0.5
    else:
        return lr<jupyter_output><empty_output><jupyter_text>**Preparing Train Dataset**<jupyter_code>clean_image, haze_image, transmission_value = load_train_dataset()
transmission_value = transmission_value.reshape(-1,1,1,1)

print ("number of training examples:", clean_image.shape[0])
print ("Clean Image Patch shape:", clean_image.shape)
print ("Haze Image Patch shape:", haze_image.shape)
print ("Transmission Value shape:", transmission_value.shape)<jupyter_output>number of training examples: 60000
Clean Image Patch shape: (60000, 16, 16, 3)
Haze Image Patch shape: (60000, 16, 16, 3)
Transmission Value shape: (60000, 1, 1, 1)<jupyter_text>Transmission Model **Designing**<jupyter_code>def TransmissionModel(input_shape):
    """
    Implementation of the Model.
    
    Arguments:
    input_shape -- shape of the images of the dataset
        (height, width, channels) as a tuple.  
        Note that this does not include the 'batch' as a dimension.
        If you have a batch like 'X_train', 
        then you can provide the input_shape using
        X_train.shape[1:]

    Returns:
    model -- a Model() instance in Keras
    """
    
    X_input = Input(input_shape, name = 'input1')

    # CONV -> RELU Block applied to X
    X = Conv2D(16, (3, 3), strides = (1, 1), kernel_initializer = weight_init, name = 'conv1')(X_input)
    X = Activation('relu', name = 'activation1')(X)

    # SLICE Block applied to X
    X1 = Lambda(lambda X: X[:,:,:,:4], name = 'slice1')(X)
    X2 = Lambda(lambda X: X[:,:,:,4:8], name = 'slice2')(X)
    X3 = Lambda(lambda X: X[:,:,:,8:12], name = 'slice3')(X)
    X4 = Lambda(lambda X: X[:,:,:,12:], name = 'slice4')(X)

    # MAXIMUM Block applied to 4 slices
    X = Maximum(name = 'merge1_maximum')([X1,X2,X3,X4])

    # CONV BLock for multi-scale mapping with filters of size 3x3, 5x5, 7x7
    X_3x3 = Conv2D(16, (3, 3), strides = (1, 1), padding = 'same', kernel_initializer = weight_init, name = 'conv2_3x3')(X)
    X_5x5 = Conv2D(16, (5, 5), strides = (1, 1), padding = 'same', kernel_initializer = weight_init, name = 'conv2_5x5')(X)
    X_7x7 = Conv2D(16, (7, 7), strides = (1, 1), padding = 'same', kernel_initializer = weight_init, name = 'conv2_7x7')(X)

    # CONCATENATE Block to join 3 multi-scale layers
    X = Concatenate(name = 'merge2_concatenate')([X_3x3,X_5x5,X_7x7])

    # MAXPOOL layer of filter size 7x7
    X = MaxPooling2D((7, 7), strides = (1, 1), name = 'maxpool1')(X)
    
    # CONV -> RELU BLock
    X = Conv2D(1, (8, 8), strides = (1, 1), kernel_initializer = weight_init, name = 'conv3')(X)
    X = Activation('relu', name = 'activation2')(X)

    # Create Keras model instance
    model = Model(inputs = X_input, outputs = X, name='TransmissionModel')

    return model<jupyter_output><empty_output><jupyter_text>**Compiling**<jupyter_code>model1 = TransmissionModel(haze_image.shape[1:])
model1.summary()
model1.compile(optimizer=SGD(0.001), loss=MeanSquaredError())<jupyter_output>Model: "TransmissionModel"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input1 (InputLayer)             (None, 16, 16, 3)    0                                            
__________________________________________________________________________________________________
conv1 (Conv2D)                  (None, 14, 14, 16)   448         input1[0][0]                     
__________________________________________________________________________________________________
activation1 (Activation)        (None, 14, 14, 16)   0           conv1[0][0]                      
__________________________________________________________________________________________________
slice1 (Lambda)                 (None, 14, 14, 4)    0           activation1[0][0][...]<jupyter_text>**Training**<jupyter_code>history1 = model1.fit(haze_image, transmission_value, batch_size = 30, epochs = 150, callbacks=[LearningRateScheduler(lr_schedule)])<jupyter_output><empty_output><jupyter_text>**Generating Models and Graph**<jupyter_code>plt.plot(history1.history['lr'])
plt.title('model learning rate')
plt.ylabel('learning rate')
plt.xlabel('epoch')
plt.legend(['train'], loc='upper right')
plt.savefig('trans150-30-lr.png')
plt.show()
plt.plot(history1.history['loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train'], loc='upper right')
plt.savefig('trans150-30-loss.png')
plt.show()
model1.save("transmodel_150_30.h5")
model1.save_weights('transmodel_150_30_weights.h5')
plot_model(model1, 'trans_model.png')
plot_model(model1, 'trans_model_shape.png', True)<jupyter_output><empty_output><jupyter_text>**Predicting**<jupyter_code>t = np.expand_dims(haze_image[500], axis=0)
print(model1.predict(t))
print(transmission_value[500])<jupyter_output>[[[[0.5641386]]]]
[[[0.65]]]
{'fraction_non_alphanumeric': 0.13594405594405595, 'fraction_numerical': 0.036783216783216784, 'mean_word_length': 4.336567164179105, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '601152', 'license': 'MIT', 'n_tokens_mistral': 2465, 'n_tokens_neox': 2273, 'n_words': 544, 'path': 'Jupyter Notebooks/Network_Model_Transmission.ipynb', 'repo_name': 'muditjoshi98/muditjoshi98-Image-dehazing-using-Residual-NN', 'size': '7150'}