-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNNetReader.py
254 lines (225 loc) · 8.62 KB
/
NNetReader.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
class NNetReader(object):
def ReadBytes(input, byteCount):
bytes = input.ReadBytes(byteCount)
Array.Reverse(bytes)
return bytes
ReadBytes = staticmethod(ReadBytes)
def ReadInt32(input):
bytes = NNetReader.ReadBytes(input, 4)
return BitConverter.ToInt32(bytes, 0)
ReadInt32 = staticmethod(ReadInt32)
def ReadSingle(input):
bytes = NNetReader.ReadBytes(input, 4)
return BitConverter.ToSingle(bytes, 0)
ReadSingle = staticmethod(ReadSingle)
def ReadInt32_TextNewLine(input):
str = input.ReadLine()
return Int32.Parse(str)
ReadInt32_TextNewLine = staticmethod(ReadInt32_TextNewLine)
def ReadSingle_TextNewLine(input):
str = input.ReadLine()
return Double.Parse(str)
ReadSingle_TextNewLine = staticmethod(ReadSingle_TextNewLine)
def ReadDataLayer(index, readInt32, readSingle, inputCoordinates, inputDim):
has_transform_param = NNetReader.readInt32()
if has_transform_param == 0:
layer = DataLayer(index, inputDim, inputCoordinates)
return Tuple[CropTransform, Layer](None, layer)
# Scale
scale = 1.0
has_scale = NNetReader.readInt32()
if has_scale != 0:
scale = NNetReader.readSingle()
# Mirror
has_mirror = NNetReader.readInt32() # ignore
# Crop size
cropT_ = None
has_crop_siz = NNetReader.readInt32()
if has_crop_siz != 0:
crop_siz = NNetReader.readInt32()
cropT_ = CropTransform(inputCoordinates, inputDim, crop_siz, True)
# Mean value
mean_val = [None] *
mean_val_cnt = NNetReader.readInt32()
x = 0
while x < mean_val_cnt:
mean_val.append(NNetReader.readSingle())
x += 1
# Mean file
has_mean_file = NNetReader.readInt32()
mean_image = None
if has_mean_file != 0:
mean_image_siz = NNetReader.readInt32()
if mean_image_siz > 0:
mean_image = [None] * mean_image_siz
x = 0
while x < mean_image_siz:
mean_image[x] = NNetReader.readSingle()
x += 1
dataLayerInputCoordinates = inputCoordinates
dataLayerInputDim = inputDim
dataLayerMeanImage = mean_image
if cropT_ != None:
dataLayerInputCoordinates = cropT_.TransformedCoordinates()
dataLayerInputDim = cropT_.TransformedDimension()
if mean_image != None:
dataLayerMeanImage = cropT_.Transform(DenseVector.OfArray(mean_image)).ToArray()
l = DataLayer(index, dataLayerInputDim, dataLayerInputCoordinates, scale, dataLayerMeanImage, mean_val)
return Tuple[CropTransform, Layer](cropT_, l)
ReadDataLayer = staticmethod(ReadDataLayer)
def ReadInnerProductLayer(index, readInt32, readSingle, inputCoordinates, inputDim):
# Caffe format:
# K : input dimension
# N : output dimension
# A : N * K dimensional matrix (row major order)
# B : N dimensional vector
# Matrix formula: output = A * input + B
# Array formula: output[i] = (\sum_j A[i][j] * x[j]) + B[i]
inputDimension = NNetReader.readInt32()
outputDimension = NNetReader.readInt32()
weights = [None] * outputDimension
i = 0
while i < outputDimension:
weights[i] = [None] * inputDimension
j = 0
while j < inputDimension:
weights[i][j] = NNetReader.readSingle()
j += 1
i += 1
intercept = [None] * outputDimension
i = 0
while i < outputDimension:
intercept[i] = NNetReader.readSingle()
i += 1
print "Dimensions: " + inputDimension + " * " + outputDimension
return InnerProductLayer(index, weights, intercept, inputCoordinates)
ReadInnerProductLayer = staticmethod(ReadInnerProductLayer)
def ReadRectifiedLinearLayer(index, readInt32, readSingle, inputCoordinates, inputDim):
return ReLULayer(index, inputDim, inputCoordinates)
ReadRectifiedLinearLayer = staticmethod(ReadRectifiedLinearLayer)
def ReadSoftmaxLayer(readInt32, readSingle):
return None
ReadSoftmaxLayer = staticmethod(ReadSoftmaxLayer)
def ReadConvolutional(index, readInt32, readSingle, inputCoordinates, inputDim):
kernelCount = NNetReader.readInt32()
kernelDimension = NNetReader.readInt32()
padding = NNetReader.readInt32()
# read kernels
kernelTotalDataCount = NNetReader.readInt32()
kernelDataCount = kernelTotalDataCount / kernelCount
kernels = [None] * kernelCount
i = 0
while i < kernelCount:
kernels[i] = [None] * kernelDataCount
j = 0
while j < kernelDataCount:
kernels[i][j] = NNetReader.readSingle()
j += 1
i += 1
# read intercepts
interceptTotalDataCount = NNetReader.readInt32()
if interceptTotalDataCount != kernelCount:
raise Exception("Invalid parameters!")
intercepts = [None] * kernelCount
i = 0
while i < kernelCount:
intercepts[i] = NNetReader.readSingle()
i += 1
channelCount = (kernelDataCount / (kernelCount * kernelDimension * kernelDimension))
kernelCoordinates = ImageCoordinates(channelCount, kernelDimension, kernelDimension)
return ConvolutionLayer(index, inputCoordinates, kernels, intercepts, kernelDimension, padding)
ReadConvolutional = staticmethod(ReadConvolutional)
def ReadPooling(index, readInt32, readSingle, inputCoordinates):
kernelDimension = NNetReader.readInt32()
stride = NNetReader.readInt32()
padding = NNetReader.readInt32()
poolMeth = NNetReader.readInt32()
if kernelDimension == 0:
print "Kernel dimension = 0, treating this as global pooling!"
Debug.Assert(inputCoordinates.ColumnCount == inputCoordinates.RowCount)
Debug.Assert(padding == 0)
kernelDimension = inputCoordinates.ColumnCount
if poolMeth == 0: # MAX
print "Pool method = MAX"
return MaxPoolingLayer(index, inputCoordinates, kernelDimension, padding, stride)
else: # AVG
print "Pool method = AVG"
return AvgPoolingLayer(index, inputCoordinates, kernelDimension, padding, stride)
ReadPooling = staticmethod(ReadPooling)
def ReadLayer(index, readInt32, readSingle, inputCoordinates, inputDim, cropT):
typeID = NNetReader.readInt32()
cropT = None
Console.Write("Reading layer with index {0,2}, of type {1}, input dimension {2}:", index, typeID, inputDim)
if typeID == 0: # "Data"
print "Data"
res = NNetReader.ReadDataLayer(index, readInt32, readSingle, inputCoordinates, inputDim)
cropT = res.Item1
return res.Item2
elif typeID == 1: # "InnerProduct"
Console.Write("InnerProduct")
return NNetReader.ReadInnerProductLayer(index, readInt32, readSingle, inputCoordinates, inputDim)
elif typeID == 2: # "ReLU"
print "ReLU"
return NNetReader.ReadRectifiedLinearLayer(index, readInt32, readSingle, inputCoordinates, inputDim)
elif typeID == 3: # "SoftmaxWithLoss"
print "SoftMax"
return NNetReader.ReadSoftmaxLayer(readInt32, readSingle)
elif typeID == 4: # "Convolution"
print "Convolution"
return NNetReader.ReadConvolutional(index, readInt32, readSingle, inputCoordinates, inputDim)
elif typeID == 5: # "Pooling"
Console.Write("Pooling, ")
return NNetReader.ReadPooling(index, readInt32, readSingle, inputCoordinates)
elif typeID == 6: # "Dropout"
Console.Write("Dropout, ")
return None
else:
raise Exception("Layer type ID not recognized: " + typeID)
ReadLayer = staticmethod(ReadLayer)
def ReadNeuralNet(readInt32, readSingle, inputDimension, inputCoordinates):
nn = NeuralNet()
layerCount = NNetReader.readInt32()
curDimension = inputDimension
curCoordinates = inputCoordinates
cropT = None
i = 0
while i < layerCount:
layer = NNetReader.ReadLayer(i, readInt32, readSingle, curCoordinates, curDimension, )
if layer != None:
nn.AddLayer(layer)
curDimension = layer.OutputDimension
curCoordinates = layer.OutputCoordinates
if layer.LayerType == LayerType.DATA_LAYER and cropT != None:
nn.AddCropTransform(cropT)
i += 1
# Currently disabled because perf gains are not enough, in fact things seem somewhat slower ...
# Console.Write("Linearizing sequences of linear layers ...");
# Coalesce affine layers for running the network more optimally
# nn.CoalesceToVirtual();
# GC.Collect(2);
# print "Done.";
return nn
ReadNeuralNet = staticmethod(ReadNeuralNet)
def ReadBinNeuralNet(file, inputDimension, inputCoordinates):
""" <summary>
Reading from our own binary format. These are the networks we get as output of our protobuf utilities from Caffe.
</summary>
<param name="file"></param>
<param name="inputDimension"></param>
<param name="inputCoordinates"></param>
<returns></returns>
"""
print "Reading neural net from file: " + file
ReadBinNeuralNet = staticmethod(ReadBinNeuralNet)
def ReadTxtNeuralNet(file, inputDimension, inputCoordinates):
""" <summary>
Reading from our own text format. These are the networks we we get as output from our Torch/LUA experiments.
</summary>
<param name="file"></param>
<param name="inputDimension"></param>
<param name="inputCoordinates"></param>
<returns></returns>
"""
print "Reading neural net from file: " + file
ReadTxtNeuralNet = staticmethod(ReadTxtNeuralNet)