2using System.Collections.Generic;
5using System.Threading.Tasks;
7using System.Diagnostics;
16 static void UseMultiModel(List<String> modelNames, List<Int32> imageSizes, Int32 iterNum)
19 if (
false == has_license) {
throw new Exception(
"no license"); }
22 List<Task> tasks =
new List<Task>();
23 foreach (var model_name
in modelNames)
25 foreach (var img_size
in imageSizes)
27 var model_path = Path.Combine(ModelDirectory, $
"{model_name}_{img_size}.wilpdl");
28 var image_path = Path.Combine(ImageDirectory, $
"test_img_{img_size}.PNG");
31 var task =
new Task(() =>
33 var str_id = $
"{model_name}_{img_size}";
34 var sw =
new Stopwatch();
38 CFviImage anomaly_map =
null;
41 List<CFviImage> images =
null;
43 var contents = str_id +
", initializing...";
49 Console.WriteLine(contents);
55 CFviImage image =
new CFviImage(image_path);
59 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
67 anomaly_map =
new CFviImage(image.HorzSize, image.VertSize, ImageType.UC8, 1);
70 images =
new List<CFviImage>();
71 for(var i_img=0; i_img < model.
NumViews; i_img++)
73 images.Add(
new CFviImage(image,
false));
77 images =
new List<CFviImage>();
78 for (var i_img = 0; i_img < model.
NumViews; i_img++)
80 images.Add(
new CFviImage(image,
false));
88 default:
throw new NotImplementedException($
"unknown model-category={model.ModelCategory}");
95 contents = str_id +
", start";
96 Console.WriteLine(contents);
98 for (var i = 0; i < iterNum; i++)
100 contents = str_id + $
", {i:000}";
115 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
116 for (var i_score = 0; i_score < scores.Length; i_score++)
118 contents += $
", {scores[i_score]}";
128 var tuple = model.
PredictAnomaly(image, (
float)threshold, anomaly_map);
134 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
135 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
151 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
152 for (var i_score = 0; i_score < scores.Length; i_score++)
154 contents += $
", {scores[i_score]}";
170 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
171 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
186 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
201 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
216 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
217 contents += $
", {detections.Count()}";
221 default:
throw new NotImplementedException($
"unknown model-category={model.ModelCategory}");
224 Console.WriteLine(contents);
230 contents = str_id +
", error" + ex.Message;
231 Console.WriteLine(contents);
232 Console.WriteLine(ex.StackTrace);
236 contents = str_id +
", finish";
237 Console.WriteLine(contents);
245 foreach(var task
in tasks)
250 Task.WaitAll(tasks.ToArray());
推論するモデルを扱うクラス
Definition: PredictionCS.cs:244
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:265
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:986
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:669
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:419
IEnumerable< ObjectDetectionData > PredictObjectDetection(CFviImage targetImage)
推論の実行(物体検出)
Definition: PredictionCS.cs:1723
CFviImage PredictSemanticSegmentation(CFviImage targetImage)
推論の実行(セマンティックセグメンテーション)
Definition: PredictionCS.cs:1434
Tuple< Boolean, float > PredictMultiViewAD(IEnumerable< CFviImage > targetImages, float threshold)
推論の実行(多視点アノマリー検出)
Definition: PredictionCS.cs:1299
Int32 NumViews
読み込んだモデルが期待する視点数
Definition: PredictionCS.cs:308
Tuple< Boolean, float > PredictAnomaly(CFviImage targetImage, float threshold)
推論の実行(アノマリー検出)
Definition: PredictionCS.cs:1077
CFviImage PredictPanopticSegmentation(CFviImage targetImage)
推論の実行(パノプティックセグメンテーション)
Definition: PredictionCS.cs:1534
float[] PredictMultiViewCNN(IEnumerable< CFviImage > targetImages)
推論の実行(MVCNN)
Definition: PredictionCS.cs:1207
WIL-PDL モジュールの名前空間
Definition: PredictionCS.cs:21
ModelCategory
モデルの種別
Definition: PredictionCS.cs:32
FVILの最上位ネームスペース
Definition: PredictionCS.cs:16
2Imports System.Collections.Generic
4Imports System.Threading.Tasks
6Imports System.Diagnostics
13 Private Shared Sub UseMultiModel(modelNames As List(Of String), imageSizes As List(Of Integer), iterNum As Integer)
14 Dim has_license = Model.CheckLicense()
15 If False = has_license Then
16 Throw New Exception("no license")
19 ' それぞれのモデルと画像を準備してタスクのリストに格納
20 Dim tasks As List(Of Task) = New List(Of Task)()
21 For Each model_name In modelNames
22 For Each img_size In imageSizes
23 Dim model_path = Path.Combine(Program.ModelDirectory, $"{model_name}_{img_size}.wilpdl")
24 Dim image_path = Path.Combine(Program.ImageDirectory, $"test_img_{img_size}.PNG")
27 Dim task = New Task(Sub()
28 Dim str_id = $"{model_name}_{img_size}"
29 Dim sw = New Stopwatch()
33 Dim anomaly_map As CFviImage = Nothing
36 Dim images As List(Of CFviImage) = Nothing
38 Dim contents = str_id & ", initializing..."
43 Console.WriteLine(contents)
46 Dim model As Model = New Model(model_path)
49 Dim image As CFviImage = New CFviImage(image_path)
52 Dim is_valid_img = model.IsValidImage(image)
53 If False = is_valid_img Then
54 Throw New Exception("invalid image")
58 Select Case model.ModelCategory
59 Case ModelCategory.Classification
60 Case ModelCategory.AnomalyDetection
61 anomaly_map = New CFviImage(image.HorzSize, image.VertSize, ImageType.UC8, 1)
62 Case ModelCategory.MultiViewCNN
63 images = New List(Of CFviImage)()
64 For i_img = 0 To model.NumViews - 1
65 images.Add(New CFviImage(image, False))
67 Case ModelCategory.MultiViewAD
68 images = New List(Of CFviImage)()
69 For i_img = 0 To model.NumViews - 1
70 images.Add(New CFviImage(image, False))
72 Case ModelCategory.SemanticSegmentation, ModelCategory.PanopticSegmentation, ModelCategory.ObjectDetection
74 Throw New NotImplementedException($"unknown model-category={model.ModelCategory}")
81 contents = str_id & ", start"
82 Console.WriteLine(contents)
84 For i = 0 To iterNum - 1
85 contents = str_id & $", {i:000}"
86 Select Case model.ModelCategory
87 Case ModelCategory.Classification ' 画像分類
92 Dim scores = model.PredictClassification(image)
98 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
99 For i_score = 0 To scores.Length - 1
100 contents += $", {scores(i_score)}"
102 Case ModelCategory.AnomalyDetection ' アノマリー検出
107 Dim tuple = model.PredictAnomaly(image, threshold, anomaly_map)
113 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
114 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
115 Case ModelCategory.MultiViewCNN ' 多視点画像分類
120 Dim scores = model.PredictMultiViewCNN(images)
126 'contents += $", {sw.ElapsedMilliseconds:000}";
127 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
128 For i_score = 0 To scores.Length - 1
129 contents += $", {scores(i_score)}"
131 Case ModelCategory.MultiViewAD ' 多視点アノマリー検出
136 Dim tuple = model.PredictMultiViewAD(images, threshold, anomaly_map)
142 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
143 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
144 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
149 Dim segm_result_image = model.PredictSemanticSegmentation(image)
155 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
156 Case ModelCategory.PanopticSegmentation ' パノプティックセグメンテーション
161 Dim segm_result_image = model.PredictPanopticSegmentation(image)
167 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
168 Case ModelCategory.ObjectDetection ' 物体検出
173 Dim detections = model.PredictObjectDetection(image)
179 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
180 contents += $", {detections.Count()}"
182 Throw New NotImplementedException($"unknown model-category={model.ModelCategory}")
185 Console.WriteLine(contents)
188 Catch ex As Exception
189 contents = str_id & ", error" & ex.Message
190 Console.WriteLine(contents)
191 Console.WriteLine(ex.StackTrace)
195 contents = str_id & ", finish"
196 Console.WriteLine(contents)
204 For Each task In tasks
208 Call Task.WaitAll(tasks.ToArray())