• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_CROP_AND_RESIZE_CPU_KERNEL_H_
18 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_CROP_AND_RESIZE_CPU_KERNEL_H_
19 
20 #include <vector>
21 #include <string>
22 #include <algorithm>
23 
24 #include "backend/kernel_compiler/cpu/cpu_kernel.h"
25 #include "backend/kernel_compiler/cpu/cpu_kernel_factory.h"
26 
27 namespace mindspore {
28 namespace kernel {
29 constexpr int BILINEAR = 1;
30 constexpr int NEAREST = 2;
31 constexpr int BILINEAR_V2 = 3;
32 constexpr size_t INPUT_NUM = 4;
33 constexpr size_t OUTPUT_NUM = 1;
34 constexpr size_t BOX_RANK = 2;
35 constexpr size_t CROP_SIZE_LEN = 2;
36 constexpr size_t IMAGE_DIM = 4;
37 constexpr size_t IMAGE = 0;
38 constexpr size_t BOXES = 1;
39 constexpr size_t BOX_INDEX = 2;
40 constexpr size_t CROP_SIZE = 3;
41 constexpr size_t IMAGE_HEIGHT = 1;
42 constexpr size_t IMAGE_WEIGHT = 2;
43 template <typename T>
44 class CropAndResizeCPUKernel : public CPUKernel {
45  public:
46   CropAndResizeCPUKernel() = default;
47   ~CropAndResizeCPUKernel() override = default;
48 
49   void InitKernel(const CNodePtr &kernel_node) override;
50 
51   bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &,
52               const std::vector<AddressPtr> &outputs) override;
53 
54  private:
55   int method_{1};
56   float extrapolation_value_{0.0};
57   int output_size_{0};
58   int input_height_{0};
59   int input_width_{0};
60   int final_height_{0};
61   int final_width_{0};
62   int channel_{0};
63 };
64 
65 MS_REG_CPU_KERNEL_T(CropAndResize,
66                     KernelAttr()
67                       .AddInputAttr(kNumberTypeFloat16)
68                       .AddInputAttr(kNumberTypeFloat32)
69                       .AddInputAttr(kNumberTypeInt32)
70                       .AddInputAttr(kNumberTypeInt32)
71                       .AddOutputAttr(kNumberTypeFloat32),
72                     CropAndResizeCPUKernel, float16);
73 
74 MS_REG_CPU_KERNEL_T(CropAndResize,
75                     KernelAttr()
76                       .AddInputAttr(kNumberTypeFloat16)
77                       .AddInputAttr(kNumberTypeFloat32)
78                       .AddInputAttr(kNumberTypeInt32)
79                       .AddInputAttr(kNumberTypeInt64)
80                       .AddOutputAttr(kNumberTypeFloat32),
81                     CropAndResizeCPUKernel, float16);
82 
83 MS_REG_CPU_KERNEL_T(CropAndResize,
84                     KernelAttr()
85                       .AddInputAttr(kNumberTypeFloat32)
86                       .AddInputAttr(kNumberTypeFloat32)
87                       .AddInputAttr(kNumberTypeInt32)
88                       .AddInputAttr(kNumberTypeInt32)
89                       .AddOutputAttr(kNumberTypeFloat32),
90                     CropAndResizeCPUKernel, float);
91 
92 MS_REG_CPU_KERNEL_T(CropAndResize,
93                     KernelAttr()
94                       .AddInputAttr(kNumberTypeFloat32)
95                       .AddInputAttr(kNumberTypeFloat32)
96                       .AddInputAttr(kNumberTypeInt32)
97                       .AddInputAttr(kNumberTypeInt64)
98                       .AddOutputAttr(kNumberTypeFloat32),
99                     CropAndResizeCPUKernel, float);
100 
101 MS_REG_CPU_KERNEL_T(CropAndResize,
102                     KernelAttr()
103                       .AddInputAttr(kNumberTypeFloat64)
104                       .AddInputAttr(kNumberTypeFloat32)
105                       .AddInputAttr(kNumberTypeInt32)
106                       .AddInputAttr(kNumberTypeInt32)
107                       .AddOutputAttr(kNumberTypeFloat32),
108                     CropAndResizeCPUKernel, double);
109 
110 MS_REG_CPU_KERNEL_T(CropAndResize,
111                     KernelAttr()
112                       .AddInputAttr(kNumberTypeFloat64)
113                       .AddInputAttr(kNumberTypeFloat32)
114                       .AddInputAttr(kNumberTypeInt32)
115                       .AddInputAttr(kNumberTypeInt64)
116                       .AddOutputAttr(kNumberTypeFloat32),
117                     CropAndResizeCPUKernel, double);
118 
119 MS_REG_CPU_KERNEL_T(CropAndResize,
120                     KernelAttr()
121                       .AddInputAttr(kNumberTypeInt8)
122                       .AddInputAttr(kNumberTypeFloat32)
123                       .AddInputAttr(kNumberTypeInt32)
124                       .AddInputAttr(kNumberTypeInt32)
125                       .AddOutputAttr(kNumberTypeFloat32),
126                     CropAndResizeCPUKernel, int8_t);
127 
128 MS_REG_CPU_KERNEL_T(CropAndResize,
129                     KernelAttr()
130                       .AddInputAttr(kNumberTypeInt8)
131                       .AddInputAttr(kNumberTypeFloat32)
132                       .AddInputAttr(kNumberTypeInt32)
133                       .AddInputAttr(kNumberTypeInt64)
134                       .AddOutputAttr(kNumberTypeFloat32),
135                     CropAndResizeCPUKernel, int8_t);
136 
137 MS_REG_CPU_KERNEL_T(CropAndResize,
138                     KernelAttr()
139                       .AddInputAttr(kNumberTypeInt16)
140                       .AddInputAttr(kNumberTypeFloat32)
141                       .AddInputAttr(kNumberTypeInt32)
142                       .AddInputAttr(kNumberTypeInt32)
143                       .AddOutputAttr(kNumberTypeFloat32),
144                     CropAndResizeCPUKernel, int16_t);
145 
146 MS_REG_CPU_KERNEL_T(CropAndResize,
147                     KernelAttr()
148                       .AddInputAttr(kNumberTypeInt16)
149                       .AddInputAttr(kNumberTypeFloat32)
150                       .AddInputAttr(kNumberTypeInt32)
151                       .AddInputAttr(kNumberTypeInt64)
152                       .AddOutputAttr(kNumberTypeFloat32),
153                     CropAndResizeCPUKernel, int16_t);
154 
155 MS_REG_CPU_KERNEL_T(CropAndResize,
156                     KernelAttr()
157                       .AddInputAttr(kNumberTypeInt32)
158                       .AddInputAttr(kNumberTypeFloat32)
159                       .AddInputAttr(kNumberTypeInt32)
160                       .AddInputAttr(kNumberTypeInt32)
161                       .AddOutputAttr(kNumberTypeFloat32),
162                     CropAndResizeCPUKernel, int32_t);
163 
164 MS_REG_CPU_KERNEL_T(CropAndResize,
165                     KernelAttr()
166                       .AddInputAttr(kNumberTypeInt32)
167                       .AddInputAttr(kNumberTypeFloat32)
168                       .AddInputAttr(kNumberTypeInt32)
169                       .AddInputAttr(kNumberTypeInt64)
170                       .AddOutputAttr(kNumberTypeFloat32),
171                     CropAndResizeCPUKernel, int32_t);
172 
173 MS_REG_CPU_KERNEL_T(CropAndResize,
174                     KernelAttr()
175                       .AddInputAttr(kNumberTypeInt64)
176                       .AddInputAttr(kNumberTypeFloat32)
177                       .AddInputAttr(kNumberTypeInt32)
178                       .AddInputAttr(kNumberTypeInt32)
179                       .AddOutputAttr(kNumberTypeFloat32),
180                     CropAndResizeCPUKernel, int64_t);
181 
182 MS_REG_CPU_KERNEL_T(CropAndResize,
183                     KernelAttr()
184                       .AddInputAttr(kNumberTypeInt64)
185                       .AddInputAttr(kNumberTypeFloat32)
186                       .AddInputAttr(kNumberTypeInt32)
187                       .AddInputAttr(kNumberTypeInt64)
188                       .AddOutputAttr(kNumberTypeFloat32),
189                     CropAndResizeCPUKernel, int64_t);
190 
191 MS_REG_CPU_KERNEL_T(CropAndResize,
192                     KernelAttr()
193                       .AddInputAttr(kNumberTypeUInt8)
194                       .AddInputAttr(kNumberTypeFloat32)
195                       .AddInputAttr(kNumberTypeInt32)
196                       .AddInputAttr(kNumberTypeInt32)
197                       .AddOutputAttr(kNumberTypeFloat32),
198                     CropAndResizeCPUKernel, uint8_t);
199 
200 MS_REG_CPU_KERNEL_T(CropAndResize,
201                     KernelAttr()
202                       .AddInputAttr(kNumberTypeUInt8)
203                       .AddInputAttr(kNumberTypeFloat32)
204                       .AddInputAttr(kNumberTypeInt32)
205                       .AddInputAttr(kNumberTypeInt64)
206                       .AddOutputAttr(kNumberTypeFloat32),
207                     CropAndResizeCPUKernel, uint8_t);
208 
209 MS_REG_CPU_KERNEL_T(CropAndResize,
210                     KernelAttr()
211                       .AddInputAttr(kNumberTypeUInt16)
212                       .AddInputAttr(kNumberTypeFloat32)
213                       .AddInputAttr(kNumberTypeInt32)
214                       .AddInputAttr(kNumberTypeInt32)
215                       .AddOutputAttr(kNumberTypeFloat32),
216                     CropAndResizeCPUKernel, uint16_t);
217 
218 MS_REG_CPU_KERNEL_T(CropAndResize,
219                     KernelAttr()
220                       .AddInputAttr(kNumberTypeUInt16)
221                       .AddInputAttr(kNumberTypeFloat32)
222                       .AddInputAttr(kNumberTypeInt32)
223                       .AddInputAttr(kNumberTypeInt64)
224                       .AddOutputAttr(kNumberTypeFloat32),
225                     CropAndResizeCPUKernel, uint16_t);
226 }  // namespace kernel
227 }  // namespace mindspore
228 
229 #endif  // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_CROP_AND_RESIZE_CPU_KERNEL_H_
230