-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deep Learning Image Classification Model.py
283 lines (181 loc) · 6.16 KB
/
Deep Learning Image Classification Model.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# coding: utf-8
# # Code Overview
# The following code loads image data and prepares it for a machine learning model that will be used to classify images as cats or dogs.
#
# The code does the following:
#
# Import necessary libraries for the code.
#
# Define the size of the images and the batch size for training and testing.
#
# Load the training and testing directories.
#
# Create a data frame containing the file names and the corresponding category (cat or dog) for each image.
#
# Split the data into training and validation sets.
#
# Use the ImageDataGenerator to rescale the image data and create training, validation, and testing generators.
#
# Plot sample images from the training set.
#
# Define the model architecture.
# # Importing libraries
# The first step of the code is to import the required libraries.
# In[ ]:
import os
import cv2
import numpy as np
import pandas as pd
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Rescaling, Conv2D, MaxPool2D, Flatten, Dense
from PIL import Image
import os
import matplotlib.pyplot as plt
plt.style.use("ggplot")
import seaborn as sns
from glob import glob
# In[ ]:
# In[2]:
# Define the image size and batch size
img_size = (150, 150)
batch_size = 32
# In[3]:
# Define the paths to the train and test directories
train_dir = 'D:/MLPROJECT/dogs-vs-cats/train/train'
# # Create a data frame
# The code creates a data frame containing the file names and the corresponding category (cat or dog) for each image.
# In[5]:
file_names = glob('D:/MLPROJECT/dogs-vs-cats/train/train/*.jpg')
categories = [1 if 'dog' in pic else 0 for pic in os.listdir("D:/MLPROJECT/dogs-vs-cats/train/train")]
df = pd.DataFrame({'filename': file_names, 'category':categories})
df["category"] = df["category"].replace({0: 'cat', 1: 'dog'})
print("shape:", df.shape)
df.head()
# # Split the data
# The code then splits the data into training and validation sets.
# In[6]:
from sklearn.model_selection import train_test_split
train_df, validate_df = train_test_split(df, test_size=0.2, random_state=10)
train_df = train_df.reset_index(drop=True)
validate_df = validate_df.reset_index(drop=True)
# print shape
train_df.shape, validate_df.shape
# In[7]:
# plot label counts
fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,5))
sns.countplot(train_df.category, ax=ax[0])
ax[0].set_title('Training dataset', fontsize=14)
sns.countplot(validate_df.category, ax=ax[1])
ax[1].set_title('Valiadtion dataset', fontsize=14);
# In[8]:
file_names = os.listdir("D:/MLPROJECT/dogs-vs-cats/test1/test1")
test_df = pd.DataFrame({'filename': file_names})
print("shape:", test_df.shape)
test_df.head()
# In[9]:
from tensorflow.keras.preprocessing.image import ImageDataGenerator
IMAGE_SIZE = (200,200)
BATCH_SIZE = 32
datagen = ImageDataGenerator(rescale = 1/255)
# In[10]:
train_generator = datagen.flow_from_dataframe(
dataframe=train_df,
# directory='./Images/train',
x_col='filename',
y_col='category',
class_mode='binary',
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE
)
# In[11]:
valid_generator = datagen.flow_from_dataframe(
dataframe=validate_df,
# directory='./Images/train',
x_col='filename',
y_col='category',
class_mode='binary',
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE
)
# In[12]:
test_generator = datagen.flow_from_dataframe(
dataframe=test_df,
directory="D:/MLPROJECT/dogs-vs-cats/test1/test1",
x_col='filename',
y_col=None,
class_mode=None,
target_size=IMAGE_SIZE,
batch_size=BATCH_SIZE
)
# In[13]:
# get a batch of 32 training images
images = train_generator.next()[:9]
# plot 9 original training images
plt.figure(figsize=(5, 5))
for i in range(9):
plt.subplot(3, 3, i+1)
plt.imshow(images[0][i])
plt.axis('off')
plt.tight_layout()
plt.show()
# In[14]:
# Design the model architecture
model = Sequential([
Input(shape=(200,200,3)),
Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'),
MaxPool2D(pool_size=2),
Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'),
MaxPool2D(pool_size=2),
Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'),
MaxPool2D(pool_size=2),
Conv2D(filters=265, kernel_size=3, padding='same', activation='relu'),
Flatten(),
Dense(1, activation='sigmoid') # Cat or dog
])
model.summary()
# In[15]:
# Compile model
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])
# In[16]:
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau
# callbacks is a reguralization technique to brevent over fitting
callbacks = [
# to stop training when you measure that the validation loss is no longer improving
EarlyStopping(patience=4, monitor='val_loss'),
# reduce learning_rate if the model is not imporving
ReduceLROnPlateau(monitor='val_acc', patience=2, verbose=1, factor=0.5, min_lr=0.00001),
# save best model
ModelCheckpoint(filepath='D:/MLPROJECT/dogs-vs-cats/Models/model.keras', save_best_only=True, monitor='val_loss')
]
# train the model
history = model.fit(train_generator, validation_data=valid_generator, epochs=10, callbacks=[callbacks])
# In[17]:
# plot model performance
pd.DataFrame(history.history).plot();
# In[19]:
from keras.preprocessing import image
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras.utils import load_img
import tensorflow as tf
#Input image
test_image = tf.keras.utils.load_img ('D:/MLPROJECT/dogs-vs-cats/test1/test1/1.jpg',target_size=(200,200))
#For show image
plt.imshow(test_image)
test_image = tf.keras.utils.img_to_array(test_image)
test_image = np.expand_dims(test_image,axis=0)
# Result array
result = model.predict(test_image)
#Mapping result array with the main name list
i=0
if(result>=0.5):
print("Dog")
else:
print("Cat")
# In[ ]:
import tensorflow as tf
from pyspark.sql import SparkSession
spark = (SparkSession.builder.getOrCreate())
model =tf.keras.models.loadmodel('D:\MLPROJECT\dogs-vs-cats\Models', compile=False)