<jupyter_start><jupyter_text><jupyter_code>#!pip install tensorflow-gpu==2.2.0
import tensorflow as tf
import os
import zipfile
print("Version of tensorflow is : ",tf.__version__)
#from google.colab import drive
#drive.mount("/content/drive/")
os.chdir("/content/drive/My Drive/CNN/monkey detection/")
print("Current path is : ",os.getcwd())
fl = zipfile.ZipFile('./data.zip',"r")
fl.extractall("./data")
train_dir = "./data/training/training/"
test_dir  = "./data/validation/validation/"
print("No of files in '%s' folder is : %d"%(train_dir,len(os.listdir(train_dir))))
print("No of files in '%s' folder is : %d"%(test_dir,len(os.listdir(test_dir))))
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=(1.0 / 255.0))
test_datagen  = ImageDataGenerator(rescale= (1.0 / 255.0))
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
train_data = train_datagen.flow_from_directory(train_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
test_data = train_datagen.flow_from_directory(test_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
print("Total no of image for training : ",train_data.n)
print("Total no of image for testing or validation : ",test_data.n)
print("Classes := ",train_data.class_indices)
class_name = list(train_data.class_indices.keys())
class_name
#plt.imshow(train_data[0][0][10])
#plt.title(train_data[0][1][10])
train_data[0][1][10].shape
model1 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (256,256,3)),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model1.compile(optimizer="adam",loss=tf.keras.losses.categorical_crossentropy,metrics=['acc'])
history1 = model1.fit(train_data,
                      epochs=20,
                      steps_per_epoch=train_data.n//train_data.batch_size ,
                      validation_data=test_data,
                      validation_steps=test_data.n//test_data.batch_size
)
def plotHistory(history):
  fig,ax = plt.subplots(1,2,figsize=(16,6))
  ax[0].plot(history['acc'],label="accurecy")
  ax[0].plot(history['val_acc'],label="validation accurecy")
  ax[0].set_title("Accurecy")
  ax[0].grid(True)
  ax[1].plot(history['loss'],label="Loss")
  ax[1].plot(history['val_loss'],label="validation Loss")
  ax[1].set_title("Loss")
  ax[1].grid()
plotHistory(history1.history)
train_datagen = ImageDataGenerator(rescale=(1.0 / 255.0),
                                   rotation_range=25,
                                   width_shift_range=0.25,
                                   height_shift_range=0.25,
                                   zoom_range=0.4,
                                   shear_range=0.2,
                                   horizontal_flip=True,
                                   fill_mode="nearest")
test_datagen  = ImageDataGenerator(rescale= (1.0 / 255.0))
train_data = train_datagen.flow_from_directory(train_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
test_data = train_datagen.flow_from_directory(test_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
_,ax = plt.subplots(1,5,figsize=(20,4))
for i in range(5):
  ax[i].imshow(train_data[0][0][10])
model2 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (256,256,3)),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model2.compile(optimizer="adam",loss=tf.keras.losses.categorical_crossentropy,metrics=['acc'])
history1 = model2.fit(train_data,
                      epochs=15,
                      steps_per_epoch=train_data.n//train_data.batch_size ,
                      validation_data=test_data,
                      validation_steps=test_data.n//test_data.batch_size
)
train_datagen = ImageDataGenerator(rescale=(1.0 / 255.0),
                                   width_shift_range=0.05,
                                   height_shift_range=0.05,
                                   zoom_range=0.25,
                                   shear_range=0.1,
                                   horizontal_flip=True,
                                   fill_mode="nearest")
test_datagen  = ImageDataGenerator(rescale= (1.0 / 255.0))

train_data = train_datagen.flow_from_directory(train_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
test_data = train_datagen.flow_from_directory(test_dir,target_size=(256,256),class_mode='categorical',batch_size=32)

model3 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (256,256,3)),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model3.compile(optimizer="adam",loss=tf.keras.losses.categorical_crossentropy,metrics=['acc'])
history3 = model3.fit(train_data,
                      epochs=25,
                      steps_per_epoch=train_data.n//train_data.batch_size ,
                      validation_data=test_data,
                      validation_steps=test_data.n//test_data.batch_size
)
train_datagen = ImageDataGenerator(rescale=(1.0 / 255.0),
                                   zoom_range=0.20,
                                   fill_mode="nearest")
test_datagen  = ImageDataGenerator(rescale= (1.0 / 255.0))

train_data = train_datagen.flow_from_directory(train_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
test_data = train_datagen.flow_from_directory(test_dir,target_size=(256,256),class_mode='categorical',batch_size=32)

model3 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (256,256,3)),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model3.compile(optimizer="adam",loss=tf.keras.losses.categorical_crossentropy,metrics=['acc'])
history3 = model3.fit(train_data,
                      epochs=25,
                      steps_per_epoch=train_data.n//train_data.batch_size ,
                      validation_data=test_data,
                      validation_steps=test_data.n//test_data.batch_size
)
train_datagen = ImageDataGenerator(rescale=(1.0 / 255.0),
                                   zoom_range=0.30,
                                   horizontal_flip=True,
                                   fill_mode="nearest")
test_datagen  = ImageDataGenerator(rescale= (1.0 / 255.0))

train_data = train_datagen.flow_from_directory(train_dir,target_size=(256,256),class_mode='categorical',batch_size=32)
test_data = train_datagen.flow_from_directory(test_dir,target_size=(256,256),class_mode='categorical',batch_size=32)

model4 = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape = (256,256,3)),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
    tf.keras.layers.MaxPool2D(2,2),

    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128,activation="relu"),
    tf.keras.layers.Dense(256,activation="relu"),
    tf.keras.layers.Dense(10,activation="softmax")
])
model4.compile(optimizer="adam",loss=tf.keras.losses.categorical_crossentropy,metrics=['acc'])
history4 = model4.fit(train_data,
                      epochs=25,
                      steps_per_epoch=train_data.n//train_data.batch_size ,
                      validation_data=test_data,
                      validation_steps=test_data.n//test_data.batch_size
)<jupyter_output>Found 1098 images belonging to 10 classes.
Found 272 images belonging to 10 classes.
Epoch 1/25
34/34 [==============================] - 48s 1s/step - loss: 2.2929 - acc: 0.1360 - val_loss: 2.0526 - val_acc: 0.3047
Epoch 2/25
34/34 [==============================] - 48s 1s/step - loss: 1.7917 - acc: 0.3433 - val_loss: 1.6972 - val_acc: 0.3516
Epoch 3/25
34/34 [==============================] - 49s 1s/step - loss: 1.4383 - acc: 0.4934 - val_loss: 1.3677 - val_acc: 0.5352
Epoch 4/25
34/34 [==============================] - 49s 1s/step - loss: 1.2536 - acc: 0.5403 - val_loss: 1.3551 - val_acc: 0.5117
Epoch 5/25
34/34 [==============================] - 48s 1s/step - loss: 1.1354 - acc: 0.6182 - val_loss: 1.2340 - val_acc: 0.5977
Epoch 6/25
34/34 [==============================] - 48s 1s/step - loss: 1.0209 - acc: 0.6407 - val_loss: 1.1755 - val_acc: 0.5898
Epoch 7/25
34/34 [==============================] - 49s 1s/step - loss: 0.8974 - acc: 0.6932 - val_loss: 1.3572 - val_acc: 0.5938
Epoch[...]
{'fraction_non_alphanumeric': 0.17120503962609226, 'fraction_numerical': 0.06655151391993497, 'mean_word_length': 3.37855871886121, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '400749', 'license': 'MIT', 'n_tokens_mistral': 3886, 'n_tokens_neox': 3480, 'n_words': 388, 'path': 'MonkeyDetection.ipynb', 'repo_name': 'ANKITPODDER2000/CNN_TF', 'size': '9842'}