-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn.py
179 lines (137 loc) · 4.4 KB
/
cnn.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
# -*- coding: utf-8 -*-
"""CNN.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1fWf71-8bdVIEsaik_UukYboNEyLRsw88
"""
import tqdm
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
import seaborn as sns
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import BatchNormalization
from keras.layers import Dropout
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import MaxPooling2D
from keras.layers import Conv2D
from keras.models import Sequential
import tensorflow as tf
import pickle
import random
import cv2
import os
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from google.colab import files
! pip install - q kaggle
files.upload()
!nvidia-smi
!gdown - -id 1gR5-Tzr-HZicaORJEHg6uVOOcPy0YY49
!unzip rooms_dataset.zip
# Importing the libraries
# Dataset path
dataset_path = "/content/rooms_dataset"
fish_classes = os.listdir(dataset_path)
print(fish_classes)
# Storing images with their labels in a list
data = []
for fish_class in fish_classes:
# Get all the file names
fish_class_path = os.path.join(dataset_path, fish_class)
fish_class_images = os.listdir(fish_class_path)
# Add them to the list
for image in fish_class_images:
data.append([fish_class, os.path.join(fish_class_path, image)])
# Printing the list
print(data)
# Building a dataframe
df = pd.DataFrame(data, columns=['Fish_Class', 'Image_Path'])
print(df.head())
print()
print(df.tail())
# Checking the number of images in each class
print(df['Fish_Class'].value_counts())
# Resizing images to 75 * 75 also using tqdm for progress bar
image = []
labels = []
for i in tqdm.tqdm(range(df.shape[0])):
img = cv2.imread(df['Image_Path'][i])
img = cv2.resize(img, (75, 75), interpolation=cv2.INTER_AREA)
image.append(img)
labels.append(df['Fish_Class'][i])
# Converting the list to numpy array
image = np.array(image)
print(image.shape)
y = labels
print(y[:5])
# for y
y_labelencoder = LabelEncoder()
y = y_labelencoder.fit_transform(y)
print(y)
# Performing one hot encoding on y Output (9000, 9)
y = y.reshape(-1, 1)
onehotencoder = OneHotEncoder()
Y = onehotencoder.fit_transform(y).toarray()
print(Y.shape)
images, Y = shuffle(image, Y, random_state=1)
train_x, test_x, train_y, test_y = train_test_split(
images, Y, test_size=0.2, random_state=1)
print(train_x.shape)
print(test_x.shape)
print(train_y.shape)
print(test_y.shape)
num_classes = 9
# architecture of the model
epochs = 20
batch_size = 32
input_shape = (75, 75, 3)
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu', input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Conv2D(128, kernel_size=(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
model.summary()
# Training the model
history = model.fit(train_x, train_y, batch_size=batch_size,
epochs=epochs, verbose=1, validation_data=(test_x, test_y))
# Evaluating the model
score = model.evaluate(test_x, test_y, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
# Predicting the model
y_pred = model.predict(test_x)
print(y_pred[:5])
# Plotting training and testing loss vs epochs
plt.plot(history.history['loss'], label='train loss')
plt.plot(history.history['val_loss'], label='val loss')
plt.legend()
plt.show()
# classification report
print(classification_report(test_y.argmax(axis=1),
y_pred.argmax(axis=1), target_names=fish_classes))
y_pred = np.argmax(y_pred, axis=1)
y_test = np.argmax(test_y, axis=1)
cm = confusion_matrix(y_test, y_pred)
print(cm)