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

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

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

Documentation copyright © 2025 TOKYO ELECTRON DEVICE LIMITED https://www.teldevice.co.jp/
Generated on Thu Jan 23 2025 16:11:31 for WIL-PDL Reference ( C++ ) 1.0.5 by Doxygen 1.9.5.