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}";
99 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
103 msecs[i] = sw.ElapsedMilliseconds;
105 Console.WriteLine(contents);
117 Console.WriteLine($
"Ave: {msecs.Average():.00}");
118 Console.WriteLine($
"Min: {msecs.Min():.00}");
119 Console.WriteLine($
"Max: {msecs.Max():.00}");
121 catch (CFviException ex)
123 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
124 Console.WriteLine(ex.StackTrace);
128 Console.WriteLine($
"Message={ex.Message}");
129 Console.WriteLine(ex.StackTrace);
133 static void BenchMarkMultiView(String modelPath, String imageFolder, UInt32 iterNum)
136 const Double threshold = 10.0;
138 const ImageType anomaly_map_image_type = ImageType.UC8;
143 Console.WriteLine(
"iterNum must be positive ( >0 )");
150 if (
false == has_license) {
throw new Exception(
"no license"); }
166 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
170 List<CFviImage> target_images =
new List<CFviImage>();
171 for (var i_img = 0; i_img < model.
NumViews; i_img++)
174 var img_path = System.IO.Path.Combine(imageFolder, String.Format(
"{0}.bmp", i_img));
175 var image =
new CFviImage(img_path);
179 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
182 target_images.Add(image);
186 CFviImage anomaly_map =
new CFviImage(target_images[0].HorzSize, target_images[0].VertSize, anomaly_map_image_type, 1);
189 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
192 long[] msecs =
new long[iterNum];
194 var contents =
"msec, score0,...";
195 Console.WriteLine(
"msec, score0,...");
196 for (var i = 0; i < iterNum; i++)
214 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
215 for (var i_score = 0; i_score < scores.Length; i_score++)
217 contents += $
", {scores[i_score]}";
234 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
235 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
243 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
248 msecs[i] = sw.ElapsedMilliseconds;
250 Console.WriteLine(contents);
262 Console.WriteLine($
"Ave: {msecs.Average():.00}");
263 Console.WriteLine($
"Min: {msecs.Min():.00}");
264 Console.WriteLine($
"Max: {msecs.Max():.00}");
266 catch (CFviException ex)
268 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
269 Console.WriteLine(ex.StackTrace);
273 Console.WriteLine($
"Message={ex.Message}");
274 Console.WriteLine(ex.StackTrace);
278 static void BenchMarkSegmentation(String modelPath, String imagePath, UInt32 iterNum)
283 Console.WriteLine(
"iterNum must be positive ( >0 )");
290 if (
false == has_license) {
throw new Exception(
"no license"); }
296 CFviImage image =
new CFviImage(imagePath);
300 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
303 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
306 long[] msecs =
new long[iterNum];
308 var contents =
"msec, score0,...";
309 Console.WriteLine(
"msec, score0,...");
310 for (var i = 0; i < iterNum; i++)
326 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
344 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
352 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
356 msecs[i] = sw.ElapsedMilliseconds;
358 Console.WriteLine(contents);
370 Console.WriteLine($
"Ave: {msecs.Average():.00}");
371 Console.WriteLine($
"Min: {msecs.Min():.00}");
372 Console.WriteLine($
"Max: {msecs.Max():.00}");
374 catch (CFviException ex)
376 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
377 Console.WriteLine(ex.StackTrace);
381 Console.WriteLine($
"Message={ex.Message}");
382 Console.WriteLine(ex.StackTrace);
推論するモデルを扱うクラス
Definition: PredictionCS.cs:146
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:167
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:888
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:571
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:321
CFviImage PredictSemanticSegmentation(CFviImage targetImage)
推論の実行(セマンティックセグメンテーション)
Definition: PredictionCS.cs:1336
Tuple< Boolean, float > PredictMultiViewAD(IEnumerable< CFviImage > targetImages, float threshold)
推論の実行(多視点アノマリー検出)
Definition: PredictionCS.cs:1201
Int32 NumViews
読み込んだモデルが期待する視点数
Definition: PredictionCS.cs:210
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:979
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:1109
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:20
ModelCategory
モデルの種別
Definition: PredictionCS.cs:31
FVILの最上位ネームスペース
Definition: PredictionCS.cs:15
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}"
84 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
88 msecs(i) = sw.ElapsedMilliseconds
90 Console.WriteLine(contents)
93 ' - "result.csv" は任意のパスを設定
94 ' - 上記の contents 生成に改行を追加
95 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
102 Console.WriteLine($"Ave: {msecs.Average():.00}")
103 Console.WriteLine($"Min: {msecs.Min():.00}")
104 Console.WriteLine($"Max: {msecs.Max():.00}")
105 Catch ex As CFviException
106 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
107 Console.WriteLine(ex.StackTrace)
108 Catch ex As Exception
109 Console.WriteLine($"Message={ex.Message}")
110 Console.WriteLine(ex.StackTrace)
114 Private Shared Sub BenchMarkMultiView(modelPath As String, imageFolder As String, iterNum As UInteger)
115 ' 閾値は任意、必要であれば引数としても良い
116 Const threshold = 10.0
117 ' アノマリー検出での異常度マップ画像の型 ( UC8 または F32 )
118 Const anomaly_map_image_type = ImageType.UC8
122 Console.WriteLine("iterNum must be positive ( >0 )")
127 Dim has_license = PDL.Model.CheckLicense()
128 If False = has_license Then
129 Throw New Exception("no license")
132 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
133 Dim model As Model = New Model(modelPath)
136 Select Case model.ModelCategory
137 Case ModelCategory.MultiViewCNN, ModelCategory.MultiViewAD
139 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
143 Dim target_images As List(Of CFviImage) = New List(Of CFviImage)()
144 For i_img = 0 To model.NumViews - 1
145 ' imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
146 Dim img_path = Path.Combine(imageFolder, String.Format("{0}.bmp", i_img))
147 Dim image = New CFviImage(img_path)
150 Dim is_valid_img = model.IsValidImage(image)
151 If False = is_valid_img Then
152 Throw New Exception("invalid image")
156 target_images.Add(image)
160 Dim anomaly_map As CFviImage = New CFviImage(target_images(0).HorzSize, target_images(0).VertSize, anomaly_map_image_type, 1)
163 Dim sw As Stopwatch = New Stopwatch()
166 Dim msecs = New Long(iterNum - 1) {}
168 Dim contents = "msec, score0,..."
169 Console.WriteLine("msec, score0,...")
170 For i = 0 To iterNum - 1
172 Select Case model.ModelCategory
173 Case ModelCategory.MultiViewCNN
178 Dim scores = model.PredictMultiViewCNN(target_images)
184 'contents = $"{sw.ElapsedMilliseconds:000}";
185 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
186 For i_score = 0 To scores.Length - 1
187 contents += $", {scores(i_score)}"
189 Case ModelCategory.MultiViewAD
194 Dim tuple = model.PredictMultiViewAD(target_images, threshold, anomaly_map)
200 'contents = $"{sw.ElapsedMilliseconds:000}";
201 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
202 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
204 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
209 msecs(i) = sw.ElapsedMilliseconds
211 Console.WriteLine(contents)
214 ' - "result.csv" は任意のパスを設定
215 ' - 上記の contents 生成に改行を追加
216 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
223 Console.WriteLine($"Ave: {msecs.Average():.00}")
224 Console.WriteLine($"Min: {msecs.Min():.00}")
225 Console.WriteLine($"Max: {msecs.Max():.00}")
226 Catch ex As CFviException
227 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
228 Console.WriteLine(ex.StackTrace)
229 Catch ex As Exception
230 Console.WriteLine($"Message={ex.Message}")
231 Console.WriteLine(ex.StackTrace)
235 Private Shared Sub BenchMarkSegmentation(modelPath As String, imagePath As String, iterNum As UInteger)
238 Console.WriteLine("iterNum must be positive ( >0 )")
243 Dim has_license = PDL.Model.CheckLicense()
244 If False = has_license Then
245 Throw New Exception("no license")
248 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
249 Dim model As Model = New Model(modelPath)
252 Dim image As CFviImage = New CFviImage(imagePath)
255 Dim is_valid_img = model.IsValidImage(image)
256 If False = is_valid_img Then
257 Throw New Exception("invalid image")
261 Dim sw As Stopwatch = New Stopwatch()
264 Dim msecs = New Long(iterNum - 1) {}
266 Dim contents = "msec, score0,..."
267 Console.WriteLine("msec, score0,...")
268 For i = 0 To iterNum - 1
269 Select Case model.ModelCategory
270 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
274 ' 推論実行: 後処理を行わない最も単純なメソッド
275 Dim segm_result_image = model.PredictSemanticSegmentation(image)
281 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
282 Case ModelCategory.PanopticSegmentation
283 ' パノプティックセグメンテーション固有の検出パラメータの設定 ( ここではデフォルト値と同じ値を設定 )
284 'model.SetPanopticSegmentationParams(1024, 0.1, 32, -1);
289 ' 推論実行: 後処理を行わない最も単純なメソッド
290 Dim segm_result_image = model.PredictSemanticSegmentation(image)
296 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
298 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
302 msecs(i) = sw.ElapsedMilliseconds
304 Console.WriteLine(contents)
307 ' - "result.csv" は任意のパスを設定
308 ' - 上記の contents 生成に改行を追加
309 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
316 Console.WriteLine($"Ave: {msecs.Average():.00}")
317 Console.WriteLine($"Min: {msecs.Min():.00}")
318 Console.WriteLine($"Max: {msecs.Max():.00}")
319 Catch ex As CFviException
320 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
321 Console.WriteLine(ex.StackTrace)
322 Catch ex As Exception
323 Console.WriteLine($"Message={ex.Message}")
324 Console.WriteLine(ex.StackTrace)