summaryrefslogtreecommitdiff
path: root/mv_machine_learning/inference/src/InputMetadata.cpp
blob: 8d488b6e82e541d20532060fd29da03e270cc0ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mv_private.h"

#include <unistd.h>
#include <fstream>
#include <string>
#include <queue>
#include <algorithm>
#include "InputMetadata.h"
#include <mv_common.h>
#include "Utils.h"

namespace mediavision
{
namespace inference
{
InputMetadata::InputMetadata() : parsed(false), layer(), option()
{
	// shape_type
	mSupportedShapeType.insert({ "NCHW", INFERENCE_TENSOR_SHAPE_NCHW });
	mSupportedShapeType.insert({ "NHWC", INFERENCE_TENSOR_SHAPE_NHWC });

	// data_type
	mSupportedDataType.insert({ "FLOAT32", MV_INFERENCE_DATA_FLOAT32 });
	mSupportedDataType.insert({ "UINT8", MV_INFERENCE_DATA_UINT8 });

	// color_space
	mSupportedColorSpace.insert({ "RGB888", MEDIA_VISION_COLORSPACE_RGB888 });
	mSupportedColorSpace.insert({ "GRAY8", MEDIA_VISION_COLORSPACE_Y800 });
}

int InputMetadata::GetTensorInfo(JsonObject *root, std::string key_name)
{
	LOGI("ENTER");

	if (!json_object_has_member(root, key_name.c_str())) {
		LOGE("No input.");
		return MEDIA_VISION_ERROR_INVALID_OPERATION;
	}

	LOGI("Parse tensor name : %s", key_name.c_str());

	// tensor_info
	JsonNode *node = json_object_get_member(root, key_name.c_str());
	JsonObject *object = json_node_get_object(node);

	std::map<std::string, LayerInfo>().swap(layer);
	// TODO: handling error
	// FIXME: LayerInfo.set()??
	LayerInfo info;

	info.name = static_cast<const char *>(json_object_get_string_member(object, "name"));
	LOGI("layer: %s", info.name.c_str());
	char *node_string = json_to_string(node, 0);
	LOGI("tensor name : %s", node_string);
	free(node_string);

	try {
		info.shapeType = GetSupportedType(object, "shape_type", mSupportedShapeType);
		info.dataType = GetSupportedType(object, "data_type", mSupportedDataType);
		info.colorSpace = GetSupportedType(object, "color_space", mSupportedColorSpace);
	} catch (const std::exception &e) {
		LOGE("Invalid %s", e.what());
		return MEDIA_VISION_ERROR_INVALID_OPERATION;
	}

	// dims
	JsonArray *array = json_object_get_array_member(object, "shape_dims");
	unsigned int elements2 = json_array_get_length(array);
	LOGI("shape dim: size[%u]", elements2);
	for (unsigned int elem2 = 0; elem2 < elements2; ++elem2) {
		auto dim = static_cast<int>(json_array_get_int_element(array, elem2));
		info.dims.push_back(dim);
		LOGI("%d", dim);
	}

	layer.insert(std::make_pair(info.name, info));

	if (json_object_has_member(object, "preprocess")) {
		// Parse preprocess.
		int ret = GetPreProcess(object);
		if (ret != MEDIA_VISION_ERROR_NONE) {
			LOGE("Fail to get preprocess element.");
			return ret;
		}
	}

	LOGI("LEAVE");

	return MEDIA_VISION_ERROR_NONE;
}

int InputMetadata::GetPreProcess(JsonObject *root)
{
	LOGI("ENTER");

	JsonNode *preprocess_node = json_object_get_member(root, "preprocess");
	JsonObject *preprocess_object = json_node_get_object(preprocess_node);

	std::map<std::string, Options>().swap(option);
	// TODO: iterLayer should be the same with elements.
	auto iterLayer = layer.begin();
	// TODO: handling error
	Options opt;

	// normalization
	if (json_object_has_member(preprocess_object, "normalization")) {
		JsonNode *node = json_object_get_member(preprocess_object, "normalization");
		JsonObject *object = json_node_get_object(node);

		opt.normalization.use = true;
		LOGI("use normalization");

		JsonArray *arrayMean = json_object_get_array_member(object, "mean");
		JsonArray *arrayStd = json_object_get_array_member(object, "std");
		unsigned int elemMean = json_array_get_length(arrayMean);
		unsigned int elemStd = json_array_get_length(arrayStd);
		if (elemMean != elemStd) {
			LOGE("Invalid mean and std values");
			return MEDIA_VISION_ERROR_INVALID_OPERATION;
		}

		for (unsigned int elem = 0; elem < elemMean; ++elem) {
			auto m = static_cast<double>(json_array_get_double_element(arrayMean, elem));
			auto s = static_cast<double>(json_array_get_double_element(arrayStd, elem));
			opt.normalization.mean.push_back(m);
			opt.normalization.std.push_back(s);
			LOGI("%u: mean[%3.2f], std[%3.2f]", elem, m, s);
		}
	}

	if (json_object_has_member(preprocess_object, "quantization")) {
		JsonNode *node = json_object_get_member(preprocess_object, "quantization");
		JsonObject *object = json_node_get_object(node);

		opt.quantization.use = true;
		LOGI("use quantization");

		JsonArray *arrayScale = json_object_get_array_member(object, "scale");
		JsonArray *arrayZero = json_object_get_array_member(object, "zeropoint");
		unsigned int elemScale = json_array_get_length(arrayScale);
		unsigned int elemZero = json_array_get_length(arrayZero);
		if (elemScale != elemZero) {
			LOGE("Invalid scale and zero values");
			return MEDIA_VISION_ERROR_INVALID_OPERATION;
		}

		for (unsigned int elem = 0; elem < elemScale; ++elem) {
			auto s = static_cast<double>(json_array_get_double_element(arrayScale, elem));
			auto z = static_cast<double>(json_array_get_double_element(arrayZero, elem));
			opt.quantization.scale.push_back(s);
			opt.quantization.zeropoint.push_back(z);
			LOGI("%u: scale[%3.2f], zeropoint[%3.2f]", elem, s, z);
		}
	}

	if (json_object_has_member(preprocess_object, "resize")) {
		const char *resizer = static_cast<const char *>(json_object_get_string_member(preprocess_object, "resize"));
		if (!resizer) {
			LOGE("Fail to get a string value to the resize member.");
			return MEDIA_VISION_ERROR_INVALID_OPERATION;
		}

		if (strcmp(resizer, "LETTERBOX") == 0) {
			opt.resizer = Resizer::LETTERBOX;
			LOGI("resizer changed to letterbox");
		}
	}

	option.insert(std::make_pair(iterLayer->first, opt));

	LOGI("LEAVE");

	return MEDIA_VISION_ERROR_NONE;
}

int InputMetadata::Parse(JsonObject *root, std::string key_name)
{
	LOGI("ENTER");

	JsonArray *inputList = json_object_get_array_member(root, key_name.c_str());
	LOGI("input tensor count : %d", json_array_get_length(inputList));

	for (guint idx = 0; idx < json_array_get_length(inputList); ++idx) {
		JsonNode *node = json_array_get_element(inputList, idx);
		char *node_string = json_to_string(node, 1);
		std::string token(node_string);
		free(node_string);
		int pos = token.find(":");
		std::string tensor_name = token.substr(0, pos);
		const std::vector<char> delimiters = { '{', ' ', ':', '\n', '\"' };

		for (auto &delimiter : delimiters)
			tensor_name.erase(std::remove(tensor_name.begin(), tensor_name.end(), delimiter), tensor_name.end());

		if (tensor_name.compare((std::string("tensor") + std::to_string(idx + 1))) != 0) {
			LOGE("Invalid tensor element. A tensor element form should be `tensorN`.");
			return MEDIA_VISION_ERROR_INVALID_OPERATION;
		}

		JsonObject *object = json_node_get_object(node);

		int ret = GetTensorInfo(object, tensor_name);
		if (ret != MEDIA_VISION_ERROR_NONE) {
			LOGE("Fail to GetTensorInfo[%d]", ret);
			return ret;
		}
	}

	parsed = true;
	LOGI("LEAVE");

	return MEDIA_VISION_ERROR_NONE;
}

} /* Inference */
} /* MediaVision */