WIL-PDL Reference ( C++ ) 1.0.1
サンプルコード ( MVCNN )

MVCNN のサンプルコードです

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

Documentation copyright © 2008 FAST Corporation. https://www.fast-corp.co.jp/
Generated on Sun Jan 15 2023 13:40:43 for WIL-PDL Reference ( C++ ) 1.0.1 by Doxygen 1.9.5.