<jupyter_start><jupyter_text><jupyter_code># For more ram
a = []
while(True):
  a.append('1')
!nvidia-smi
from google.colab import drive
drive.mount('/content/drive')
# Importing libraries
import os
import pickle
import numpy as np
import pandas as pd

from keras import applications
from keras.preprocessing.image import ImageDataGenerator
from keras import optimizers
from keras.optimizers import Adam
from keras.models import Sequential, Model
from keras.layers import Dense, GlobalAveragePooling2D,AvgPool2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import backend as K
import tensorflow as tf
from keras.layers import Conv2D,MaxPooling2D,Dropout,Flatten,Dense,BatchNormalization,LeakyReLU,MaxPool2D
from keras.models import Sequential
from keras.layers import Conv2DTranspose,Reshape
PATH = './drive/My Drive/TIP/Dataset/'
PATH_TO_MAIN = './drive/My Drive/TIP/'
YT_LINK = 'www.youtube.com/watch?v='
with open(PATH_TO_MAIN + 'Pickles/encoder_trainX_81.pkl','rb') as f:
    x_enc_train = pickle.load(f)
with open(PATH_TO_MAIN + 'Pickles/encoder_trainY_81.pkl','rb') as f:
    y_train_encoder= pickle.load(f)
x_enc_train.shape
from sklearn.model_selection import train_test_split

X_train,X_val,y_train,y_val = train_test_split(x_enc_train,y_train_encoder,test_size = 0.1)
X_train.shape
# from keras import backend as K
# def root_mean_squared_error(y_true, y_pred):
#         return K.sqrt(K.mean(K.square(y_pred - y_true))) 
  
# def custom_loss(y_pred, y_true):
#   y_pred= tf.cast(y_pred, tf.float64) 
#   y_true= tf.cast(y_true, tf.float64)
#   y_pred=tf.nn.relu(y_pred) 
#   return tf.sqrt(tf.reduce_mean(tf.squared_difference(tf.log1p(y_pred), tf.log1p(y_true))))
model_encoder = Sequential()
model_encoder.add(Conv2D(64, kernel_size=(4,4),strides=(1,1),  input_shape=[598,257,2],use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(Conv2D(64, kernel_size=(4,4),strides=(1,1), use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(Conv2D(128, kernel_size=(4,4),strides=(1,1), use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(MaxPool2D(pool_size=(2,1)))

model_encoder.add(Conv2D(128, kernel_size=(4,4),strides=(1,1), use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(MaxPool2D(pool_size=(2,1)))

model_encoder.add(Conv2D(256, kernel_size=(4,4),strides=(1,1), use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(MaxPool2D(pool_size=(2,1)))

model_encoder.add(Conv2D(512, kernel_size=(4,4),strides=(1,1), use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())
 
model_encoder.add(Conv2D(512, kernel_size=(4,4),strides=(2,2), use_bias=False))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(Conv2D(512, kernel_size=(4,4),strides=(2,2), use_bias=False))

model_encoder.add(AvgPool2D(pool_size=(15,1),strides = (1,1)))
model_encoder.add(LeakyReLU(alpha=0.1))
model_encoder.add(BatchNormalization())

model_encoder.add(Flatten())
model_encoder.add(Dense(4096,activation='relu'))
model_encoder.add(Dropout(0.2))

model_encoder.add(Dense(2048,activation='relu'))

model_encoder.summary()

model_encoder.compile(loss='mse',optimizer=Adam(lr=1e-3),metrics=['accuracy'])
checkpoint_path = "./cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)
 
callbacks = [
    tf.keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=5),
    tf.keras.callbacks.EarlyStopping(patience=7, monitor='val_loss'),
    tf.keras.callbacks.TensorBoard(log_dir='./logs'),
    cp_callback
]
 
 
# model_encoder.fit(X_train, y_train, batch_size=2,validation_data=(X_val, y_val), epochs=10 ,verbose=1)
model_encoder.fit(x_enc_train,y_train_encoder, batch_size=2,validation_data=(X_val, y_val), epochs=10 ,verbose=1)

y_pred_encoder = model_encoder.predict(x_enc_train)
with open(PATH_TO_MAIN + 'Pickles/y_pred_encoder_81.pkl','wb') as f:
    pickle.dump(y_pred_encoder,f)
model_encoder.save(PATH_TO_MAIN + 'Models/Encoder1_81.h5')<jupyter_output><empty_output>