WIL-PDL Reference ( C++ ) 1.0.6
サンプルコード ( 多視点画像分類 )

多視点画像分類のサンプルコードです

  • 最も単純なサンプルコード: L.6- smp_mvcnn_simple()
  • ベンチマークを計測するサンプルコード: L.119: smp_mvcnn_benchmark()
  • FIE の関数を前処理として行うサンプルコード: L.247: smp_mvcnn_with_fie()
1#pragma once
2#include "samples.h"
3#include "oal_aloc.h"
4#include <string>
5
6int smp_mvcnn_simple(const char*model_name, const char* img_folder)
7{
8 INT ret = F_ERR_NONE;
9 H_MODEL hmodel = NULL;
10 MODEL_CATEGORY model_category;
11 MODEL_BACKEND model_backend;
12 INT n_views = 0;
13 FHANDLE* hsrcs = NULL;
14 INT ch, w, h;
15 FLOAT* scores = NULL;
16 size_t n_scores;
17
18 // FIEライブラリの使用前に必ずコールする必要があります。
19 fnFIE_setup();
20
21
22 //------- ここから -------//
23
24 // ライセンスチェック
25 ret = fnPDL_check_license();
26 printf_s("lic : %d\r\n", ret);
27 if (F_ERR_NONE != ret) { goto finally; }
28
29 // ハンドルの生成
30 hmodel = fnPDL_create_handle();
31 if (NULL == hmodel) {
32 ret = F_ERR_NOMEMORY;
33 printf_s("failed to create handle\r\n");
34 goto finally;
35 }
36 else { printf_s("handle created\r\n"); }
37
38 // モデル読込
39 ret = fnPDL_load_model(model_name, hmodel);
40 printf_s("load: %d\r\n", ret);
41 if (F_ERR_NONE != ret) { goto finally; }
42
43 // モデルの種別の確認
44 ret = fnPDL_get_model_category(hmodel, &model_category);
45 printf_s("par : %d category=%d\r\n", ret, model_category);
46 if (F_ERR_NONE != ret) { goto finally; }
47 if (MULTI_VIEW_CNN != model_category) {
48 ret = F_ERR_INVALID_OBJECT;
49 printf_s("unmatch model\r\n");
50 goto finally;
51 }
52
53 // モデルの推論バックエンドの確認
54 ret = fnPDL_get_model_backend(hmodel, &model_backend);
55 printf_s("ret : %d backend=%d\r\n", ret, model_backend);
56 if (F_ERR_NONE != ret) { goto finally; }
57
58 // モデルの期待する画像パラメータの確認
59 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
60 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
61 if (F_ERR_NONE != ret) { goto finally; }
62
63 // モデルの視点数の取得
64 ret = fnPDL_get_how_many_views(hmodel, &n_views);
65 printf_s("# of views=%d\r\n", n_views);
66 if (F_ERR_NONE != ret) { goto finally; }
67
68 // 入力画像配列のメモリ確保と画像読込
69 hsrcs = (FHANDLE*)fnOAL_calloc(n_views, sizeof(FHANDLE));
70 if (NULL == hsrcs) {
71 ret = F_ERR_NOMEMORY;
72 printf_s("malloc error\r\n");
73 goto finally;
74 }
75 else {
76 INT i_img = 0;
77 for (i_img = 0; i_img < n_views; i_img++) {
78 std::string image_path = std::string(img_folder) + std::to_string(i_img) + ".bmp";
79 ret = fnFIE_load_bmp(image_path.c_str(), &(hsrcs[i_img]), F_COLOR_IMG_TYPE_UC8);
80 printf_s("img ( #=%d ): %d\r\n", i_img, ret);
81 if (F_ERR_NONE != ret) { goto finally; }
82 }
83 }
84
85 // 推論実行
86 ret = fnPDL_predict_multi_images(hmodel, n_views, hsrcs, &scores, &n_scores);
87 printf_s("pred: %d\r\n", ret);
88 if (F_ERR_NONE != ret) { goto finally; }
89 if (NULL != scores && 0 < n_scores) {
90 INT i_score;
91 printf_s(" # of scores = %zd\r\n", n_scores);
92 for (i_score = 0; i_score < n_scores; i_score++) {
93 printf_s(" label%d: %.6e\r\n", i_score, scores[i_score]);
94 }
95 }
96 printf_s("\r\n");
97
98finally:
99 // ハンドルの破棄
100 fnPDL_dispose(hmodel);
101
102 //------- ここまで -------//
103
104
105 // 終了処理
106 if (NULL != hsrcs) {
107 INT i_img = 0;
108 for (i_img = 0; i_img < n_views; i_img++) {
109 fnFIE_free_object(hsrcs[i_img]);
110 }
111 fnOAL_free(hsrcs);
112 }
113 if (NULL != scores) { fnOAL_free(scores); }
114 fnFIE_teardown();
115
116 return ret;
117}
118
119int smp_mvcnn_benchmark(const char*model_name, const char* img_folder, const int bench_iter)
120{
121 // benchmark 用
122 std::chrono::system_clock::time_point start, end;
123
124 INT ret = F_ERR_NONE;
125 H_MODEL hmodel = NULL;
126 MODEL_CATEGORY model_category;
127 INT n_views = 0;
128 FHANDLE* hsrcs = NULL;
129 INT ch, w, h;
130 FLOAT* scores = NULL;
131 size_t n_scores;
132
133 // FIEライブラリの使用前に必ずコールする必要があります。
134 fnFIE_setup();
135
136 // ライセンスチェック
137 ret = fnPDL_check_license();
138 printf_s("lic : %d\r\n", ret);
139 if (F_ERR_NONE != ret) { goto finally; }
140
141 // ハンドルの生成
142 hmodel = fnPDL_create_handle();
143 if (NULL == hmodel) {
144 ret = F_ERR_NOMEMORY;
145 printf_s("failed to create handle\r\n");
146 goto finally;
147 }
148 else { printf_s("handle created\r\n"); }
149
150 // モデル読込
151 ret = fnPDL_load_model(model_name, hmodel);
152 printf_s("load: %d\r\n", ret);
153 if (F_ERR_NONE != ret) { goto finally; }
154
155 // モデルの種別の確認
156 ret = fnPDL_get_model_category(hmodel, &model_category);
157 printf_s("par : %d category=%d\r\n", ret, model_category);
158 if (F_ERR_NONE != ret) { goto finally; }
159 if (MULTI_VIEW_CNN != model_category) {
160 ret = F_ERR_INVALID_OBJECT;
161 printf_s("unmatch model\r\n");
162 goto finally;
163 }
164
165 // モデルの期待する画像パラメータの確認
166 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
167 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
168 if (F_ERR_NONE != ret) { goto finally; }
169
170 // モデルの視点数の取得
171 ret = fnPDL_get_how_many_views(hmodel, &n_views);
172 printf_s("# of views=%d\r\n", n_views);
173 if (F_ERR_NONE != ret) { goto finally; }
174
175 // 入力画像配列のメモリ確保と画像読込
176 hsrcs = (FHANDLE*)fnOAL_calloc(n_views, sizeof(FHANDLE));
177 if (NULL == hsrcs) {
178 ret = F_ERR_NOMEMORY;
179 printf_s("malloc error\r\n");
180 goto finally;
181 }
182 else {
183 INT i_img = 0;
184 for (i_img = 0; i_img < n_views; i_img++) {
185 std::string image_path = std::string(img_folder) + std::to_string(i_img) + ".bmp";
186 ret = fnFIE_load_bmp(image_path.c_str(), &(hsrcs[i_img]), F_COLOR_IMG_TYPE_UC8);
187 printf_s("img ( #=%d ): %d\r\n", i_img, ret);
188 if (F_ERR_NONE != ret) { goto finally; }
189 }
190 }
191
192
193 //------- 計測ループ ここから -------//
194
195 printf_s("--- start ---\r\n");
196 printf_s("number, elapsed[msec], scores\r\n");
197 for (int i = 0; i < bench_iter; i++) {
198 double elapsed;
199
200 // 開始時刻
201 start = std::chrono::system_clock::now();
202
203 // 推論実行
204 ret = fnPDL_predict_multi_images(hmodel, n_views, hsrcs, &scores, &n_scores);
205
206 // 完了時刻
207 end = std::chrono::system_clock::now();
208
209 if (F_ERR_NONE != ret) {
210 printf_s("pred. err: %d\r\n", ret);
211 break;
212 }
213
214 // 推論時間 [msec]
215 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / (double)1000;
216
217 // コンソールに出力
218 printf_s("%04d, %.3e", i, elapsed);
219 for (int i_score = 0; i_score < n_scores; i_score++) {
220 printf_s(", %.6e", scores[i_score]);
221 }
222 printf_s("\r\n");
223 }
224 printf_s("--- finish ---\r\n");
225 printf_s("\r\n");
226
227 //------- 計測ループ ここまで -------//
228
229finally:
230 // ハンドルの破棄
231 fnPDL_dispose(hmodel);
232
233 // 終了処理
234 if (NULL != hsrcs) {
235 INT i_img = 0;
236 for (i_img = 0; i_img < n_views; i_img++) {
237 fnFIE_free_object(hsrcs[i_img]);
238 }
239 fnOAL_free(hsrcs);
240 }
241 if (NULL != scores) { fnOAL_free(scores); }
242 fnFIE_teardown();
243
244 return ret;
245}
246
247int smp_mvcnn_with_fie(const char*model_name, const char* img_folder)
248{
249 INT ret = F_ERR_NONE;
250 H_MODEL hmodel = NULL;
251 MODEL_CATEGORY model_category;
252 INT n_views = 0;
253 FHANDLE* hsrcs = NULL;
254 FHANDLE* hfiltereds = NULL;
255 INT ch, w, h;
256 FLOAT* scores = NULL;
257 size_t n_scores;
258
259 // FIEライブラリの使用前に必ずコールする必要があります。
260 fnFIE_setup();
261
262 // ライセンスチェック
263 ret = fnPDL_check_license();
264 printf_s("lic : %d\r\n", ret);
265 if (F_ERR_NONE != ret) { goto finally; }
266
267 // ハンドルの生成
268 hmodel = fnPDL_create_handle();
269 if (NULL == hmodel) {
270 ret = F_ERR_NOMEMORY;
271 printf_s("failed to create handle\r\n");
272 goto finally;
273 }
274 else { printf_s("handle created\r\n"); }
275
276 // モデル読込
277 ret = fnPDL_load_model(model_name, hmodel);
278 printf_s("load: %d\r\n", ret);
279 if (F_ERR_NONE != ret) { goto finally; }
280
281 // モデルの種別の確認
282 ret = fnPDL_get_model_category(hmodel, &model_category);
283 printf_s("par : %d category=%d\r\n", ret, model_category);
284 if (F_ERR_NONE != ret) { goto finally; }
285 if (MULTI_VIEW_CNN != model_category) {
286 ret = F_ERR_INVALID_OBJECT;
287 printf_s("unmatch model\r\n");
288 goto finally;
289 }
290
291 // モデルの期待する画像パラメータの確認
292 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
293 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
294 if (F_ERR_NONE != ret) { goto finally; }
295
296 // モデルの視点数の取得
297 ret = fnPDL_get_how_many_views(hmodel, &n_views);
298 printf_s("# of views=%d\r\n", n_views);
299 if (F_ERR_NONE != ret) { goto finally; }
300
301 // 入力画像配列のメモリ確保と画像読込
302 hsrcs = (FHANDLE*)fnOAL_calloc(n_views, sizeof(FHANDLE));
303 if (NULL == hsrcs) {
304 ret = F_ERR_NOMEMORY;
305 printf_s("malloc error\r\n");
306 goto finally;
307 }
308 else {
309 INT i_img = 0;
310 for (i_img = 0; i_img < n_views; i_img++) {
311 std::string image_path = std::string(img_folder) + std::to_string(i_img) + ".bmp";
312 ret = fnFIE_load_bmp(image_path.c_str(), &(hsrcs[i_img]), F_COLOR_IMG_TYPE_UC8);
313 printf_s("img ( #=%d ): %d\r\n", i_img, ret);
314 if (F_ERR_NONE != ret) { goto finally; }
315 }
316 }
317
319 // 読み込んだ画像をそのまま推論する
321
322 // 推論実行
323 ret = fnPDL_predict_multi_images(hmodel, n_views, hsrcs, &scores, &n_scores);
324 printf_s("pred: %d\r\n", ret);
325 if (F_ERR_NONE != ret) { goto finally; }
326 if (NULL != scores && 0 < n_scores) {
327 INT i_score;
328 printf_s(" # of scores = %zd\r\n", n_scores);
329 for (i_score = 0; i_score < n_scores; i_score++) {
330 printf_s(" label%d: %.6e\r\n", i_score, scores[i_score]);
331 }
332 }
333
335 // フィルタをかけた画像を推論する
337
338 // ここでは単純な平均化フィルタ
339 hfiltereds = (FHANDLE*)fnOAL_calloc(n_views, sizeof(FHANDLE));
340 if (NULL == hfiltereds) {
341 ret = F_ERR_NOMEMORY;
342 printf_s("malloc error ( filtered images )\r\n");
343 goto finally;
344 }
345 else {
346 INT i_img = 0;
347 for (i_img = 0; i_img < n_views; i_img++) {
348 hfiltereds[i_img] = fnFIE_img_root_alloc(F_IMG_UC8, ch, w, h);
349 if (NULL == hfiltereds[i_img]) {
350 ret = F_ERR_NOMEMORY;
351 printf_s("malloc error ( filtered images )\r\n");
352 goto finally;
353 }
354 }
355 }
356 {
357 INT i_img = 0;
358 for (i_img = 0; i_img < n_views; i_img++) {
359 FHANDLE hsrc = hsrcs[i_img];
360 FHANDLE hdst = hfiltereds[i_img];
361 ret = fnFIE_average(hsrc, hdst, 0, 0);
362 if (F_ERR_NONE != ret) { goto finally; }
363 }
364 }
365
366 // 推論実行
367 ret = fnPDL_predict_multi_images(hmodel, n_views, hfiltereds, &scores, &n_scores);
368 printf_s("pred(average): %d\r\n", ret);
369 if (F_ERR_NONE != ret) { goto finally; }
370 if (NULL != scores && 0 < n_scores) {
371 INT i_score;
372 printf_s(" # of scores = %zd\r\n", n_scores);
373 for (i_score = 0; i_score < n_scores; i_score++) {
374 printf_s(" label%d: %.6e\r\n", i_score, scores[i_score]);
375 }
376 }
377 printf_s("\r\n");
378
379finally:
380 // ハンドルの破棄
381 fnPDL_dispose(hmodel);
382
383 // メモリの破棄
384 if (NULL != hsrcs) {
385 INT i_img = 0;
386 for (i_img = 0; i_img < n_views; i_img++) {
387 fnFIE_free_object(hsrcs[i_img]);
388 }
389 fnOAL_free(hsrcs);
390 }
391 if (NULL != hfiltereds) {
392 INT i_img = 0;
393 for (i_img = 0; i_img < n_views; i_img++) {
394 fnFIE_free_object(hfiltereds[i_img]);
395 }
396 fnOAL_free(hfiltereds);
397 }
398 if (NULL != scores) { fnOAL_free(scores); }
399
400 // 終了処理
401 fnFIE_teardown();
402
403 return ret;
404
405}
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_get_model_category(const H_MODEL hmodel, MODEL_CATEGORY *model_category)
モデルの種別の取得
Definition: prediction_cpp.cpp:1937
INT FVALGAPI fnPDL_get_how_many_views(const H_MODEL hmodel, INT *num_views)
モデルパラメータの取得
Definition: prediction_cpp.cpp:2013
INT FVALGAPI fnPDL_predict_multi_images(const H_MODEL hmodel, INT num_images, FHANDLE *hsrcs, FLOAT **scores, size_t *num_scores)
推論の実行 ( 多視点画像分類、多視点アノマリー検出 )
Definition: prediction_cpp.cpp:2295
VOID FVALGAPI fnPDL_dispose(H_MODEL hmodel)
モデルハンドルの解放
Definition: prediction_cpp.cpp:2556
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_VIEW_CNN
Definition: fv_pdl.h:54

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 by Doxygen 1.9.5.