-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.py
205 lines (165 loc) · 6.92 KB
/
main.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
# -*- coding: utf_8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import argparse
import time
import nsml
import numpy as np
from nsml import DATASET_PATH
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Activation
from keras.layers import Conv2D, MaxPooling2D
from keras.callbacks import ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
def bind_model(model):
def save(dir_name):
os.makedirs(dir_name, exist_ok=True)
model.save_weights(os.path.join(dir_name, 'model'))
print('model saved!')
def load(file_path):
model.load_weights(file_path)
print('model loaded!')
def infer(queries, _):
test_path = DATASET_PATH + '/test/test_data'
db = [os.path.join(test_path, 'reference', path) for path in os.listdir(os.path.join(test_path, 'reference'))]
queries = [v.split('/')[-1].split('.')[0] for v in queries]
db = [v.split('/')[-1].split('.')[0] for v in db]
queries.sort()
db.sort()
queries, query_vecs, references, reference_vecs = get_feature(model, queries, db)
# l2 normalization
query_vecs = l2_normalize(query_vecs)
reference_vecs = l2_normalize(reference_vecs)
# Calculate cosine similarity
sim_matrix = np.dot(query_vecs, reference_vecs.T)
indices = np.argsort(sim_matrix, axis=1)
indices = np.flip(indices, axis=1)
retrieval_results = {}
for (i, query) in enumerate(queries):
ranked_list = [references[k] for k in indices[i]]
ranked_list = ranked_list[:1000]
retrieval_results[query] = ranked_list
print('done')
return list(zip(range(len(retrieval_results)), retrieval_results.items()))
# DONOTCHANGE: They are reserved for nsml
nsml.bind(save=save, load=load, infer=infer)
def l2_normalize(v):
norm = np.linalg.norm(v, axis=1, keepdims=True)
return np.divide(v, norm, where=norm!=0)
# data preprocess
def get_feature(model, queries, db):
img_size = (224, 224)
test_path = DATASET_PATH + '/test/test_data'
intermediate_layer_model = Model(inputs=model.input, outputs=model.get_layer('dense_2').output)
test_datagen = ImageDataGenerator(rescale=1. / 255, dtype='float32')
query_generator = test_datagen.flow_from_directory(
directory=test_path,
target_size=(224, 224),
classes=['query'],
color_mode="rgb",
batch_size=32,
class_mode=None,
shuffle=False
)
query_vecs = intermediate_layer_model.predict_generator(query_generator, steps=len(query_generator), verbose=1)
reference_generator = test_datagen.flow_from_directory(
directory=test_path,
target_size=(224, 224),
classes=['reference'],
color_mode="rgb",
batch_size=32,
class_mode=None,
shuffle=False
)
reference_vecs = intermediate_layer_model.predict_generator(reference_generator, steps=len(reference_generator),
verbose=1)
return queries, query_vecs, db, reference_vecs
if __name__ == '__main__':
args = argparse.ArgumentParser()
# hyperparameters
args.add_argument('--epoch', type=int, default=5)
args.add_argument('--batch_size', type=int, default=64)
args.add_argument('--num_classes', type=int, default=1383)
# DONOTCHANGE: They are reserved for nsml
args.add_argument('--mode', type=str, default='train', help='submit일때 해당값이 test로 설정됩니다.')
args.add_argument('--iteration', type=str, default='0',
help='fork 명령어를 입력할때의 체크포인트로 설정됩니다. 체크포인트 옵션을 안주면 마지막 wall time 의 model 을 가져옵니다.')
args.add_argument('--pause', type=int, default=0, help='model 을 load 할때 1로 설정됩니다.')
config = args.parse_args()
# training parameters
nb_epoch = config.epoch
batch_size = config.batch_size
num_classes = config.num_classes
input_shape = (224, 224, 3) # input image shape
""" Model """
model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same', input_shape=input_shape))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
model.summary()
bind_model(model)
if config.pause:
nsml.paused(scope=locals())
bTrainmode = False
if config.mode == 'train':
bTrainmode = True
""" Initiate RMSprop optimizer """
opt = keras.optimizers.rmsprop(lr=0.00045, decay=1e-6)
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
print('dataset path', DATASET_PATH)
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
train_generator = train_datagen.flow_from_directory(
directory=DATASET_PATH + '/train/train_data',
target_size=input_shape[:2],
color_mode="rgb",
batch_size=batch_size,
class_mode="categorical",
shuffle=True,
seed=42
)
""" Callback """
monitor = 'acc'
reduce_lr = ReduceLROnPlateau(monitor=monitor, patience=3)
""" Training loop """
STEP_SIZE_TRAIN = train_generator.n // train_generator.batch_size
t0 = time.time()
for epoch in range(nb_epoch):
t1 = time.time()
res = model.fit_generator(generator=train_generator,
steps_per_epoch=STEP_SIZE_TRAIN,
initial_epoch=epoch,
epochs=epoch + 1,
callbacks=[reduce_lr],
verbose=1,
shuffle=True)
t2 = time.time()
print(res.history)
print('Training time for one epoch : %.1f' % ((t2 - t1)))
train_loss, train_acc = res.history['loss'][0], res.history['acc'][0]
nsml.report(summary=True, epoch=epoch, epoch_total=nb_epoch, loss=train_loss, acc=train_acc)
nsml.save(epoch)
print('Total training time : %.1f' % (time.time() - t0))