WIL-PDL Reference ( C++ ) 1.0.6-gpu
サンプルコード ( マルチラベル画像分類 )

マルチラベル画像分類のサンプルコードです

  • 最も単純なサンプルコード: L.5- smp_mlcls_simple()
  • ベンチマークを計測するサンプルコード: L.99: smp_mlcls_benchmark()
  • FIE の関数を前処理として行うサンプルコード: L.218: smp_mlcls_with_fie()
1#pragma once
2#include "samples.h"
3#include "oal_aloc.h"
4
5int smp_mlcls_simple(const char* model_name, const char* img_name)
6{
7 INT ret = F_ERR_NONE;
8 FHANDLE hsrc = NULL;
9 H_MODEL hmodel = NULL;
10 MODEL_CATEGORY model_category;
11 MODEL_BACKEND model_backend;
12 INT ch, w, h;
13 FLOAT* scores = NULL;
14 size_t n_scores;
15 FLOAT score_thresh = 0.5;
16 UINT* result_cls_ids = NULL;
17 FLOAT* result_scores = NULL;
18 size_t num_pred_labels = 0;
19
20 // FIEライブラリの使用前に必ずコールする必要があります。
21 fnFIE_setup();
22
23 // 画像読込
24 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
25 printf_s("img: %d\r\n", ret);
26 if (F_ERR_NONE != ret) { goto finally; }
27
28 // ライセンスチェック
29 ret = fnPDL_check_license();
30 printf_s("lic : %d\r\n", ret);
31 if (F_ERR_NONE != ret) { goto finally; }
32
33 // ハンドルの生成
34 hmodel = fnPDL_create_handle();
35 if (NULL == hmodel) {
36 ret = F_ERR_NOMEMORY;
37 printf_s("failed to create handle\r\n");
38 goto finally;
39 }
40 else { printf_s("handle created\r\n"); }
41 if (F_ERR_NONE != ret) { goto finally; }
42
43 // モデル読込
44 ret = fnPDL_load_model(model_name, hmodel);
45 printf_s("load: %d\r\n", ret);
46 if (F_ERR_NONE != ret) { goto finally; }
47
48 // モデルの種別の確認
49 ret = fnPDL_get_model_category(hmodel, &model_category);
50 printf_s("par : %d category=%d\r\n", ret, model_category);
51 if (F_ERR_NONE != ret) { goto finally; }
52 if (MULTI_LABEL_CLASSIFICATION != model_category) {
53 ret = F_ERR_INVALID_OBJECT;
54 printf_s("unmatch model\r\n");
55 goto finally;
56 }
57
58 // モデルの推論バックエンドの確認
59 ret = fnPDL_get_model_backend(hmodel, &model_backend);
60 printf_s("ret : %d backend=%d\r\n", ret, model_backend);
61 if (F_ERR_NONE != ret) { goto finally; }
62
63 // モデルの期待する画像パラメータの確認
64 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
65 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
66 if (F_ERR_NONE != ret) { goto finally; }
67
68 // 推論実行
69 ret = fnPDL_predict(hmodel, hsrc, &scores, &n_scores);
70 printf_s("pred: %d\r\n", ret);
71 if (F_ERR_NONE != ret) { goto finally; }
73 hmodel, scores, n_scores, score_thresh, &result_cls_ids, &result_scores, &num_pred_labels
74 );
75 printf_s("postproc: %d\r\n", ret);
76 if (F_ERR_NONE != ret) { goto finally; }
77 if (NULL != result_cls_ids && NULL != result_scores) {
78 size_t i_result;
79 for (i_result = 0; i_result < num_pred_labels; i_result++) {
80 printf_s(" label%d: %.6e\r\n", result_cls_ids[i_result], result_scores[i_result]);
81 }
82 }
83 printf_s("\r\n");
84
85finally:
86 // ハンドルの破棄
87 fnPDL_dispose(hmodel);
88
89 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
90 if (NULL != scores) { fnOAL_free(scores); }
91 if (NULL != result_cls_ids) { fnOAL_free(result_cls_ids); }
92 if (NULL != result_scores) { fnOAL_free(result_scores); }
93
94 fnFIE_teardown();
95
96 return ret;
97}
98
99int smp_mlcls_benchmark(const char* model_name, const char* img_name, const int bench_iter)
100{
101 // benchmark 用
102 std::chrono::system_clock::time_point start, end;
103
104 INT ret = F_ERR_NONE;
105 FHANDLE hsrc = NULL;
106 H_MODEL hmodel = NULL;
107 MODEL_CATEGORY model_category;
108 FLOAT* scores = NULL;
109 size_t n_scores;
110 FLOAT score_thresh = 0.5;
111 UINT* result_cls_ids = NULL;
112 FLOAT* result_scores = NULL;
113 size_t num_pred_labels = 0;
114
115 // FIEライブラリの使用前に必ずコールする必要があります。
116 fnFIE_setup();
117
118 // 画像読込
119 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
120 printf_s("img: %d\r\n", ret);
121 if (F_ERR_NONE != ret) { goto finally; }
122
123 // ライセンスチェック
124 ret = fnPDL_check_license();
125 printf_s("lic : %d\r\n", ret);
126 if (F_ERR_NONE != ret) { goto finally; }
127
128 // ハンドルの生成
129 hmodel = fnPDL_create_handle();
130 if (NULL == hmodel) {
131 ret = F_ERR_NOMEMORY;
132 printf_s("failed to create handle\r\n");
133 goto finally;
134 }
135 else { printf_s("handle created\r\n"); }
136
137 // モデル読込
138 ret = fnPDL_load_model(model_name, hmodel);
139 printf_s("load: %d\r\n", ret);
140 if (F_ERR_NONE != ret) { goto finally; }
141
142 // モデルの種別の確認
143 ret = fnPDL_get_model_category(hmodel, &model_category);
144 printf_s("par : %d category=%d\r\n", ret, model_category);
145 if (F_ERR_NONE != ret) { goto finally; }
146 if (MULTI_LABEL_CLASSIFICATION != model_category) {
147 ret = F_ERR_INVALID_OBJECT;
148 printf_s("unmatch model\r\n");
149 goto finally;
150 }
151
152 printf_s("--- start ---\r\n");
153 printf_s("number, elapsed[msec]\r\n");
154 printf_s(" result(class, score)\r\n");
155 // 本体ループ
156 for (int i = 0; i < bench_iter; i++) {
157 double elapsed;
158
159 // 開始時刻
160 start = std::chrono::system_clock::now();
161
162 // 推論実行
163 ret = fnPDL_predict(hmodel, hsrc, &scores, &n_scores);
164 if (F_ERR_NONE != ret) {
165 printf_s("pred. err: %d\r\n", ret);
166 goto finally;
167 }
169 hmodel, scores, n_scores, score_thresh, &result_cls_ids, &result_scores, &num_pred_labels
170 );
171 if (F_ERR_NONE != ret) {
172 printf_s("postproc. err: %d\r\n", ret);
173 goto finally;
174 }
175
176 // 完了時刻
177 end = std::chrono::system_clock::now();
178
179 // 推論時間 [msec]
180 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / (double)1000;
181
182 // コンソールに出力
183 printf_s("%04d, %.3e\r\n", i, elapsed);
184
185 if (NULL != result_cls_ids && NULL != result_scores) {
186 size_t i_result;
187 for (i_result = 0; i_result < num_pred_labels; i_result++) {
188 printf_s(" label%d: %.6e\r\n", result_cls_ids[i_result], result_scores[i_result]);
189 }
190 }
191
192 if (NULL != result_cls_ids) {
193 fnOAL_free(result_cls_ids);
194 result_cls_ids = NULL;
195 }
196 if (NULL != result_scores) {
197 fnOAL_free(result_scores);
198 result_scores = NULL;
199 }
200 }
201 printf_s("--- finish ---\r\n");
202 printf_s("\r\n");
203
204finally:
205 // ハンドルの破棄
206 fnPDL_dispose(hmodel);
207
208 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
209 if (NULL != scores) { fnOAL_free(scores); }
210 if (NULL != result_cls_ids) { fnOAL_free(result_cls_ids); }
211 if (NULL != result_scores) { fnOAL_free(result_scores); }
212
213 fnFIE_teardown();
214
215 return ret;
216}
217
218int smp_mlcls_with_fie(const char* model_name, const char* img_name)
219{
220 INT ret = F_ERR_NONE;
221 FHANDLE hsrc = NULL;
222 H_MODEL hmodel = NULL;
223 MODEL_CATEGORY model_category;
224 FLOAT* scores = NULL;
225 size_t n_scores;
226 FLOAT score_thresh = 0.5;
227 UINT* result_cls_ids = NULL;
228 FLOAT* result_scores = NULL;
229 size_t num_pred_labels = 0;
230
231 FHANDLE hfiltered = NULL;
232 INT ch, w, h, type;
233 INT_PTR step;
234
235 // FIEライブラリの使用前に必ずコールする必要があります。
236 fnFIE_setup();
237
238 // 画像読込
239 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
240 printf_s("img: %d\r\n", ret);
241 if (F_ERR_NONE != ret) { goto finally; }
242
243 // ライセンスチェック
244 ret = fnPDL_check_license();
245 printf_s("lic : %d\r\n", ret);
246 if (F_ERR_NONE != ret) { goto finally; }
247
248 // ハンドルの生成
249 hmodel = fnPDL_create_handle();
250 if (NULL == hmodel) {
251 ret = F_ERR_NOMEMORY;
252 printf_s("failed to create handle\r\n");
253 goto finally;
254 }
255 else { printf_s("handle created\r\n"); }
256
257 // モデル読込
258 ret = fnPDL_load_model(model_name, hmodel);
259 printf_s("load: %d\r\n", ret);
260 if (F_ERR_NONE != ret) { goto finally; }
261
262 // モデルの種別の確認
263 ret = fnPDL_get_model_category(hmodel, &model_category);
264 printf_s("par : %d category=%d\r\n", ret, model_category);
265 if (F_ERR_NONE != ret) { goto finally; }
266 if (MULTI_LABEL_CLASSIFICATION != model_category) {
267 ret = F_ERR_INVALID_OBJECT;
268 printf_s("unmatch model\r\n");
269 goto finally;
270 }
271
273 // 読み込んだ画像をそのまま推論する
275
276 // 推論実行
277 ret = fnPDL_predict(hmodel, hsrc, &scores, &n_scores);
278 printf_s("pred(original): %d\r\n", ret);
279 if (F_ERR_NONE != ret) { goto finally; }
281 hmodel, scores, n_scores, score_thresh, &result_cls_ids, &result_scores, &num_pred_labels
282 );
283 printf_s("postproc(original): %d\r\n", ret);
284 if (F_ERR_NONE != ret) { goto finally; }
285 if (NULL != result_cls_ids && NULL != result_scores) {
286 size_t i_result;
287 for (i_result = 0; i_result < num_pred_labels; i_result++) {
288 printf_s(" label%d: %.6e\r\n", result_cls_ids[i_result], result_scores[i_result]);
289 }
290 }
291
292 // 次で使うので開放してNULLを代入しなおす
293 if (NULL != result_cls_ids) {
294 fnOAL_free(result_cls_ids);
295 result_cls_ids = NULL;
296 }
297 if (NULL != result_scores) {
298 fnOAL_free(result_scores);
299 result_scores = NULL;
300 }
301
303 // フィルタをかけた画像を推論する
305
306 // ここでは単純な平均化フィルタ
307 ret = fnFIE_img_get_params(hsrc, &ch, &type, &step, &w, &h);
308 if (F_ERR_NONE != ret) { goto finally; }
309 hfiltered = fnFIE_img_root_alloc(type, ch, w, h);
310 if (NULL == hfiltered) { ret = F_ERR_NOMEMORY; goto finally; }
311 ret = fnFIE_average(hsrc, hfiltered, 0, 0);
312 if (F_ERR_NONE != ret) { goto finally; }
313
314 // 推論実行
315 ret = fnPDL_predict(hmodel, hfiltered, &scores, &n_scores);
316 printf_s("pred(average): %d\r\n", ret);
317 if (F_ERR_NONE != ret) { goto finally; }
319 hmodel, scores, n_scores, score_thresh, &result_cls_ids, &result_scores, &num_pred_labels
320 );
321 printf_s("postproc(average): %d\r\n", ret);
322 if (F_ERR_NONE != ret) { goto finally; }
323 if (NULL != result_cls_ids && NULL != result_scores) {
324 size_t i_result;
325 for (i_result = 0; i_result < num_pred_labels; i_result++) {
326 printf_s(" label%d: %.6e\r\n", result_cls_ids[i_result], result_scores[i_result]);
327 }
328 }
329 printf_s("\r\n");
330
331finally:
332 // ハンドルの破棄
333 fnPDL_dispose(hmodel);
334
335 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
336 if (NULL != hfiltered) { fnFIE_free_object(hfiltered); }
337 if (NULL != scores) { fnOAL_free(scores); }
338 if (NULL != result_cls_ids) { fnOAL_free(result_cls_ids); }
339 if (NULL != result_scores) { fnOAL_free(result_scores); }
340
341 // 終了処理
342 fnFIE_teardown();
343
344 return ret;
345}
INT FVALGAPI fnPDL_get_input_image_size(const H_MODEL hmodel, INT *channels, INT *width, INT *height)
モデルパラメータの取得
Definition: prediction_cpp.cpp:1989
INT FVALGAPI fnPDL_get_model_backend(const H_MODEL hmodel, MODEL_BACKEND *model_backend)
モデルのバックエンドの取得
Definition: prediction_cpp.cpp:1968
MODEL_CATEGORY
モデルの種別
Definition: fv_pdl.h:46
INT fnPDL_check_license()
ライセンス確認
Definition: check_licence.cpp:160
MODEL_BACKEND
推論バックエンド
Definition: fv_pdl.h:30
INT FVALGAPI fnPDL_postproc_multi_label_cls(const H_MODEL hmodel, const FLOAT *scores, const size_t num_scores, const FLOAT threshold, UINT **score_ids, FLOAT **score_values, size_t *num_pred_labels)
推論結果の加工 ( マルチラベル画像分類 )
Definition: prediction_cpp.cpp:2471
INT FVALGAPI fnPDL_get_model_category(const H_MODEL hmodel, MODEL_CATEGORY *model_category)
モデルの種別の取得
Definition: prediction_cpp.cpp:1937
VOID FVALGAPI fnPDL_dispose(H_MODEL hmodel)
モデルハンドルの解放
Definition: prediction_cpp.cpp:2556
INT FVALGAPI fnPDL_predict(const H_MODEL hmodel, const FHANDLE hsrc, FLOAT **scores, size_t *num_scores)
推論の実行
Definition: prediction_cpp.cpp:2239
INT FVALGAPI fnPDL_load_model(const CHAR *filename, H_MODEL hmodel)
モデルの読み込み
Definition: prediction_cpp.cpp:1810
VOID * H_MODEL
モデルハンドル
Definition: fv_pdl.h:18
H_MODEL *FVALGAPI fnPDL_create_handle()
モデルハンドルの生成
Definition: prediction_cpp.cpp:1755
@ MULTI_LABEL_CLASSIFICATION
Definition: fv_pdl.h:64

Documentation copyright © 2026 TOKYO ELECTRON DEVICE LIMITED https://www.teldevice.co.jp/
Generated on Fri Jun 19 2026 14:08:06 for WIL-PDL Reference ( C++ ) 1.0.6-gpu by Doxygen 1.9.5.