forked from alibaba/MNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMomentsTf.cpp
More file actions
72 lines (60 loc) · 2.31 KB
/
Copy pathMomentsTf.cpp
File metadata and controls
72 lines (60 loc) · 2.31 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
//
// MomentsTf.cpp
// MNNConvertor
//
// Created by MNN on 2019/01/31.
// Copyright © 2018, Alibaba Group Holding Limited
//
#include <string.h>
#include "TfUtils.hpp"
#include "tfOpConverter.hpp"
#include "graph.pb.h"
DECLARE_OP_CONVERTER(MomentsTf);
MNN::OpType MomentsTf::opType() {
return MNN::OpType_Moments;
}
MNN::OpParameter MomentsTf::type() {
return MNN::OpParameter_MomentsParam;
}
void MomentsTf::run(MNN::OpT *dstOp, TmpNode *srcNode, TmpGraph *tempGraph) {
auto momentsParam = new MNN::MomentsParamT;
tensorflow::AttrValue value;
momentsParam->dType = MNN::DataType_DT_FLOAT;
if (find_attr_value(srcNode->tfNode, "T", value)) {
momentsParam->dType = static_cast<MNN::DataType>(value.type());
}
momentsParam->keepDims = false;
if (find_attr_value(srcNode->tfNode, "keep_dims", value)) {
momentsParam->keepDims = value.b();
}
auto dimNode = tempGraph->_getTmpNode(srcNode->inEdges[1]);
DCHECK(dimNode->opType == "Const") << "Moments should have one Const dim node " << srcNode->opName;
if (find_attr_value(dimNode->tfNode, "value", value)) {
const tensorflow::TensorProto &momentsIndices = value.tensor();
const tensorflow::TensorShapeProto &momentsShape = momentsIndices.tensor_shape();
int dimSize = 1;
if (momentsShape.dim_size() > 0) {
dimSize = momentsShape.dim(0).size();
}
momentsParam->dim.resize(dimSize);
if (momentsIndices.int_val_size() > 0) {
for (int i = 0; i < dimSize; ++i) {
momentsParam->dim[i] = momentsIndices.int_val(i);
}
} else {
DCHECK((MNN::DataType)momentsIndices.dtype() == MNN::DataType_DT_INT32);
DCHECK(momentsIndices.tensor_content().size() > 0);
auto dimData = reinterpret_cast<const int *>(momentsIndices.tensor_content().data());
for (int i = 0; i < dimSize; i++) {
momentsParam->dim[i] = dimData[i];
}
}
}
// default set the Moments'layout to NCHW, so change the dimension
const int axisMap[4] = {0, 2, 3, 1};
for (int i = 0; i < momentsParam->dim.size(); ++i) {
momentsParam->dim[i] = axisMap[momentsParam->dim[i]];
}
dstOp->main.value = momentsParam;
}
REGISTER_CONVERTER(MomentsTf, Moments);