| 21 | virtual ~InterpGradTest() = default; |
| 22 | |
| 23 | virtual bool run(int precision) { |
| 24 | std::vector<int> shape = {2, 3, 2, 3}; |
| 25 | const int len = shape[0] * shape[1] * shape[2] * shape[3]; |
| 26 | auto input = _Input(shape, NCHW); |
| 27 | const float inpudata[] = { 0.5500, 0.6721, 0.4343, 0.8518, 0.9456, 0.6444, 0.5927, 0.4439, 0.9329, |
| 28 | 0.1434, 0.6933, 0.0180, 0.3173, 0.2903, 0.4159, 0.8706, 0.1812, 0.5890, |
| 29 | 0.3834, 0.0335, 0.9997, 0.7504, 0.5379, 0.9836, 0.3202, 0.4824, 0.9982, |
| 30 | 0.8029, 0.2889, 0.8386, 0.2282, 0.6912, 0.2678, 0.9031, 0.7055, 0.9389}; |
| 31 | auto inputPtr = input->writeMap<float>(); |
| 32 | memcpy(inputPtr, inpudata, len * sizeof(float)); |
| 33 | |
| 34 | float wScale = 2.5; |
| 35 | float hScale = 2.5; |
| 36 | int outputW = int(floor(wScale * 3)); |
| 37 | int outputH = int(floor(hScale * 2)); |
| 38 | |
| 39 | int mode = 1; // 1:near 2: bilinear 3: cubic 4: nearest_round |
| 40 | bool alignCorners = false; |
| 41 | float scales[] = {1.0, 1.0, hScale, wScale}; |
| 42 | auto scaleVar = _Const((void*)scales, {4}, NCHW); |
| 43 | |
| 44 | auto output = _Interp({input, scaleVar}, wScale, hScale, outputW, outputH, mode, alignCorners); |
| 45 | auto outputPtr = output->readMap<float>(); |
| 46 | |
| 47 | const int len2 = shape[0] * shape[1] * outputH * outputW; |
| 48 | |
| 49 | std::vector<float> outputTorch = { 0.5500, 0.5500, 0.5500, 0.6721, 0.6721, 0.4343, 0.4343, 0.5500, 0.5500, |
| 50 | 0.5500, 0.6721, 0.6721, 0.4343, 0.4343, 0.5500, 0.5500, 0.5500, 0.6721, |
| 51 | 0.6721, 0.4343, 0.4343, 0.8518, 0.8518, 0.8518, 0.9456, 0.9456, 0.6444, |
| 52 | 0.6444, 0.8518, 0.8518, 0.8518, 0.9456, 0.9456, 0.6444, 0.6444, 0.5927, |
| 53 | 0.5927, 0.5927, 0.4439, 0.4439, 0.9329, 0.9329, 0.5927, 0.5927, 0.5927, |
| 54 | 0.4439, 0.4439, 0.9329, 0.9329, 0.5927, 0.5927, 0.5927, 0.4439, 0.4439, |
| 55 | 0.9329, 0.9329, 0.1434, 0.1434, 0.1434, 0.6933, 0.6933, 0.0180, 0.0180, |
| 56 | 0.1434, 0.1434, 0.1434, 0.6933, 0.6933, 0.0180, 0.0180, 0.3173, 0.3173, |
| 57 | 0.3173, 0.2903, 0.2903, 0.4159, 0.4159, 0.3173, 0.3173, 0.3173, 0.2903, |
| 58 | 0.2903, 0.4159, 0.4159, 0.3173, 0.3173, 0.3173, 0.2903, 0.2903, 0.4159, |
| 59 | 0.4159, 0.8706, 0.8706, 0.8706, 0.1812, 0.1812, 0.5890, 0.5890, 0.8706, |
| 60 | 0.8706, 0.8706, 0.1812, 0.1812, 0.5890, 0.5890, 0.3834, 0.3834, 0.3834, |
| 61 | 0.0335, 0.0335, 0.9997, 0.9997, 0.3834, 0.3834, 0.3834, 0.0335, 0.0335, |
| 62 | 0.9997, 0.9997, 0.3834, 0.3834, 0.3834, 0.0335, 0.0335, 0.9997, 0.9997, |
| 63 | 0.7504, 0.7504, 0.7504, 0.5379, 0.5379, 0.9836, 0.9836, 0.7504, 0.7504, |
| 64 | 0.7504, 0.5379, 0.5379, 0.9836, 0.9836, 0.3202, 0.3202, 0.3202, 0.4824, |
| 65 | 0.4824, 0.9982, 0.9982, 0.3202, 0.3202, 0.3202, 0.4824, 0.4824, 0.9982, |
| 66 | 0.9982, 0.3202, 0.3202, 0.3202, 0.4824, 0.4824, 0.9982, 0.9982, 0.8029, |
| 67 | 0.8029, 0.8029, 0.2889, 0.2889, 0.8386, 0.8386, 0.8029, 0.8029, 0.8029, |
| 68 | 0.2889, 0.2889, 0.8386, 0.8386, 0.2282, 0.2282, 0.2282, 0.6912, 0.6912, |
| 69 | 0.2678, 0.2678, 0.2282, 0.2282, 0.2282, 0.6912, 0.6912, 0.2678, 0.2678, |
| 70 | 0.2282, 0.2282, 0.2282, 0.6912, 0.6912, 0.2678, 0.2678, 0.9031, 0.9031, |
| 71 | 0.9031, 0.7055, 0.7055, 0.9389, 0.9389, 0.9031, 0.9031, 0.9031, 0.7055, |
| 72 | 0.7055, 0.9389, 0.9389}; |
| 73 | |
| 74 | for (int i = 0, count = 0; i < len2; ++i) { |
| 75 | auto diff = ::fabsf(outputPtr[i] - outputTorch[i]); |
| 76 | if (diff > 0.0001) { |
| 77 | count++; |
| 78 | MNN_ERROR("%d: %s type 1 output test failed, expected: %f, but got: %f!\n", count, name, outputTorch[i], outputPtr[i]); |
| 79 | return false; |
| 80 | } else { |