-
Notifications
You must be signed in to change notification settings - Fork 5
/
delf_trainer_v1.py
263 lines (222 loc) · 9.89 KB
/
delf_trainer_v1.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# -*- coding: utf_8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import sys
import tensorflow as tf
from data_loader import time_checker, check_train_dataset, check_infer_dataset, pipe_data
from train_models import train_model
dirname = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(dirname, "models/research/delf/delf"))
sys.path.insert(1, os.path.join(dirname, "models/research/delf"))
sys.path.insert(2, os.path.join(dirname, "models/research/slim"))
sys.path.insert(3, os.path.join(dirname, "models/research"))
from python import delf_v1
from nets import resnet_v1
slim = tf.contrib.slim
####################[ Download pretrained resnet_v1_50.ckpt ]##################
# This code block is selectable
# You can also download resnet_v1_50.ckpt from
# http://download.tensorflow.org/models/resnet_v1_50_2016_08_28.tar.gz
from google_drive_downloader import GoogleDriveDownloader as gdd
if not os.path.exists("resnet_v1_50.ckpt"):
ckpt_id = "1EorhNWDmU1uILq3qetrdz3-fieH3QFtK"
gdd.download_file_from_google_drive(
file_id=ckpt_id,
dest_path=os.path.join(dirname, 'resnet_v1_50.ckpt'),
unzip=False)
print("resnet_v1_50.ckpt download is completed")
###############################################################################
_SUPPORTED_TRAINING_STEP = ['resnet_finetune', 'att_learning']
_SUPPORTED_ATTENTION_TYPES = [
'use_l2_normalized_feature', 'use_default_input_feature'
]
_SUPPORTED_CHECKPOINT_TYPE= ['resnet_ckpt', 'attention_ckpt']
@time_checker
def build_resnet(images, num_classes, last_layer, is_training=True,
reuse=None):
model = delf_v1.DelfV1()
with slim.arg_scope(resnet_v1.resnet_arg_scope(use_batch_norm=True)):
net, end_points = model.GetResnet50Subnetwork(
images, global_pool=True, is_training=is_training, reuse=reuse)
with slim.arg_scope(
resnet_v1.resnet_arg_scope(
weight_decay=0.0001, batch_norm_scale=True)):
with slim.arg_scope([slim.batch_norm], is_training=True):
feature_map = end_points[last_layer]
# 첫 번째 conv2d with kernel_size 1 + reduce_mean + expand_dims = Global Average Pooling
feature_map = slim.conv2d(
feature_map,
512,
1,
rate=1,
activation_fn=tf.nn.relu,
scope='conv1')
feature_map = tf.reduce_mean(feature_map, [1, 2])
feature_map = tf.expand_dims(tf.expand_dims(feature_map, 1), 2)
# 두 번째 conv2d with kernel_size 1 + squeeze = Fully connected Softmax layer
logits = slim.conv2d(
feature_map,
num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='logits')
logits = tf.squeeze(logits, [1, 2], name='spatial_squeeze')
return logits, feature_map
def build_attention_model(images, num_classes, sess, is_training=True, reuse=False):
use_batch_norm=True
weight_decay=0.0001
attention_nonlinear='softplus'
attention_type=_SUPPORTED_ATTENTION_TYPES[0]
training_resnet=False
training_attention=True
kernel=1
model = delf_v1.DelfV1()
with slim.arg_scope(
resnet_v1.resnet_arg_scope(use_batch_norm=use_batch_norm)):
attention_feat, attention_prob, attention_score, feature_map, _ = (
model.GetAttentionPrelogit(
images,
weight_decay,
attention_nonlinear=attention_nonlinear,
attention_type=attention_type,
kernel=kernel,
training_resnet=training_resnet,
training_attention=training_attention,
reuse=reuse))
with slim.arg_scope(
resnet_v1.resnet_arg_scope(
weight_decay=weight_decay, batch_norm_scale=True)):
with slim.arg_scope([slim.batch_norm], is_training=training_attention):
with tf.variable_scope(
"attention_block", values=[attention_feat], reuse=reuse):
logits = slim.conv2d(
attention_feat,
num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='logits')
logits = tf.squeeze(logits, [1, 2], name='spatial_squeeze')
return logits, attention_prob, feature_map
def restore_weight(sess, ckpt_type, ckpt_path='resnet_v1_50.ckpt'):
"""
If you have "abc.meta, abc.index, abc.data" then just give "abc" to ckpt_path.
"""
#########################[ Initialize all variable ]#############################
global_init = tf.global_variables_initializer()
sess.run(global_init)
############## [ restore variable from exist checkpoint] ##############
if ckpt_type == _SUPPORTED_CHECKPOINT_TYPE[0]:
restore_var = [v for v in tf.global_variables() if 'resnet' in v.name]
elif ckpt_type == _SUPPORTED_CHECKPOINT_TYPE[1]:
restore_var = [v for v in tf.global_variables() if ('resnet' in v.name) or ('attention_block' in v.name)]
else:
raise Exception("You should fill valid config.ckpt_type: Check delf_train.py _SUPPORTED_CHECKPOINT_TYPE")
saver = tf.train.Saver(restore_var)
saver.restore(sess, ckpt_path)
print("=== weights loaded === ")
return None
@time_checker
def build_attention(images, num_classes, sess, is_training=True, reuse=False):
use_batch_norm=True
weight_decay=0.0001
attention_nonlinear='softplus'
attention_type=_SUPPORTED_ATTENTION_TYPES[0]
training_resnet=False
training_attention=True
kernel=1
model = delf_v1.DelfV1()
with slim.arg_scope(
resnet_v1.resnet_arg_scope(use_batch_norm=use_batch_norm)):
attention_feat, attention_prob, attention_score, feature_map, _ = (
model.GetAttentionPrelogit(
images,
weight_decay,
attention_nonlinear=attention_nonlinear,
attention_type=attention_type,
kernel=kernel,
training_resnet=training_resnet,
training_attention=training_attention,
reuse=reuse))
with slim.arg_scope(
resnet_v1.resnet_arg_scope(
weight_decay=weight_decay, batch_norm_scale=True)):
with slim.arg_scope([slim.batch_norm], is_training=training_attention):
with tf.variable_scope(
"attention_block", values=[attention_feat], reuse=reuse):
logits = slim.conv2d(
attention_feat,
num_classes, [1, 1],
activation_fn=None,
normalizer_fn=None,
scope='logits')
logits = tf.squeeze(logits, [1, 2], name='spatial_squeeze')
return logits, attention_prob, feature_map
class Config():
def __init__(self):
self.batch_size = 64
self.num_preprocess_threads = 8
self.nb_epoch = 10
self.fc_learning_rate = 0.0001
self.fc_epoch = 5
self.conv_learning_rate = 0.0001
self.att_learning_rate = 0.0001
self.img_shape = (224, 224, 3)
self.restore_file = "resnet_v1_50.ckpt"
self.last_layer = 'resnet_v1_50/block3'
self.data_path = "Fill it"
self.train_data_path = "Don't Fill it"
self.save_name = 'local_ckpt/resnet_tune'
self.dash_size = 80
self.sess = "Don't Fill it"
self.images = "Don't Fill it"
self.labels = "Don't Fill it"
self.num_train_batches = "Don't Fill it"
self.num_val_batches = "Don't Fill it"
self.images_holder = "Don't Fill it"
self.logits = "Don't Fill it"
self.feature_map = "Don't Fill it"
self.num_classes = "Don't Fill it"
self.ckpt_type = "Fill it"
self.train_step = _SUPPORTED_TRAINING_STEP[0]
class DelfTrainerV1(object):
# delf trainer initializer
def __init__(self, config):
# ==== [ config, sess, pipeline ] =====
self.config = config
check_train_dataset(config.data_path)
check_infer_dataset(config.data_path)
config.train_data_path = config.data_path + "/train"
config.sess = tf.Session()
self._pipeline_data()
# ======== [ build model ] ==========
if config.train_step is 'resnet_finetune':
self._build_resnet_graph()
elif config.train_step is 'att_learning':
self._build_attention_graph()
else:
raise Exception("You should specify correct train step")
# ================= [ restore checkpoint ] =====================
restore_weight(config.sess, config.ckpt_type, config.restore_file)
def _pipeline_data(self):
config = self.config
config.images, config.labels, config.num_train_batches, \
config.num_val_batches = pipe_data(self.config, config.train_data_path)
batch_shape = (None, *config.img_shape)
config.images_holder = tf.placeholder(shape=batch_shape,
dtype=tf.float32)
# === build resnet model to fine-tune ===
def _build_resnet_graph(self):
config = self.config
config.logits, config.feature_map = build_resnet(config.images_holder, config.num_classes, config.last_layer)
return None
# === build attention model to learn attention =====
def _build_attention_graph(self):
config = self.config
config.logits, config.attention_prob, feature_map = build_attention(config.images_holder, config.num_classes, config.sess)
return None
# === execute training with builted graph ===
def run(self):
config = self.config
train_model(config)