WIL-PDL Reference ( C++ ) 1.0.6
サンプルコード ( アノマリー検出 )

アノマリー検出のサンプルコードです

  • 最も単純なサンプルコード: L.5- smp_ad_simple()
  • ベンチマークを計測するサンプルコード: L.101: smp_ad_benchmark()
  • FIE の関数を前処理として行うサンプルコード: L.195: smp_ad_with_fie()
1#pragma once
2#include "samples.h"
3#include "oal_aloc.h"
4
5int smp_ad_simple(const char* model_name, const char* img_name, const FLOAT threshold)
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* anomaly_data = NULL;
14 size_t anomaly_data_len;
15
16 BOOL is_anomaly = false;
17 FHANDLE hmap = NULL;
18 FLOAT max_anomaly = .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
30 // ライセンスチェック
31 ret = fnPDL_check_license();
32 printf_s("lic : %d\r\n", ret);
33 if (F_ERR_NONE != ret) { goto finally; }
34
35 // ハンドルの生成
36 hmodel = fnPDL_create_handle();
37 if (NULL == hmodel) {
38 ret = F_ERR_NOMEMORY;
39 printf_s("failed to create handle\r\n");
40 goto finally;
41 }
42 else { printf_s("handle created\r\n"); }
43
44 // モデル読込
45 ret = fnPDL_load_model(model_name, hmodel);
46 printf_s("load: %d\r\n", ret);
47 if (F_ERR_NONE != ret) { goto finally; }
48
49 // モデルの種別の確認
50 ret = fnPDL_get_model_category(hmodel, &model_category);
51 printf_s("ret : %d category=%d\r\n", ret, model_category);
52 if (F_ERR_NONE != ret) { goto finally; }
53 if (ANOMALY_DETECTION != model_category) {
54 ret = F_ERR_INVALID_OBJECT;
55 printf_s("unmatch model\r\n");
56 goto finally;
57 }
58
59 // モデルの推論バックエンドの確認
60 ret = fnPDL_get_model_backend(hmodel, &model_backend);
61 printf_s("ret : %d backend=%d\r\n", ret, model_backend);
62 if (F_ERR_NONE != ret) { goto finally; }
63
64 // モデルの期待する画像パラメータの確認
65 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
66 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
67 if (F_ERR_NONE != ret) { goto finally; }
68
69 // 推論実行
70 ret = fnPDL_predict(hmodel, hsrc, &anomaly_data, &anomaly_data_len);
71 printf_s("pred: %d\r\n", ret);
72 if (F_ERR_NONE != ret) { goto finally; }
73
74 // 後処理1: is_anomaly の判定のみ
75 ret = fnPDL_postproc_ad(hmodel, anomaly_data, anomaly_data_len, threshold, &is_anomaly, NULL, NULL);
76 printf_s(" is_anomaly: %d, %s\r\n", ret, (is_anomaly ? "true" : "false"));
77 if (F_ERR_NONE != ret) { goto finally; }
78
79 // 後処理2: 最大異常度
80 ret = fnPDL_postproc_ad(hmodel, anomaly_data, anomaly_data_len, NULL, NULL, NULL, &max_anomaly);
81 printf_s(" max_anomaly: %d, %6e\r\n", ret, max_anomaly);
82 if (F_ERR_NONE != ret) { goto finally; }
83
84 FLOAT adrs[1024] = {0};
85 FHANDLE test_org = fnFIE_img_root_import_alloc((VOID**)&adrs, 1, F_IMG_FLOAT, 32, 32, 32);
86 FHANDLE test = fnFIE_img_root_alloc(2, 1, 32, 32);
87 ret = fnFIE_img_copy_ex(test_org, test, 1, 0, 0);
88 FHANDLE test2 = fnFIE_img_root_alloc(2, 1, 34, 34);
89 ret = fnFIE_copy_border(test, test2, 1, 1, F_BORDER_CONTINUOUS, 0);
90
91 // 後処理3: 最大異常度マップ出力 ( uc8 版 )
92 hmap = fnFIE_img_root_alloc(F_IMG_UC8, 1, w, h);
93 if (NULL == hmap) {
94 ret = F_ERR_NOMEMORY;
95 printf_s("map image: allocation error\r\n");
96 goto finally;
97 }
98
99 ret = fnPDL_postproc_ad(hmodel, anomaly_data, anomaly_data_len, NULL, NULL, &hmap, NULL);
100 printf_s(" calc_map: %d\r\n", ret);
101 if (F_ERR_NONE != ret) { goto finally; }
102
103 ret = fnFIE_save_bmp("map_uc8.bmp", hmap);
104 printf_s(" save map: %d\r\n", ret);
105 if (F_ERR_NONE != ret) { goto finally; }
106 printf_s("\r\n");
107
108finally:
109 // ハンドルの破棄
110 fnPDL_dispose(hmodel);
111
112 //------- ここまで -------//
113
114 // 終了処理
115 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
116 if (NULL != anomaly_data) { fnOAL_free(anomaly_data); }
117 if (NULL != hmap) { fnFIE_free_object(hmap); }
118 fnFIE_teardown();
119
120 return ret;
121}
122
123int smp_ad_benchmark(const char* model_name, const char* img_name, const FLOAT threshold, const int bench_iter)
124{
125 // benchmark 用
126 std::chrono::system_clock::time_point start, end;
127
128 INT ret = F_ERR_NONE;
129 FHANDLE hsrc = NULL;
130 H_MODEL hmodel = NULL;
131 MODEL_CATEGORY model_category;
132 INT ch, w, h;
133 FLOAT* anomaly_data = NULL;
134 size_t anomaly_data_len;
135
136 BOOL is_anomaly = false;
137 FHANDLE hmap = NULL;
138 FLOAT max_anomaly = .0;
139
140 // FIEライブラリの使用前に必ずコールする必要があります。
141 fnFIE_setup();
142
143 // 画像読込
144 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
145 printf_s("img: %d\r\n", ret);
146 if (F_ERR_NONE != ret) { goto finally; }
147
148 // ライセンスチェック
149 ret = fnPDL_check_license();
150 printf_s("lic : %d\r\n", ret);
151 if (F_ERR_NONE != ret) { goto finally; }
152
153 // ハンドルの生成
154 hmodel = fnPDL_create_handle();
155 if (NULL == hmodel) {
156 ret = F_ERR_NOMEMORY;
157 printf_s("failed to create handle\r\n");
158 goto finally;
159 }
160 else { printf_s("handle created\r\n"); }
161
162 // モデル読込
163 ret = fnPDL_load_model(model_name, hmodel);
164 printf_s("load: %d\r\n", ret);
165 if (F_ERR_NONE != ret) { goto finally; }
166
167 // モデルの種別の確認
168 ret = fnPDL_get_model_category(hmodel, &model_category);
169 printf_s("par : %d category=%d\r\n", ret, model_category);
170 if (F_ERR_NONE != ret) { goto finally; }
171 if (ANOMALY_DETECTION != model_category) {
172 ret = F_ERR_INVALID_OBJECT;
173 printf_s("unmatch model\r\n");
174 goto finally;
175 }
176
177 // モデルの期待する画像パラメータの確認
178 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
179 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
180 if (F_ERR_NONE != ret) { goto finally; }
181
182 // uc8 で確保
183 hmap = fnFIE_img_root_alloc(F_IMG_UC8, 1, w, h);
184 if (NULL == hmap) {
185 ret = F_ERR_NOMEMORY;
186 printf_s("map image: allocation error\r\n");
187 goto finally;
188 }
189
190 printf_s("--- start ---\r\n");
191 printf_s("ret, elapsed[msec], scores\r\n");
192 // 本体ループ
193 for (int i = 0; i < bench_iter; i++) {
194 double elapsed;
195
196 // 開始時刻
197 start = std::chrono::system_clock::now();
198
199 // 推論実行
200 ret = fnPDL_predict(hmodel, hsrc, &anomaly_data, &anomaly_data_len);
201 if (F_ERR_NONE != ret) {
202 printf_s("pred. err: %d\r\n", ret);
203 goto finally;
204 }
205 // 後処理: アノマリー判定、最大異常度取得、マップ画像算出の全て
206 ret = fnPDL_postproc_ad(hmodel, anomaly_data, anomaly_data_len, threshold, &is_anomaly, &hmap, &max_anomaly);
207 if (F_ERR_NONE != ret) {
208 printf_s("postproc. err: %d\r\n", ret);
209 goto finally;
210 }
211 // 完了時刻
212 end = std::chrono::system_clock::now();
213
214 // 推論時間 [msec]
215 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / (double)1000;
216
217 // コンソールに出力
218 printf_s("%04d, %.3e, %s, %.6e\r\n", i, elapsed, (is_anomaly?"anomaly":"normal"), max_anomaly);
219 }
220 printf_s("--- finish ---\r\n");
221 printf_s("\r\n");
222
223finally:
224 // ハンドルの破棄
225 fnPDL_dispose(hmodel);
226
227 // 終了処理
228 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
229 if (NULL != anomaly_data) { fnOAL_free(anomaly_data); }
230 if (NULL != hmap) { fnFIE_free_object(hmap); }
231 fnFIE_teardown();
232
233 return ret;
234}
235
236int smp_ad_with_fie(const char* model_name, const char* img_name, const FLOAT threshold)
237{
238 INT ret = F_ERR_NONE;
239 FHANDLE hsrc = NULL;
240 H_MODEL hmodel = NULL;
241 MODEL_CATEGORY model_category;
242 FLOAT* anomaly_data = NULL;
243 size_t anomaly_data_len;
244
245 BOOL is_anomaly = false;
246 FLOAT max_anomaly = .0;
247
248 FHANDLE hfiltered = NULL;
249 INT ch, w, h, type;
250 INT_PTR step;
251
252 // FIEライブラリの使用前に必ずコールする必要があります。
253 fnFIE_setup();
254
255 // 画像読込
256 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
257 printf_s("img: %d\r\n", ret);
258 if (F_ERR_NONE != ret) { goto finally; }
259
260 //------- ここから -------//
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 (ANOMALY_DETECTION != model_category) {
286 ret = F_ERR_INVALID_OBJECT;
287 printf_s("unmatch model\r\n");
288 goto finally;
289 }
290
292 // 読み込んだ画像をそのまま推論する
294
295 // 推論実行
296 ret = fnPDL_predict(hmodel, hsrc, &anomaly_data, &anomaly_data_len);
297 printf_s("pred(original): %d\r\n", ret);
298 if (F_ERR_NONE != ret) { goto finally; }
299 // 後処理
300 ret = fnPDL_postproc_ad(hmodel, anomaly_data, anomaly_data_len, threshold, &is_anomaly, NULL, &max_anomaly);
301 printf_s("postproc(original): %d %s, %.6e\r\n", ret, (is_anomaly ? "anomaly" : "normal"), max_anomaly);
302 if (F_ERR_NONE != ret) { goto finally; }
303
305 // フィルタをかけた画像を推論する
307
308 // ここでは単純な平均化フィルタ
309 ret = fnFIE_img_get_params(hsrc, &ch, &type, &step, &w, &h);
310 if (F_ERR_NONE != ret) { goto finally; }
311 hfiltered = fnFIE_img_root_alloc(type, ch, w, h);
312 if (NULL == hfiltered) { ret = F_ERR_NOMEMORY; goto finally; }
313 ret = fnFIE_average(hsrc, hfiltered, 0, 0);
314 if (F_ERR_NONE != ret) { goto finally; }
315
316 // 推論実行
317 ret = fnPDL_predict(hmodel, hfiltered, &anomaly_data, &anomaly_data_len);
318 printf_s("pred(original): %d\r\n", ret);
319 if (F_ERR_NONE != ret) { goto finally; }
320 // 後処理
321 ret = fnPDL_postproc_ad(hmodel, anomaly_data, anomaly_data_len, threshold, &is_anomaly, NULL, &max_anomaly);
322 printf_s("postproc(original): %d %s, %.6e\r\n", ret, (is_anomaly ? "anomaly" : "normal"), max_anomaly);
323 if (F_ERR_NONE != ret) { goto finally; }
324 printf_s("\r\n");
325
326finally:
327 // ハンドルの破棄
328 fnPDL_dispose(hmodel);
329
330 //------- ここまで -------//
331
332 // 終了処理
333 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
334 if (NULL != anomaly_data) { fnOAL_free(anomaly_data); }
335 if (NULL != hfiltered) { fnFIE_free_object(hfiltered); }
336 fnFIE_teardown();
337
338 return ret;
339}
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_postproc_ad(const H_MODEL hmodel, const FLOAT *anomaly_data, const size_t num_scores, const FLOAT threshold, BOOL *is_anomaly, FHANDLE *hmap, FLOAT *max_anomaly)
推論結果の加工 ( アノマリー検出 )
Definition: prediction_cpp.cpp:2530
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
@ ANOMALY_DETECTION
Definition: fv_pdl.h:52

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.