-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgg_ssd.py
176 lines (152 loc) · 6.78 KB
/
vgg_ssd.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pdb
import cv2
import sys
import argparse
import numpy as np
import os
import shutil
import multiprocessing
import json
import tensorflow as tf
from tensorflow.contrib.layers import variance_scaling_initializer
from tensorflow.contrib.framework.python.ops import variables
from tensorflow.python.ops import init_ops
from tensorpack import *
from tensorpack.tfutils.symbolic_functions import *
from tensorpack.tfutils.common import get_tf_version_number
from tensorpack.tfutils.summary import *
from tensorpack.utils.gpu import get_nr_gpu
try:
from .cfgs.config import cfg
from .evaluate import do_python_eval
from .ssd_utils import SSDModel, get_data, get_config
except Exception:
from cfgs.config import cfg
from evaluate import do_python_eval
from ssd_utils import SSDModel, get_data, get_config
class VGGSSD(SSDModel):
def get_logits(self, image):
with argscope(Conv2D, kernel_shape=3, nl=tf.nn.relu, kernel_initializer=tf.contrib.layers.xavier_initializer()):
conv4_3 = (LinearWrap(image)
.Conv2D('conv1_1', 64)
.Conv2D('conv1_2', 64)
.MaxPooling('pool1', 2, padding="SAME")
# 150
.Conv2D('conv2_1', 128)
.Conv2D('conv2_2', 128)
.MaxPooling('pool2', 2, padding="SAME")
# 75
.Conv2D('conv3_1', 256)
.Conv2D('conv3_2', 256)
.Conv2D('conv3_3', 256)
.MaxPooling('pool3', 2, padding="SAME")
# 38
.Conv2D('conv4_1', 512)
.Conv2D('conv4_2', 512)
.Conv2D('conv4_3', 512)())
conv5 = (LinearWrap(conv4_3)
.MaxPooling('pool4', 2, padding="SAME")
# 19
.Conv2D('conv5_1', 512)
.Conv2D('conv5_2', 512)
.Conv2D('conv5_3', 512)
.MaxPooling('pool5', 3, 1, padding='SAME')())
if cfg.freeze_backbone == True:
conv4_3 = tf.stop_gradient(conv4_3)
conv5 = tf.stop_gradient(conv5)
if get_tf_version_number() >= 1.5:
conv6 = Conv2D('conv6', conv5, 1024, 3, dilation_rate=6)
else:
filter_shape = [3, 3, 512, 1024]
W_init = tf.contrib.layers.xavier_initializer()
b_init = tf.constant_initializer()
W = tf.get_variable('W', filter_shape, initializer=W_init)
b = tf.get_variable('b', [1024], initializer=b_init)
conv6 = tf.nn.atrous_conv2d(conv5, W, rate=6, padding="SAME")
conv6 = tf.nn.bias_add(conv6, b)
conv6 = tf.nn.relu(conv6, name='conv6')
conv7 = Conv2D('conv7', conv6, 1024, 1)
# 10
conv8 = (LinearWrap(conv7)
.Conv2D('conv8_1', 256, 1)
.Conv2D('conv8_2', 512, 3, stride=2)())
# 5
conv9 = (LinearWrap(conv8)
.Conv2D('conv9_1', 128, 1)
.Conv2D('conv9_2', 256, 3, stride=2)())
# 3
conv10 = (LinearWrap(conv9)
.Conv2D('conv10_1', 128, 1)
.Conv2D('conv10_2', 256, 3, padding="VALID")())
# 1
conv11 = (LinearWrap(conv10)
.Conv2D('conv11_1', 128, 1)
.Conv2D('conv11_2', 256, 3, padding="VALID")())
conv4_3_shape = conv4_3.get_shape()
if self.data_format == 'NHWC':
norm_dim = 3
scale_shape = conv4_3_shape[-1:]
else:
norm_dim = 1
scale_shape = (conv4_3_shape[1])
scale = variables.model_variable('conv4_3_scale',
shape=scale_shape,
dtype=conv4_3.dtype.base_dtype,
# initializer=init_ops.ones_initializer(),
initializer=tf.constant_initializer(20),
trainable=True)
if self.data_format == 'NCHW':
scale = tf.expand_dims(scale, axis=-1)
scale = tf.expand_dims(scale, axis=-1)
conv4_3_norm = tf.nn.l2_normalize(conv4_3, norm_dim)
conv4_3_scale = tf.multiply(conv4_3_norm, scale)
features = [conv4_3_scale, conv7, conv8, conv9, conv10, conv11]
return features
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', help='comma separated list of GPU(s) to use.', default='0,1')
parser.add_argument('--batch_size_per_gpu', help='batch size per gpu', type=int, default=32)
parser.add_argument('--itr', help='number of iterations', type=int, default=60000)
parser.add_argument('--load', help='load model')
parser.add_argument('--debug', action='store_true')
parser.add_argument('--logdir', help="directory of logging", default=None)
parser.add_argument('--flops', action="store_true", help="print flops and exit")
args = parser.parse_args()
if args.gpu:
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
model = VGGSSD()
if args.flops:
input_desc = [
InputDesc(tf.uint8, [1, cfg.img_h, cfg.img_w, 3], 'input'),
InputDesc(tf.float32, [1, cfg.max_gt_box_shown, 4], 'gt_bboxes'),
InputDesc(tf.int32, [1, cfg.tot_anchor_num], 'conf_label'),
InputDesc(tf.bool, [1, cfg.tot_anchor_num], 'neg_mask'),
InputDesc(tf.float32, [1, cfg.tot_anchor_num, 4], 'loc_label'),
InputDesc(tf.float32, [1, 3], 'ori_shape'),
]
input = PlaceholderInput()
input.setup(input_desc)
with TowerContext('', is_training=True):
model.build_graph(*input.get_input_tensors())
tf.profiler.profile(
tf.get_default_graph(),
cmd='op',
options=tf.profiler.ProfileOptionBuilder.float_operation())
else:
# assert args.gpu is not None, "Need to specify a list of gpu for training!"
if args.logdir != None:
logger.set_logger_dir(os.path.join("train_log", args.logdir))
else:
logger.auto_set_dir()
config = get_config(args, model)
if args.gpu != None:
config.nr_tower = len(args.gpu.split(','))
if args.load:
if args.load.endswith('npz'):
config.session_init = DictRestore(dict(np.load(args.load)))
else:
config.session_init = SaverRestore(args.load)
trainer = SyncMultiGPUTrainerParameterServer(max(get_nr_gpu(), 1))
launch_train_with_config(config, trainer)