MCPcopy Create free account
hub / github.com/alibaba/MNN / onComputeSize

Method onComputeSize

source/shape/ShapeConvolution.cpp:25–117  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23 return layer;
24 }
25 virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs,
26 const std::vector<Tensor*>& outputs) const override {
27 MNN_ASSERT(inputs.size() >= 1);
28 MNN_ASSERT(1 == outputs.size());
29 const Convolution2DCommon* layer = loadCommon(op);
30 int kX = layer->kernelX();
31 int kY = layer->kernelY();
32 auto outputCount = layer->outputCount();
33 if (inputs.size() > 1 && outputCount == 0) {
34 // From TF's multi input convolution
35 outputCount = inputs[1]->length(0);
36 kX = inputs[1]->length(3);
37 kY = inputs[1]->length(2);
38 }
39 int kernel_width = layer->dilateX() * (kX - 1) + 1;
40 int kernel_height = layer->dilateY() * (kY - 1) + 1;
41
42 int output_width = 1;
43 int output_height = 1;
44
45 auto input = inputs[0];
46 if (input->dimensions() <= 1) {
47 // Convolution is not valid for dimension <= 1
48 return false;
49 }
50
51 auto inputCount = layer->inputCount();
52 bool depthwiseMatch =
53 inputCount == layer->outputCount() &&
54 inputCount == layer->group() &&
55 inputCount == input->channel();
56 int commonChannelMatch =
57 inputCount == inputs[0]->channel() || // real relationship in express
58 (inputCount * layer->group() == input->channel()); // standard definition of group convolution
59 bool valid = inputCount == 0 || depthwiseMatch || commonChannelMatch;
60
61 // For Tensorflow Group Convolution, the inputCount is the size of filter's input count
62 if (inputs.size() == 1 && !valid && OpType_Convolution == op->type()) {
63 input->printShape();
64 MNN_ERROR(
65 "Error for compute convolution shape, inputCount:%d, outputCount:%d, KH:%d, KW:%d, group:%d\ninputChannel: %d, batch:%d, width:%d, height:%d. "
66 "Input data channel may be mismatch with filter channel count\n",
67 layer->inputCount(), outputCount, kY, kX, layer->group(),
68 input->channel(), input->batch(), input->width(), input->height());
69 return false;
70 }
71
72 if (layer->padMode() == PadMode_SAME) {
73 // Tensorflow padding mode SAME
74 output_width = ceil((float)input->width() / (float)layer->strideX());
75 output_height = ceil((float)input->height() / (float)layer->strideY());
76 } else if (layer->padMode() == PadMode_VALID) {
77 // Tensorflow padding mode VALID
78 output_width = ceil((float)(input->width() - kernel_width + 1) / (float)layer->strideX());
79 output_height = ceil((float)(input->height() - kernel_height + 1) / (float)layer->strideY());
80 } else {
81 // Pad_Caffe means User setted padding
82 if (nullptr != layer->pads()) {

Callers

nothing calls this directly

Calls 12

printShapeMethod · 0.80
setTypeMethod · 0.80
ceilFunction · 0.50
sizeMethod · 0.45
lengthMethod · 0.45
dimensionsMethod · 0.45
typeMethod · 0.45
widthMethod · 0.45
heightMethod · 0.45
dataMethod · 0.45
bufferMethod · 0.45
getTypeMethod · 0.45

Tested by

no test coverage detected