-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageClassification.py
More file actions
182 lines (148 loc) · 5.66 KB
/
ImageClassification.py
File metadata and controls
182 lines (148 loc) · 5.66 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
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
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style="whitegrid")
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import os
import glob as gb
import cv2
import tensorflow as tf
import keras
from keras import layers, optimizers, models
from tensorflow.keras.applications import ResNet50
"""
Define the paths to the train test and pred folders
"""
trainpath = '/content/seg_train/'
testpath = '/content/seg_test/'
predpath = '/content/seg_pred/'
"""
Integer encoding - Dictionary with 6 categories & function to retreive the encoding
"""
code_to_num = {'buildings': 0, 'forest': 1, 'glacier': 2, 'mountain': 3, 'sea': 4, 'street': 5}
num_to_code = {0: 'buildings', 1: 'forest', 2: 'glacier', 3: 'mountain', 4: 'sea', 5: 'street'}
def get_code(n):
if n in num_to_code:
return num_to_code[n]
def get_num(c):
if c in code_to_num:
return code_to_num[c]
"""
Reading & Resizing of test, train & pred images without loss of accuracy
"""
# size of image
s = 100
X_train = []
y_train = []
for folder in os.listdir(trainpath + 'seg_train'):
files = trainpath + 'seg_train//' + folder + '/*.jpg'
for file in files:
image = cv2.imread(file)
image_array = cv2.resize(image, (s, s))
X_train.append(list(image_array))
y_train.append(get_num(folder))
X_test = []
y_test = []
for folder in os.listdir(testpath + 'seg_test'):
files = testpath + 'seg_test//' + folder + '/*.jpg'
for file in files:
image = cv2.imread(file)
image_array = cv2.resize(image, (s, s))
X_test.append(list(image_array))
y_test.append(get_num(folder))
X_pred = []
files = predpath + 'seg_pred/*.jpg'
for file in files:
image = cv2.imread(file)
image_array = cv2.resize(image, (s, s))
X_pred.append(list(image_array))
#Converting into array
X_train = np.array(X_train)
X_test = np.array(X_test)
X_pred_array = np.array(X_pred)
y_train = np.array(y_train)
y_test = np.array(y_test)
"""
Visualsation of required Prediction Images
"""
plt.figure(figsize=(20,20))
for n , i in enumerate(list(np.random.randint(0,len(X_pred),36))) :
plt.subplot(6,6,n+1)
plt.imshow(X_pred[i])
plt.axis('off')
"""
CNN Architecture for image classification
"""
KerasModel = models.Sequential()
KerasModel.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
KerasModel.add(layers.MaxPooling2D((2, 2)))
KerasModel.add(layers.Conv2D(64, (3, 3), activation='relu'))
KerasModel.add(layers.MaxPooling2D((2, 2)))
KerasModel.add(layers.Conv2D(128, (3, 3), activation='relu'))
KerasModel.add(layers.MaxPooling2D((2, 2)))
KerasModel.add(layers.Conv2D(128, (3, 3), activation='relu'))
KerasModel.add(layers.MaxPooling2D((2, 2)))
KerasModel.add(layers.Flatten())
KerasModel.add(layers.Dropout(0.5))
KerasModel.add(layers.Dense(512, activation='relu'))
KerasModel.add(layers.Dense(6, activation='softmax'))
KerasModel.compile(optimizer=optimizers.Adam(lr=0.001), loss=['sparse_categorical_crossentropy'], metrics=['accuracy'])
#Model Summary
print('Model Details are : ')
print(KerasModel.summary())
"""
Training of model & Evaluating Test Loss & Accuracy
"""
ThisModel = KerasModel.fit(X_train, y_train, epochs=50, batch_size=256, validation_data=(X_test, y_test))
ModelLoss, ModelAccuracy = KerasModel.evaluate(X_test, y_test)
print('Test Loss is {}'.format(ModelLoss))
print('Test Accuracy is {}'.format(ModelAccuracy))
"""
Obtaining Prediction for the unlabeled data
"""
y_pred = KerasModel.predict(X_test)
y_result = KerasModel.predict(X_pred_array)
"""
Random predicted pictures & its predicting category
"""
plt.figure(figsize=(20, 20))
for n, i in enumerate(list(np.random.randint(0, len(X_pred), 36))):
plt.subplot(6, 6, n + 1)
plt.imshow(X_pred_array[i])
plt.axis('off')
plt.title(get_code(np.argmax(y_result[i])))
# Transfer Learning Approach - Using ResNet50
conv_base = ResNet50(weights='imagenet', include_top=False, input_shape=(100, 100, 3))
datagen = ImageDataGenerator(
rotation_range=10, # randomly rotate images in the range (degrees, 0 to 180)\n",
zoom_range=0.1, # Randomly zoom image\n",
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)\n",
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)\n",
horizontal_flip=True, # randomly flip images horizontally\n",
vertical_flip=False, # Don't randomly flip images vertically\n",
)
batch_size = 32
img_iter = datagen.flow(X_train, y_train, batch_size=batch_size)
# Making last layer trainable and all other layers are static.
for layer in conv_base.layers[:143]:
layer.trainable = False
conv_base.trainable = True
"""
Transfer Learning Model
"""
KerasModel2 = models.Sequential()
KerasModel2.add(conv_base)
KerasModel2.add(layers.Flatten())
KerasModel2.add(layers.Dropout(0.5))
KerasModel2.add(layers.Dense(64, activation='relu'))
KerasModel2.add(layers.Dense(6, activation='softmax'))
KerasModel2.compile(optimizer=optimizers.Adam(learning_rate=0.00001), loss=['sparse_categorical_crossentropy'],
metrics=['accuracy'])
"""
Training of model & Evaluating Test Loss & Accuracy
"""
ThisModel = KerasModel2.fit(img_iter, epochs=3, steps_per_epoch=len(X_train) / batch_size)
ModelLoss, ModelAccuracy = KerasModel2.evaluate(X_test, y_test)
print('Test Loss is {}'.format(ModelLoss))
print('Test Accuracy is {}'.format(ModelAccuracy))