2using System.Collections.Generic;
5using System.Threading.Tasks;
14 static void SimpleSample(String modelPath, String imagePath)
19 if (
false == has_license) {
throw new Exception(
"no license"); }
25 CFviImage image =
new CFviImage(imagePath);
28 Console.WriteLine($
"backend: {model.ModelBackend}");
32 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
42 for (var i_score = 0; i_score < scores.Length; i_score++)
44 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
49 Console.WriteLine($
"top label{top_score_pair.Item1}, score{top_score_pair.Item2}");
59 var is_anomaly = tuple.Item1;
60 var max_anomaly = tuple.Item2;
63 var str_is_anomaly = is_anomaly ?
"anomaly" :
"normal";
64 Console.WriteLine($
"{str_is_anomaly}, max_anomaly={max_anomaly}");
73 for (var i_score = 0; i_score< scores.Length; i_score++)
75 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
79 float score_threshold = 0.5f;
83 foreach(var kv
in dict)
85 Console.WriteLine($
"label{kv.Key}: {kv.Value}");
95 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
98 catch (CFviException ex)
100 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
101 Console.WriteLine(ex.StackTrace);
105 Console.WriteLine($
"Message={ex.Message}");
106 Console.WriteLine(ex.StackTrace);
110 static void SimpleSampleMultiView(String modelPath, String imageFolder)
115 if (
false == has_license) {
throw new Exception(
"no license"); }
121 Console.WriteLine($
"backend: {model.ModelBackend}");
124 List<CFviImage> target_images =
new List<CFviImage>();
125 for (var i_img = 0; i_img < model.
NumViews; i_img++)
128 var img_path = System.IO.Path.Combine(imageFolder, String.Format(
"{0}.bmp", i_img));
129 var image =
new CFviImage(img_path);
133 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
136 target_images.Add(image);
147 for (var i_score = 0; i_score < scores.Length; i_score++)
149 Console.WriteLine($
"label{i_score}: {scores[i_score]}");
154 Console.WriteLine($
"top label{top_score_pair.Item1}, score{top_score_pair.Item2}");
160 var threshold = 10.0;
164 var is_anomaly = tuple.Item1;
165 var max_anomaly = tuple.Item2;
168 var str_is_anomaly = is_anomaly ?
"anomaly" :
"normal";
169 Console.WriteLine($
"{str_is_anomaly}, max_anomaly={max_anomaly}");
177 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
180 catch (CFviException ex)
182 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
183 Console.WriteLine(ex.StackTrace);
187 Console.WriteLine($
"Message={ex.Message}");
188 Console.WriteLine(ex.StackTrace);
192 static void SimpleSampleSegmentation(String modelPath, String imagePath)
197 if (
false == has_license) {
throw new Exception(
"no license"); }
203 CFviImage image =
new CFviImage(imagePath);
206 Console.WriteLine($
"backend: {model.ModelBackend}");
210 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
217 var category_masks =
new Dictionary<UInt32, CFviImage>();
218 category_masks.Add(1,
new CFviImage(image.HorzSize, image.VertSize, ImageType.BIN, 1));
219 category_masks.Add(2,
new CFviImage());
220 category_masks.Add(5,
new CFviImage());
227 foreach (var kv
in category_masks)
229 var category_id = kv.Key;
230 var category_image = kv.Value;
231 var region =
new CFviRegion(category_image);
232 Console.WriteLine($
"No.{category_id}: area={region.Area}");
242 var category_masks =
new Dictionary<UInt32, List<CFviImage>>();
243 category_masks.Add(1,
new List<CFviImage>());
244 category_masks.Add(2,
new List<CFviImage>() {
new CFviImage() });
245 category_masks.Add(5,
new List<CFviImage>());
252 foreach (var kv
in category_masks)
254 var category_id = kv.Key;
255 var mask_images = kv.Value;
256 for(var i=0;i<mask_images.Count; i++)
258 var region =
new CFviRegion(mask_images[i]);
259 Console.WriteLine($
"category={category_id}: no.={i}, area={region.Area}");
269 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
272 catch (CFviException ex)
274 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
275 Console.WriteLine(ex.StackTrace);
279 Console.WriteLine($
"Message={ex.Message}");
280 Console.WriteLine(ex.StackTrace);
284 static void SimpleSampleObjectDetection(String modelPath, String imagePath)
289 if (
false == has_license) {
throw new Exception(
"no license"); }
295 CFviImage image =
new CFviImage(imagePath);
298 Console.WriteLine($
"backend: {model.ModelBackend}");
302 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
314 CFviRectangle box = detection.
Box;
315 Console.WriteLine($
"ClassId: {detection.ClassId}, Score: {detection.Score:0.000}, " +
316 $
"Box: (({box.Left:0.0},{box.Top:0.0}),({box.Right:0.0},{box.Bottom:0.0}))");
320 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
323 catch (CFviException ex)
325 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
326 Console.WriteLine(ex.StackTrace);
330 Console.WriteLine($
"Message={ex.Message}");
331 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
void SetPanopticSegmentationParams(UInt32 stuffArea=1024, Double threshold=0.1, Int32 nmsKernel=31, UInt16 requestInstanceNum=0)
パノプティックセグメンテーションの検出パラメータの設定
Definition: PredictionCS.cs:752
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:1083
CFviImage PredictPanopticSegmentation(CFviImage targetImage)
推論の実行(パノプティックセグメンテーション)
Definition: PredictionCS.cs:1540
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:1213
物体検出結果データ構造クラス
Definition: PredictionCS.cs:106
FVIL.Data.CFviRectangle Box
物体の位置を表すバウンディングボックス
Definition: PredictionCS.cs:120
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:21
ModelCategory
モデルの種別
Definition: PredictionCS.cs:32
FVILの最上位ネームスペース
Definition: PredictionCS.cs:16
2Imports System.Collections.Generic
10 Private Shared Sub SimpleSample(modelPath As String, imagePath As String)
12 Dim has_license = PDL.Model.CheckLicense()
13 If False = has_license Then
14 Throw New Exception("no license")
17 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
18 Dim model As Model = New Model(modelPath)
21 Dim image As CFviImage = New CFviImage(imagePath)
24 Console.WriteLine($"backend: {model.ModelBackend}")
27 Dim is_valid_img = model.IsValidImage(image)
28 If False = is_valid_img Then
29 Throw New Exception("invalid image")
32 Select Case model.ModelCategory
33 Case ModelCategory.Classification ' 画像分類
35 Dim scores = model.PredictClassification(image)
38 For i_score = 0 To scores.Length - 1
39 Console.WriteLine($"label{i_score}: {scores(i_score)}")
43 Dim top_score_pair = model.PredictClassification(image, scores)
44 Console.WriteLine($"top label{top_score_pair.Item1}, score{top_score_pair.Item2}")
45 Case ModelCategory.AnomalyDetection ' アノマリー検出
46 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
48 Dim tuple = model.PredictAnomaly(image, threshold)
51 Dim is_anomaly = tuple.Item1
52 Dim max_anomaly = tuple.Item2
55 Dim str_is_anomaly = If(is_anomaly, "anomaly", "normal")
56 Console.WriteLine($"{str_is_anomaly}, max_anomaly={max_anomaly}")
57 Case ModelCategory.MultiLabelClassification ' マルチラベル画像分類
59 Dim scores = model.PredictMultiLabelClassification(image)
62 For i_score = 0 To scores.Length - 1
63 Console.WriteLine($"label{i_score}: {scores(i_score)}")
66 ' 推論実行 ( スコア閾値を満たすラベルのインデックスとそのスコアの取得 )
67 Dim score_threshold = 0.5F ' 省略可能、省略されたときは 0.5f で実行される
68 Dim dict = model.PredictMultiLabelClassification(image, scores, score_threshold)
70 ' スコア閾値を満たしたものだけをコンソール出力
72 Console.WriteLine($"label{kv.Key}: {kv.Value}")
76 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
78 Catch ex As CFviException
79 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
80 Console.WriteLine(ex.StackTrace)
82 Console.WriteLine($"Message={ex.Message}")
83 Console.WriteLine(ex.StackTrace)
87 Private Shared Sub SimpleSampleMultiView(modelPath As String, imageFolder As String)
89 Dim has_license = PDL.Model.CheckLicense()
90 If False = has_license Then
91 Throw New Exception("no license")
94 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
95 Dim model As Model = New Model(modelPath)
98 Console.WriteLine($"backend: {model.ModelBackend}")
101 Dim target_images As List(Of CFviImage) = New List(Of CFviImage)()
102 For i_img = 0 To model.NumViews - 1
103 ' imageFolder フォルダ内に、0-index の通し番号の画像が保存されていることを想定
104 Dim img_path = Path.Combine(imageFolder, String.Format("{0}.bmp", i_img))
105 Dim image = New CFviImage(img_path)
108 Dim is_valid_img = model.IsValidImage(image)
109 If False = is_valid_img Then
110 Throw New Exception("invalid image")
114 target_images.Add(image)
118 Select Case model.ModelCategory
119 Case ModelCategory.MultiViewCNN ' 多視点画像分類
121 Dim scores = model.PredictMultiViewCNN(target_images)
123 For i_score = 0 To scores.Length - 1
124 Console.WriteLine($"label{i_score}: {scores(i_score)}")
127 ' 推論実行 ( 最大スコアのみを取得 )
128 Dim top_score_pair = model.PredictMultiViewCNN(target_images, scores)
129 Console.WriteLine($"top label{top_score_pair.Item1}, score{top_score_pair.Item2}")
130 Case ModelCategory.MultiViewAD ' 多視点アノマリー検出
131 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
133 Dim tuple = model.PredictMultiViewAD(target_images, threshold)
136 Dim is_anomaly = tuple.Item1
137 Dim max_anomaly = tuple.Item2
140 Dim str_is_anomaly = If(is_anomaly, "anomaly", "normal")
141 Console.WriteLine($"{str_is_anomaly}, max_anomaly={max_anomaly}")
143 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
145 Catch ex As CFviException
146 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
147 Console.WriteLine(ex.StackTrace)
148 Catch ex As Exception
149 Console.WriteLine($"Message={ex.Message}")
150 Console.WriteLine(ex.StackTrace)
154 Private Shared Sub SimpleSampleSegmentation(modelPath As String, imagePath As String)
156 Dim has_license = PDL.Model.CheckLicense()
157 If False = has_license Then
158 Throw New Exception("no license")
161 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
162 Dim model As Model = New Model(modelPath)
165 Dim image As CFviImage = New CFviImage(imagePath)
168 Console.WriteLine($"backend: {model.ModelBackend}")
171 Dim is_valid_img = model.IsValidImage(image)
172 If False = is_valid_img Then
173 Throw New Exception("invalid image")
176 Select Case model.ModelCategory
177 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
178 ' 検出結果の領域を表す二値画像を取得したいカテゴリ ( ラベル ) の番号をキーに持つ辞書
179 Dim category_masks = New Dictionary(Of UInteger, CFviImage)()
180 category_masks.Add(1, New CFviImage(image.HorzSize, image.VertSize, ImageType.BIN, 1)) ' 0 は背景なので、1 が最初のカテゴリ ( ラベル )
181 category_masks.Add(2, New CFviImage()) ' ペアとなる値には画像サイズやチャネル数、型の異なる画像を入力することが可能 ( 内部で自動調整されるため )
182 category_masks.Add(5, New CFviImage()) ' 連番でなくても良いので、必要なカテゴリ ( ラベル ) のみを指定することが可能
185 Dim segm_result_image = model.PredictSemanticSegmentation(image, category_masks)
186 'var segm_result_image = model.PredictSemanticSegmentation(image);
188 ' ここでは単に各カテゴリの二値画像をリージョンに変換して領域の面積のみを表示
189 For Each kv In category_masks
190 Dim category_id = kv.Key
191 Dim category_image = kv.Value
192 Dim region = New CFviRegion(category_image)
193 Console.WriteLine($"No.{category_id}: area={region.Area}")
195 Case ModelCategory.PanopticSegmentation
196 ' パノプティックセグメンテーション固有の検出パラメータの設定 ( ここではデフォルト値と同じ値を設定 )
197 model.SetPanopticSegmentationParams(1024, 0.1, 32, 0)
199 ' 検出結果の領域を表す二値画像を取得したいカテゴリ ( ラベル ) の番号をキーに持つ辞書
200 Dim category_masks = New Dictionary(Of UInteger, List(Of CFviImage))()
201 category_masks.Add(1, New List(Of CFviImage)()) ' 0 は背景なので、1 が最初のカテゴリ ( ラベル )
202 category_masks.Add(2, New List(Of CFviImage)() From {
204 }) ' ペアとなる値のリストには既に画像が存在しているリストを入力することも可能 ( 内部でリストはクリアされるため )
205 category_masks.Add(5, New List(Of CFviImage)()) ' 連番でなくても良いので、必要なカテゴリ ( ラベル ) のみを指定することが可能
208 Dim segm_result_image = model.PredictPanopticSegmentation(image, category_masks)
209 'var segm_result_image = model.PredictPanopticSegmentation(image);
211 ' ここでは単に各カテゴリの二値画像をリージョンに変換して領域の面積のみを表示
212 For Each kv In category_masks
213 Dim category_id = kv.Key
214 Dim mask_images = kv.Value
215 For i = 0 To mask_images.Count - 1
216 Dim region = New CFviRegion(mask_images(i))
217 Console.WriteLine($"category={category_id}: no.={i}, area={region.Area}")
222 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
224 Catch ex As CFviException
225 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
226 Console.WriteLine(ex.StackTrace)
227 Catch ex As Exception
228 Console.WriteLine($"Message={ex.Message}")
229 Console.WriteLine(ex.StackTrace)
233 Private Shared Sub SimpleSampleObjectDetection(modelPath As String, imagePath As String)
235 Dim has_license = PDL.Model.CheckLicense()
236 If False = has_license Then
237 Throw New Exception("no license")
240 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
241 Dim model As Model = New Model(modelPath)
244 Dim image As CFviImage = New CFviImage(imagePath)
247 Console.WriteLine($"backend: {model.ModelBackend}")
250 Dim is_valid_img = model.IsValidImage(image)
251 If False = is_valid_img Then
252 Throw New Exception("invalid image")
255 Select Case model.ModelCategory
256 Case ModelCategory.ObjectDetection ' 物体検出
258 Dim detections = model.PredictObjectDetection(image)
261 For Each detection In detections
262 Dim box = detection.Box
263 Console.WriteLine($"ClassId: {detection.ClassId}, Score: {detection.Score:0.000}, " & $"Box: (({box.Left:0.0},{box.Top:0.0}),({box.Right:0.0},{box.Bottom:0.0}))")
267 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
269 Catch ex As CFviException
270 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
271 Console.WriteLine(ex.StackTrace)
272 Catch ex As Exception
273 Console.WriteLine($"Message={ex.Message}")
274 Console.WriteLine(ex.StackTrace)