WIL-PDL Reference ( C++ ) 1.0.6-gpu
サンプルコード ( セマンティックセグメンテーション )

セマンティックセグメンテーションのサンプルコードです

  • 最も単純なサンプルコード: L.5- smp_semseg_simple()
  • ベンチマークを計測するサンプルコード: L.87: smp_semseg_benchmark()
  • 推論結果画像から FIE の関数を使用して各カテゴリのマスク画像 ( 二値画像 ) を取得するサンプルコード: L.168: smp_semseg_with_fie()
1#pragma once
2#include "samples.h"
3#include "oal_aloc.h"
4
5int smp_semseg_simple(const char* model_name, const char* img_name)
6{
7 INT ret = F_ERR_NONE;
8 FHANDLE hsrc = NULL;
9 FHANDLE hdst = NULL;
10 H_MODEL hmodel = NULL;
11 MODEL_CATEGORY model_category;
12 MODEL_BACKEND model_backend;
13 INT ch, w, h;
14
15 // FIEライブラリの使用前に必ずコールする必要があります。
16 fnFIE_setup();
17
18 // 画像読込
19 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
20 printf_s("img: %d\r\n", ret);
21 if (F_ERR_NONE != ret) { goto finally; }
22
23 //------- ここから -------//
24
25 // ライセンスチェック
26 ret = fnPDL_check_license();
27 printf_s("lic : %d\r\n", ret);
28 if (F_ERR_NONE != ret) { goto finally; }
29
30 // ハンドルの生成
31 hmodel = fnPDL_create_handle();
32 if (NULL == hmodel) {
33 ret = F_ERR_NOMEMORY;
34 printf_s("failed to create handle\r\n");
35 goto finally;
36 }
37 else { printf_s("handle created\r\n"); }
38
39 // モデル読込
40 ret = fnPDL_load_model(model_name, hmodel);
41 printf_s("load: %d\r\n", ret);
42 if (F_ERR_NONE != ret) { goto finally; }
43
44 // モデルの種別の確認
45 ret = fnPDL_get_model_category(hmodel, &model_category);
46 printf_s("par : %d category=%d\r\n", ret, model_category);
47 if (F_ERR_NONE != ret) { goto finally; }
48 if (SEMANTIC_SEGMENTATION != model_category) {
49 ret = F_ERR_INVALID_OBJECT;
50 printf_s("unmatch model\r\n");
51 goto finally;
52 }
53
54 // モデルの推論バックエンドの確認
55 ret = fnPDL_get_model_backend(hmodel, &model_backend);
56 printf_s("ret : %d backend=%d\r\n", ret, model_backend);
57 if (F_ERR_NONE != ret) { goto finally; }
58
59 // モデルの期待する画像パラメータの確認
60 ret = fnPDL_get_input_image_size(hmodel, &ch, &w, &h);
61 printf_s("par : %d ch=%d w=%d h=%d\r\n", ret, ch, w, h);
62 if (F_ERR_NONE != ret) { goto finally; }
63
64 // セグメンテーション結果画像の確保 ( セマンティックセグメンテーションでは、元画像と同じサイズで画像型は US16、チャネル数は 1 )
65 hdst = fnFIE_img_root_alloc(F_IMG_US16, 1, w, h);
66 if (NULL == hdst) {
67 ret = F_ERR_NOMEMORY;
68 printf_s("hdst allocation error\r\n");
69 goto finally;
70 }
71
72 // 推論実行
73 ret = fnPDL_predict_segmentation(hmodel, hsrc, hdst);
74 printf_s("pred: %d\r\n", ret);
75 if (F_ERR_NONE != ret) { goto finally; }
76
77 {
78 DOUBLE maxs[1] = {0};
79 DOUBLE mins[1] = {0};
80 // 各ピクセルにカテゴリ番号が格納されているので、その最大値を取得
81 INT max_cate_id = 0;
82
83 // 画像全体のリージョンに対して fnFIE_img_maxpixel() を使用することも可
84 ret = fnFIE_img_minmaxdens(hdst, mins, maxs);
85 if (F_ERR_NONE != ret) { goto finally; }
86 max_cate_id = (INT)maxs[0];
87 // 最大値が 0 となるのは、全てのピクセルが背景と判定されたとき
88 if (0 == max_cate_id) { printf_s("all pixels are background\r\n"); }
89 // 1 以上となるのは、いずれかのカテゴリと判定されたピクセルが存在するとき ( 元が US16 なので負の値になることは無い )
90 else { printf_s("there are some masks, largest category id = %d\r\n", max_cate_id); }
91 }
92 printf_s("\r\n");
93
94finally:
95 // ハンドルの破棄
96 fnPDL_dispose(hmodel);
97
98 //------- ここまで -------//
99
100 // 終了処理
101 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
102 if (NULL != hdst) { fnFIE_free_object(hdst); }
103 fnFIE_teardown();
104
105 return ret;
106}
107
108int smp_semseg_benchmark(const char* model_name, const char* img_name, const int bench_iter)
109{
110 // benchmark 用
111 std::chrono::system_clock::time_point start, end;
112
113 INT ret = F_ERR_NONE;
114 FHANDLE hsrc = NULL;
115 FHANDLE hdst = NULL;
116 H_MODEL hmodel = NULL;
117 MODEL_CATEGORY model_category;
118
119 // FIEライブラリの使用前に必ずコールする必要があります。
120 fnFIE_setup();
121
122 // 画像読込
123 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
124 printf_s("img: %d\r\n", ret);
125 if (F_ERR_NONE != ret) { goto finally; }
126
127 // ライセンスチェック
128 ret = fnPDL_check_license();
129 printf_s("lic : %d\r\n", ret);
130 if (F_ERR_NONE != ret) { goto finally; }
131
132 // ハンドルの生成
133 hmodel = fnPDL_create_handle();
134 if (NULL == hmodel) {
135 ret = F_ERR_NOMEMORY;
136 printf_s("failed to create handle\r\n");
137 goto finally;
138 }
139 else { printf_s("handle created\r\n"); }
140
141 // モデル読込
142 ret = fnPDL_load_model(model_name, hmodel);
143 printf_s("load: %d\r\n", ret);
144 if (F_ERR_NONE != ret) { goto finally; }
145
146 // モデルの種別の確認
147 ret = fnPDL_get_model_category(hmodel, &model_category);
148 printf_s("par : %d category=%d\r\n", ret, model_category);
149 if (F_ERR_NONE != ret) { goto finally; }
150 if (SEMANTIC_SEGMENTATION != model_category) {
151 ret = F_ERR_INVALID_OBJECT;
152 printf_s("unmatch model\r\n");
153 goto finally;
154 }
155
156 // セグメンテーション結果画像の確保 ( セマンティックセグメンテーションでは、元画像と同じサイズで画像型は US16、チャネル数は 1 )
157 hdst = fnFIE_img_root_alloc(F_IMG_US16, 1, fnFIE_img_get_width(hsrc), fnFIE_img_get_height(hsrc));
158 if (NULL == hdst) {
159 ret = F_ERR_NOMEMORY;
160 printf_s("hdst allocation error\r\n");
161 goto finally;
162 }
163
164 printf_s("--- start ---\r\n");
165 printf_s("ret, elapsed[msec], scores\r\n");
166 // 本体ループ
167 for (int i = 0; i < bench_iter; i++) {
168 double elapsed;
169
170 // 開始時刻
171 start = std::chrono::system_clock::now();
172
173 // 推論実行
174 ret = fnPDL_predict_segmentation(hmodel, hsrc, hdst);
175
176 // 完了時刻
177 end = std::chrono::system_clock::now();
178
179 if (F_ERR_NONE != ret) {
180 printf_s("pred. err: %d\r\n", ret);
181 goto finally;
182 }
183
184 // 推論時間 [msec]
185 elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / (double)1000;
186
187 // コンソールに出力
188 printf_s("%04d, %.3e\r\n", i, elapsed);
189 }
190 printf_s("--- finish ---\r\n");
191 printf_s("\r\n");
192
193finally:
194 // ハンドルの破棄
195 fnPDL_dispose(hmodel);
196
197 // 終了処理
198 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
199 if (NULL != hdst) { fnFIE_free_object(hdst); }
200 fnFIE_teardown();
201
202 return ret;
203}
204
205int smp_semseg_with_fie(const char* model_name, const char* img_name, const INT category_num)
206{
207 INT ret = F_ERR_NONE;
208 FHANDLE hsrc = NULL;
209 FHANDLE hdst = NULL;
210 FHANDLE hmask = NULL;
211 H_MODEL hmodel = NULL;
212 MODEL_CATEGORY model_category;
213
214 INT ch, w, h, type;
215 INT_PTR step;
216
217 // FIEライブラリの使用前に必ずコールする必要があります。
218 fnFIE_setup();
219
220 // 画像読込
221 ret = fnFIE_load_img_file(img_name, &hsrc, F_COLOR_IMG_TYPE_UC8);
222 printf_s("img: %d\r\n", ret);
223 if (F_ERR_NONE != ret) { goto finally; }
224
225 //------- ここから -------//
226
227 // ライセンスチェック
228 ret = fnPDL_check_license();
229 printf_s("lic : %d\r\n", ret);
230 if (F_ERR_NONE != ret) { goto finally; }
231
232 // ハンドルの生成
233 hmodel = fnPDL_create_handle();
234 if (NULL == hmodel) {
235 ret = F_ERR_NOMEMORY;
236 printf_s("failed to create handle\r\n");
237 goto finally;
238 }
239 else { printf_s("handle created\r\n"); }
240
241 // モデル読込
242 ret = fnPDL_load_model(model_name, hmodel);
243 printf_s("load: %d\r\n", ret);
244 if (F_ERR_NONE != ret) { goto finally; }
245
246 // モデルの種別の確認
247 ret = fnPDL_get_model_category(hmodel, &model_category);
248 printf_s("par : %d category=%d\r\n", ret, model_category);
249 if (F_ERR_NONE != ret) { goto finally; }
250 if (SEMANTIC_SEGMENTATION != model_category) {
251 ret = F_ERR_INVALID_OBJECT;
252 printf_s("unmatch model\r\n");
253 goto finally;
254 }
255
256 // セグメンテーション結果画像の確保 ( セマンティックセグメンテーションでは、元画像と同じサイズで画像型は US16、チャネル数は 1 )
257 ret = fnFIE_img_get_params(hsrc, &ch, &type, &step, &w, &h);
258 if (F_ERR_NONE != ret) { goto finally; }
259 hdst = fnFIE_img_root_alloc(F_IMG_US16, 1, w, h);
260 if (NULL == hdst) {
261 ret = F_ERR_NOMEMORY;
262 printf_s("hdst allocation error\r\n");
263 goto finally;
264 }
265
266 // 推論実行
267 ret = fnPDL_predict_segmentation(hmodel, hsrc, hdst);
268 printf_s("pred: %d\r\n", ret);
269 if (F_ERR_NONE != ret) { goto finally; }
270
271 // 各カテゴリのマスク画像の確保 ( 元画像・結果画像と同じサイズで画像型は BIN、チャネル数は 1 )
272 hmask = fnFIE_img_root_alloc(F_IMG_BIN, 1, w, h);
273 if (NULL == hmask) {
274 ret = F_ERR_NOMEMORY;
275 printf_s("hmask allocation error\r\n");
276 goto finally;
277 }
278
279 // 結果画像から各カテゴリのマスク画像を取得 ( カテゴリ番号は 1 始まり )
280 for (int i_cate = 1; i_cate <= category_num; i_cate++) {
281 // カテゴリ番号に一致するピクセルを取得することで、そのカテゴリのマスク画像を取得することが可能です
282 ret = fnFIE_band_threshold(hdst, hmask, (DOUBLE)i_cate, (DOUBLE)i_cate);
283 if (F_ERR_NONE != ret) { continue; }
284 printf_s("category=%d: hmask written\r\n", i_cate);
285 // hmask に対して、ブローブ解析やリージョンへの変換などの後処理を行うことが可能です
286 }
287 printf_s("\r\n");
288
289finally:
290 // ハンドルの破棄
291 fnPDL_dispose(hmodel);
292
293 //------- ここまで -------//
294
295 if (NULL != hsrc) { fnFIE_free_object(hsrc); }
296 if (NULL != hdst) { fnFIE_free_object(hdst); }
297 if (NULL != hmask) { fnFIE_free_object(hmask); }
298
299 // 終了処理
300 fnFIE_teardown();
301
302 return ret;
303}
304
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_load_model(const CHAR *filename, H_MODEL hmodel)
モデルの読み込み
Definition: prediction_cpp.cpp:1810
INT FVALGAPI fnPDL_predict_segmentation(const H_MODEL hmodel, const FHANDLE hsrc, FHANDLE hdst)
推論の実行 ( セグメンテーション )
Definition: prediction_cpp.cpp:2347
VOID * H_MODEL
モデルハンドル
Definition: fv_pdl.h:18
H_MODEL *FVALGAPI fnPDL_create_handle()
モデルハンドルの生成
Definition: prediction_cpp.cpp:1755
@ SEMANTIC_SEGMENTATION
Definition: fv_pdl.h:58

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.