You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

deconv2d_infer.c 3.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "nnacl/infer/deconv2d_infer.h"
  17. #include "nnacl/infer/infer_register.h"
  18. int Deconv2dInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
  19. OpParameter *parameter) {
  20. #ifdef Debug
  21. int check_ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
  22. if (check_ret != NNACL_OK) {
  23. return check_ret;
  24. }
  25. #endif
  26. const TensorC *input = inputs[0];
  27. const TensorC *weight = inputs[1];
  28. TensorC *output = outputs[0];
  29. output->format_ = input->format_;
  30. output->data_type_ = input->data_type_;
  31. ConvParameter *param = (ConvParameter *)parameter;
  32. if (param->group_ == 0) {
  33. param->group_ = weight->shape_[0];
  34. }
  35. if (!parameter->infer_flag_) {
  36. return NNACL_INFER_INVALID;
  37. }
  38. int32_t input_h = GetHeight(input);
  39. int32_t input_w = GetWidth(input);
  40. int32_t output_n = GetBatch(input);
  41. int32_t output_h = 0;
  42. int32_t output_w = 0;
  43. int32_t output_c = GetChannel(weight);
  44. if (param->group_ == GetChannel(input) && param->group_ == GetBatch(weight) && 1 == GetChannel(weight)) {
  45. output_c = GetBatch(weight); /* depthwise */
  46. }
  47. int kernel_w = param->kernel_w_;
  48. int kernel_h = param->kernel_h_;
  49. int stride_w = param->stride_w_;
  50. int stride_h = param->stride_h_;
  51. int dilate_w = param->dilation_w_;
  52. int dilate_h = param->dilation_h_;
  53. int pad_mode = param->pad_mode_;
  54. if (pad_mode == Pad_pad) {
  55. output_h = (input_h - 1) * stride_h + ((kernel_h - 1) * dilate_h + 1) - param->pad_u_ - param->pad_d_;
  56. output_w = (input_w - 1) * stride_w + ((kernel_w - 1) * dilate_w + 1) - param->pad_l_ - param->pad_r_;
  57. } else if (pad_mode == Pad_same) {
  58. output_h = input_h * stride_h;
  59. output_w = input_w * stride_w;
  60. } else if (pad_mode == Pad_valid) {
  61. output_h = (input_h - 1) * stride_h + kernel_h;
  62. output_w = (input_w - 1) * stride_w + kernel_w;
  63. } else {
  64. return NNACL_ERR;
  65. }
  66. output_h += param->output_padding_h_;
  67. output_w += param->output_padding_w_;
  68. output->shape_size_ = 4;
  69. output->shape_[0] = output_n;
  70. output->shape_[1] = output_h;
  71. output->shape_[2] = output_w;
  72. output->shape_[3] = output_c;
  73. if (pad_mode == Pad_same) {
  74. param->pad_u_ = ((input_h - 1) * stride_h + (kernel_h - 1) * dilate_h + 1 - output_h) / 2;
  75. param->pad_l_ = ((input_w - 1) * stride_w + (kernel_w - 1) * dilate_w + 1 - output_w) / 2;
  76. } else if (pad_mode == Pad_valid) {
  77. param->pad_u_ = 0;
  78. param->pad_l_ = 0;
  79. }
  80. const int *in_shape = input->shape_;
  81. param->input_batch_ = in_shape[0];
  82. param->input_h_ = in_shape[1];
  83. param->input_w_ = in_shape[2];
  84. param->input_channel_ = in_shape[3];
  85. param->output_batch_ = output_n;
  86. param->output_h_ = output_h;
  87. param->output_w_ = output_w;
  88. param->output_channel_ = output_c;
  89. return NNACL_OK;
  90. }
  91. REG_INFER(Conv2dTranspose, PrimType_Conv2dTransposeFusion, Deconv2dInferShape)