-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodel.py
318 lines (270 loc) · 15.4 KB
/
model.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: utf-8 -*-
import sys
import glob
import cv2
import dlib
import numpy as np
# from vgg16 import vgg16
from input_kitti import *
from util import *
from parse_xml import parseXML
from base_vgg16 import Vgg16
import tensorflow as tf
# from utility.image.data_augmentation.flip import Flip
sys.path.append("/Users/tsujiyuuki/env_python/code/my_code/Data_Augmentation")
"""
・collect dataset of cars
・Preprocessing BBOX and Label for training
・try roi_pooling layer
・Extract ROI using mitmul tools
・NMS
"""
"""Flow of Fast RCNN
###############################################################################
In this state, Create Input Images and ROI Labels
1. input batch images and GroundTruth BBox from datasets *folder name, batch size
Image shape is [batch size, width, height, channel], tf.float32, vgg normalized, bgr
Bounding Box shape is [batch size, center_x, center_y, width, height]
2. get candicate bounding box from images.
# Implemented
3. resize input images to input size *size of resize if needed.
if this operation was done, you should adjust bounding box according to it.
Both of Candicate and GroundTruth Bounding Boxes.
In thesis, Image size is in [600, 1000]
In this Implemention, input image has dynamic shape between [600, 1000]
4. convert candicate bounding box to ROI label.
5. calculate IOU between ROI label and GroundTruth label.
IOU is Intersection Over Union.
6. Select Bounding Box from IOU.
IOU > 0.5 is correct label, IOU = [0.1 0.5) is a false label(background).
Correct Label is 25%, BackGround Label is 75%.
Number of Label is 128, Batch Size is 2, so each image has 64 ROIs
###############################################################################
In this stage, Calculate Loss
7. Input data to ROI Pooling Layer is Conv5_3 Feature Map and ROIs
Input shape is Feature map (batch, width, height, 512), ROIs (Num of ROIs, 5)
ROIs, ex:) [0, left, height, right, bottom]. First Element is the index of batch
8. Through ROI Pooling Layer, Output Shape is [Num of ROIs, 7, 7, 512]
9. Reshape it to [Num of ROIs, -1], and then connect to Fully Connected Layer.
10.Output Layer has two section, one is class prediction, the other is its bounding box prediction.
class prediction shape is [Num of ROIs, Num of Class + 1]
bounding box prediction shape is [Num of ROIs, 4 * (Num of Class + 1)]
11.Loss Function
Regularize bounding box value [center_x, center_y, w, h] into
[(GroundTruth x - pred_x) / pred_w, (GroundTruth y - pred_y) / pred_h, log(GroundTruth w / pred_w), log(GroundTruth h / pred_h)]
Class prediction is by softmax with loss.
Bounding Box prediction is by smooth_L1 loss
###############################################################################
In this stage, Describe Datasets.
1. PASCAL VOC2007
2. KITTI Datasets
3. Udacity Datasets
"""
# TODO: datasetsを丸ごとメモリに展開できるか。Generatorを用いるか。
def loss(obj_class, bbox_regression, g_obj_class, g_bbox_regression):
"""Calculate Class Loss and Bounding Regression Loss.
# Args:
obj_class: Prediction of object class. Shape is [ROIs*Batch_Size, 2]
bbox_regression: Prediction of bounding box. Shape is [ROIs*Batch_Size, 4]
"""
pass
def fast_rcnn(sess, rois, roi_size=(7, 7), vggpath=None, image_shape=(300, 300), \
is_training=None, use_batchnorm=False, activation=tf.nn.relu, num_of_rois=128):
"""Model Definition of Fast RCNN
In thesis, Roi Size is (7, 7), channel is 512
"""
# images = tf.placeholder(tf.float32, [None, image_shape[0], image_shape[1], 3])
# images = tf.placeholder(tf.float32, [None, image_shape[0], image_shape[1], 3])
vgg = Vgg(vgg16_npy_path=vggpath)
vgg.build_model(images)
feature_map = vgg.conv5_3 # (batch, kernel, kernel, channel)
with tf.variable_scope("fast_rcnn"):
# roi shape [Num of ROIs, X, Y, W, H]
roi_layer = roi_pooling(feature_map, rois, roi_size[0], roi_size[1])
# input_shape [num_of_rois, channel, roi size, roi size]
pool_5 = tf.reshape(roi_layer, [num_of_rois, roi_size[0]*roi_size[1]*512])
fc6 = fully_connected(pool_5, [roi_size[0]*roi_size[1]*512, 4096], name="fc6", is_training=is_training)
fc7 = fully_connected(fc6, [4096, 4096], name="fc7", is_training=is_training)
# output shape [num_of_rois, 2]
obj_class = tf.nn.softmax(fully_connected(fc7, [4096, 2], name="fc_class", activation=None, use_batchnorm=None), dim=-1)
# output shape [num_of_rois, 8]
bbox_regression = fully_connected(fc7, [4096, 8], name="fc_bbox", activation=None, use_batchnorm=None)
def fully_connected(input_layer, shape, name="", is_training=True, use_batchnorm=True, activation=tf.nn.relu):
with tf.variable_scope("fully" + name):
kernel = tf.get_variable("weights", shape=shape, \
dtype=tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.01))
fully = tf.matmul(input_layer, kernel)
if activation:
fully = activation(fully)
if use_batchnorm:
fully = batch_norm(fully, is_training)
return fully
def batch_norm(inputs, phase_train, decay=0.9, eps=1e-5):
"""Batch Normalization
Args:
inputs: input data(Batch size) from last layer
phase_train: when you test, please set phase_train "None"
Returns:
output for next layer
"""
gamma = tf.get_variable("gamma", shape=inputs.get_shape()[-1], dtype=tf.float32, initializer=tf.constant_initializer(1.0))
beta = tf.get_variable("beta", shape=inputs.get_shape()[-1], dtype=tf.float32, initializer=tf.constant_initializer(0.0))
pop_mean = tf.get_variable("pop_mean", trainable=False, shape=inputs.get_shape()[-1], dtype=tf.float32, initializer=tf.constant_initializer(0.0))
pop_var = tf.get_variable("pop_var", trainable=False, shape=inputs.get_shape()[-1], dtype=tf.float32, initializer=tf.constant_initializer(1.0))
axes = range(len(inputs.get_shape()) - 1)
if phase_train != None:
batch_mean, batch_var = tf.nn.moments(inputs, axes)
train_mean = tf.assign(pop_mean, pop_mean * decay + batch_mean*(1 - decay))
train_var = tf.assign(pop_var, pop_var * decay + batch_var * (1 - decay))
with tf.control_dependencies([train_mean, train_var]):
return tf.nn.batch_normalization(inputs, batch_mean, batch_var, beta, gamma, eps)
else:
return tf.nn.batch_normalization(inputs, pop_mean, pop_var, beta, gamma, eps)
def convBNLayer(input_layer, use_batchnorm, is_training, input_dim, output_dim, \
kernel_size, stride, activation=tf.nn.relu, padding="SAME", name="", atrous=False, rate=1):
with tf.variable_scope("convBN" + name):
w = tf.get_variable("weights", \
shape=[kernel_size, kernel_size, input_dim, output_dim], initializer=tf.contrib.layers.xavier_initializer())
if atrous:
conv = tf.nn.atrous_conv2d(input_layer, w, rate, padding="SAME")
else:
conv = tf.nn.conv2d(input_layer, w, strides=[1, stride, stride, 1], padding=padding)
if use_batchnorm:
bn = batch_norm(conv, is_training)
if activation != None:
return activation(conv, name="activation")
return bn
b = tf.get_variable("bias", \
shape=[output_dim], initializer=tf.constant_initializer(0.0))
bias = tf.nn.bias_add(conv, b)
if activation != None:
return activation(bias, name="activation")
return bias
def maxpool2d(x, kernel=2, stride=1, name="", padding="SAME"):
"""define max pooling layer"""
with tf.variable_scope("pool" + name):
return tf.nn.max_pool(
x,
ksize = [1, kernel, kernel, 1],
strides = [1, stride, stride, 1],
padding=padding)
class ExtendedLayer(object):
def __init__(self):
pass
def build_model(self, input_layer, use_batchnorm=False, is_training=True, atrous=False, \
rate=1, activation=tf.nn.relu, implement_atrous=False, lr_mult=1):
if implement_atrous:
if atrous:
self.pool_5 = maxpool2d(input_layer, kernel=3, stride=1, name="pool5", padding="SAME")
else:
self.pool_5 = maxpool2d(input_layer, kernel=2, stride=2, name="pool5", padding="SAME") #TODO: padding is valid or same
kernel_size = 3
if atrous:
rate *= 6
# pad = int(((kernel_size + (rate - 1) * (kernel_size - 1)) - 1) / 2)
self.conv_6 = convBNLayer(self.pool_5, use_batchnorm, is_training, 512, 1024, kernel_size, 1, \
name="conv_6", activation=tf.nn.relu, atrous=True, rate=rate)
else:
rate *= 3
# pad = int(((kernel_size + (rate - 1) * (kernel_size - 1)) - 1) / 2)
self.conv_6 = convBNLayer(self.pool_5, use_batchnorm, is_training, 512, 1024, kernel_size, 1, \
name="conv_6", activation=tf.nn.relu, atrous=True, rate=rate)
else:
self.pool_5 = maxpool2d(input_layer, kernel=3, stride=1, name="pool5", padding="SAME")
self.conv_6 = convBNLayer(self.pool_5, use_batchnorm, is_training, 512, 1024, 3, 1, \
name="conv_6", activation=tf.nn.relu, atrous=False, rate=rate)
self.conv_7 = convBNLayer(self.conv_6, use_batchnorm, is_training, 1024, 1024, 1, 1, name="conv_7", activation=activation)
self.conv_8_1 = convBNLayer(self.conv_7, use_batchnorm, is_training, 1024, 256, 1, 1, name="conv_8_1", activation=activation)
self.conv_8_2 = convBNLayer(self.conv_8_1, use_batchnorm, is_training, 256, 512, 3, 2, name="conv_8_2", activation=activation)
self.conv_9_1 = convBNLayer(self.conv_8_2, use_batchnorm, is_training, 512, 128, 1, 1, name="conv_9_1", activation=activation)
self.conv_9_2 = convBNLayer(self.conv_9_1, use_batchnorm, is_training, 128, 256, 3, 2, name="conv_9_2", activation=activation)
self.conv_10_1 = convBNLayer(self.conv_9_2, use_batchnorm, is_training, 256, 128, 1, 1, name="conv_10_1", activation=activation)
self.conv_10_2 = convBNLayer(self.conv_10_1, use_batchnorm, is_training, 128, 256, 3, 1, name="conv_10_2", activation=activation, padding="VALID")
self.conv_11_1 = convBNLayer(self.conv_10_2, use_batchnorm, is_training, 256, 128, 1, 1, name="conv_11_1", activation=activation)
self.conv_11_2 = convBNLayer(self.conv_11_1, use_batchnorm, is_training, 128, 256, 3, 1, name="conv_11_2", activation=activation, padding="VALID")
def ssd_model(sess, vggpath=None, image_shape=(300, 300), \
is_training=None, use_batchnorm=False, activation=tf.nn.relu, \
num_classes=0, normalization=[], atrous=False, rate=1, implement_atrous=False):
"""
1. input RGB images and labels
2. edit images like [-1, image_shape[0], image_shape[1], 3]
3. Create Annotate Layer?
4. input x into Vgg16 architecture(pretrained)
5.
"""
images = tf.placeholder(tf.float32, [None, image_shape[0], image_shape[1], 3])
vgg = Vgg(vgg16_npy_path=vggpath)
vgg.build_model(images)
with tf.variable_scope("extended_model") as scope:
phase_train = tf.placeholder(tf.bool, name="phase_traing") if is_training else None
extended_model = ExtendedLayer()
extended_model.build_model(vgg.conv5_3, use_batchnorm=use_batchnorm, atrous=atrous, rate=rate, \
is_training=phase_train, activation=activation, lr_mult=1, implement_atrous=implement_atrous)
# with tf.variable_scope("multibox_layer"):
# from_layers = [vgg.conv4_3, extended_model.conv_7, extended_model.conv_8_2,
# extended_model.conv_9_2, extended_model.conv_10_2, extended_model.conv_11_2]
# multibox_layer = MultiboxLayer()
# multibox_layer.build_model(from_layers, num_classes=0, normalization=normalization)
#
initialized_var = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope="extended_model")
sess.run(tf.variables_initializer(initialized_var))
return extended_model
class MultiboxLayer(object):
def __init__(self):
pass
# TODO: validate this is correct or not
def l2_normalization(self, input_layer, scale=20):
return tf.nn.l2_normalize(input_layer, dim) * scale
def createMultiBoxHead(self, from_layers, num_classes=0, normalizations=[], \
use_batchnorm=False, is_training=None, activation=None, \
kernel_size=3, prior_boxes=[], kernel_sizes=[]):
"""
# Args:
from_layers(list) : list of input layers
num_classes(int) : num of label's classes that this architecture detects
normalizations(list): list of scale for normalizations
if value <= 0, not apply normalization to the specified layer
"""
assert num_classes > 0, "num of label's class must be positive number"
if normalizations:
assert len(from_layers) == len(normalizations), "from_layers and normalizations should have same length"
num_list = len(from_layers)
for index, kernel_size, layer, norm in zip(range(num_list), kernel_sizes, from_layers, normalizations):
input_layer = layer
with tf.variable_scope("layer" + str(index+1)):
if norm > 0:
scale = tf.get_variable("scale", trainable=True, initializer=tf.constant(norm))#initialize = norm
input_layer = self.l2_normalization(input_layer, scale)
# create location prediction layer
loc_output_dim = 4 * prior_num # (center_x, center_y, width, height)
location_layer = convBNLayer(input_layer, use_batchnorm, is_training, input_layer.get_shape()[0], loc_output_dim, kernel_size, 1, name="loc_layer", activation=activation)
# from shape : (batch, from_kernel, from_kernel, loc_output_dim)
# to : (batch, )
location_pred = tf.reshape(location_layer, [-1, ])
# create confidence prediction layer
conf_output_dim = num_classes * prior_num
confidence_layer = convBNLayer(input_layer, use_batchnorm, is_training, input_layer.get_shape()[0], conf_output_dim, kernel_size, 1, name="conf_layer", activation=activation)
confidence_pred = tf.reshape(confidence_pred, [-1, ])
# Flatten each output
# append result of each results
return None
if __name__ == '__main__':
import sys
import matplotlib.pyplot as plt
from PIL import Image as im
sys.path.append('/home/katou01/code/grid/DataAugmentation')
# from resize import resize
image_dir = "/home/katou01/download/training/image_2/*.png"
label_dir = "/home/katou01/download/training/label_2/*.txt"
get_Image_Roi_All(image_dir, label_dir, 80)
#
# image = im.open("./test_images/test1.jpg")
# image = np.array(image, dtype=np.float32)
# new_image = image[np.newaxis, :]
# batch_image = np.vstack((new_image, new_image))
# batch_image = resize(batch_image, size=(300, 300))
#
# with tf.Session() as sess:
# model = ssd_model(sess, batch_image, activation=None, atrous=False, rate=1, implement_atrous=False)
# print(vars(model))
# # tf.summary.scalar('model', model)