-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_project_4_cifar_10_object_recognition_using_resnet50.py
233 lines (151 loc) · 4.92 KB
/
dl_project_4_cifar_10_object_recognition_using_resnet50.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
# -*- coding: utf-8 -*-
"""DL Project 4. CIFAR-10 Object Recognition using ResNet50.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1T46DXyngES0gbtxZrJ41mCzYS7ss3KUn
"""
!pip install kaggle
# configuring the path of Kaggle.json file
!mkdir -p ~/.kaggle
!cp kaggle.json ~/.kaggle/
!chmod 600 ~/.kaggle/kaggle.json
# daatset api
!kaggle competitions download -c cifar-10
!ls
# extracting the compessed Dataset
from zipfile import ZipFile
dataset = '/content/cifar-10.zip'
with ZipFile(dataset,'r') as zip:
zip.extractall()
print('The dataset is extracted')
!ls
!pip install py7zr
import py7zr
archive = py7zr.SevenZipFile('/content/train.7z', mode='r')
archive.extractall() #archive.extractall(path='/content/Training Data')
archive.close()
!ls
"""Importing the Dependencies"""
import os
import numpy as np
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from sklearn.model_selection import train_test_split
filenames = os.listdir('/content/train')
type(filenames)
len(filenames)
print(filenames[0:5])
print(filenames[-5:])
"""**Labels Processing**"""
labels_df = pd.read_csv('/content/trainLabels.csv')
labels_df.shape
labels_df.head()
labels_df[labels_df['id'] == 7796]
labels_df.head(10)
labels_df.tail(10)
labels_df['label'].value_counts()
labels_df['label']
labels_dictionary = {'airplane':0, 'automobile':1, 'bird':2, 'cat':3, 'deer':4, 'dog':5, 'frog':6, 'horse':7, 'ship':8, 'truck':9}
labels = [labels_dictionary[i] for i in labels_df['label']]
print(labels[0:5])
print(labels[-5:])
# displaying sample image
import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/train/7796.png')
cv2_imshow(img)
# displaying sample image
import cv2
from google.colab.patches import cv2_imshow
img = cv2.imread('/content/train/45888.png')
cv2_imshow(img)
labels_df[labels_df['id'] == 45888]
labels_df.head()
id_list = list(labels_df['id'])
print(id_list[0:5])
print(id_list[-5:])
"""**Image Processing**"""
# convert images to numpy arrays
train_data_folder = '/content/train/'
data = []
for id in id_list:
image = Image.open(train_data_folder + str(id) + '.png')
image = np.array(image)
data.append(image)
type(data)
len(data)
type(data[0])
data[0].shape
data[0]
# convert image list and label list to numpy arrays
X = np.array(data)
Y = np.array(labels)
type(X)
print(X.shape)
print(Y.shape)
"""**Train Test Split**"""
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=2)
print(X.shape, X_train.shape, X_test.shape)
# scaling the data
X_train_scaled = X_train/255
X_test_scaled = X_test/255
X_train_scaled
X_train[0]
"""**Building the Neural Network**"""
import tensorflow as tf
from tensorflow import keras
num_of_classes = 10
# setting up the layers of Neural Network
model = keras.Sequential([
keras.layers.Flatten(input_shape=(32,32,3)),
keras.layers.Dense(64, activation='relu'),
keras.layers.Dense(num_of_classes, activation='softmax')
])
# compile the neural network
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['acc'])
# training the neural network
model.fit(X_train_scaled, Y_train, validation_split=0.1, epochs=10)
"""**ResNet50**"""
from tensorflow.keras import Sequential, models, layers
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.models import load_model
from tensorflow.keras.models import Model
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras import optimizers
convolutional_base = ResNet50(weights='imagenet', include_top=False, input_shape=(256,256,3))
convolutional_base.summary()
num_of_classes = 10
model = models.Sequential()
model.add(layers.UpSampling2D((2,2)))
model.add(layers.UpSampling2D((2,2)))
model.add(layers.UpSampling2D((2,2)))
model.add(convolutional_base)
model.add(layers.Flatten())
model.add(layers.BatchNormalization())
model.add(layers.Dense(128, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.BatchNormalization())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.BatchNormalization())
model.add(layers.Dense(num_of_classes, activation='softmax'))
model.compile(optimizer=optimizers.RMSprop(lr=2e-5), loss='sparse_categorical_crossentropy', metrics=['acc'])
history = model.fit(X_train_scaled, Y_train, validation_split=0.1, epochs=10)
loss, accuracy = model.evaluate(X_test_scaled, Y_test)
print('Test Accuracy =', accuracy)
h = history
# plot the loss value
plt.plot(h.history['loss'], label='train loss')
plt.plot(h.history['val_loss'], label='validation loss')
plt.legend()
plt.show()
# plot the accuracy value
plt.plot(h.history['acc'], label='train accuracy')
plt.plot(h.history['val_acc'], label='validation accuracy')
plt.legend()
plt.show()