Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/dnn/src/tensorflow/tf_graph_simplifier.cpp
16345 views
1
// This file is part of OpenCV project.
2
// It is subject to the license terms in the LICENSE file found in the top-level directory
3
// of this distribution and at http://opencv.org/license.html.
4
5
// Copyright (C) 2018, Intel Corporation, all rights reserved.
6
// Third party copyrights are property of their respective owners.
7
8
#include "../precomp.hpp"
9
10
#ifdef HAVE_PROTOBUF
11
12
#include "tf_graph_simplifier.hpp"
13
14
namespace cv { namespace dnn {
15
CV__DNN_INLINE_NS_BEGIN
16
17
using ::google::protobuf::RepeatedField;
18
using ::google::protobuf::MapPair;
19
20
class Subgraph // Interface to match and replace TensorFlow subgraphs.
21
{
22
public:
23
virtual ~Subgraph() {}
24
25
// Add a node to be matched in the origin graph. Specify ids of nodes that
26
// are expected to be inputs. Returns id of a newly added node.
27
// TODO: Replace inputs to std::vector<int> in C++11
28
int addNodeToMatch(const std::string& op, int input_0 = -1, int input_1 = -1,
29
int input_2 = -1, int input_3 = -1)
30
{
31
int nodeInputs[] = {input_0, input_1, input_2, input_3};
32
int numInputs = 0;
33
for (int i = 0; i < 4; ++i)
34
{
35
numInputs += (int)(nodeInputs[i] != -1);
36
}
37
return addNodeToMatch(op, std::vector<int>(&nodeInputs[0], &nodeInputs[0] + numInputs));
38
}
39
40
int addNodeToMatch(const std::string& op, const std::vector<int>& inputs_)
41
{
42
for (int i = 0; i < inputs_.size(); ++i)
43
{
44
CV_Assert(inputs_[i] < (int)nodes.size());
45
}
46
nodes.push_back(op);
47
inputs.push_back(inputs_);
48
return nodes.size() - 1;
49
}
50
51
// Specify resulting node. All the matched nodes in subgraph excluding
52
// input nodes will be fused into this single node.
53
// TODO: Replace inputs to std::vector<int> in C++11
54
void setFusedNode(const std::string& op, int input_0 = -1, int input_1 = -1,
55
int input_2 = -1, int input_3 = -1, int input_4 = -1,
56
int input_5 = -1)
57
{
58
int nodeInputs[] = {input_0, input_1, input_2, input_3, input_4, input_5};
59
int numInputs = 0;
60
for (int i = 0; i < 6; ++i)
61
{
62
CV_Assert(nodeInputs[i] < (int)nodes.size());
63
numInputs += (int)(nodeInputs[i] != -1);
64
}
65
setFusedNode(op, std::vector<int>(&nodeInputs[0], &nodeInputs[0] + numInputs));
66
}
67
68
void setFusedNode(const std::string& op, const std::vector<int>& inputs_)
69
{
70
fusedNodeInputs = inputs_;
71
fusedNodeOp = op;
72
nodesToFuse.clear();
73
for (int i = 0; i < nodes.size(); ++i)
74
{
75
if (std::find(fusedNodeInputs.begin(), fusedNodeInputs.end(), i) == fusedNodeInputs.end() &&
76
nodes[i] != "Const")
77
nodesToFuse.push_back(i);
78
}
79
}
80
81
static const tensorflow::NodeDef& getInputNode(const tensorflow::GraphDef& net,
82
const tensorflow::NodeDef& node,
83
int inpId)
84
{
85
CV_Assert(inpId < node.input_size());
86
std::string name = node.input(inpId);
87
// If operation produces several tensors, they are specified by index
88
// after ':' character. In example, "input:0".
89
name = name.substr(0, name.rfind(':'));
90
const int numNodes = net.node_size();
91
for (int i = 0; i < numNodes; ++i)
92
{
93
if (net.node(i).name() == name)
94
return net.node(i);
95
}
96
CV_Error(Error::StsParseError, "Input node with name " + name + " not found");
97
}
98
99
// Match TensorFlow subgraph starting from <nodeId> with a set of nodes to be fused.
100
// Const nodes are skipped during matching. Returns true if nodes are matched and can be fused.
101
virtual bool match(const tensorflow::GraphDef& net, int nodeId, std::vector<int>& matchedNodesIds)
102
{
103
matchedNodesIds.clear();
104
matchedNodesIds.reserve(nodesToFuse.size());
105
106
int numNodes = net.node_size();
107
for (int i = 0; i < nodesToFuse.size(); ++i)
108
{
109
while (nodeId < numNodes && net.node(nodeId).op() == "Const")
110
{
111
nodeId += 1;
112
}
113
if (nodeId > numNodes - 1)
114
return false;
115
116
const tensorflow::NodeDef& node = net.node(nodeId);
117
118
if (node.op() != nodes[nodesToFuse[i]])
119
return false;
120
121
std::vector<int>& inputNodes = inputs[nodesToFuse[i]];
122
if (inputNodes.size() != node.input_size())
123
return false;
124
for (int j = 0; j < inputNodes.size(); ++j)
125
{
126
if (nodes[inputNodes[j]].empty()) // Unknown input node type.
127
continue;
128
const tensorflow::NodeDef& inpNode = getInputNode(net, node, j);
129
if (inpNode.op() != nodes[inputNodes[j]])
130
return false;
131
}
132
133
matchedNodesIds.push_back(nodeId);
134
nodeId += 1;
135
}
136
return true;
137
}
138
139
// Fuse matched subgraph.
140
void replace(tensorflow::GraphDef& net, const std::vector<int>& matchedNodesIds)
141
{
142
// Extract names of input nodes.
143
std::vector<std::string> inputsNames(fusedNodeInputs.size());
144
for (int i = 0; i < fusedNodeInputs.size(); ++i)
145
{
146
std::string inpName;
147
// Find input node name looking at inputs of fused nodes.
148
for (int j = 0; j < matchedNodesIds.size() && inpName.empty(); ++j)
149
{
150
const tensorflow::NodeDef &node = net.node(matchedNodesIds[j]);
151
std::vector<int>& inpIndices = inputs[nodesToFuse[j]];
152
153
CV_Assert(node.input_size() == inpIndices.size());
154
for (int k = 0; k < inpIndices.size(); ++k)
155
{
156
if (inpIndices[k] == fusedNodeInputs[i])
157
{
158
inpName = node.input(k);
159
break;
160
}
161
}
162
}
163
CV_Assert(!inpName.empty());
164
inputsNames[i] = inpName;
165
}
166
167
// Remove matched nodes except the last one. Indices in ascending order are expected.
168
tensorflow::NodeDef* node = net.mutable_node(matchedNodesIds.back());
169
for (int i = matchedNodesIds.size() - 2; i >= 0; --i)
170
net.mutable_node()->DeleteSubrange(matchedNodesIds[i], 1);
171
172
// Modify the last node to be a fused one.
173
node->set_op(fusedNodeOp);
174
node->clear_input();
175
for (int i = 0; i < inputsNames.size(); ++i)
176
{
177
node->add_input(inputsNames[i]);
178
}
179
180
std::vector<tensorflow::NodeDef*> inputNodes(inputsNames.size());
181
for (int i = 0; i < inputsNames.size(); ++i)
182
{
183
inputNodes[i] = (tensorflow::NodeDef*)&getInputNode(net, *node, i);
184
}
185
finalize(net, node, inputNodes);
186
}
187
188
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef*,
189
std::vector<tensorflow::NodeDef*>&) {}
190
191
private:
192
std::vector<std::string> nodes; // Nodes to be matched in the origin graph.
193
std::vector<std::vector<int> > inputs; // Connections of an every node to it's inputs.
194
195
std::string fusedNodeOp; // Operation name of resulting fused node.
196
std::vector<int> nodesToFuse; // Set of nodes to be fused.
197
std::vector<int> fusedNodeInputs; // Inputs of fused node.
198
};
199
200
class BatchNormSubgraph : public Subgraph
201
{
202
public:
203
BatchNormSubgraph()
204
{
205
int input = addNodeToMatch("");
206
int epsilon = addNodeToMatch("Const");
207
int moving_variance = addNodeToMatch("Const");
208
int moving_mean = addNodeToMatch("Const");
209
int beta = addNodeToMatch("Const");
210
int gamma = addNodeToMatch("Const");
211
int add = addNodeToMatch("Add", moving_variance, epsilon);
212
int rsqrt = addNodeToMatch("Rsqrt", add);
213
int mul = addNodeToMatch("Mul", rsqrt, gamma);
214
int mul_1 = addNodeToMatch("Mul", input, mul);
215
int mul_2 = addNodeToMatch("Mul", moving_mean, mul);
216
int sub = addNodeToMatch("Sub", beta, mul_2);
217
addNodeToMatch("Add", mul_1, sub);
218
219
setFusedNode("FusedBatchNorm", input, gamma, beta, moving_mean, moving_variance, epsilon);
220
}
221
222
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,
223
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE
224
{
225
Mat epsMat = getTensorContent(inputNodes.back()->attr().at("value").tensor());
226
CV_CheckEQ(epsMat.total(), (size_t)1, ""); CV_CheckTypeEQ(epsMat.type(), CV_32FC1, "");
227
228
fusedNode->mutable_input()->RemoveLast();
229
fusedNode->clear_attr();
230
tensorflow::AttrValue epsilon;
231
epsilon.set_f(epsMat.at<float>(0));
232
fusedNode->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("epsilon", epsilon));
233
}
234
};
235
236
class BatchNormNoGammaSubgraph : public Subgraph
237
{
238
public:
239
BatchNormNoGammaSubgraph()
240
{
241
int input = addNodeToMatch("");
242
int epsilon = addNodeToMatch("Const");
243
int moving_variance = addNodeToMatch("Const");
244
int moving_mean = addNodeToMatch("Const");
245
int beta = addNodeToMatch("Const");
246
int add = addNodeToMatch("Add", moving_variance, epsilon);
247
int rsqrt = addNodeToMatch("Rsqrt", add);
248
int mul = addNodeToMatch("Mul", input, rsqrt);
249
int mul_1 = addNodeToMatch("Mul", moving_mean, rsqrt);
250
int sub = addNodeToMatch("Sub", beta, mul_1);
251
addNodeToMatch("Add", mul, sub);
252
253
// There is a fake reference to beta that will be replaced to a new gamma tensor.
254
setFusedNode("FusedBatchNorm", input, beta, beta, moving_mean, moving_variance, epsilon);
255
}
256
257
virtual void finalize(tensorflow::GraphDef& net, tensorflow::NodeDef* fusedNode,
258
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE
259
{
260
Mat epsMat = getTensorContent(inputNodes.back()->attr().at("value").tensor());
261
CV_CheckEQ(epsMat.total(), (size_t)1, ""); CV_CheckTypeEQ(epsMat.type(), CV_32FC1, "");
262
263
fusedNode->mutable_input()->RemoveLast();
264
fusedNode->clear_attr();
265
tensorflow::AttrValue epsilon;
266
epsilon.set_f(epsMat.at<float>(0));
267
fusedNode->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("epsilon", epsilon));
268
269
tensorflow::NodeDef* gamma = net.add_node();
270
gamma->set_op("Const");
271
gamma->set_name(fusedNode->name() + "/gamma");
272
// Just put a single value to recognize this node as Const.
273
gamma->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("value", epsilon));
274
fusedNode->set_input(1, gamma->name());
275
}
276
};
277
278
// tf.contrib.layers.flatten
279
class FlattenSubgraph : public Subgraph
280
{
281
public:
282
FlattenSubgraph()
283
{
284
int input = addNodeToMatch("");
285
int shape = addNodeToMatch("Const");
286
int stack = addNodeToMatch("Const");
287
int stack_1 = addNodeToMatch("Const");
288
int stack_2 = addNodeToMatch("Const");
289
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
290
int shape_pack = addNodeToMatch("Const");
291
int pack = addNodeToMatch("Pack", strided_slice, shape_pack);
292
addNodeToMatch("Reshape", input, pack);
293
294
setFusedNode("Flatten", input);
295
}
296
};
297
298
// tf.contrib.layers.flatten in case of unknown batch size
299
class FlattenShapeSubgraph : public Subgraph
300
{
301
public:
302
FlattenShapeSubgraph()
303
{
304
int input = addNodeToMatch("");
305
int shape = addNodeToMatch("Shape", input);
306
int stack = addNodeToMatch("Const");
307
int stack_1 = addNodeToMatch("Const");
308
int stack_2 = addNodeToMatch("Const");
309
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
310
int shape_pack = addNodeToMatch("Const");
311
int pack = addNodeToMatch("Pack", strided_slice, shape_pack);
312
addNodeToMatch("Reshape", input, pack);
313
314
setFusedNode("Flatten", input);
315
}
316
};
317
318
// K.layers.Softmax
319
class SoftMaxKerasSubgraph : public Subgraph
320
{
321
public:
322
SoftMaxKerasSubgraph()
323
{
324
int input = addNodeToMatch("");
325
int maxReductionIndices = addNodeToMatch("Const");
326
int smMax = addNodeToMatch("Max", input, maxReductionIndices);
327
int smSub = addNodeToMatch("Sub", input, smMax);
328
int smExp = addNodeToMatch("Exp", smSub);
329
int sumReductionIndices = addNodeToMatch("Const");
330
int smSum = addNodeToMatch("Sum", smExp, sumReductionIndices);
331
addNodeToMatch("RealDiv", smExp, smSum);
332
333
setFusedNode("Softmax", input);
334
}
335
};
336
337
class ReLU6KerasSubgraph : public Subgraph
338
{
339
public:
340
ReLU6KerasSubgraph()
341
{
342
int input = addNodeToMatch("");
343
int relu = addNodeToMatch("Relu", input);
344
int maxValue = addNodeToMatch("Const");
345
int clipValue = addNodeToMatch("Const");
346
int minimum = addNodeToMatch("Minimum", relu, maxValue);
347
addNodeToMatch("Maximum", minimum, clipValue);
348
349
setFusedNode("Relu6", input);
350
}
351
352
virtual bool match(const tensorflow::GraphDef& net, int nodeId, std::vector<int>& matchedNodesIds) CV_OVERRIDE
353
{
354
if (!Subgraph::match(net, nodeId, matchedNodesIds))
355
return false;
356
Mat maxValue = getTensorContent(net.node(nodeId + 1).attr().at("value").tensor());
357
return maxValue.type() == CV_32FC1 && maxValue.total() == 1 && maxValue.at<float>(0) == 6;
358
}
359
};
360
361
// Keras' reshape stores output shape in separate Const nodes by one value.
362
// Need to merge them into a single Const node.
363
class ReshapeKerasSubgraph : public Subgraph
364
{
365
public:
366
ReshapeKerasSubgraph(int _numOutDims) : numOutDims(_numOutDims)
367
{
368
int input = addNodeToMatch("");
369
int shape = addNodeToMatch("Shape", input);
370
int stack = addNodeToMatch("Const");
371
int stack_1 = addNodeToMatch("Const");
372
int stack_2 = addNodeToMatch("Const");
373
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
374
375
std::vector<int> ids(1 + numOutDims);
376
ids[0] = strided_slice;
377
for (int i = 0; i < numOutDims; ++i)
378
ids[1 + i] = addNodeToMatch("Const");
379
int pack = addNodeToMatch("Pack", ids);
380
addNodeToMatch("Reshape", input, pack);
381
382
ids[0] = input;
383
setFusedNode("Reshape", ids);
384
}
385
386
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,
387
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE
388
{
389
std::vector<int> shape(numOutDims + 1); // batch size in Keras is implicit.
390
shape[0] = -1;
391
for (int i = 0; i < numOutDims; ++i)
392
{
393
shape[1 + i] = inputNodes[1 + i]->attr().at("value").tensor().int_val(0);
394
}
395
tensorflow::TensorProto* shapeTensor = inputNodes[1]->mutable_attr()->at("value").mutable_tensor();
396
fusedNode->mutable_input()->DeleteSubrange(2, numOutDims - 1);
397
398
shapeTensor->clear_int_val();
399
for (int i = 0; i < shape.size(); ++i)
400
{
401
shapeTensor->add_int_val(shape[i]);
402
}
403
}
404
405
private:
406
int numOutDims;
407
};
408
409
class L2NormalizeSubgraph : public Subgraph
410
{
411
public:
412
L2NormalizeSubgraph()
413
{
414
int input = addNodeToMatch("");
415
int square = addNodeToMatch("Square", input);
416
int reductionIndices = addNodeToMatch("Const");
417
int sum = addNodeToMatch("Sum", square, reductionIndices);
418
int y = addNodeToMatch("Const");
419
int maximum = addNodeToMatch("Maximum", sum, y);
420
int rsqrt = addNodeToMatch("Rsqrt", maximum);
421
addNodeToMatch("Mul", input, rsqrt);
422
setFusedNode("L2Normalize", input, reductionIndices);
423
}
424
};
425
426
class DeconvolutionValidKerasSubgraph : public Subgraph
427
{
428
public:
429
DeconvolutionValidKerasSubgraph()
430
{
431
int input = addNodeToMatch("");
432
int shape = addNodeToMatch("Shape", input);
433
int kernel = addNodeToMatch("Const");
434
435
int stack = addNodeToMatch("Const");
436
int stack_1 = addNodeToMatch("Const");
437
int stack_2 = addNodeToMatch("Const");
438
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
439
440
stack = addNodeToMatch("Const");
441
stack_1 = addNodeToMatch("Const");
442
stack_2 = addNodeToMatch("Const");
443
int strided_slice_1 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
444
445
stack = addNodeToMatch("Const");
446
stack_1 = addNodeToMatch("Const");
447
stack_2 = addNodeToMatch("Const");
448
int strided_slice_2 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
449
450
int mul = addNodeToMatch("Mul", strided_slice_1, addNodeToMatch("Const"));
451
int add = addNodeToMatch("Add", mul, addNodeToMatch("Const"));
452
453
int mul_1 = addNodeToMatch("Mul", strided_slice_2, addNodeToMatch("Const"));
454
int add_1 = addNodeToMatch("Add", mul_1, addNodeToMatch("Const"));
455
int pack = addNodeToMatch("Pack", strided_slice, add, add_1, addNodeToMatch("Const"));
456
addNodeToMatch("Conv2DBackpropInput", pack, kernel, input);
457
// Put any unused Const op to the first input.
458
setFusedNode("Conv2DBackpropInput", stack, kernel, input);
459
}
460
461
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,
462
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE
463
{
464
// Disable adjusted paddings (see Conv2DBackpropInput layer at tf_importer.cpp)
465
// adj_w = (outW - (pad == "SAME") ? 1 : kernelW) % strideX;
466
// adj_h = (outH - (pad == "SAME") ? 1 : kernelH) % strideY;
467
// Where outH and outW are 1st and 2nd dimensions (NHWC) or 2nd and third (NCHW).
468
std::string padMode = fusedNode->attr().at("padding").s();
469
CV_Assert(padMode == "VALID");
470
471
const tensorflow::TensorShapeProto& kernelShape =
472
inputNodes[1]->mutable_attr()->at("value").tensor().tensor_shape();
473
474
CV_Assert(kernelShape.dim_size() == 4);
475
const int kernelHeight = kernelShape.dim(0).size();
476
const int kernelWidth = kernelShape.dim(1).size();
477
478
tensorflow::TensorProto* outShape = inputNodes[0]->mutable_attr()->at("value").mutable_tensor();
479
outShape->clear_int_val();
480
outShape->add_int_val(-1);
481
outShape->add_int_val(kernelHeight);
482
outShape->add_int_val(kernelWidth);
483
outShape->add_int_val(-1);
484
}
485
};
486
487
class DeconvolutionSameKerasSubgraph : public Subgraph
488
{
489
public:
490
DeconvolutionSameKerasSubgraph()
491
{
492
int input = addNodeToMatch("");
493
int shape = addNodeToMatch("Shape", input);
494
int kernel = addNodeToMatch("Const");
495
496
int stack = addNodeToMatch("Const");
497
int stack_1 = addNodeToMatch("Const");
498
int stack_2 = addNodeToMatch("Const");
499
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
500
501
stack = addNodeToMatch("Const");
502
stack_1 = addNodeToMatch("Const");
503
stack_2 = addNodeToMatch("Const");
504
int strided_slice_1 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
505
506
stack = addNodeToMatch("Const");
507
stack_1 = addNodeToMatch("Const");
508
stack_2 = addNodeToMatch("Const");
509
int strided_slice_2 = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
510
511
int mul = addNodeToMatch("Mul", strided_slice_1, addNodeToMatch("Const"));
512
513
int mul_1 = addNodeToMatch("Mul", strided_slice_2, addNodeToMatch("Const"));
514
int pack = addNodeToMatch("Pack", strided_slice, mul, mul_1, addNodeToMatch("Const"));
515
addNodeToMatch("Conv2DBackpropInput", pack, kernel, input);
516
// Put any unused Const op to the first input.
517
setFusedNode("Conv2DBackpropInput", stack, kernel, input);
518
}
519
520
virtual void finalize(tensorflow::GraphDef&, tensorflow::NodeDef* fusedNode,
521
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE
522
{
523
// Disable adjusted paddings (see Conv2DBackpropInput layer at tf_importer.cpp)
524
// adj_w = (outW - (pad == "SAME") ? 1 : kernelW) % strideX;
525
// adj_h = (outH - (pad == "SAME") ? 1 : kernelH) % strideY;
526
// Where outH and outW are 1st and 2nd dimensions (NHWC) or 2nd and third (NCHW).
527
std::string padMode = fusedNode->attr().at("padding").s();
528
CV_Assert(padMode == "SAME");
529
530
const tensorflow::AttrValue_ListValue& strides = fusedNode->attr().at("strides").list();
531
CV_Assert(strides.i_size() == 4);
532
533
const int strideY = strides.i(1);
534
const int strideX = strides.i(2);
535
536
tensorflow::TensorProto* outShape = inputNodes[0]->mutable_attr()->at("value").mutable_tensor();
537
outShape->clear_int_val();
538
outShape->add_int_val(-1);
539
outShape->add_int_val(strideY);
540
outShape->add_int_val(strideX);
541
outShape->add_int_val(-1);
542
}
543
};
544
545
// In case of resizing by factor.
546
class ResizeBilinearSubgraph : public Subgraph
547
{
548
public:
549
ResizeBilinearSubgraph()
550
{
551
int input = addNodeToMatch("");
552
553
int shape = addNodeToMatch("Shape", input);
554
int stack = addNodeToMatch("Const");
555
int stack_1 = addNodeToMatch("Const");
556
int stack_2 = addNodeToMatch("Const");
557
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
558
int factorY = addNodeToMatch("Const");
559
int mul = addNodeToMatch("Mul", strided_slice, factorY);
560
561
shape = addNodeToMatch("Shape", input);
562
stack = addNodeToMatch("Const");
563
stack_1 = addNodeToMatch("Const");
564
stack_2 = addNodeToMatch("Const");
565
strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
566
int factorX = addNodeToMatch("Const");
567
int mul_1 = addNodeToMatch("Mul", strided_slice, factorX);
568
569
int pack = addNodeToMatch("Pack", mul, mul_1);
570
571
addNodeToMatch("ResizeBilinear", input, pack);
572
setFusedNode("ResizeBilinear", input, factorY, factorX);
573
}
574
};
575
576
// In case of resizing by factor.
577
class UpsamplingKerasSubgraph : public Subgraph
578
{
579
public:
580
UpsamplingKerasSubgraph()
581
{
582
int input = addNodeToMatch("");
583
int shape = addNodeToMatch("Shape", input);
584
int stack = addNodeToMatch("Const");
585
int stack_1 = addNodeToMatch("Const");
586
int stack_2 = addNodeToMatch("Const");
587
int strided_slice = addNodeToMatch("StridedSlice", shape, stack, stack_1, stack_2);
588
int factors = addNodeToMatch("Const");
589
int mul = addNodeToMatch("Mul", strided_slice, factors);
590
addNodeToMatch("ResizeNearestNeighbor", input, mul);
591
setFusedNode("ResizeNearestNeighbor", input, factors);
592
}
593
594
virtual void finalize(tensorflow::GraphDef& net, tensorflow::NodeDef* fusedNode,
595
std::vector<tensorflow::NodeDef*>& inputNodes) CV_OVERRIDE
596
{
597
Mat factorsMat = getTensorContent(inputNodes[1]->attr().at("value").tensor());
598
CV_CheckEQ(factorsMat.total(), (size_t)2, ""); CV_CheckTypeEQ(factorsMat.type(), CV_32SC1, "");
599
600
// Height scale factor
601
tensorflow::TensorProto* factorY = inputNodes[1]->mutable_attr()->at("value").mutable_tensor();
602
factorY->clear_int_val();
603
factorY->clear_tensor_content();
604
factorY->add_int_val(factorsMat.at<int>(0, 0));
605
606
// Width scale factor.
607
tensorflow::NodeDef* factorXNode = net.add_node();
608
factorXNode->set_op("Const");
609
factorXNode->set_name(fusedNode->name() + "/factor_y");
610
611
tensorflow::AttrValue factorX;
612
factorX.mutable_tensor()->set_dtype(tensorflow::DT_INT32);
613
factorX.mutable_tensor()->add_int_val(factorsMat.at<int>(0, 1));
614
factorXNode->mutable_attr()->insert(MapPair<std::string, tensorflow::AttrValue>("value", factorX));
615
616
fusedNode->add_input(factorXNode->name());
617
}
618
};
619
620
class ReshapeAsShapeSubgraph : public Subgraph
621
{
622
public:
623
ReshapeAsShapeSubgraph()
624
{
625
int input = addNodeToMatch("");
626
int shapeSrc = addNodeToMatch("");
627
int shape = addNodeToMatch("Shape", shapeSrc);
628
addNodeToMatch("Reshape", input, shape);
629
setFusedNode("Reshape", input, shapeSrc);
630
}
631
};
632
633
void simplifySubgraphs(tensorflow::GraphDef& net)
634
{
635
std::vector<Ptr<Subgraph> > subgraphs;
636
subgraphs.push_back(Ptr<Subgraph>(new BatchNormSubgraph()));
637
subgraphs.push_back(Ptr<Subgraph>(new BatchNormNoGammaSubgraph()));
638
subgraphs.push_back(Ptr<Subgraph>(new FlattenSubgraph()));
639
subgraphs.push_back(Ptr<Subgraph>(new FlattenShapeSubgraph()));
640
subgraphs.push_back(Ptr<Subgraph>(new SoftMaxKerasSubgraph()));
641
subgraphs.push_back(Ptr<Subgraph>(new ReLU6KerasSubgraph()));
642
subgraphs.push_back(Ptr<Subgraph>(new ReshapeKerasSubgraph(3)));
643
subgraphs.push_back(Ptr<Subgraph>(new L2NormalizeSubgraph()));
644
subgraphs.push_back(Ptr<Subgraph>(new DeconvolutionValidKerasSubgraph()));
645
subgraphs.push_back(Ptr<Subgraph>(new DeconvolutionSameKerasSubgraph()));
646
subgraphs.push_back(Ptr<Subgraph>(new ResizeBilinearSubgraph()));
647
subgraphs.push_back(Ptr<Subgraph>(new UpsamplingKerasSubgraph()));
648
subgraphs.push_back(Ptr<Subgraph>(new ReshapeAsShapeSubgraph()));
649
650
int numNodes = net.node_size();
651
std::vector<int> matchedNodesIds;
652
for (int i = 0; i < numNodes; ++i)
653
{
654
for (int j = 0; j < subgraphs.size(); ++j)
655
{
656
if (subgraphs[j]->match(net, i, matchedNodesIds))
657
{
658
subgraphs[j]->replace(net, matchedNodesIds);
659
numNodes -= matchedNodesIds.size() - 1; // #matchedNodes removed and one added.
660
break;
661
}
662
}
663
}
664
}
665
666
void RemoveIdentityOps(tensorflow::GraphDef& net)
667
{
668
typedef std::map<String, String> IdentityOpsMap;
669
IdentityOpsMap identity_ops;
670
671
std::vector<int> identity_ops_idx;
672
673
int layersCount = net.node_size();
674
for (int li = 0; li < layersCount; li++)
675
{
676
const tensorflow::NodeDef &layer = net.node(li);
677
String type = layer.op();
678
679
if (type == "Identity" || type == "Dropout") {
680
identity_ops_idx.push_back(li);
681
identity_ops[layer.name()] = layer.input(0);
682
}
683
}
684
685
for (int li = 0; li < layersCount; li++)
686
{
687
tensorflow::NodeDef* layer = net.mutable_node(li);
688
for (int input_id = 0; input_id < layer->input_size(); input_id++) {
689
String input_op_name = layer->input(input_id);
690
IdentityOpsMap::iterator it = identity_ops.find(input_op_name);
691
692
if (it != identity_ops.end()) {
693
layer->set_input(input_id, it->second);
694
}
695
}
696
}
697
698
std::sort(identity_ops_idx.begin(), identity_ops_idx.end());
699
700
int removed_nodes = 0;
701
for(size_t i = 0; i < identity_ops_idx.size(); i++) {
702
int start_id = identity_ops_idx[i] - removed_nodes;
703
net.mutable_node()->DeleteSubrange(start_id, 1);
704
removed_nodes++;
705
}
706
}
707
708
Mat getTensorContent(const tensorflow::TensorProto &tensor)
709
{
710
const std::string& content = tensor.tensor_content();
711
switch (tensor.dtype())
712
{
713
case tensorflow::DT_FLOAT:
714
{
715
if (!content.empty())
716
return Mat(1, content.size() / sizeof(float), CV_32FC1, (void*)content.c_str()).clone();
717
else
718
{
719
const RepeatedField<float>& field = tensor.float_val();
720
CV_Assert(!field.empty());
721
return Mat(1, field.size(), CV_32FC1, (void*)field.data()).clone();
722
}
723
}
724
case tensorflow::DT_DOUBLE:
725
{
726
if (!content.empty())
727
return Mat(1, content.size() / sizeof(double), CV_64FC1, (void*)content.c_str()).clone();
728
else
729
{
730
const RepeatedField<double>& field = tensor.double_val();
731
CV_Assert(!field.empty());
732
return Mat(1, field.size(), CV_64FC1, (void*)field.data()).clone();
733
}
734
}
735
case tensorflow::DT_INT32:
736
{
737
if (!content.empty())
738
return Mat(1, content.size() / sizeof(int32_t), CV_32SC1, (void*)content.c_str()).clone();
739
else
740
{
741
const RepeatedField<int32_t>& field = tensor.int_val();
742
CV_Assert(!field.empty());
743
return Mat(1, field.size(), CV_32SC1, (void*)field.data()).clone();
744
}
745
}
746
case tensorflow::DT_HALF:
747
{
748
Mat halfs;
749
if (!content.empty())
750
{
751
static const int kHalfSize = 2;
752
halfs = Mat(1, content.size() / kHalfSize, CV_16UC1, (void*)content.c_str());
753
}
754
else
755
{
756
const RepeatedField<int32_t>& field = tensor.half_val();
757
CV_Assert(!field.empty());
758
Mat ints(1, field.size(), CV_32SC1, (void*)field.data());
759
ints.convertTo(halfs, CV_16UC1);
760
}
761
// Reinterpret as a signed shorts just for a convertFp16 call.
762
Mat halfsSigned(halfs.size(), CV_16SC1, halfs.data);
763
Mat floats(halfs.size(), CV_32FC1);
764
convertFp16(halfsSigned, floats);
765
return floats;
766
}
767
case tensorflow::DT_QUINT8:
768
{
769
CV_Assert(!content.empty());
770
return Mat(1, content.size(), CV_8UC1, (void*)content.c_str()).clone();
771
}
772
default:
773
CV_Error(Error::StsError, "Tensor's data type is not supported");
774
break;
775
}
776
return Mat();
777
}
778
779
void releaseTensor(tensorflow::TensorProto* tensor)
780
{
781
if (!tensor->mutable_tensor_content()->empty())
782
{
783
delete tensor->release_tensor_content();
784
}
785
}
786
787
static void permute(google::protobuf::RepeatedPtrField<tensorflow::NodeDef>* data,
788
const std::vector<int>& indices)
789
{
790
const int num = data->size();
791
CV_Assert(num == indices.size());
792
793
std::vector<int> elemIdToPos(num);
794
std::vector<int> posToElemId(num);
795
for (int i = 0; i < num; ++i)
796
{
797
elemIdToPos[i] = i;
798
posToElemId[i] = i;
799
}
800
for (int i = 0; i < num; ++i)
801
{
802
int elemId = indices[i];
803
int pos = elemIdToPos[elemId];
804
if (pos != i)
805
{
806
data->SwapElements(i, pos);
807
const int swappedElemId = posToElemId[i];
808
elemIdToPos[elemId] = i;
809
elemIdToPos[swappedElemId] = pos;
810
811
posToElemId[i] = elemId;
812
posToElemId[pos] = swappedElemId;
813
}
814
}
815
}
816
817
// Is based on tensorflow::graph_transforms::SortByExecutionOrder
818
void sortByExecutionOrder(tensorflow::GraphDef& net)
819
{
820
// Maps node's name to index at net.node() list.
821
std::map<std::string, int> nodesMap;
822
std::map<std::string, int>::iterator nodesMapIt;
823
for (int i = 0; i < net.node_size(); ++i)
824
{
825
const tensorflow::NodeDef& node = net.node(i);
826
nodesMap.insert(std::make_pair(node.name(), i));
827
}
828
829
// Indices of nodes which use specific node as input.
830
std::vector<std::vector<int> > edges(nodesMap.size());
831
std::vector<int> numRefsToAdd(nodesMap.size(), 0);
832
std::vector<int> nodesToAdd;
833
for (int i = 0; i < net.node_size(); ++i)
834
{
835
const tensorflow::NodeDef& node = net.node(i);
836
for (int j = 0; j < node.input_size(); ++j)
837
{
838
std::string inpName = node.input(j);
839
inpName = inpName.substr(0, inpName.rfind(':'));
840
inpName = inpName.substr(inpName.find('^') + 1);
841
842
nodesMapIt = nodesMap.find(inpName);
843
CV_Assert(nodesMapIt != nodesMap.end());
844
edges[nodesMapIt->second].push_back(i);
845
}
846
if (node.input_size() == 0)
847
nodesToAdd.push_back(i);
848
else
849
{
850
if (node.op() == "Merge" || node.op() == "RefMerge")
851
{
852
int numControlEdges = 0;
853
for (int j = 0; j < node.input_size(); ++j)
854
numControlEdges += node.input(j)[0] == '^';
855
numRefsToAdd[i] = numControlEdges + 1;
856
}
857
else
858
numRefsToAdd[i] = node.input_size();
859
}
860
}
861
862
std::vector<int> permIds;
863
permIds.reserve(net.node_size());
864
while (!nodesToAdd.empty())
865
{
866
int nodeToAdd = nodesToAdd.back();
867
nodesToAdd.pop_back();
868
869
permIds.push_back(nodeToAdd);
870
// std::cout << net.node(nodeToAdd).name() << '\n';
871
872
for (int i = 0; i < edges[nodeToAdd].size(); ++i)
873
{
874
int consumerId = edges[nodeToAdd][i];
875
if (numRefsToAdd[consumerId] > 0)
876
{
877
if (numRefsToAdd[consumerId] == 1)
878
nodesToAdd.push_back(consumerId);
879
else
880
CV_Assert(numRefsToAdd[consumerId] >= 0);
881
numRefsToAdd[consumerId] -= 1;
882
}
883
}
884
}
885
CV_Assert(permIds.size() == net.node_size());
886
permute(net.mutable_node(), permIds);
887
}
888
889
CV__DNN_INLINE_NS_END
890
}} // namespace dnn, namespace cv
891
892
#endif // HAVE_PROTOBUF
893
894