<jupyter_start><jupyter_text>Libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt 
import os
import cv2
import tensorflow as tf
import tensorflow_datasets as tfds

from tensorflow import keras
from tensorflow.keras import models, layers
from tensorflow.keras.layers import MaxPooling2D, Dense, Flatten, Conv2D, BatchNormalization
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.vgg19 import decode_predictions
from tensorflow.keras.models import Sequential
from tensorflow.keras import backend as K
print(tf.__version__)<jupyter_output>2.7.0<jupyter_text>Mount with Google drive<jupyter_code>from google.colab import drive
drive.mount("/content/drive")<jupyter_output>Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).<jupyter_text>Download dataset<jupyter_code>train_path = ('/content/drive/MyDrive/dataset_new/eyes_train')
test_path = ('/content/drive/MyDrive/dataset_new/eyes_test')

directories = ['/Closed', '/Open']<jupyter_output><empty_output><jupyter_text>Data Generator<jupyter_code>def plot_imgs(directory, top=10):
    all_item_dirs = os.listdir(directory)
    item_files = [os.path.join(directory, file) for file in all_item_dirs][:5]
  
    plt.figure(figsize=(20, 20))
  
    for i, img_path in enumerate(item_files):
        plt.subplot(10, 10, i+1)
        img = plt.imread(img_path)
        plt.tight_layout()         
        plt.imshow(img, cmap='gray') 
for j in directories:
    plot_imgs(train_path+j)
train_datagen = ImageDataGenerator(rescale=1.0/255.0,  
                                   rotation_range = 40, 
                                   width_shift_range = 0.2, 
                                   height_shift_range = 0.2,
                                   shear_range = 0.2, 
                                   zoom_range = 0.2, 
                                   horizontal_flip = True,
                                   validation_split= 0.2)
test_datagen = ImageDataGenerator(rescale=1.0/255.0)


eyes_train = train_datagen.flow_from_directory(
    directory = train_path, 
    target_size=(224, 224),
    classes =['Closed', 'Open'],
    batch_size= 32,
    shuffle = True,
    subset="training")

eyes_val = train_datagen.flow_from_directory(\
    directory = train_path, 
    target_size=(224, 224),
    classes =['Closed', 'Open'],
    batch_size= 32,
    shuffle = True,
    subset="validation")


eyes_test = test_datagen.flow_from_directory(\
    directory=test_path, 
    target_size=(224,224), 
    classes =['Closed', 'Open'],
    batch_size=32, 
    shuffle= True)<jupyter_output>Found 988 images belonging to 2 classes.
Found 246 images belonging to 2 classes.
Found 218 images belonging to 2 classes.<jupyter_text>CNN from Scratch<jupyter_code><jupyter_output><empty_output><jupyter_text>Transfer Learning Use pre-trained modelst to fit with data and find the best given accuracy and minimum loss  1. VGG19 libraries<jupyter_code>from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet import preprocess_input
from tensorflow.keras.applications import VGG19

from tensorflow.keras.applications.mobilenet import decode_predictions
from pprint import pprint
# Apply Transfer Learning
K.clear_session()

VGG19_base = tf.keras.applications.VGG19(
    include_top = False,
    input_shape = (224,224,3))

VGG19_base.trainable = False 

modelVGG19 = models.Sequential()

modelVGG19.add(VGG19_base)
modelVGG19.add(layers.Flatten())
modelVGG19.add(layers.Dense(32, activation ="relu"))
modelVGG19.add(layers.Dense(2, activation ="sigmoid"))

modelVGG19.summary()

modelVGG19.compile(
    optimizer ="adam",
    loss = "binary_crossentropy",
    metrics = ["accuracy"]
)

history = modelVGG19.fit(
    eyes_train,
    epochs = 20,
    validation_data = eyes_val)
history = modelVGG19.history

plt.suptitle("Tranfer Learning - VGG19", fontsize=10)
plt.plot(history.history["accuracy"], label = "Training Accuracy")
plt.plot(history.history["val_accuracy"], label ="Validation Accuracy")
plt.legend()
plt.show()
plt.close()

plt.plot(history.history["loss"], label ="Training Loss")
plt.plot(history.history["val_loss"], label ="Validation Loss")
plt.legend()
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>2. MobilNetV2<jupyter_code>from tensorflow.keras.applications import MobileNetV2
K.clear_session()

base_mobilv2 = tf.keras.applications.MobileNetV2(
    include_top = False,
    input_shape = (224,224,3))

base_mobilv2.trainable = False 

mobilv2_model = models.Sequential()

mobilv2_model.add(base_mobilv2)
mobilv2_model.add(layers.Flatten())
mobilv2_model.add(layers.Dense(32, activation ="relu"))
mobilv2_model.add(layers.Dropout(0.5))
mobilv2_model.add(layers.Dense(1, activation ="sigmoid"))
mobilv2_model.summary()
mobilv2_model.compile(
    optimizer ="adam",
    loss = "binary_crossentropy",
    metrics = ["accuracy"]
)

history = mobilv2_model.fit(
    eyes_train,
    epochs = 20,
    validation_data = eyes_val
)
history = mobilv2_model.history

plt.suptitle("Tranfer Learning - MobilNetV2", fontsize=10)
plt.plot(history.history["accuracy"], label = "Training Accuracy")
plt.plot(history.history["val_accuracy"], label ="Validation Accuracy")
plt.legend()
plt.show()
plt.close()

plt.plot(history.history["loss"], label ="Training Loss")
plt.plot(history.history["val_loss"], label ="Validation Loss")
plt.legend()
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>3. NasNet Mobile<jupyter_code>from tensorflow.keras.applications import NASNetMobile
K.clear_session()

nasnet = tf.keras.applications.NASNetMobile(
   
    include_top = True,
    input_shape = None, 
    weights="imagenet")

nasnet.trainable = False

modelnasnet = models.Sequential()
modelnasnet.add(nasnet)
modelnasnet.add(layers.Flatten())
modelnasnet.add(layers.Dense(64, activation ="relu"))
modelnasnet.add(layers.Dense(1, activation ="sigmoid"))
modelnasnet.summary()



modelnasnet.compile(
    optimizer ="adam",
    loss = "binary_crossentropy",
    metrics = ["accuracy"]
)

history = modelnasnet.fit(
    eyes_train,
    epochs = 20,
    validation_data = eyes_val)

#history_dictionary["nasnet"] = history.history
history = modelnasnet.history

plt.suptitle("Tranfer Learning - NASNet", fontsize=10)
plt.plot(history.history["accuracy"], label = "Training Accuracy")
plt.plot(history.history["val_accuracy"], label ="Validation Accuracy")
plt.legend()
plt.show()
plt.close()

plt.plot(history.history["loss"], label ="Training Loss")
plt.plot(history.history["val_loss"], label ="Validation Loss")
plt.legend()
plt.show()
plt.close()<jupyter_output><empty_output><jupyter_text>4. InceptionV3<jupyter_code>from tensorflow.keras.applications import InceptionV3
K.clear_session()

incepV3 = tf.keras.applications.InceptionV3(
   
    include_top = True,
    input_shape = None, 
    weights="imagenet")

incepV3.trainable = False

modelincepv3 = models.Sequential()


#mobilv2_model.add(base_mobilv2)
#mobilv2_model.add(layers.Flatten())
#mobilv2_model.add(layers.Dense(32, activation ="relu"))


modelincepv3.add(incepV3)
modelincepv3.add(layers.Flatten())
modelincepv3.add(layers.Dense(128, activation ="relu"))
modelincepv3.add(layers.Dropout(0.2))
modelincepv3.add(layers.Dense(1, activation ="sigmoid"))
modelincepv3.summary()
modelincepv3.compile(
    optimizer ="RMSprop",
    loss = "binary_crossentropy",
    metrics = ["accuracy"]
)

history = modelincepv3.fit(
    eyes_train,
    epochs = 20,
    validation_data = eyes_val
)
history = modelincepv3.history

plt.suptitle("Tranfer Learning - NASNet", fontsize=10)
plt.plot(history.history["accuracy"], label = "Training Accuracy")
plt.plot(history.history["val_accuracy"], label ="Validation Accuracy")
plt.legend()
plt.show()
plt.close()

plt.plot(history.history["loss"], label ="Training Loss")
plt.plot(history.history["val_loss"], label ="Validation Loss")
plt.legend()
plt.show()
plt.close()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12261009004563957, 'fraction_numerical': 0.024916738620944862, 'mean_word_length': 4.7462792345854, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '356101', 'license': 'MIT', 'n_tokens_mistral': 2970, 'n_tokens_neox': 2771, 'n_words': 514, 'path': 'Are_you_sleeping.ipynb', 'repo_name': 'KulissaraSingha/Eyes_Status_Classfication', 'size': '8107'}