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

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

  • 最も単純なサンプルコード: L.5- smp_simple()
  • ベンチマークを計測するサンプルコード: L.95: smp_benchmark()
  • FIE の関数を前処理として行うサンプルコード: L.190: smp_with_fie()
1#pragma once
2#include "samples.h"
3#include "oal_aloc.h"
4
5int smp_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
16 // FIEライブラリの使用前に必ずコールする必要があります。
17 fnFIE_setup();
18
19 // 画像読込
20 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
21 printf_s("img: %d\r\n", ret);
22 if (F_ERR_NONE != ret) { goto finally; }
23
24 //------- ここから -------//
25
26 // ライセンスチェック
27 ret = fnPDL_check_license();
28 printf_s("lic : %d\r\n", ret);
29 if (F_ERR_NONE != ret) { goto finally; }
30
31 // ハンドルの生成
32 hmodel = fnPDL_create_handle();
33 if (NULL == hmodel) {
34 ret = F_ERR_NOMEMORY;
35 printf_s("failed to create handle\r\n");
36 goto finally;
37 }
38 else { printf_s("handle created\r\n"); }
39
40 // モデル読込
41 ret = fnPDL_load_model(model_name, hmodel);
42 printf_s("load: %d\r\n", ret);
43 if (F_ERR_NONE != ret) { goto finally; }
44
45 // モデルの種別の確認
46 ret = fnPDL_get_model_category(hmodel, &model_category);
47 printf_s("par : %d category=%d\r\n", ret, model_category);
48 if (F_ERR_NONE != ret) { goto finally; }
49 if (CLASSIFICATION != model_category) {
50 ret = F_ERR_INVALID_OBJECT;
51 printf_s("unmatch model\r\n");
52 goto finally;
53 }
54
55 // モデルの推論バックエンドの確認
56 ret = fnPDL_get_model_backend(hmodel, &model_backend);
57 printf_s("ret : %d backend=%d\r\n", ret, model_backend);
58 if (F_ERR_NONE != ret) { goto finally; }
59
60 // モデルの期待する画像パラメータの確認
61 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
62 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
63 if (F_ERR_NONE != ret) { goto finally; }
64
65 // 推論実行
66 ret = fnPDL_predict(hmodel, hsrc, &scores, &n_scores);
67 printf_s("pred: %d\r\n", ret);
68 if (F_ERR_NONE != ret) { goto finally; }
69 if (NULL != scores && 0 < n_scores) {
70 INT i_score;
71 printf_s(" # of scores = %zd\r\n", n_scores);
72 for (i_score = 0; i_score < n_scores; i_score++) {
73 printf_s(" label%d: %.6e\r\n", i_score, scores[i_score]);
74 }
75 //printf_s(" label0: %.6e\r\n", scores[0]);
76 //printf_s(" label1: %.6e\r\n", scores[1]);
77 //printf_s(" label2: %.6e\r\n", scores[2]);
78 }
79 printf_s("\r\n");
80
81finally:
82 // ハンドルの破棄
83 fnPDL_dispose(hmodel);
84
85 //------- ここまで -------//
86
87 // 終了処理
88 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
89 if (NULL != scores) { fnOAL_free(scores); }
90 fnFIE_teardown();
91
92 return ret;
93}
94
95int smp_benchmark(const char* model_name, const char* img_name, const int bench_iter)
96{
97 // benchmark 用
98 std::chrono::system_clock::time_point start, end;
99
100 INT ret = F_ERR_NONE;
101 FHANDLE hsrc = NULL;
102 H_MODEL hmodel = NULL;
103 MODEL_CATEGORY model_category;
104 FLOAT* scores = NULL;
105 size_t n_scores;
106
107 // FIEライブラリの使用前に必ずコールする必要があります。
108 fnFIE_setup();
109
110 // 画像読込
111 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
112 printf_s("img: %d\r\n", ret);
113 if (F_ERR_NONE != ret) { goto finally; }
114
115 // ライセンスチェック
116 ret = fnPDL_check_license();
117 printf_s("lic : %d\r\n", ret);
118 if (F_ERR_NONE != ret) { goto finally; }
119
120 // ハンドルの生成
121 hmodel = fnPDL_create_handle();
122 if (NULL == hmodel) {
123 ret = F_ERR_NOMEMORY;
124 printf_s("failed to create handle\r\n");
125 goto finally;
126 }
127 else { printf_s("handle created\r\n"); }
128
129 // モデル読込
130 ret = fnPDL_load_model(model_name, hmodel);
131 printf_s("load: %d\r\n", ret);
132 if (F_ERR_NONE != ret) { goto finally; }
133
134 // モデルの種別の確認
135 ret = fnPDL_get_model_category(hmodel, &model_category);
136 printf_s("par : %d category=%d\r\n", ret, model_category);
137 if (F_ERR_NONE != ret) { goto finally; }
138 if (CLASSIFICATION != model_category) {
139 ret = F_ERR_INVALID_OBJECT;
140 printf_s("unmatch model\r\n");
141 goto finally;
142 }
143
144 printf_s("--- start ---\r\n");
145 printf_s("number, elapsed[msec], scores\r\n");
146 // 本体ループ
147 for (int i = 0; i < bench_iter; i++) {
148 double elapsed;
149
150 // 開始時刻
151 start = std::chrono::system_clock::now();
152
153 // 推論実行
154 ret = fnPDL_predict(hmodel, hsrc, &scores, &n_scores);
155
156 // 完了時刻
157 end = std::chrono::system_clock::now();
158
159 if (F_ERR_NONE != ret) {
160 printf_s("pred. err: %d\r\n", ret);
161 goto finally;
162 }
163
164 // 推論時間 [msec]
165 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / (double)1000;
166
167 // コンソールに出力
168 printf_s("%04d, %.3e", i, elapsed);
169 for (int i_score = 0; i_score < n_scores; i_score++) {
170 printf_s(", %.6e", scores[i_score]);
171 }
172 printf_s("\r\n");
173 //printf_s("%d, %.3e, %.6e, %.6e, %.6e\r\n", ret, elapsed, scores[0], scores[1], scores[2]);
174 }
175 printf_s("--- finish ---\r\n");
176 printf_s("\r\n");
177
178finally:
179 // ハンドルの破棄
180 fnPDL_dispose(hmodel);
181
182 // 終了処理
183 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
184 if (NULL != scores) { fnOAL_free(scores); }
185 fnFIE_teardown();
186
187 return ret;
188}
189
190int smp_with_fie(const char* model_name, const char* img_name)
191{
192 INT ret = F_ERR_NONE;
193 FHANDLE hsrc = NULL;
194 H_MODEL hmodel = NULL;
195 MODEL_CATEGORY model_category;
196 FLOAT* scores = NULL;
197 size_t n_scores;
198
199 FHANDLE hfiltered = NULL;
200 INT ch, w, h, type;
201 INT_PTR step;
202
203 // FIEライブラリの使用前に必ずコールする必要があります。
204 fnFIE_setup();
205
206 // 画像読込
207 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
208 printf_s("img: %d\r\n", ret);
209 if (F_ERR_NONE != ret) { goto finally; }
210
211 //------- ここから -------//
212
213 // ライセンスチェック
214 ret = fnPDL_check_license();
215 printf_s("lic : %d\r\n", ret);
216 if (F_ERR_NONE != ret) { goto finally; }
217
218 // ハンドルの生成
219 hmodel = fnPDL_create_handle();
220 if (NULL == hmodel) {
221 ret = F_ERR_NOMEMORY;
222 printf_s("failed to create handle\r\n");
223 goto finally;
224 }
225 else { printf_s("handle created\r\n"); }
226
227 // モデル読込
228 ret = fnPDL_load_model(model_name, hmodel);
229 printf_s("load: %d\r\n", ret);
230 if (F_ERR_NONE != ret) { goto finally; }
231
232 // モデルの種別の確認
233 ret = fnPDL_get_model_category(hmodel, &model_category);
234 printf_s("par : %d category=%d\r\n", ret, model_category);
235 if (F_ERR_NONE != ret) { goto finally; }
236 if (CLASSIFICATION != model_category) {
237 ret = F_ERR_INVALID_OBJECT;
238 printf_s("unmatch model\r\n");
239 goto finally;
240 }
241
243 // 読み込んだ画像をそのまま推論する
245
246 // 推論実行
247 ret = fnPDL_predict(hmodel, hsrc, &scores, &n_scores);
248 printf_s("pred(original): %d\r\n", ret);
249 if (F_ERR_NONE != ret) { goto finally; }
250 if (NULL != scores && 0 < n_scores) {
251 INT i_score;
252 printf_s(" # of scores = %zd\r\n", n_scores);
253 for (i_score = 0; i_score < n_scores; i_score++) {
254 printf_s(" label%d: %.6e\r\n", i_score, scores[i_score]);
255 }
256 }
257
259 // フィルタをかけた画像を推論する
261
262 // ここでは単純な平均化フィルタ
263 ret = fnFIE_img_get_params(hsrc, &ch, &type, &step, &w, &h);
264 if (F_ERR_NONE != ret) { goto finally; }
265 hfiltered = fnFIE_img_root_alloc(type, ch, w, h);
266 if (NULL == hfiltered) { ret = F_ERR_NOMEMORY; goto finally; }
267 ret = fnFIE_average(hsrc, hfiltered, 0, 0);
268 if (F_ERR_NONE != ret) { goto finally; }
269
270 // 推論実行
271 ret = fnPDL_predict(hmodel, hfiltered, &scores, &n_scores);
272 printf_s("pred(average): %d\r\n", ret);
273 if (F_ERR_NONE != ret) { goto finally; }
274 if (NULL != scores && 0 < n_scores) {
275 INT i_score;
276 printf_s(" # of scores = %zd\r\n", n_scores);
277 for (i_score = 0; i_score < n_scores; i_score++) {
278 printf_s(" label%d: %.6e\r\n", i_score, scores[i_score]);
279 }
280 }
281 printf_s("\r\n");
282
283finally:
284 // ハンドルの破棄
285 fnPDL_dispose(hmodel);
286
287 //------- ここまで -------//
288
289 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
290 if (NULL != hfiltered) { fnFIE_free_object(hfiltered); }
291 if (NULL != scores) { fnOAL_free(scores); }
292
293 // 終了処理
294 fnFIE_teardown();
295
296 return ret;
297}
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
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
@ CLASSIFICATION
Definition: fv_pdl.h:50

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.