-
Notifications
You must be signed in to change notification settings - Fork 12
/
ResNet_image_classification.py
171 lines (142 loc) · 5.92 KB
/
ResNet_image_classification.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
# -*- coding: utf-8 -*-
import os
import tensorflow as tf #tf 2.0.0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras import Model
from tensorflow.keras.layers import Conv2D, Dense, MaxPooling2D, Dropout, Flatten,GlobalAveragePooling2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau
# Image `Augmentation` for Increasing Dataset (Run Augmentation.py )
# ImageDataGenerator (in-place augmentation)
train_data_gen = ImageDataGenerator(rotation_range=50,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant',
cval=0,
rescale=1./255)
valid_data_gen = ImageDataGenerator(rotation_range=45,
width_shift_range=0.2,
height_shift_range=0.2,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant',
cval=0,
rescale=1./255)
test_data_gen = ImageDataGenerator(rescale=1./255)
dataset_dir = os.path.join('/content/drive/My Drive/Colab Notebooks/myDataset', 'DT')
Batch_size = 8
img_h = 256
img_w = 256
num_classes=20
classes = ['owl', # 0
'galaxy', # 1
'lightning', # 2
'wine-bottle', # 3
't-shirt', # 4
'waterfall',# 5
'sword', # 6
'school-bus',# 7
'calculator', # 8
'sheet-music', #9
'airplanes',#10
'lightbulb', # 11
'skyscraper',#12
'mountain-bike',#13
'fireworks', #14
'computer-monitor',#15
'bear',# 16
'grand-piano', # 17
'kangaroo', # 18
'laptop', #19
]
# Training
SEED = 1234
tf.random.set_seed(SEED)
training_dir = os.path.join(dataset_dir, 'training')
train_gen = train_data_gen.flow_from_directory(training_dir,
target_size=(256, 256),
batch_size=Batch_size,
classes=classes,
class_mode='categorical',
shuffle=True,
seed=SEED) # targets are directly converted into one-hot vectors
# Validation
valid_dir = os.path.join(dataset_dir, 'valid')
valid_gen = valid_data_gen.flow_from_directory(valid_dir,
target_size=(256, 256),
batch_size=Batch_size,
classes=classes,
class_mode='categorical',
shuffle=False,
seed=SEED)
# Test
test_dir = os.path.join(dataset_dir, 'testing')
test_gen = test_data_gen.flow_from_directory(test_dir,
target_size=(256, 256),
batch_size=10,
shuffle=False,
seed=SEED,
class_mode=None,
)
# ResNet152V2 Model
ResNet_model = tf.keras.applications.ResNet152V2(weights='imagenet', include_top=False, input_shape=(img_h, img_w, 3))
# The last 15 layers fine tune
for layer in ResNet_model.layers[:-15]:
layer.trainable = False
x = ResNet_model.output
x = GlobalAveragePooling2D()(x)
x = Flatten()(x)
x = Dense(units=512, activation='relu')(x)
x = Dropout(0.3)(x)
x = Dense(units=512, activation='relu')(x)
x = Dropout(0.3)(x)
output = Dense(units=20, activation='softmax')(x)
model = Model(ResNet_model.input, output)
model.summary()
loss = tf.keras.losses.CategoricalCrossentropy()
optimizer = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=optimizer, loss=loss, metrics= ['accuracy'])
lrr = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.4,
min_lr=0.0001)
callbacks = [lrr]
# model fit_generator
STEP_SIZE_TRAIN=train_gen.n//train_gen.batch_size
STEP_SIZE_VALID=valid_gen.n//valid_gen.batch_size
transfer_learning_history = model.fit_generator(generator=train_gen,
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=valid_gen,
validation_steps=STEP_SIZE_VALID,
epochs=20,
callbacks=callbacks,
class_weight='auto',
)
# model evaluate with validation set
model.evaluate(valid_gen, steps=STEP_SIZE_VALID,verbose=1)
STEP_SIZE_TEST=test_gen.n//test_gen.batch_size
test_gen.reset()
pred=model.predict(test_gen,
steps=STEP_SIZE_TEST,
verbose=1)
predicted_class_indices=np.argmax(pred,axis=1)
# CSV file for kaggle submission
labels = train_gen.class_indices
labels = dict((v,k) for k,v in labels.items())
predictions = [k for k in predicted_class_indices]
filenames=test_gen.filenames
FN=[]
for i in filenames:
f = i[5:]
FN.append(f)
results=pd.DataFrame({"Id":FN,
"Category":predictions})
results.to_csv("submission.csv",index=False)