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}";
96 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
100 msecs[i] = sw.ElapsedMilliseconds;
102 Console.WriteLine(contents);
114 Console.WriteLine($
"Ave: {msecs.Average():.00}");
115 Console.WriteLine($
"Min: {msecs.Min():.00}");
116 Console.WriteLine($
"Max: {msecs.Max():.00}");
118 catch (CFviException ex)
120 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
121 Console.WriteLine(ex.StackTrace);
125 Console.WriteLine($
"Message={ex.Message}");
126 Console.WriteLine(ex.StackTrace);
130 static void BenchMarkMVCNN(String modelPath, String imageFolder, UInt32 iterNum)
135 Console.WriteLine(
"iterNum must be positive ( >0 )");
142 if (
false == has_license) {
throw new Exception(
"no license"); }
155 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
159 List<CFviImage> target_images =
new List<CFviImage>();
160 for (var i_img = 0; i_img < model.
NumViews; i_img++)
163 var img_path = System.IO.Path.Combine(imageFolder, String.Format(
"{0}.bmp", i_img));
164 var image =
new CFviImage(img_path);
168 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
171 target_images.Add(image);
175 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
178 long[] msecs =
new long[iterNum];
180 var contents =
"msec, score0,...";
181 Console.WriteLine(
"msec, score0,...");
182 for (var i = 0; i < iterNum; i++)
195 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
196 for (var i_score = 0; i_score < scores.Length; i_score++)
198 contents += $
", {scores[i_score]}";
202 msecs[i] = sw.ElapsedMilliseconds;
204 Console.WriteLine(contents);
216 Console.WriteLine($
"Ave: {msecs.Average():.00}");
217 Console.WriteLine($
"Min: {msecs.Min():.00}");
218 Console.WriteLine($
"Max: {msecs.Max():.00}");
220 catch (CFviException ex)
222 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
223 Console.WriteLine(ex.StackTrace);
227 Console.WriteLine($
"Message={ex.Message}");
228 Console.WriteLine(ex.StackTrace);
推論するモデルを扱うクラス
Definition: PredictionCS.cs:101
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:122
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:599
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:425
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:241
Int32 NumViews
読み込んだモデルが期待する視点数
Definition: PredictionCS.cs:151
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:680
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:829
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:20
ModelCategory
モデルの種別
Definition: PredictionCS.cs:30
FVILの最上位ネームスペース
Definition: PredictionCS.cs:15
2Imports System.Collections.Generic
11 Private Shared Sub BenchMark(ByVal modelPath As String, ByVal imagePath As String, ByVal 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 BenchMarkMVCNN(ByVal modelPath As String, ByVal imageFolder As String, ByVal iterNum As UInteger)
117 Console.WriteLine("iterNum must be positive ( >0 )")
122 Dim has_license = PDL.Model.CheckLicense()
123 If False = has_license Then
124 Throw New Exception("no license")
127 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
128 Dim model As Model = New Model(modelPath)
131 Select Case model.ModelCategory
132 Case ModelCategory.MultiViewCNN
134 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
138 Dim target_images As List(Of CFviImage) = New List(Of CFviImage)()
139 For i_img = 0 To model.NumViews - 1
140 ' imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
141 Dim img_path = Path.Combine(imageFolder, String.Format("{0}.bmp", i_img))
142 Dim image = New CFviImage(img_path)
145 Dim is_valid_img = model.IsValidImage(image)
146 If False = is_valid_img Then
147 Throw New Exception("invalid image")
151 target_images.Add(image)
155 Dim sw As Stopwatch = New Stopwatch()
158 Dim msecs = New Long(iterNum - 1) {}
160 Dim contents = "msec, score0,..."
161 Console.WriteLine("msec, score0,...")
162 For i = 0 To iterNum - 1
167 Dim scores = model.PredictMultiViewCNN(target_images)
173 'contents = $"{sw.ElapsedMilliseconds:000}";
174 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
175 For i_score = 0 To scores.Length - 1
176 contents += $", {scores(i_score)}"
180 msecs(i) = sw.ElapsedMilliseconds
182 Console.WriteLine(contents)
185 ' - "result.csv" は任意のパスを設定
186 ' - 上記の contents 生成に改行を追加
187 'System.IO.File.AppendAllText("result.csv", contents + Environment.NewLine);
194 Console.WriteLine($"Ave: {msecs.Average():.00}")
195 Console.WriteLine($"Min: {msecs.Min():.00}")
196 Console.WriteLine($"Max: {msecs.Max():.00}")
197 Catch ex As CFviException
198 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
199 Console.WriteLine(ex.StackTrace)
200 Catch ex As Exception
201 Console.WriteLine($"Message={ex.Message}")
202 Console.WriteLine(ex.StackTrace)