WIL-PDL Reference ( .NET Framework ) 1.0.6
ベンチマークを計測するサンプルコード

ベンチマークを計測するサンプルコードです。
iterNum で繰り返し回数を指定します。


C# 版

  • 画像分類の場合: L.14- BenchMark()
  • アノマリー検出の場合: L.14- BenchMark()
  • 多視点画像分類の場合: L.152- BenchMarkMultiView()
  • 多視点アノマリー検出の場合: L.152- BenchMarkMultiView()
  • セマンティックセグメンテーションの場合: L.297- BenchMarkSegmentation()
  • パノプティックセグメンテーションの場合: L.297- BenchMarkSegmentation()
  • 物体検出の場合: L.405- BenchMarkObjectDetection()
  • マルチラベル画像分類の場合: L.14- BenchMark()
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using FVIL;
7using FVIL.Data;
8using FVIL.PDL;
9
10namespace SamplesCS
11{
12 partial class Program
13 {
14 static void BenchMark(String modelPath, String imagePath, UInt32 iterNum)
15 {
16 // 閾値は任意、必要であれば引数としても良い
17 const Double threshold = 10.0;
18 // アノマリー検出での異常度マップ画像の型 ( UC8 または F32 )
19 const ImageType anomaly_map_image_type = ImageType.UC8;
20
21 // 計測回数のエラーチェック
22 if (0 >= iterNum)
23 {
24 Console.WriteLine("iterNum must be positive ( >0 )");
25 return;
26 }
27
28 try
29 {
30 var has_license = Model.CheckLicense();
31 if (false == has_license) { throw new Exception("no license"); }
32
33 // モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
34 Model model = new Model(modelPath);
35
36 // 推論対象画像の読込
37 CFviImage image = new CFviImage(imagePath);
38
39 // 推論対象画像の有効性確認
40 var is_valid_img = model.IsValidImage(image);
41 if (false == is_valid_img) { throw new Exception("invalid image"); }
42
43 // アノマリー検出での異常度マップ画像
44 CFviImage anomaly_map = new CFviImage(image.HorzSize, image.VertSize, anomaly_map_image_type, 1);
45
46 // 計測用のストップウォッチ
47 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
48
49 // 計測結果のリスト
50 long[] msecs = new long[iterNum];
51
52 var contents = "msec, score0,...";
53 Console.WriteLine("msec, score0,...");
54 for (var i = 0; i < iterNum; i++)
55 {
56 switch (model.ModelCategory)
57 {
58 case ModelCategory.Classification:// 画像分類
59 {
60 // 計測開始
61 sw.Start();
62
63 // 推論実行
64 var scores = model.PredictClassification(image);
65
66 // 計測終了
67 sw.Stop();
68
69 // 結果の文字列
70 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
71 for (var i_score = 0; i_score < scores.Length; i_score++)
72 {
73 contents += $", {scores[i_score]}";
74 }
75 }
76 break;
77 case ModelCategory.AnomalyDetection:// アノマリー検出
78 {
79 // 計測開始
80 sw.Start();
81
82 // 推論実行
83 var tuple = model.PredictAnomaly(image, (float)threshold, anomaly_map);
84
85 // 計測終了
86 sw.Stop();
87
88 // 結果の文字列
89 //contents = $"{sw.ElapsedMilliseconds:000}";
90 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
91 contents += ", " + (tuple.Item1 ? "anomaly" : "normal") + $", {tuple.Item2}";
92 }
93 break;
94 case ModelCategory.MultiLabelClassification:// マルチラベル画像分類
95 {
96 // 計測開始
97 sw.Start();
98
99 // 推論実行
100 var scores = model.PredictMultiLabelClassification(image);
101
102 // 計測終了
103 sw.Stop();
104
105 // 結果の文字列
106 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
107 for (var i_score = 0; i_score < scores.Length; i_score++)
108 {
109 contents += $", {scores[i_score]}";
110 }
111 }
112 break;
113 case ModelCategory.MultiViewCNN:
114 case ModelCategory.MultiViewAD:
115 case ModelCategory.SemanticSegmentation:
116 case ModelCategory.PanopticSegmentation:
117 case ModelCategory.Unknown:
118 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
119 }
120
121 // リストへ格納
122 msecs[i] = sw.ElapsedMilliseconds;
123
124 Console.WriteLine(contents);
125
126 // ファイルに保存したいとき
127 // - "result.csv" は任意のパスを設定
128 // - 上記の contents 生成に改行を追加
129 //System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
130
131 // 次の計測のためのリセット
132 sw.Reset();
133 }
134
135 // 全体の結果
136 Console.WriteLine($"Ave: {msecs.Average():.00}");
137 Console.WriteLine($"Min: {msecs.Min():.00}");
138 Console.WriteLine($"Max: {msecs.Max():.00}");
139 }
140 catch (CFviException ex)
141 {
142 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
143 Console.WriteLine(ex.StackTrace);
144 }
145 catch (Exception ex)
146 {
147 Console.WriteLine($"Message={ex.Message}");
148 Console.WriteLine(ex.StackTrace);
149 }
150 }
151
152 static void BenchMarkMultiView(String modelPath, String imageFolder, UInt32 iterNum)
153 {
154 // 閾値は任意、必要であれば引数としても良い
155 const Double threshold = 10.0;
156 // アノマリー検出での異常度マップ画像の型 ( UC8 または F32 )
157 const ImageType anomaly_map_image_type = ImageType.UC8;
158
159 // 計測回数のエラーチェック
160 if (0 >= iterNum)
161 {
162 Console.WriteLine("iterNum must be positive ( >0 )");
163 return;
164 }
165
166 try
167 {
168 var has_license = Model.CheckLicense();
169 if (false == has_license) { throw new Exception("no license"); }
170
171 // モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
172 Model model = new Model(modelPath);
173
174 // モデル種別のエラーチェック
175 switch (model.ModelCategory)
176 {
177 case ModelCategory.MultiViewCNN:
178 case ModelCategory.MultiViewAD:
179 break;
180 case ModelCategory.Unknown:
181 case ModelCategory.Classification:
182 case ModelCategory.AnomalyDetection:
183 case ModelCategory.SemanticSegmentation:
184 case ModelCategory.PanopticSegmentation:
185 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
186 }
187
188 // 推論対象画像の読込と有効性の確認
189 List<CFviImage> target_images = new List<CFviImage>();
190 for (var i_img = 0; i_img < model.NumViews; i_img++)
191 {
192 // imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
193 var img_path = System.IO.Path.Combine(imageFolder, String.Format("{0}.bmp", i_img));
194 var image = new CFviImage(img_path);
195
196 // 推論対象画像の有効性確認
197 var is_valid_img = model.IsValidImage(image);
198 if (false == is_valid_img) { throw new Exception("invalid image"); }
199
200 // 推論対象画像のリストに追加
201 target_images.Add(image);
202 }
203
204 // アノマリー検出での異常度マップ画像
205 CFviImage anomaly_map = new CFviImage(target_images[0].HorzSize, target_images[0].VertSize, anomaly_map_image_type, 1);
206
207 // 計測用のストップウォッチ
208 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
209
210 // 計測結果のリスト
211 long[] msecs = new long[iterNum];
212
213 var contents = "msec, score0,...";
214 Console.WriteLine("msec, score0,...");
215 for (var i = 0; i < iterNum; i++)
216 {
217 // モデル種別のエラーチェック
218 switch (model.ModelCategory)
219 {
220 case ModelCategory.MultiViewCNN:
221 {
222 // 計測開始
223 sw.Start();
224
225 // 推論実行
226 var scores = model.PredictMultiViewCNN(target_images);
227
228 // 計測終了
229 sw.Stop();
230
231 // 結果の文字列
232 //contents = $"{sw.ElapsedMilliseconds:000}";
233 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
234 for (var i_score = 0; i_score < scores.Length; i_score++)
235 {
236 contents += $", {scores[i_score]}";
237 }
238 }
239 break;
240 case ModelCategory.MultiViewAD:
241 {
242 // 計測開始
243 sw.Start();
244
245 // 推論実行
246 var tuple = model.PredictMultiViewAD(target_images, (float)threshold, anomaly_map);
247
248 // 計測終了
249 sw.Stop();
250
251 // 結果の文字列
252 //contents = $"{sw.ElapsedMilliseconds:000}";
253 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
254 contents += ", " + (tuple.Item1 ? "anomaly" : "normal") + $", {tuple.Item2}";
255 }
256 break;
257 case ModelCategory.Unknown:
258 case ModelCategory.Classification:
259 case ModelCategory.AnomalyDetection:
260 case ModelCategory.SemanticSegmentation:
261 case ModelCategory.PanopticSegmentation:
262 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
263 }
264
265
266 // リストへ格納
267 msecs[i] = sw.ElapsedMilliseconds;
268
269 Console.WriteLine(contents);
270
271 // ファイルに保存したいとき
272 // - "result.csv" は任意のパスを設定
273 // - 上記の contents 生成に改行を追加
274 //System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
275
276 // 次の計測のためのリセット
277 sw.Reset();
278 }
279
280 // 全体の結果
281 Console.WriteLine($"Ave: {msecs.Average():.00}");
282 Console.WriteLine($"Min: {msecs.Min():.00}");
283 Console.WriteLine($"Max: {msecs.Max():.00}");
284 }
285 catch (CFviException ex)
286 {
287 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
288 Console.WriteLine(ex.StackTrace);
289 }
290 catch (Exception ex)
291 {
292 Console.WriteLine($"Message={ex.Message}");
293 Console.WriteLine(ex.StackTrace);
294 }
295 }
296
297 static void BenchMarkSegmentation(String modelPath, String imagePath, UInt32 iterNum)
298 {
299 // 計測回数のエラーチェック
300 if (0 >= iterNum)
301 {
302 Console.WriteLine("iterNum must be positive ( >0 )");
303 return;
304 }
305
306 try
307 {
308 var has_license = Model.CheckLicense();
309 if (false == has_license) { throw new Exception("no license"); }
310
311 // モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
312 Model model = new Model(modelPath);
313
314 // 推論対象画像の読込
315 CFviImage image = new CFviImage(imagePath);
316
317 // 推論対象画像の有効性確認
318 var is_valid_img = model.IsValidImage(image);
319 if (false == is_valid_img) { throw new Exception("invalid image"); }
320
321 // 計測用のストップウォッチ
322 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
323
324 // 計測結果のリスト
325 long[] msecs = new long[iterNum];
326
327 var contents = "msec, score0,...";
328 Console.WriteLine("msec, score0,...");
329 for (var i = 0; i < iterNum; i++)
330 {
331 switch (model.ModelCategory)
332 {
333 case ModelCategory.SemanticSegmentation:// セマンティックセグメンテーション
334 {
335 // 計測開始
336 sw.Start();
337
338 // 推論実行: 後処理を行わない最も単純なメソッド
339 var segm_result_image = model.PredictSemanticSegmentation(image);
340
341 // 計測終了
342 sw.Stop();
343
344 // 結果の文字列
345 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
346 }
347 break;
348 case ModelCategory.PanopticSegmentation:
349 {
350 // パノプティックセグメンテーション固有の検出パラメータの設定 ( ここではデフォルト値と同じ値を設定 )
351 //model.SetPanopticSegmentationParams(1024, 0.1, 32, -1);
352
353 // 計測開始
354 sw.Start();
355
356 // 推論実行: 後処理を行わない最も単純なメソッド
357 var segm_result_image = model.PredictSemanticSegmentation(image);
358
359 // 計測終了
360 sw.Stop();
361
362 // 結果の文字列
363 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
364 }
365 break;
366 case ModelCategory.Classification:
367 case ModelCategory.AnomalyDetection:
368 case ModelCategory.MultiViewCNN:
369 case ModelCategory.MultiViewAD:
370 case ModelCategory.Unknown:
371 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
372 }
373
374 // リストへ格納
375 msecs[i] = sw.ElapsedMilliseconds;
376
377 Console.WriteLine(contents);
378
379 // ファイルに保存したいとき
380 // - "result.csv" は任意のパスを設定
381 // - 上記の contents 生成に改行を追加
382 //System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
383
384 // 次の計測のためのリセット
385 sw.Reset();
386 }
387
388 // 全体の結果
389 Console.WriteLine($"Ave: {msecs.Average():.00}");
390 Console.WriteLine($"Min: {msecs.Min():.00}");
391 Console.WriteLine($"Max: {msecs.Max():.00}");
392 }
393 catch (CFviException ex)
394 {
395 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
396 Console.WriteLine(ex.StackTrace);
397 }
398 catch (Exception ex)
399 {
400 Console.WriteLine($"Message={ex.Message}");
401 Console.WriteLine(ex.StackTrace);
402 }
403 }
404
405 static void BenchMarkObjectDetection(String modelPath, String imagePath, UInt32 iterNum)
406 {
407 // 計測回数のエラーチェック
408 if (0 >= iterNum)
409 {
410 Console.WriteLine("iterNum must be positive ( >0 )");
411 return;
412 }
413
414 try
415 {
416 var has_license = Model.CheckLicense();
417 if (false == has_license) { throw new Exception("no license"); }
418
419 // モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
420 Model model = new Model(modelPath);
421
422 // 推論対象画像の読込
423 CFviImage image = new CFviImage(imagePath);
424
425 // 推論対象画像の有効性確認
426 var is_valid_img = model.IsValidImage(image);
427 if (false == is_valid_img) { throw new Exception("invalid image"); }
428
429 // 計測用のストップウォッチ
430 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
431
432 // 計測結果のリスト
433 long[] msecs = new long[iterNum];
434
435 var contents = "msec, num_detections";
436 Console.WriteLine("msec, num_detections");
437 for (var i = 0; i < iterNum; i++)
438 {
439 switch (model.ModelCategory)
440 {
441 case ModelCategory.ObjectDetection:// 物体検出
442 {
443 // 計測開始
444 sw.Start();
445
446 // 推論実行
447 var detections = model.PredictObjectDetection(image);
448
449 // 計測終了
450 sw.Stop();
451
452 // 結果の文字列
453 contents = $"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
454 contents += $", {detections.Count()}";
455 }
456 break;
457 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
458 }
459
460 // リストへ格納
461 msecs[i] = sw.ElapsedMilliseconds;
462
463 Console.WriteLine(contents);
464
465 // ファイルに保存したいとき
466 // - "result.csv" は任意のパスを設定
467 // - 上記の contents 生成に改行を追加
468 //System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
469
470 // 次の計測のためのリセット
471 sw.Reset();
472 }
473
474 // 全体の結果
475 Console.WriteLine($"Ave: {msecs.Average():.00}");
476 Console.WriteLine($"Min: {msecs.Min():.00}");
477 Console.WriteLine($"Max: {msecs.Max():.00}");
478 }
479 catch (CFviException ex)
480 {
481 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
482 Console.WriteLine(ex.StackTrace);
483 }
484 catch (Exception ex)
485 {
486 Console.WriteLine($"Message={ex.Message}");
487 Console.WriteLine(ex.StackTrace);
488 }
489 }
490 }
491}
推論するモデルを扱うクラス
Definition: PredictionCS.cs:250
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:271
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:992
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:675
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:425
IEnumerable< ObjectDetectionData > PredictObjectDetection(CFviImage targetImage)
推論の実行(物体検出)
Definition: PredictionCS.cs:1729
CFviImage PredictSemanticSegmentation(CFviImage targetImage)
推論の実行(セマンティックセグメンテーション)
Definition: PredictionCS.cs:1440
Tuple< Boolean, float > PredictMultiViewAD(IEnumerable< CFviImage > targetImages, float threshold)
推論の実行(多視点アノマリー検出)
Definition: PredictionCS.cs:1305
float[] PredictMultiLabelClassification(CFviImage targetImage)
推論の実行(マルチラベル画像分類)
Definition: PredictionCS.cs:1760
Int32 NumViews
読み込んだモデルが期待する視点数
Definition: PredictionCS.cs:314
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:1083
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:1213
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:21
ModelCategory
モデルの種別
Definition: PredictionCS.cs:32
FVILの最上位ネームスペース
Definition: PredictionCS.cs:16

VB 版

  • 画像分類の場合: L.11- BenchMark()
  • アノマリー検出の場合: L.11- BenchMark()
  • 多視点画像分類の場合: L.130- BenchMarkMultiView()
  • 多視点アノマリー検出の場合: L.130- BenchMarkMultiView()
  • セマンティックセグメンテーションの場合: L.251- BenchMarkSegmentation()
  • パノプティックセグメンテーションの場合: L.251- BenchMarkSegmentation()
  • 物体検出の場合: L.344- BenchMarkObjectDetection()
  • マルチラベル画像分類の場合: L.11- BenchMark()
1Imports System
2Imports System.Collections.Generic
3Imports System.IO
4Imports System.Linq
5Imports FVIL
6Imports FVIL.Data
7Imports FVIL.PDL
8
9Namespace SamplesCS
10 Partial Class Program
11 Private Shared Sub BenchMark(modelPath As String, imagePath As String, iterNum As UInteger)
12 ' 閾値は任意、必要であれば引数としても良い
13 Const threshold = 10.0
14 ' アノマリー検出での異常度マップ画像の型 ( UC8 または F32 )
15 Const anomaly_map_image_type = ImageType.UC8
16
17 ' 計測回数のエラーチェック
18 If 0 >= iterNum Then
19 Console.WriteLine("iterNum must be positive ( >0 )")
20 Return
21 End If
22
23 Try
24 Dim has_license = PDL.Model.CheckLicense()
25 If False = has_license Then
26 Throw New Exception("no license")
27 End If
28
29 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
30 Dim model As Model = New Model(modelPath)
31
32 ' 推論対象画像の読込
33 Dim image As CFviImage = New CFviImage(imagePath)
34
35 ' 推論対象画像の有効性確認
36 Dim is_valid_img = model.IsValidImage(image)
37 If False = is_valid_img Then
38 Throw New Exception("invalid image")
39 End If
40
41 ' アノマリー検出での異常度マップ画像
42 Dim anomaly_map As CFviImage = New CFviImage(image.HorzSize, image.VertSize, anomaly_map_image_type, 1)
43
44 ' 計測用のストップウォッチ
45 Dim sw As Stopwatch = New Stopwatch()
46
47 ' 計測結果のリスト
48 Dim msecs = New Long(iterNum - 1) {}
49
50 Dim contents = "msec, score0,..."
51 Console.WriteLine("msec, score0,...")
52 For i = 0 To iterNum - 1
53 Select Case model.ModelCategory
54 Case ModelCategory.Classification ' 画像分類
55 ' 計測開始
56 sw.Start()
57
58 ' 推論実行
59 Dim scores = model.PredictClassification(image)
60
61 ' 計測終了
62 sw.Stop()
63
64 ' 結果の文字列
65 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
66 For i_score = 0 To scores.Length - 1
67 contents += $", {scores(i_score)}"
68 Next
69 Case ModelCategory.AnomalyDetection ' アノマリー検出
70 ' 計測開始
71 sw.Start()
72
73 ' 推論実行
74 Dim tuple = model.PredictAnomaly(image, threshold, anomaly_map)
75
76 ' 計測終了
77 sw.Stop()
78
79 ' 結果の文字列
80 'contents = $"{sw.ElapsedMilliseconds:000}";
81 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
82 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
83 Case ModelCategory.MultiLabelClassification ' マルチラベル画像分類
84 ' 計測開始
85 sw.Start()
86
87 ' 推論実行
88 Dim scores = model.PredictMultiLabelClassification(image)
89
90 ' 計測終了
91 sw.Stop()
92
93 ' 結果の文字列
94 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
95 For i_score = 0 To scores.Length - 1
96 contents += $", {scores(i_score)}"
97 Next
98
99 Case Else
100 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
101 End Select
102
103 ' リストへ格納
104 msecs(i) = sw.ElapsedMilliseconds
105
106 Console.WriteLine(contents)
107
108 ' ファイルに保存したいとき
109 ' - "result.csv" は任意のパスを設定
110 ' - 上記の contents 生成に改行を追加
111 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
112
113 ' 次の計測のためのリセット
114 sw.Reset()
115 Next
116
117 ' 全体の結果
118 Console.WriteLine($"Ave: {msecs.Average():.00}")
119 Console.WriteLine($"Min: {msecs.Min():.00}")
120 Console.WriteLine($"Max: {msecs.Max():.00}")
121 Catch ex As CFviException
122 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
123 Console.WriteLine(ex.StackTrace)
124 Catch ex As Exception
125 Console.WriteLine($"Message={ex.Message}")
126 Console.WriteLine(ex.StackTrace)
127 End Try
128 End Sub
129
130 Private Shared Sub BenchMarkMultiView(modelPath As String, imageFolder As String, iterNum As UInteger)
131 ' 閾値は任意、必要であれば引数としても良い
132 Const threshold = 10.0
133 ' アノマリー検出での異常度マップ画像の型 ( UC8 または F32 )
134 Const anomaly_map_image_type = ImageType.UC8
135
136 ' 計測回数のエラーチェック
137 If 0 >= iterNum Then
138 Console.WriteLine("iterNum must be positive ( >0 )")
139 Return
140 End If
141
142 Try
143 Dim has_license = PDL.Model.CheckLicense()
144 If False = has_license Then
145 Throw New Exception("no license")
146 End If
147
148 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
149 Dim model As Model = New Model(modelPath)
150
151 ' モデル種別のエラーチェック
152 Select Case model.ModelCategory
153 Case ModelCategory.MultiViewCNN, ModelCategory.MultiViewAD
154 Case Else
155 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
156 End Select
157
158 ' 推論対象画像の読込と有効性の確認
159 Dim target_images As List(Of CFviImage) = New List(Of CFviImage)()
160 For i_img = 0 To model.NumViews - 1
161 ' imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
162 Dim img_path = Path.Combine(imageFolder, String.Format("{0}.bmp", i_img))
163 Dim image = New CFviImage(img_path)
164
165 ' 推論対象画像の有効性確認
166 Dim is_valid_img = model.IsValidImage(image)
167 If False = is_valid_img Then
168 Throw New Exception("invalid image")
169 End If
170
171 ' 推論対象画像のリストに追加
172 target_images.Add(image)
173 Next
174
175 ' アノマリー検出での異常度マップ画像
176 Dim anomaly_map As CFviImage = New CFviImage(target_images(0).HorzSize, target_images(0).VertSize, anomaly_map_image_type, 1)
177
178 ' 計測用のストップウォッチ
179 Dim sw As Stopwatch = New Stopwatch()
180
181 ' 計測結果のリスト
182 Dim msecs = New Long(iterNum - 1) {}
183
184 Dim contents = "msec, score0,..."
185 Console.WriteLine("msec, score0,...")
186 For i = 0 To iterNum - 1
187 ' モデル種別のエラーチェック
188 Select Case model.ModelCategory
189 Case ModelCategory.MultiViewCNN
190 ' 計測開始
191 sw.Start()
192
193 ' 推論実行
194 Dim scores = model.PredictMultiViewCNN(target_images)
195
196 ' 計測終了
197 sw.Stop()
198
199 ' 結果の文字列
200 'contents = $"{sw.ElapsedMilliseconds:000}";
201 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
202 For i_score = 0 To scores.Length - 1
203 contents += $", {scores(i_score)}"
204 Next
205 Case ModelCategory.MultiViewAD
206 ' 計測開始
207 sw.Start()
208
209 ' 推論実行
210 Dim tuple = model.PredictMultiViewAD(target_images, threshold, anomaly_map)
211
212 ' 計測終了
213 sw.Stop()
214
215 ' 結果の文字列
216 'contents = $"{sw.ElapsedMilliseconds:000}";
217 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
218 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
219 Case Else
220 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
221 End Select
222
223
224 ' リストへ格納
225 msecs(i) = sw.ElapsedMilliseconds
226
227 Console.WriteLine(contents)
228
229 ' ファイルに保存したいとき
230 ' - "result.csv" は任意のパスを設定
231 ' - 上記の contents 生成に改行を追加
232 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
233
234 ' 次の計測のためのリセット
235 sw.Reset()
236 Next
237
238 ' 全体の結果
239 Console.WriteLine($"Ave: {msecs.Average():.00}")
240 Console.WriteLine($"Min: {msecs.Min():.00}")
241 Console.WriteLine($"Max: {msecs.Max():.00}")
242 Catch ex As CFviException
243 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
244 Console.WriteLine(ex.StackTrace)
245 Catch ex As Exception
246 Console.WriteLine($"Message={ex.Message}")
247 Console.WriteLine(ex.StackTrace)
248 End Try
249 End Sub
250
251 Private Shared Sub BenchMarkSegmentation(modelPath As String, imagePath As String, iterNum As UInteger)
252 ' 計測回数のエラーチェック
253 If 0 >= iterNum Then
254 Console.WriteLine("iterNum must be positive ( >0 )")
255 Return
256 End If
257
258 Try
259 Dim has_license = PDL.Model.CheckLicense()
260 If False = has_license Then
261 Throw New Exception("no license")
262 End If
263
264 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
265 Dim model As Model = New Model(modelPath)
266
267 ' 推論対象画像の読込
268 Dim image As CFviImage = New CFviImage(imagePath)
269
270 ' 推論対象画像の有効性確認
271 Dim is_valid_img = model.IsValidImage(image)
272 If False = is_valid_img Then
273 Throw New Exception("invalid image")
274 End If
275
276 ' 計測用のストップウォッチ
277 Dim sw As Stopwatch = New Stopwatch()
278
279 ' 計測結果のリスト
280 Dim msecs = New Long(iterNum - 1) {}
281
282 Dim contents = "msec, score0,..."
283 Console.WriteLine("msec, score0,...")
284 For i = 0 To iterNum - 1
285 Select Case model.ModelCategory
286 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
287 ' 計測開始
288 sw.Start()
289
290 ' 推論実行: 後処理を行わない最も単純なメソッド
291 Dim segm_result_image = model.PredictSemanticSegmentation(image)
292
293 ' 計測終了
294 sw.Stop()
295
296 ' 結果の文字列
297 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
298 Case ModelCategory.PanopticSegmentation
299 ' パノプティックセグメンテーション固有の検出パラメータの設定 ( ここではデフォルト値と同じ値を設定 )
300 'model.SetPanopticSegmentationParams(1024, 0.1, 32, -1);
301
302 ' 計測開始
303 sw.Start()
304
305 ' 推論実行: 後処理を行わない最も単純なメソッド
306 Dim segm_result_image = model.PredictSemanticSegmentation(image)
307
308 ' 計測終了
309 sw.Stop()
310
311 ' 結果の文字列
312 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
313 Case Else
314 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
315 End Select
316
317 ' リストへ格納
318 msecs(i) = sw.ElapsedMilliseconds
319
320 Console.WriteLine(contents)
321
322 ' ファイルに保存したいとき
323 ' - "result.csv" は任意のパスを設定
324 ' - 上記の contents 生成に改行を追加
325 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
326
327 ' 次の計測のためのリセット
328 sw.Reset()
329 Next
330
331 ' 全体の結果
332 Console.WriteLine($"Ave: {msecs.Average():.00}")
333 Console.WriteLine($"Min: {msecs.Min():.00}")
334 Console.WriteLine($"Max: {msecs.Max():.00}")
335 Catch ex As CFviException
336 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
337 Console.WriteLine(ex.StackTrace)
338 Catch ex As Exception
339 Console.WriteLine($"Message={ex.Message}")
340 Console.WriteLine(ex.StackTrace)
341 End Try
342 End Sub
343
344 Private Shared Sub BenchMarkObjectDetection(modelPath As String, imagePath As String, iterNum As UInteger)
345 ' 計測回数のエラーチェック
346 If 0 >= iterNum Then
347 Console.WriteLine("iterNum must be positive ( >0 )")
348 Return
349 End If
350
351 Try
352 Dim has_license = PDL.Model.CheckLicense()
353 If False = has_license Then
354 Throw New Exception("no license")
355 End If
356
357 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
358 Dim model As Model = New Model(modelPath)
359
360 ' 推論対象画像の読込
361 Dim image As CFviImage = New CFviImage(imagePath)
362
363 ' 推論対象画像の有効性確認
364 Dim is_valid_img = model.IsValidImage(image)
365 If False = is_valid_img Then
366 Throw New Exception("invalid image")
367 End If
368
369 ' 計測用のストップウォッチ
370 Dim sw As Stopwatch = New Stopwatch()
371
372 ' 計測結果のリスト
373 Dim msecs = New Long(iterNum - 1) {}
374
375 Dim contents = "msec, num_detections"
376 Console.WriteLine("msec, num_detections")
377 For i = 0 To iterNum - 1
378 Select Case model.ModelCategory
379 Case ModelCategory.ObjectDetection ' 物体検出
380 ' 計測開始
381 sw.Start()
382
383 ' 推論実行
384 Dim detections = model.PredictObjectDetection(image)
385
386 ' 計測終了
387 sw.Stop()
388
389 ' 結果の文字列
390 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
391 contents += $", {detections.Count()}"
392 Case Else
393 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
394 End Select
395
396 ' リストへ格納
397 msecs(i) = sw.ElapsedMilliseconds
398
399 Console.WriteLine(contents)
400
401 ' ファイルに保存したいとき
402 ' - "result.csv" は任意のパスを設定
403 ' - 上記の contents 生成に改行を追加
404 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
405
406 ' 次の計測のためのリセット
407 sw.Reset()
408 Next
409
410 ' 全体の結果
411 Console.WriteLine($"Ave: {msecs.Average():.00}")
412 Console.WriteLine($"Min: {msecs.Min():.00}")
413 Console.WriteLine($"Max: {msecs.Max():.00}")
414 Catch ex As CFviException
415 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
416 Console.WriteLine(ex.StackTrace)
417 Catch ex As Exception
418 Console.WriteLine($"Message={ex.Message}")
419 Console.WriteLine(ex.StackTrace)
420 End Try
421 End Sub
422 End Class
423End Namespace

Documentation copyright © 2026 TOKYO ELECTRON DEVICE LIMITED https://www.teldevice.co.jp/
Generated on Fri Jun 19 2026 14:08:07 for WIL-PDL Reference ( .NET Framework ) 1.0.6 by Doxygen 1.9.5.