2using System.Collections.Generic;
5using System.Threading.Tasks;
14 static void BenchMark(String modelPath, String imagePath, UInt32 iterNum)
17 const Double threshold = 10.0;
19 const ImageType anomaly_map_image_type = ImageType.UC8;
24 Console.WriteLine(
"iterNum must be positive ( >0 )");
31 if (
false == has_license) {
throw new Exception(
"no license"); }
37 CFviImage image =
new CFviImage(imagePath);
41 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
44 CFviImage anomaly_map =
new CFviImage(image.HorzSize, image.VertSize, anomaly_map_image_type, 1);
47 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
50 long[] msecs =
new long[iterNum];
52 var contents =
"msec, score0,...";
53 Console.WriteLine(
"msec, score0,...");
54 for (var i = 0; i < iterNum; i++)
70 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
71 for (var i_score = 0; i_score < scores.Length; i_score++)
73 contents += $
", {scores[i_score]}";
83 var tuple = model.
PredictAnomaly(image, (
float)threshold, anomaly_map);
90 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
91 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
106 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
107 for (var i_score = 0; i_score < scores.Length; i_score++)
109 contents += $
", {scores[i_score]}";
118 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
122 msecs[i] = sw.ElapsedMilliseconds;
124 Console.WriteLine(contents);
136 Console.WriteLine($
"Ave: {msecs.Average():.00}");
137 Console.WriteLine($
"Min: {msecs.Min():.00}");
138 Console.WriteLine($
"Max: {msecs.Max():.00}");
140 catch (CFviException ex)
142 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
143 Console.WriteLine(ex.StackTrace);
147 Console.WriteLine($
"Message={ex.Message}");
148 Console.WriteLine(ex.StackTrace);
152 static void BenchMarkMultiView(String modelPath, String imageFolder, UInt32 iterNum)
155 const Double threshold = 10.0;
157 const ImageType anomaly_map_image_type = ImageType.UC8;
162 Console.WriteLine(
"iterNum must be positive ( >0 )");
169 if (
false == has_license) {
throw new Exception(
"no license"); }
185 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
189 List<CFviImage> target_images =
new List<CFviImage>();
190 for (var i_img = 0; i_img < model.
NumViews; i_img++)
193 var img_path = System.IO.Path.Combine(imageFolder, String.Format(
"{0}.bmp", i_img));
194 var image =
new CFviImage(img_path);
198 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
201 target_images.Add(image);
205 CFviImage anomaly_map =
new CFviImage(target_images[0].HorzSize, target_images[0].VertSize, anomaly_map_image_type, 1);
208 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
211 long[] msecs =
new long[iterNum];
213 var contents =
"msec, score0,...";
214 Console.WriteLine(
"msec, score0,...");
215 for (var i = 0; i < iterNum; i++)
233 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
234 for (var i_score = 0; i_score < scores.Length; i_score++)
236 contents += $
", {scores[i_score]}";
253 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
254 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
262 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
267 msecs[i] = sw.ElapsedMilliseconds;
269 Console.WriteLine(contents);
281 Console.WriteLine($
"Ave: {msecs.Average():.00}");
282 Console.WriteLine($
"Min: {msecs.Min():.00}");
283 Console.WriteLine($
"Max: {msecs.Max():.00}");
285 catch (CFviException ex)
287 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
288 Console.WriteLine(ex.StackTrace);
292 Console.WriteLine($
"Message={ex.Message}");
293 Console.WriteLine(ex.StackTrace);
297 static void BenchMarkSegmentation(String modelPath, String imagePath, UInt32 iterNum)
302 Console.WriteLine(
"iterNum must be positive ( >0 )");
309 if (
false == has_license) {
throw new Exception(
"no license"); }
315 CFviImage image =
new CFviImage(imagePath);
319 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
322 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
325 long[] msecs =
new long[iterNum];
327 var contents =
"msec, score0,...";
328 Console.WriteLine(
"msec, score0,...");
329 for (var i = 0; i < iterNum; i++)
345 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
363 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
371 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
375 msecs[i] = sw.ElapsedMilliseconds;
377 Console.WriteLine(contents);
389 Console.WriteLine($
"Ave: {msecs.Average():.00}");
390 Console.WriteLine($
"Min: {msecs.Min():.00}");
391 Console.WriteLine($
"Max: {msecs.Max():.00}");
393 catch (CFviException ex)
395 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
396 Console.WriteLine(ex.StackTrace);
400 Console.WriteLine($
"Message={ex.Message}");
401 Console.WriteLine(ex.StackTrace);
405 static void BenchMarkObjectDetection(String modelPath, String imagePath, UInt32 iterNum)
410 Console.WriteLine(
"iterNum must be positive ( >0 )");
417 if (
false == has_license) {
throw new Exception(
"no license"); }
423 CFviImage image =
new CFviImage(imagePath);
427 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
430 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
433 long[] msecs =
new long[iterNum];
435 var contents =
"msec, num_detections";
436 Console.WriteLine(
"msec, num_detections");
437 for (var i = 0; i < iterNum; i++)
453 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
454 contents += $
", {detections.Count()}";
457 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
461 msecs[i] = sw.ElapsedMilliseconds;
463 Console.WriteLine(contents);
475 Console.WriteLine($
"Ave: {msecs.Average():.00}");
476 Console.WriteLine($
"Min: {msecs.Min():.00}");
477 Console.WriteLine($
"Max: {msecs.Max():.00}");
479 catch (CFviException ex)
481 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
482 Console.WriteLine(ex.StackTrace);
486 Console.WriteLine($
"Message={ex.Message}");
487 Console.WriteLine(ex.StackTrace);
推論するモデルを扱うクラス
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
2Imports System.Collections.Generic
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
19 Console.WriteLine("iterNum must be positive ( >0 )")
24 Dim has_license = PDL.Model.CheckLicense()
25 If False = has_license Then
26 Throw New Exception("no license")
29 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
30 Dim model As Model = New Model(modelPath)
33 Dim image As CFviImage = New CFviImage(imagePath)
36 Dim is_valid_img = model.IsValidImage(image)
37 If False = is_valid_img Then
38 Throw New Exception("invalid image")
42 Dim anomaly_map As CFviImage = New CFviImage(image.HorzSize, image.VertSize, anomaly_map_image_type, 1)
45 Dim sw As Stopwatch = New Stopwatch()
48 Dim msecs = New Long(iterNum - 1) {}
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 ' 画像分類
59 Dim scores = model.PredictClassification(image)
65 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
66 For i_score = 0 To scores.Length - 1
67 contents += $", {scores(i_score)}"
69 Case ModelCategory.AnomalyDetection ' アノマリー検出
74 Dim tuple = model.PredictAnomaly(image, threshold, anomaly_map)
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 ' マルチラベル画像分類
88 Dim scores = model.PredictMultiLabelClassification(image)
94 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
95 For i_score = 0 To scores.Length - 1
96 contents += $", {scores(i_score)}"
100 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
104 msecs(i) = sw.ElapsedMilliseconds
106 Console.WriteLine(contents)
109 ' - "result.csv" は任意のパスを設定
110 ' - 上記の contents 生成に改行を追加
111 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
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)
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
138 Console.WriteLine("iterNum must be positive ( >0 )")
143 Dim has_license = PDL.Model.CheckLicense()
144 If False = has_license Then
145 Throw New Exception("no license")
148 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
149 Dim model As Model = New Model(modelPath)
152 Select Case model.ModelCategory
153 Case ModelCategory.MultiViewCNN, ModelCategory.MultiViewAD
155 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
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)
166 Dim is_valid_img = model.IsValidImage(image)
167 If False = is_valid_img Then
168 Throw New Exception("invalid image")
172 target_images.Add(image)
176 Dim anomaly_map As CFviImage = New CFviImage(target_images(0).HorzSize, target_images(0).VertSize, anomaly_map_image_type, 1)
179 Dim sw As Stopwatch = New Stopwatch()
182 Dim msecs = New Long(iterNum - 1) {}
184 Dim contents = "msec, score0,..."
185 Console.WriteLine("msec, score0,...")
186 For i = 0 To iterNum - 1
188 Select Case model.ModelCategory
189 Case ModelCategory.MultiViewCNN
194 Dim scores = model.PredictMultiViewCNN(target_images)
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)}"
205 Case ModelCategory.MultiViewAD
210 Dim tuple = model.PredictMultiViewAD(target_images, threshold, anomaly_map)
216 'contents = $"{sw.ElapsedMilliseconds:000}";
217 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
218 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
220 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
225 msecs(i) = sw.ElapsedMilliseconds
227 Console.WriteLine(contents)
230 ' - "result.csv" は任意のパスを設定
231 ' - 上記の contents 生成に改行を追加
232 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
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)
251 Private Shared Sub BenchMarkSegmentation(modelPath As String, imagePath As String, iterNum As UInteger)
254 Console.WriteLine("iterNum must be positive ( >0 )")
259 Dim has_license = PDL.Model.CheckLicense()
260 If False = has_license Then
261 Throw New Exception("no license")
264 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
265 Dim model As Model = New Model(modelPath)
268 Dim image As CFviImage = New CFviImage(imagePath)
271 Dim is_valid_img = model.IsValidImage(image)
272 If False = is_valid_img Then
273 Throw New Exception("invalid image")
277 Dim sw As Stopwatch = New Stopwatch()
280 Dim msecs = New Long(iterNum - 1) {}
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 ' セマンティックセグメンテーション
290 ' 推論実行: 後処理を行わない最も単純なメソッド
291 Dim segm_result_image = model.PredictSemanticSegmentation(image)
297 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
298 Case ModelCategory.PanopticSegmentation
299 ' パノプティックセグメンテーション固有の検出パラメータの設定 ( ここではデフォルト値と同じ値を設定 )
300 'model.SetPanopticSegmentationParams(1024, 0.1, 32, -1);
305 ' 推論実行: 後処理を行わない最も単純なメソッド
306 Dim segm_result_image = model.PredictSemanticSegmentation(image)
312 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
314 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
318 msecs(i) = sw.ElapsedMilliseconds
320 Console.WriteLine(contents)
323 ' - "result.csv" は任意のパスを設定
324 ' - 上記の contents 生成に改行を追加
325 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
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)
344 Private Shared Sub BenchMarkObjectDetection(modelPath As String, imagePath As String, iterNum As UInteger)
347 Console.WriteLine("iterNum must be positive ( >0 )")
352 Dim has_license = PDL.Model.CheckLicense()
353 If False = has_license Then
354 Throw New Exception("no license")
357 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
358 Dim model As Model = New Model(modelPath)
361 Dim image As CFviImage = New CFviImage(imagePath)
364 Dim is_valid_img = model.IsValidImage(image)
365 If False = is_valid_img Then
366 Throw New Exception("invalid image")
370 Dim sw As Stopwatch = New Stopwatch()
373 Dim msecs = New Long(iterNum - 1) {}
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 ' 物体検出
384 Dim detections = model.PredictObjectDetection(image)
390 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
391 contents += $", {detections.Count()}"
393 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
397 msecs(i) = sw.ElapsedMilliseconds
399 Console.WriteLine(contents)
402 ' - "result.csv" は任意のパスを設定
403 ' - 上記の contents 生成に改行を追加
404 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
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)