# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="NNylsgsF7sh4" colab_type="text"
# **Importing Essential Libraries**
#

# + id="LGLgP-4B2xk1" colab_type="code" colab={}
# %matplotlib inline

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import os
import zipfile
import cv2
import tensorflow as tf
import pickle

from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D ,AveragePooling2D, Flatten, Dropout
from keras.layers.core import Dense
from keras.optimizers import RMSprop,Adam,SGD
from keras.layers.normalization import BatchNormalization
from keras.layers.core import Activation
from keras.preprocessing.image import ImageDataGenerator
from keras.models import model_from_json
from keras import regularizers

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelBinarizer
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

sns.set()

# + [markdown] id="L7t6MW6266IT" colab_type="text"
# **Unzipping the Train and Test sets**

# + id="jLy_moUs5qdO" colab_type="code" outputId="16586414-0f34-4010-ce37-a098532f0735" colab={"base_uri": "https://localhost:8080/", "height": 34}
os.getcwd()

# + id="MXLkoPXU5yD6" colab_type="code" colab={}
handle_train=zipfile.ZipFile(r'/content/Train.zip')
handle_train.extractall('/content/train')
handle_train.close()
handle_test=zipfile.ZipFile(r'/content/Test.zip')
handle_test.extractall('/content/test')
handle_test.close()

# + id="qxjrbYfU54HA" colab_type="code" colab={}
train_images=os.listdir('/content/train/Train/')
test_images = os.listdir('/content/test/Test')

filepath_train = '/content/train/Train/'
filepath_test = '/content/test/Test/' 

# + id="U_8LmO0U69qf" colab_type="code" outputId="c2841ce4-20f6-46ec-acf6-902f8f4cae52" colab={"base_uri": "https://localhost:8080/", "height": 343}
df_train = pd.read_csv('/content/train.csv')
df_train.head(10)

# + id="7ACwwtVNPvtH" colab_type="code" outputId="51eff2d5-75c0-4fae-d76b-12c02a14a46e" colab={"base_uri": "https://localhost:8080/", "height": 195}
sample_submn = pd.read_csv('/content/sample_submission_sDO3m7O.csv')
sample_submn.head()

# + [markdown] id="Jvhi0rCX787K" colab_type="text"
# **Reading & Resizing Training and Testing images**

# + id="hvXb3AUj6z8v" colab_type="code" colab={}

images=[]
labels=[]

for index, row in df_train.iterrows():
  image=cv2.imread(filepath_train+row['ID'])
  image=cv2.resize(image , (72,72))
  images.append(image)
  labels.append(row['Class'])
  #print(row['ID'])


# + id="w7qzyfofasc8" colab_type="code" colab={}
images_test=[]
outputs=[]

for index,row in sample_submn.iterrows():
  image=cv2.imread(filepath_test+row['ID'])
  image=cv2.resize(image , (72,72))
  images_test.append(image) 
  outputs.append(image)

# + [markdown] id="EM0u7h-m8WcA" colab_type="text"
# **Displaying couple of images for Sanity check**

# + id="3nNeWGPA9qYy" colab_type="code" outputId="48a8741d-fe83-4e49-c493-12e7f3267b2a" colab={"base_uri": "https://localhost:8080/", "height": 287}
plt.imshow(images[1])

# + [markdown] id="vXE3Fa6h8hD4" colab_type="text"
# Converting into an n-d array and normalizing the image pixels

# + id="zf-XfMvD-4A6" colab_type="code" colab={}
images = np.array(images, dtype="float") / 255.0
images_test = np.array(images_test, dtype="float") / 255.0
labels = np.array(labels)

# + [markdown] id="9nWiIPpi82Bd" colab_type="text"
# **Splitting into train and test set for training**

# + id="YuOAXfJT_KRY" colab_type="code" colab={}
(trainX, testX, trainY, testY) = train_test_split(images,labels, test_size=0.10, random_state=42)

# + id="4GbOd2DW_P7L" colab_type="code" outputId="83d99a16-b77f-4aac-ed75-6f155ab4ac8f" colab={"base_uri": "https://localhost:8080/", "height": 151}
print(type(trainX))
print(trainX.shape)
print(type(trainY))
print(trainY.shape)
print(type(testX))
print(testX.shape)
print(type(testY))
print(testY.shape)

# + [markdown] id="1wDvTmjW8-zK" colab_type="text"
# Binarizing the output categories 

# + id="5dOPPpO6_SC8" colab_type="code" colab={}
lb = LabelBinarizer()
trainY = lb.fit_transform(trainY)
testY = lb.transform(testY)

# + id="By4suEdA_WsG" colab_type="code" outputId="9fde9bd7-1a81-4823-d131-276fa0cf3189" colab={"base_uri": "https://localhost:8080/", "height": 34}
lb.classes_

# + id="1x8RUY1Odw9i" colab_type="code" outputId="1c00276d-9c86-4a02-ffdd-09f1afa5c9c5" colab={"base_uri": "https://localhost:8080/", "height": 134}
trainY


# + id="RKpTpjVBFubs" colab_type="code" colab={}
class myCB(tf.keras.callbacks.Callback):
  def on_epoch_end(self,epoch,logs={}):
    if(logs.get('val_accuracy')>0.91):
      print('\nReached least val_loss')
      self.model.stop_training = True



# + id="0ts_VOAVHPOY" colab_type="code" colab={}
cb = myCB()

# + [markdown] id="e2H6sjsT9HtI" colab_type="text"
# **Model Implementation**

# + id="PYzJ8mGa_WuT" colab_type="code" colab={}
model = Sequential()

model.add(Conv2D(filters = 32, kernel_size = (3,3),padding = "same", activation ='relu', input_shape = (72,72,3)))
model.add(BatchNormalization(axis=-1))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters=64,kernel_size=(3,3), padding="same",activation="relu"))
model.add(BatchNormalization(axis=-1))
model.add(Conv2D(filters=64, kernel_size=(3,3), padding="same",activation="relu"))
model.add(BatchNormalization(axis=-1))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(filters=128, kernel_size=(3, 3), padding="same",activation="relu"))
model.add(BatchNormalization(axis=-1))
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding="same",activation="relu"))
model.add(BatchNormalization(axis=-1))
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding="same",activation="relu"))
model.add(BatchNormalization(axis=-1))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))


model.add(Flatten())
model.add(Dense(1024, activation="relu",kernel_regularizer=regularizers.l2(0.001)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Dense(84, activation="relu",kernel_regularizer=regularizers.l2(0.001)))
model.add(BatchNormalization())
model.add(Dropout(0.5))

# softmax classifier
model.add(Dense(3,activation="softmax"))

# + id="IHi3Jqs1d2eP" colab_type="code" outputId="b386745e-2377-47aa-d31c-9bbb44796740" colab={"base_uri": "https://localhost:8080/", "height": 1000}
model.summary()

# + id="q_jgn9Lq_Ww5" colab_type="code" colab={}
INIT_LR = 0.025 #0.05
EPOCHS = 300
BS = 64

opt=SGD(lr=INIT_LR) #Adam,Adagrad,RMSprop

model.compile(loss="categorical_crossentropy", optimizer=opt,	metrics=["accuracy"])

# + [markdown] id="s21wykgZ9Va0" colab_type="text"
# **Image Data Augmentation**

# + id="k1rOMYpdhKom" colab_type="code" colab={}
aug = ImageDataGenerator(rotation_range=20, width_shift_range=0.1, height_shift_range=0.1, 
                         shear_range=0.2, zoom_range=0.2,horizontal_flip=True, fill_mode="nearest")

# + [markdown] id="bx26wKMw9Uxx" colab_type="text"
# **Fitting the model onto the training set**

# + id="6QQqkrgp_hkf" colab_type="code" outputId="aca369fa-99b3-4ed7-8867-66d8b7f2b66c" colab={"base_uri": "https://localhost:8080/", "height": 1000}
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS),validation_data=(testX, testY), 
                        steps_per_epoch=len(trainX) // BS,epochs=EPOCHS,callbacks=[cb])

# + id="mnPPzEwb_kQ4" colab_type="code" outputId="a41f4300-3996-4e3f-da34-bdaf30c00f95" colab={"base_uri": "https://localhost:8080/", "height": 185}
predictions = model.predict(testX, batch_size=BS)
print(classification_report(testY.argmax(axis=1),predictions.argmax(axis=1), target_names=lb.classes_))

# + [markdown] id="0Zg5pYHH9iH6" colab_type="text"
# **Loss-Accuracy Tradeoff graph**

# + id="i6L4Hg39Artq" colab_type="code" outputId="aa96f7d3-c4e7-4dd5-b90c-201f41a9f303" colab={"base_uri": "https://localhost:8080/", "height": 628}
# plot the training loss and accuracy
N = np.arange(0, EPOCHS) #setting up x axis
plt.style.use("ggplot")
plt.figure(figsize=(15,10))
plt.plot(N, H.history["loss"],'y', label="train_loss")
plt.plot(N, H.history["val_loss"],'g', label="val_loss")
plt.plot(N, H.history["accuracy"],'r', label="train_acc")
plt.plot(N, H.history["val_accuracy"],'b', label="val_acc")
plt.title("Training Loss and Accuracy (CNN for age classification)")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend()
plt.show()

# + [markdown] id="B84MWXcC9sgA" colab_type="text"
# **Predicting for test set values**

# + id="r4HB9KsyVpCS" colab_type="code" outputId="d119f71c-1193-44b1-8ae4-fc493632ddd3" colab={"base_uri": "https://localhost:8080/", "height": 134}
pred = model.predict(images_test)
pred

# + id="A5gN8LFy0TP4" colab_type="code" outputId="d8aa99bc-6c76-4c79-ea28-303dd5c40cff" colab={"base_uri": "https://localhost:8080/", "height": 50}
indexes = np.random.randint(0,6636,16)
indexes

# + id="vNjvJXxtWqhN" colab_type="code" colab={}
i_vals = []

for i in indexes:
  val = pred.argmax(axis=1)[i]
  i_vals.append(val)

# + id="B26kCmgN0tgg" colab_type="code" outputId="477842f1-a87f-4a54-e448-ff3c5ee8259a" colab={"base_uri": "https://localhost:8080/", "height": 34}
i_vals

# + id="lVOni6ruXDs3" colab_type="code" colab={}
vals = []

for i in indexes:
  val = np.amax(pred , axis=1)[i]
  vals.append(val)

# + id="5fI0Abr41DEt" colab_type="code" outputId="422eabe9-8d76-4f55-ff97-16080752cf71" colab={"base_uri": "https://localhost:8080/", "height": 286}
vals

# + id="lZA-gr_B1Eqb" colab_type="code" outputId="e07b9a8c-c1c5-4387-aa87-2062bac8d34b" colab={"base_uri": "https://localhost:8080/", "height": 286}
vals = [i*100 for i in vals]
vals

# + id="2GyIfPZb1MQx" colab_type="code" outputId="60f3b65a-40db-476c-d838-1d1742d2f44f" colab={"base_uri": "https://localhost:8080/", "height": 286}
vals = [round(num,2) for num in vals]
vals

# + id="XV1n3x861WM2" colab_type="code" colab={}
labels = []

for i in range(16):
  label = lb.classes_[i_vals[i]]
  labels.append(label)

# + id="nU1qTs241xNV" colab_type="code" outputId="cc86a14f-ee4c-4cdf-eb64-281bd071182a" colab={"base_uri": "https://localhost:8080/", "height": 286}
labels

# + id="EBtNiIkF2GoZ" colab_type="code" colab={}
from imutils import build_montages

# + id="lhk4eSef10O3" colab_type="code" colab={}
results = []

for i in range(16):
  image = outputs[indexes[i]]

  text = labels[i] + " : " + str(vals[i])

  image = cv2.resize(image , (300,300))

  cv2.putText(image,text,(10,50),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)

  results.append(image)



# + id="jnEBlhPyYckI" colab_type="code" colab={}
from google.colab.patches import cv2_imshow

# + id="NSgQRTUB2hmy" colab_type="code" outputId="ed20ebd1-44a6-4d3c-e9e1-1a416097a73f" colab={"base_uri": "https://localhost:8080/", "height": 818}
montage = build_montages(results,(196,196),(4,4))[0]

cv2_imshow(montage)
cv2.waitKey(0)

# + [markdown] id="2sc0ifCs90mT" colab_type="text"
# **Displaying final image again after classification**

# + id="CtfHbSd8Y04l" colab_type="code" outputId="1f226d0f-b62d-419d-e09d-51b2264284f1" colab={"base_uri": "https://localhost:8080/", "height": 334}
text = labels[1]+": "+str(vals[1])

outputs[1] = cv2.resize(outputs[1] , (300,300))
cv2.putText(outputs[1], text , (10,50),  cv2.FONT_HERSHEY_SIMPLEX, 0.7 ,(0, 255, 0), 2)

# show the output image

cv2_imshow(outputs[1])
cv2.waitKey(0)

# + id="_QAs529mY5dS" colab_type="code" outputId="efa50ea1-d84e-48d7-c7a4-feb3b4e60612" colab={"base_uri": "https://localhost:8080/", "height": 287}
plt.imshow(images_test[1])

# + id="PzKA8W0TPzmo" colab_type="code" outputId="88380ceb-69e7-484a-98a1-11d8e71383cf" colab={"base_uri": "https://localhost:8080/", "height": 34}
all_indexes = pred.argmax(axis=1)
all_indexes

# + id="aotEvUglPmlm" colab_type="code" outputId="3537702f-7554-48ad-9766-5be84472d7aa" colab={"base_uri": "https://localhost:8080/", "height": 50}
output_labels = lb.classes_[all_indexes]
output_labels

# + id="LZjytih_YiWL" colab_type="code" outputId="3b9e289c-4ae4-46fd-8a66-e63157ec4326" colab={"base_uri": "https://localhost:8080/", "height": 402}
submission = pd.DataFrame({'Class':output_labels,'ID':sample_submn['ID']})
submission

# + id="lpd4l-aaY29c" colab_type="code" colab={}
submission.to_csv('submission_agenet7.csv',index=False)

# + id="1AbsyP9bZNP8" colab_type="code" colab={}
from google.colab import files
files.download("submission_agenet7.csv")

# + [markdown] id="yLJPvCDY984G" colab_type="text"
# **Exporting .h5 and .json files for Deployment in Flask**

# + id="9A6XkVMpb3pu" colab_type="code" colab={}
#model_json = model.to_json()
#with open("model.json", "w") as json_file:
 #   json_file.write(model_json)

# serialize weights to HDF5
#model.save_weights("model.h5")

# + id="JqzvBglbdsb7" colab_type="code" colab={}
#from google.colab import files
#files.download("model.h5")

# + id="JYgsPVecd8DH" colab_type="code" colab={}
#from google.colab import files
#files.download("model.json")

# + id="KLGI_ff9eDN7" colab_type="code" colab={}


# + id="hy_XWRGQeDRC" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered