-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathTFConverter+Mappers.swift
281 lines (238 loc) · 11.9 KB
/
TFConverter+Mappers.swift
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
//
// TFMappers.swift
// Bender
//
// Created by Mathias Claassen on 5/24/17.
//
//
import Foundation
public extension TFConverter {
private func addMapper(node: TFNode) -> NetworkLayer {
return Add(id: node.nodeDef.name)
}
private func neuronMapper(_ type: ActivationNeuronType) -> (TFNode) -> NetworkLayer {
return { node in Neuron(type: type, id: node.nodeDef.name) }
}
private func softMaxMapper(node: TFNode) -> NetworkLayer {
return Softmax(id: node.nodeDef.name)
}
private func poolingMapper(_ type: PoolingType) -> (TFNode) -> NetworkLayer {
return { node in
guard let pad = node.nodeDef.attr["padding"]?.s,
let padString = String(data: pad, encoding: .utf8),
let strides = node.nodeDef.strides,
let size = node.nodeDef.ksize else {
fatalError("Cannot create MaxPool")
}
return Pooling(type: type,
padding: PaddingType.fromTF(padString),
kernelSize: (width: Int(size.width), height: Int(size.height)),
stride: (x: strides.x, y: strides.y),
id: node.nodeDef.name)
}
}
private func convMapper(node: TFNode) -> NetworkLayer {
guard let pad = node.nodeDef.attr["padding"]?.s,
let padString = String(data: pad, encoding: .utf8),
let strides = node.nodeDef.strides else {
fatalError("Cannot create Conv2D")
}
let dilations = node.nodeDef.dilations ?? (1, 1)
guard let weightData = TFWeightData.getWeightData(node: node) else {
fatalError("Could not get weight information for this Conv2DTranspose")
}
var edgeMode: MPSImageEdgeMode = .zero
var pads: [Int] = [0, 0, 0, 0]
if let paddings = node.nodeDef.attr["explicit_paddings"]?.tensor.tensorContent {
let padArray: [Int32] = paddings.toArray()
if padArray.filter({ $0 != 0 }).count > 1 {
// This only supports padding H and W dimensions
pads = padArray[2..<6].map { Int($0) }
}
}
if let padMode = node.nodeDef.attr["pad_mode"]?.s,
let padString = String(data: padMode, encoding: .utf8) {
if padString == "REFLECT" || padString == "SYMMETRIC" {
edgeMode = .clamp
}
}
if node.nodeDef.isTFDepthwiseConvOp, #available(iOS 11.0, *) {
//transpose weights
let weights = weightData.weights != nil ? HWIOtoIOWH(weights: weightData.weights!, shape: weightData.weightShape.toShape) : nil
// Depthwise does not change feature channels count
let convSize = ConvSize(outputChannels: weightData.weightShape.inputChannels,
kernelWidth: weightData.weightShape.kernelHeight,
kernelHeight: weightData.weightShape.kernelWidth,
strideX: Int(strides.x),
strideY: Int(strides.y),
dilationX: dilations.x,
dilationY: dilations.y)
return DepthwiseConvolution(convSize: convSize,
neuronType: node.nodeDef.activationNeuron(),
useBias: weightData.useBias,
padding: PaddingType.fromTF(padString),
weights: weights,
bias: weightData.bias,
paddings: pads,
edgeMode: edgeMode,
id: node.nodeDef.name)
} else {
//transpose weights
let weights = weightData.weights != nil ? HWIOtoOHWI(weights: weightData.weights!, shape: weightData.weightShape.toShape) : nil
let convSize = ConvSize(shape: weightData.weightShape,
strideX: Int(strides.x),
strideY: Int(strides.y),
dilationX: dilations.x,
dilationY: dilations.y)
return Convolution(convSize: convSize,
neuronType: node.nodeDef.activationNeuron(),
useBias: weightData.useBias,
padding: PaddingType.fromTF(padString),
weights: weights,
bias: weightData.bias,
paddings: pads,
edgeMode: edgeMode,
id: node.nodeDef.name)
}
}
private func convTransposeMapper(node: TFNode) -> NetworkLayer {
guard let strides = node.nodeDef.strides else {
fatalError("Cannot create Conv2DTranspose")
}
guard let weightData = TFWeightData.getWeightData(node: node) else {
fatalError("Could not get weight information for this Conv2DTranspose")
}
let convSize = ConvSize(outputChannels: Int(weightData.weightShape.dim[2].size), // outputChannels for ConvTranspose are at 3rd dim
kernelWidth: weightData.weightShape.kernelWidth,
kernelHeight: weightData.weightShape.kernelHeight,
strideX: Int(strides.x),
strideY: Int(strides.y))
var weights = weightData.weights
if #available(iOS 11.3.1, *) {
// We use MPSCNNConvolutionTranspose in this case and must transpose the weights
weights = weightData.weights != nil ? permute(order: [2, 0, 1, 3])(weightData.weights!, weightData.weightShape.toShape) : nil
}
return ConvTranspose(size: convSize,
weights: weights,
bias: weightData.bias,
id: node.nodeDef.name)
}
private func denseMapper(node: TFNode) -> NetworkLayer {
guard let weightData = TFWeightData.getWeightData(node: node) else {
fatalError("Could not get weight information for this Conv2DTranspose")
}
// transpose weights is done in the layer itself
return FullyConnected(neurons: Int(weightData.weightShape.dim[1].size),
neuronType: node.nodeDef.activationNeuron(),
useBias: weightData.useBias,
weights: weightData.weights,
bias: weightData.bias,
transpose: permute(order: [3, 1, 0, 2]),
id: node.nodeDef.name)
}
private func inormMapper(node: TFNode) -> NetworkLayer {
guard let incoming = node.incomingNodes() as? [TFNode],
let shiftVar = incoming.first(where: { $0.nodeDef.isTFVariableOrConstOp }),
let mul = incoming.first(where: { $0.nodeDef.isTFInstanceNormMulOp }),
let scaleVar = mul.incomingNodes().first as? TFNode, scaleVar.nodeDef.isTFVariableOrConstOp else {
fatalError("Could not parse Instance norm node")
}
var scale: Data?
var shift: Data?
if shiftVar.nodeDef.isTFConstOp {
shift = shiftVar.nodeDef.valueData()
} else {
shift = (shiftVar.incomingNodes().first as? TFNode)?.nodeDef.valueData()
}
if scaleVar.nodeDef.isTFConstOp {
scale = scaleVar.nodeDef.valueData()
} else {
scale = (scaleVar.incomingNodes().first as? TFNode)?.nodeDef.valueData()
}
return InstanceNorm(scale: scale, shift: shift, id: node.nodeDef.name)
}
private func concatMapper(node: TFNode) -> NetworkLayer {
let inputs = node.incomingNodes().cleanMap { $0 as? TFNode }
let axisNodeV2 = inputs.first(where: { $0.nodeDef.name == "\(node.nodeDef.name)/axis" })
let axisNodeV1 = inputs.first(where: { $0.nodeDef.name == "\(node.nodeDef.name)/concat_dim" })
guard let axisNode = axisNodeV2 ?? axisNodeV1 else {
fatalError("Cannot create \(Constants.Ops.Concat). Missing input node \(node.nodeDef.name)/axis")
}
guard let axisInt32 = axisNode.nodeDef.attr["value"]?.tensor.intVal.first, let axis = LayerSizeAxis.fromTF(index: Int(axisInt32)) else {
fatalError("Cannot create \(Constants.Ops.Concat). Missing or invalid attribute axis.")
}
return Concat(axis: axis, id: node.nodeDef.name)
}
private func batchnormMapper(node: TFNode) -> NetworkLayer {
guard let variables = (node.incomingNodes() as? [TFNode])?.filter({ $0.nodeDef.isTFVariableOrConstOp }),
let meanVar = variables.first(where: { $0.nodeDef.isTFMovMean }),
let varianceVar = variables.first(where: { $0.nodeDef.isTFMovVariance }),
let betaVar = variables.first(where: { $0.nodeDef.isTFBeta }) else {
fatalError("Could not parse Batch norm node")
}
let gammaVar = variables.first(where: { $0.nodeDef.isTFGamma })
var mean: Data?
var variance: Data?
var scale: Data?
var offset: Data?
if meanVar.nodeDef.isTFConstOp {
mean = meanVar.nodeDef.valueData()
} else {
mean = (meanVar.incomingNodes().first as? TFNode)?.nodeDef.valueData()
}
if varianceVar.nodeDef.isTFConstOp {
variance = varianceVar.nodeDef.valueData()
} else {
variance = (varianceVar.incomingNodes().first as? TFNode)?.nodeDef.valueData()
}
if betaVar.nodeDef.isTFConstOp {
offset = betaVar.nodeDef.valueData()
} else {
offset = (betaVar.incomingNodes().first as? TFNode)?.nodeDef.valueData()
}
if let gammaVar = gammaVar, node.nodeDef.attr["scale_after_normalization"]?.b != false {
if gammaVar.nodeDef.isTFConstOp {
scale = gammaVar.nodeDef.valueData()
} else {
scale = (gammaVar.incomingNodes().first as? TFNode)?.nodeDef.valueData()
}
}
let epsilon = node.nodeDef.attr["variance_epsilon"]?.f ?? node.nodeDef.attr["epsilon"]?.f ?? 0.001
return BatchNorm(mean: mean, variance: variance, offset: offset, scale: scale, epsilon: epsilon, id: node.nodeDef.name)
}
func setupMappers() {
// MARK: Add
mappers[Constants.Ops.Add] = addMapper
mappers[Constants.Ops.AddV2] = addMapper
// MARK: Activation neurons
mappers[Constants.Ops.Relu] = neuronMapper(.relu)
mappers[Constants.Ops.Relu6] = neuronMapper(.relu6)
mappers[Constants.Ops.QuantizedRelu] = neuronMapper(.relu)
mappers[Constants.Ops.Tanh] = neuronMapper(.tanh)
mappers[Constants.Ops.Sigmoid] = neuronMapper(.sigmoid)
// MARK: Pooling
mappers[Constants.Ops.MaxPool] = poolingMapper(.max)
mappers[Constants.Ops.AvgPool] = poolingMapper(.avg)
// MARK: SoftMax
mappers[Constants.Ops.Softmax] = softMaxMapper
// MARK: Conv
mappers[Constants.Ops.Conv] = convMapper
mappers[Constants.Ops.QuantizedConv2D] = convMapper
if #available(iOS 11.0, *) {
mappers[Constants.Ops.DepthwiseConv] = convMapper
}
// MARK: ConvTranspose
mappers["Conv2DBackpropInput"] = convTransposeMapper
// MARK: Dense
mappers[Constants.Ops.Dense] = denseMapper
// MARK: InstanceNorm
mappers[Constants.Ops.InstanceNormAdd] = inormMapper
// MARK: Concat
mappers[Constants.Ops.Concat] = concatMapper
mappers[Constants.Ops.ConcatV1] = concatMapper
// MARK: BatchNorm
mappers[Constants.Ops.BatchNormGlobal] = batchnormMapper
mappers[Constants.Ops.FusedBatchNorm] = batchnormMapper
mappers[Constants.Ops.FusedBatchNormV3] = batchnormMapper
}
}