-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmy_netspec.py
More file actions
150 lines (133 loc) · 4.16 KB
/
my_netspec.py
File metadata and controls
150 lines (133 loc) · 4.16 KB
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
## @package my_netspec
# This was developed indepedently of caffe's netspec in 2014.
# This maybe phased out in favor of caffe's netspec in the near
# future
#
import my_pycaffe_utils as mpu
import os
def make_def_proto(nw, isSiamese=True,
baseFileStr='split_im.prototxt', getStreamTopNames=False):
'''
If is siamese then wait for the Concat layers - and make all layers until then siamese.
'''
baseFile = os.path.join(baseFileStr)
protoDef = mpu.ProtoDef(baseFile)
#if baseFileStr in ['split_im.prototxt', 'normal.prototxt']:
lastTop = 'data'
siameseFlag = isSiamese
stream1, stream2 = [], []
mainStream = []
nameGen = mpu.LayerNameGenerator()
for l in nw:
lType, lParam = l
lName = nameGen.next_name(lType)
#To account for layers that should not copied while finetuning
# Such layers need to named differently.
if lParam.has_key('nameDiff'):
lName = lName + '-%s' % lParam['nameDiff']
if lType == 'Concat':
siameseFlag = False
if not lParam.has_key('bottom2'):
lParam['bottom2'] = lastTop + '_p'
if siameseFlag:
lDef, lsDef = mpu.get_siamese_layerdef_for_proto(lType, lName, lastTop, **lParam)
stream1.append(lDef)
stream2.append(lsDef)
else:
lDef = mpu.get_layerdef_for_proto(lType, lName, lastTop, **lParam)
mainStream.append(lDef)
if lParam.has_key('shareBottomWithNext'):
assert lParam['shareBottomWithNext']
pass
else:
lastTop = lName
#Add layers
mainStream = stream1 + stream2 + mainStream
for l in mainStream:
protoDef.add_layer(l['name'][1:-1], l)
if getStreamTopNames:
if isSiamese:
top1Name = stream1[-1]['name'][1:-1]
top2Name = stream2[-1]['name'][1:-1]
else:
top1Name, top2Name = None, None
return protoDef, top1Name, top2Name
else:
return protoDef
##
# Generates a string to represent the n/w name
def nw2name(nw, getLayerNames=False):
nameGen = mpu.LayerNameGenerator()
nwName = []
allNames = []
for l in nw:
lType, lParam = l
lName = nameGen.next_name(lType)
if lParam.has_key('nameDiff'):
allNames.append(lName + '-%s' % lParam['nameDiff'])
else:
allNames.append(lName)
if lType in ['InnerProduct', 'Convolution']:
lName = lName + '-%d' % lParam['num_output']
if lType == 'Convolution':
lName = lName + 'sz%d-st%d' % (lParam['kernel_size'], lParam['stride'])
nwName.append(lName)
elif lType in ['Pooling']:
lName = lName + '-sz%d-st%d' % (lParam['kernel_size'],
lParam['stride'])
nwName.append(lName)
elif lType in ['Concat', 'Dropout', 'Sigmoid']:
nwName.append(lName)
elif lType in ['RandomNoise']:
if lParam.has_key('adaptive_sigma'):
lName = lName + '-asig%.2f' % lParam['adaptive_factor']
else:
lName = lName + '-sig%.2f' % lParam['sigma']
nwName.append(lName)
else:
pass
nwName = ''.join(s + '_' for s in nwName)
nwName = nwName[:-1]
if getLayerNames:
return nwName, allNames
else:
return nwName
##
# This is highly hand engineered to suit my current needs for ICCV submission.
def nw2name_small(nw, isLatexName=False):
nameGen = mpu.LayerNameGenerator()
nwName = []
latexName = []
for l in nw:
lType, lParam = l
lName = ''
latName = ''
if lType in ['Convolution']:
lName = 'C%d_k%d' % (lParam['num_output'], lParam['kernel_size'])
latName = 'C%d' % (lParam['num_output'])
nwName.append(lName)
latexName.append(latName)
elif lType in ['InnerProduct']:
lName = 'F%d' % lParam['num_output']
nwName.append(lName)
latexName.append(latName)
elif lType in ['Pooling']:
lName = lName + 'P'
nwName.append(lName)
latexName.append(lName)
elif lType in ['Sigmoid']:
lName = lName + 'S'
nwName.append(lName)
latexName.append(lName)
elif lType in ['Concat']:
break
else:
pass
nwName = ''.join(s + '-' for s in nwName)
nwName = nwName[:-1]
latexName = ''.join(s + '-' for s in latexName)
latexName = latexName[:-1]
if isLatexName:
return nwName, latexName
else:
return nwName