| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | //  ShapeInterp.cpp
 | 
					
						
							|  |  |  | //  MNN
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | //  Created by MNN on 2019/01/10.
 | 
					
						
							|  |  |  | //  Copyright © 2018, Alibaba Group Holding Limited
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  | #include "shape/SizeComputer.hpp"
 | 
					
						
							| 
									
										
										
										
											2019-12-27 22:16:57 +08:00
										 |  |  | #include "core/Macro.h"
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace MNN { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Size Computer
 | 
					
						
							|  |  |  | class InterpComputer : public SizeComputer { | 
					
						
							|  |  |  |     virtual bool onComputeSize(const MNN::Op* op, const std::vector<Tensor*>& inputs, | 
					
						
							|  |  |  |                                const std::vector<Tensor*>& outputs) const override { | 
					
						
							|  |  |  |         MNN_ASSERT(1 == inputs.size() || 2 == inputs.size()); | 
					
						
							|  |  |  |         MNN_ASSERT(1 == outputs.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         auto& input         = inputs[0]->buffer(); // input tensor(data)
 | 
					
						
							|  |  |  |         auto& output        = outputs[0]->buffer(); | 
					
						
							|  |  |  |         int w               = 0; | 
					
						
							|  |  |  |         int h               = 0; | 
					
						
							|  |  |  |         const int inputSize = (int)inputs.size(); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         auto iw = inputs[0]->width(); | 
					
						
							|  |  |  |         auto ih = inputs[0]->height(); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         // copy dims
 | 
					
						
							|  |  |  |         memcpy(output.dim, input.dim, sizeof(halide_dimension_t) * input.dimensions); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         outputs[0]->buffer().dimensions = inputs[0]->dimensions(); | 
					
						
							|  |  |  |         outputs[0]->buffer().type = inputs[0]->getType(); | 
					
						
							|  |  |  |         auto format = TensorUtils::getDescribe(inputs[0])->dimensionFormat; | 
					
						
							|  |  |  |         TensorUtils::getDescribe(outputs[0])->dimensionFormat = format; | 
					
						
							|  |  |  |         if (2 == inputSize) { | 
					
						
							|  |  |  |             auto shape = inputs[1]; // input shape(shape)
 | 
					
						
							|  |  |  |             if(shape->length(0) == input.dimensions) { | 
					
						
							|  |  |  |                 // For Onnx's Resize
 | 
					
						
							| 
									
										
										
										
											2021-09-03 19:22:30 +08:00
										 |  |  |                 // Don't support batch / channel resize
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |                 for (int i=0; i<2; ++i) { | 
					
						
							|  |  |  |                     output.dim[i].extent = input.dim[i].extent; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-09-03 19:22:30 +08:00
										 |  |  |                 if (shape->getType().code == halide_type_int) { | 
					
						
							|  |  |  |                     // Width / Height
 | 
					
						
							|  |  |  |                     auto shapePtr = shape->host<int>(); | 
					
						
							|  |  |  |                     for (int i=2; i<input.dimensions; ++i) { | 
					
						
							|  |  |  |                         output.dim[i].extent = shapePtr[i]; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     // Scale
 | 
					
						
							|  |  |  |                     auto scalePtr = shape->host<float>(); | 
					
						
							|  |  |  |                     for (int i=2; i<input.dimensions; ++i) { | 
					
						
							|  |  |  |                         output.dim[i].extent = (scalePtr[i] * (float)input.dim[i].extent); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         if (1 == inputSize) { | 
					
						
							| 
									
										
										
										
											2022-09-30 10:02:52 +08:00
										 |  |  |             // For old mnn model from onnx
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |             auto interp = op->main_as_Interp(); | 
					
						
							|  |  |  |             // get output dims
 | 
					
						
							|  |  |  |             w = interp->outputWidth(); | 
					
						
							|  |  |  |             h = interp->outputHeight(); | 
					
						
							|  |  |  |             if (w == 0 || h == 0) { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |                 w = iw * interp->widthScale(); | 
					
						
							|  |  |  |                 h = ih * interp->heightScale(); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2022-09-30 10:02:52 +08:00
										 |  |  |             // For mnn model from tensorflow
 | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |             auto shape = inputs[1]; // input shape(shape)
 | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |             // Tensorflow's interp: h, w
 | 
					
						
							|  |  |  |             if(2 != shape->buffer().dim[0].extent) { | 
					
						
							|  |  |  |                 MNN_ERROR("Tensorflow's interp's shape should be length two\n"); | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-03-17 15:04:56 +08:00
										 |  |  |             if (shape->getType().code == halide_type_float) { | 
					
						
							|  |  |  |                 const float *shapeData = shape->host<float>(); | 
					
						
							|  |  |  |                 w                      = shapeData[1]; | 
					
						
							|  |  |  |                 h                      = shapeData[0]; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 const int32_t *shapeData = shape->host<int32_t>(); | 
					
						
							|  |  |  |                 w                        = shapeData[1]; | 
					
						
							|  |  |  |                 h                        = shapeData[0]; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-31 18:46:38 +08:00
										 |  |  |         if (0 == w && 0 == h) { | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-05 16:41:56 +08:00
										 |  |  |         if (MNN_DATA_FORMAT_NHWC == format) { | 
					
						
							|  |  |  |             output.dim[2].extent     = w; | 
					
						
							|  |  |  |             output.dim[1].extent     = h; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             output.dim[3].extent     = w; | 
					
						
							|  |  |  |             output.dim[2].extent     = h; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     virtual float onComputeFlops(const MNN::Op* op, const std::vector<Tensor*>& inputs, | 
					
						
							|  |  |  |                                  const std::vector<Tensor*>& outputs) const override { | 
					
						
							|  |  |  |         auto elementInM = (float)outputs[0]->elementSize() / 1024.0f / 1024.0f; | 
					
						
							|  |  |  |         auto interp     = op->main_as_Interp(); | 
					
						
							|  |  |  |         auto unit       = 0; | 
					
						
							| 
									
										
										
										
											2022-09-30 10:02:52 +08:00
										 |  |  |         int dimensions = inputs[0]->dimensions(); | 
					
						
							|  |  |  |         int interpDims = dimensions - 2; | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |         switch (interp->resizeType()) { | 
					
						
							|  |  |  |             case 1: | 
					
						
							| 
									
										
										
										
											2022-09-30 10:02:52 +08:00
										 |  |  |             case 4: | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |                 unit = 1; | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case 2: | 
					
						
							| 
									
										
										
										
											2022-09-30 10:02:52 +08:00
										 |  |  |                 unit = (1 << interpDims); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             case 3: | 
					
						
							| 
									
										
										
										
											2022-09-30 10:02:52 +08:00
										 |  |  |                 unit = (4 << interpDims); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return unit * elementInM; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-22 20:13:46 +08:00
										 |  |  | REGISTER_SHAPE_INPUTS(InterpComputer, OpType_Interp, {1}); | 
					
						
							| 
									
										
										
										
											2019-04-17 10:49:11 +08:00
										 |  |  | } // namespace MNN
 |