1 /**
2 * Copyright 2019-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 "src/ops/populate/populate_register.h"
17 #include "nnacl/prior_box_parameter.h"
18 using mindspore::schema::PrimitiveType_PriorBox;
19
20 namespace mindspore {
21 namespace lite {
PopulatePriorBoxParameter(const void * prim)22 OpParameter *PopulatePriorBoxParameter(const void *prim) {
23 MS_CHECK_TRUE_RET(prim != nullptr, nullptr);
24 auto primitive = static_cast<const schema::Primitive *>(prim);
25 auto value = primitive->value_as_PriorBox();
26 MS_CHECK_TRUE_MSG(value != nullptr, nullptr, "value is nullptr");
27
28 auto *param = reinterpret_cast<PriorBoxParameter *>(malloc(sizeof(PriorBoxParameter)));
29 if (param == nullptr) {
30 MS_LOG(ERROR) << "malloc PriorBoxParameter failed.";
31 return nullptr;
32 }
33 memset(param, 0, sizeof(PriorBoxParameter));
34
35 param->op_parameter_.type_ = primitive->value_type();
36 auto min_sizes = value->min_sizes();
37 if (min_sizes == nullptr) {
38 MS_LOG(ERROR) << "min_sizes is nullptr";
39 free(param);
40 return nullptr;
41 }
42 if (min_sizes->size() > MAX_SHAPE_SIZE) {
43 MS_LOG(ERROR) << "PriorBox min_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " << min_sizes->size();
44 free(param);
45 return nullptr;
46 }
47 param->min_sizes_size = static_cast<int32_t>(min_sizes->size());
48 memcpy(param->min_sizes, min_sizes->data(), min_sizes->size() * sizeof(int32_t));
49
50 auto max_sizes = value->max_sizes();
51 if (max_sizes == nullptr) {
52 MS_LOG(ERROR) << "max_sizes is nullptr";
53 free(param);
54 return nullptr;
55 }
56 if (max_sizes->size() > MAX_SHAPE_SIZE) {
57 MS_LOG(ERROR) << "PriorBox max_sizes size exceeds max num " << MAX_SHAPE_SIZE << ", got " << max_sizes->size();
58 free(param);
59 return nullptr;
60 }
61 param->max_sizes_size = static_cast<int32_t>(max_sizes->size());
62 memcpy(param->max_sizes, max_sizes->data(), max_sizes->size() * sizeof(int32_t));
63
64 auto aspect_ratios = value->aspect_ratios();
65 if (aspect_ratios == nullptr) {
66 MS_LOG(ERROR) << "aspect_ratios is nullptr";
67 free(param);
68 return nullptr;
69 }
70 if (aspect_ratios->size() > MAX_SHAPE_SIZE) {
71 MS_LOG(ERROR) << "PriorBox aspect_ratios size exceeds max num " << MAX_SHAPE_SIZE << ", got "
72 << aspect_ratios->size();
73 free(param);
74 return nullptr;
75 }
76 param->aspect_ratios_size = static_cast<int32_t>(aspect_ratios->size());
77 memcpy(param->aspect_ratios, aspect_ratios->data(), aspect_ratios->size() * sizeof(float));
78
79 auto variances = value->variances();
80 if (variances == nullptr) {
81 MS_LOG(ERROR) << "variances is nullptr";
82 free(param);
83 return nullptr;
84 }
85 if (variances->size() != COMM_SHAPE_SIZE) {
86 MS_LOG(ERROR) << "PriorBox variances size should be " << COMM_SHAPE_SIZE << ", got " << variances->size();
87 free(param);
88 return nullptr;
89 }
90 memcpy(param->variances, variances->data(), COMM_SHAPE_SIZE * sizeof(float));
91 param->flip = value->flip();
92 param->clip = value->clip();
93 param->offset = value->offset();
94 param->image_size_h = value->image_size_h();
95 param->image_size_w = value->image_size_w();
96 param->step_h = value->step_h();
97 param->step_w = value->step_w();
98 return reinterpret_cast<OpParameter *>(param);
99 }
100 REG_POPULATE(PrimitiveType_PriorBox, PopulatePriorBoxParameter, SCHEMA_CUR)
101 } // namespace lite
102 } // namespace mindspore
103