-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmodel.py
53 lines (44 loc) · 3 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
import tensorflow as tf
import tensorflow.contrib.slim as slim
from backbones import modifiedResNet_v2, ResNet_v2
def get_embd(inputs, is_training_dropout, is_training_bn, config, reuse=False, scope='embd_extractor'):
with tf.variable_scope(scope, reuse=reuse):
net = inputs
end_points = {}
if config['backbone_type'].startswith('resnet_v2_m'):
arg_sc = modifiedResNet_v2.resnet_arg_scope(weight_decay=config['weight_decay'], batch_norm_decay=config['bn_decay'])
with slim.arg_scope(arg_sc):
if config['backbone_type'] == 'resnet_v2_m_50':
net, end_points = modifiedResNet_v2.resnet_v2_m_50(net, is_training=is_training_bn, return_raw=True)
elif config['backbone_type'] == 'resnet_v2_m_101':
net, end_points = modifiedResNet_v2.resnet_v2_m_101(net, is_training=is_training_bn, return_raw=True)
elif config['backbone_type'] == 'resnet_v2_m_152':
net, end_points = modifiedResNet_v2.resnet_v2_m_152(net, is_training=is_training_bn, return_raw=True)
elif config['backbone_type'] == 'resnet_v2_m_200':
net, end_points = modifiedResNet_v2.resnet_v2_m_200(net, is_training=is_training_bn, return_raw=True)
else:
raise ValueError('Invalid backbone type.')
elif config['backbone_type'].startswith('resnet_v2'):
arg_sc = ResNet_v2.resnet_arg_scope(weight_decay=config['weight_decay'], batch_norm_decay=config['bn_decay'])
with slim.arg_scope(arg_sc):
if config['backbone_type'] == 'resnet_v2_50':
net, end_points = ResNet_v2.resnet_v2_50(net, is_training=is_training_bn, return_raw=True)
elif config['backbone_type'] == 'resnet_v2_101':
net, end_points = ResNet_v2.resnet_v2_101(net, is_training=is_training_bn, return_raw=True)
elif config['backbone_type'] == 'resnet_v2_152':
net, end_points = ResNet_v2.resnet_v2_152(net, is_training=is_training_bn, return_raw=True)
elif config['backbone_type'] == 'resnet_v2_200':
net, end_points = ResNet_v2.resnet_v2_200(net, is_training=is_training_bn, return_raw=True)
else:
raise ValueError('Invalid backbone type.')
if config['out_type'] == 'E':
with slim.arg_scope(arg_sc):
net = slim.batch_norm(net, activation_fn=None, is_training=is_training_bn)
net = slim.dropout(net, keep_prob=config['keep_prob'], is_training=is_training_dropout)
net = slim.flatten(net)
net = slim.fully_connected(net, config['embd_size'], normalizer_fn=None, activation_fn=None)
net = slim.batch_norm(net, scale=False, activation_fn=None, is_training=is_training_bn)
end_points['embds'] = net
else:
raise ValueError('Invalid out type.')
return net, end_points