-
Notifications
You must be signed in to change notification settings - Fork 19
/
PeleeNet.py
217 lines (182 loc) · 9.69 KB
/
PeleeNet.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
# coding='utf-8'
'''
author: Youzhao Yang
date: 05/08/2018
github: https://github.com/nnuyi
'''
import tensorflow as tf
import numpy as np
import time
import os
from tqdm import tqdm
from layers import Layer
from utils import get_data, gen_batch_data
class PeleeNet:
model_name = 'PeleeNet'
'''
PeleeNet Class
'''
def __init__(self, config=None, sess=None):
self.sess = sess
self.config = config
self.num_class = self.config.num_class
self.input_height = self.config.input_height
self.input_width = self.config.input_width
self.input_channel = self.config.input_channel
self.batchsize = self.config.batchsize
self.layer = Layer()
def peleenet(self, input_x, k=32, num_init_channel=64, block_config=[3,4,8,6], bottleneck_width=[2,2,4,4], is_training=True, reuse=False):
with tf.variable_scope(self.model_name) as scope:
if reuse:
scope.reuse_variables()
'''
--------------------------------------------------------------------
feature extraction
--------------------------------------------------------------------
'''
# _stem_block(self, input_x, num_init_channel=32, is_training=True, reuse=False):
from_layer = self.layer._stem_block(input_x,
num_init_channel=num_init_channel,
is_training=is_training,
reuse=reuse)
# _dense_block(self, input_x, stage, num_block, k, bottleneck_width, is_training=True, reuse=False):
# _transition_layer(self, input_x, stage, is_avgpool=True, is_training=True, reuse=False):
stage = 0
for num_block, bottleneck_coeff in zip(block_config, bottleneck_width):
stage = stage + 1
# dense_block
from_layer = self.layer._dense_block(from_layer,
stage,
num_block,
k,
bottleneck_coeff,
is_training=is_training,
reuse=reuse)
is_avgpool = True if stage < 4 else False
output_channel = from_layer.get_shape().as_list()[-1]
# transition_layer
from_layer = self.layer._transition_layer(from_layer,
stage,
output_channel=output_channel,
is_avgpool=is_avgpool,
is_training=is_training,
reuse=reuse)
'''
--------------------------------------------------------------------
classification
--------------------------------------------------------------------
'''
# _classification_layer(self, input_x, num_class, keep_prob=0.5, is_training=True, reuse=False):
logits = self.layer._classification_layer(from_layer, self.num_class, is_training=is_training, reuse=reuse)
return logits
def build_model(self):
self.input_train = tf.placeholder(tf.float32, [self.batchsize, self.input_height, self.input_width, self.input_channel], name='input_train')
self.input_test = tf.placeholder(tf.float32, [self.batchsize, self.input_height, self.input_width, self.input_channel], name='input_test')
self.one_hot_labels = tf.placeholder(tf.float32, [self.batchsize, self.num_class], name='one_hot_labels')
# logits data and one_hot_labels
self.logits_train = self.peleenet(self.input_train, is_training=True, reuse=False)
self.logits_test = self.peleenet(self.input_test, is_training=False, reuse=True)
# self.one_hot_labels = tf.one_hot(self.input_label, self.num_class)
# loss function
def softmax_cross_entropy_with_logits(x, y):
try:
return tf.nn.softmax_cross_entropy_with_logits(logits=x, labels=y)
except:
return tf.nn.softmax_cross_entropy_with_logits(targets=x, labels=y)
# weights regularization
self.weights_reg = tf.reduce_sum(tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES))
self.loss = tf.reduce_mean(softmax_cross_entropy_with_logits(self.logits_train, self.one_hot_labels)) + self.config.weight_decay*self.weights_reg
# optimizer
'''
self.adam_optim = tf.train.AdamOptimizer(learning_rate=self.config.learning_rate,
beta1=self.config.beta1,
beta2=self.config.beta2).minimize(self.loss)
'''
self.rmsprop_optim = tf.train.RMSPropOptimizer(learning_rate=self.config.learning_rate,
momentum=self.config.momentum).minimize(self.loss)
# accuracy
self.predicetion = tf.nn.softmax(self.logits_test, 1)
self.accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(self.predicetion, 1), tf.argmax(self.one_hot_labels, 1)), tf.float32))
# summary
self.loss_summary = tf.summary.scalar('entrophy loss', self.loss)
self.accuracy_summary = tf.summary.scalar('accuracy', self.accuracy)
self.summaries = tf.summary.merge_all()
self.summary_writer = tf.summary.FileWriter('logs', self.sess.graph)
# saver
self.saver = tf.train.Saver()
def train_model(self):
# initialize variables
tf.global_variables_initializer().run()
# load model
if self.load_model():
print('load model successfully')
else:
print('fail to load model')
# get datasource
datasource = get_data(self.config.dataset, is_training=True)
gen_data = gen_batch_data(datasource, self.batchsize, is_training=True)
ites_per_epoch = int(len(datasource.images)/self.batchsize)
step = 0
for epoch in range(self.config.epochs):
for ite in tqdm(range(ites_per_epoch)):
images, labels = next(gen_data)
_, loss, accuracy, summaries = self.sess.run([self.rmsprop_optim, self.loss, self.accuracy, self.summaries], feed_dict={
self.input_train:images,
self.input_test:images,
self.one_hot_labels:labels
})
step = step + 1
self.summary_writer.add_summary(summaries, global_step=step)
# test model
if np.mod(epoch, 1) == 0:
print('--epoch_{} -- training accuracy:{}'.format(epoch, accuracy))
self.test_model()
# save model
if np.mod(epoch, 5) == 0:
self.save_model()
def test_model(self):
if not self.config.is_training:
# initialize variables
tf.global_variables_initializer().run()
# load model
if self.load_model():
print('load model successfully')
else:
print('fail to load model')
datasource = get_data(self.config.dataset, is_training=False)
gen_data = gen_batch_data(datasource, self.batchsize, is_training=False)
ites_per_epoch = int(len(datasource.images)/self.batchsize)
accuracy = []
for ite in range(ites_per_epoch):
images, labels = next(gen_data)
accuracy_per_epoch = self.sess.run([self.accuracy], feed_dict={
self.input_test:images,
self.one_hot_labels:labels
})
accuracy.append(accuracy_per_epoch[0])
acc = np.mean(accuracy)
print('--test epoch -- accuracy:{:.4f}'.format(acc))
# load model
def load_model(self):
if not os.path.isfile(os.path.join(self.model_dir, 'checkpoint')):
return False
self.save.restore(self.sess, self.model_pos)
# save model
def save_model(self):
if not os.path.exists(self.model_dir):
os.mkdir(self.model_dir)
self.saver.save(self.sess, self.model_pos)
@property
def model_dir(self):
return '{}/{}'.format(self.config.checkpoint_dir, self.config.dataset)
@property
def model_pos(self):
return '{}/{}/{}'.format(self.config.checkpoint_dir, self.config.dataset, self.model_name)
if __name__=='__main__':
input_x = tf.placeholder(tf.float32, [64, 224,224,3], name='input_train')
peleenet = PeleeNet()
start_time = time.time()
output = peleenet.peleenet(input_x)
end_time = time.time()
print('total time:{}'.format(end_time-start_time))
print(output.get_shape().as_list())