-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDog_And_Cat_Image_Classifier.py
More file actions
156 lines (129 loc) · 5.09 KB
/
Copy pathDog_And_Cat_Image_Classifier.py
File metadata and controls
156 lines (129 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# # Libraries to use
# Data extraction
import io
import os
import requests
import numpy as np
# Display function
from matplotlib import pyplot as plt
# For machine learning
# Using pip for instaling => !pip install tensorflow==2.8
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras import layers
import numpy
# For image
from PIL import Image
import cv2
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import models
import matplotlib.pyplot as plt
# # Variables and data
# Link containing training data
LINK_TRAIN = "link_1"
# Link containing validation data
LINK_VAL = "link_2"
# Type of data to take into account
CLASS_MODE = "binary"
# Type of color to take into account (grayscale)
COLOR_MODE1 = "grayscale"
# Type of color to take into account (rgb)
COLOR_MODE2 = "rgb"
# Size in pixels
TARGET_SIZE = (150, 150)
# Batch Size
BATCH_SIZE = 3
# Object creator (training)
train_datagen = ImageDataGenerator(rescale=1.0/255,zoom_range=0.1,rotation_range=25,width_shift_range=0.05,height_shift_range=0.05)
# Object creator (validation)
validation_datagen = ImageDataGenerator(rescale=1.0/255,zoom_range=0.1,rotation_range=25,width_shift_range=0.05,height_shift_range=0.05)
# Validation data generator
train_generator = train_datagen.flow_from_directory(
LINK_TRAIN,
target_size=TARGET_SIZE,
batch_size=BATCH_SIZE,
class_mode=CLASS_MODE,
color_mode=COLOR_MODE2
)
# Validation data generator
validation_generator = validation_datagen.flow_from_directory(
LINK_VAL,
target_size=TARGET_SIZE,
batch_size=BATCH_SIZE,
class_mode=CLASS_MODE,
color_mode=COLOR_MODE2
)
# # Function to build the model
def design_model(training_data):
print("\nBuilding model...")
# Define the CNN model
model = Sequential(name='CyD_AI')
# Convolutional network input neurons
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
# Experimental pooling
# model.add(layers.MaxPooling2D((2, 2)))
# The maxpooling layers and dropout layers as well
model.add(layers.Conv2D(64, (3, 3), strides=3, activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
model.add(layers.Dropout(0.1))
model.add(layers.Conv2D(128, (3, 3), strides=1, activation="relu"))
model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
model.add(layers.Dropout(0.2))
# Experimenting with extra layer's
model.add(tf.keras.layers.Conv2D(3, 3, strides=1, activation="relu"))
model.add(tf.keras.layers.Conv2D(1, 1, strides=1, activation="relu"))
model.add(tf.keras.layers.Dropout(0.1))
model.add(layers.Flatten())
# Hidden layer (activation fun = reLU)
model.add(layers.Dense(32, activation = "relu"))
# Output layer with softmax activation function
model.add(layers.Dense(1,activation="sigmoid")) # sigmoid
# Compile model with Adam optimizer
# Loss function is categorical crossentropy
# Compiling the CNN model
print("\nCompiling model...")
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss='binary_crossentropy', metrics=['accuracy'])
# model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001), loss=tf.keras.losses.CategoricalCrossentropy(), metrics=[tf.keras.metrics.CategoricalAccuracy(),tf.keras.metrics.AUC()],)
# Summarize model
model.summary()
print('Model done.')
return model
# Use model function
model = design_model(train_generator)
# # Training the model
history = model.fit(
train_generator,
steps_per_epoch=200, # Number of steps per epoch
epochs=20, # Number of training epochs
validation_data=validation_generator, # Our validation for training data
validation_steps=100 # Number of validation steps
)
# # Plot using the Matplotlib library
# Graph training metrics
acc = history.history['accuracy'] # Accuracy
val_acc = history.history['val_accuracy'] # Accuracy validation
loss = history.history['loss'] # Loss from accuracy
val_loss = history.history['val_loss'] # Loss from accuracy validation
epochs = range(1, len(acc) + 1) # Epochs for data
plt.figure(figsize=(12, 4)) # Graph size
plt.subplot(1, 2, 1) # The accuracy graph
plt.plot(epochs, acc, color='red', label='Accuracy') # Taking the data
plt.plot(epochs, val_acc, label='Validation accuracy') # Taking the data for validation
plt.title('Accuracy of training and validation') # Title
plt.xlabel('Epochs') # X-label Epochs
plt.ylabel('Accuracy') # Y-label Accuracy
plt.legend() # Legend for data
plt.subplot(1, 2, 2) # The loss graph
plt.plot(epochs, loss, color='red', label='Loss graph') # Taking the data
plt.plot(epochs, val_loss, label='Loss of validation') # Taking the data for validation
plt.title('Loss of training and validation') # Title
plt.xlabel('Epochs') # X-label Epochs
plt.ylabel('Accuracy') # Y-label Accuracy
plt.legend() # Legend for data
plt.show() # Show graph