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));
87 default:
throw new NotImplementedException($
"unknown model-category={model.ModelCategory}");
94 contents = str_id +
", start";
95 Console.WriteLine(contents);
97 for (var i = 0; i < iterNum; i++)
99 contents = str_id + $
", {i:000}";
114 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
115 for (var i_score = 0; i_score < scores.Length; i_score++)
117 contents += $
", {scores[i_score]}";
127 var tuple = model.
PredictAnomaly(image, (
float)threshold, anomaly_map);
133 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
134 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
150 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
151 for (var i_score = 0; i_score < scores.Length; i_score++)
153 contents += $
", {scores[i_score]}";
169 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
170 contents +=
", " + (tuple.Item1 ?
"anomaly" :
"normal") + $
", {tuple.Item2}";
185 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
200 contents = $
"{(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}";
204 default:
throw new NotImplementedException($
"unknown model-category={model.ModelCategory}");
207 Console.WriteLine(contents);
213 contents = str_id +
", error" + ex.Message;
214 Console.WriteLine(contents);
215 Console.WriteLine(ex.StackTrace);
219 contents = str_id +
", finish";
220 Console.WriteLine(contents);
228 foreach(var task
in tasks)
233 Task.WaitAll(tasks.ToArray());
推論するモデルを扱うクラス
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
CFviImage PredictPanopticSegmentation(CFviImage targetImage)
推論の実行(パノプティックセグメンテーション)
Definition: PredictionCS.cs:1436
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
3Imports System.Threading.Tasks
5Imports System.Diagnostics
12 Private Shared Sub UseMultiModel(modelNames As List(Of String), imageSizes As List(Of Integer), iterNum As Integer)
13 Dim has_license = Model.CheckLicense()
14 If False = has_license Then
15 Throw New Exception("no license")
18 ' それぞれのモデルと画像を準備してタスクのリストに格納
19 Dim tasks As List(Of Task) = New List(Of Task)()
20 For Each model_name In modelNames
21 For Each img_size In imageSizes
22 Dim model_path = Path.Combine(Program.ModelDirectory, $"{model_name}_{img_size}.wilpdl")
23 Dim image_path = Path.Combine(Program.ImageDirectory, $"test_img_{img_size}.PNG")
26 Dim task = New Task(Sub()
27 Dim str_id = $"{model_name}_{img_size}"
28 Dim sw = New Stopwatch()
32 Dim anomaly_map As CFviImage = Nothing
35 Dim images As List(Of CFviImage) = Nothing
37 Dim contents = str_id & ", initializing..."
42 Console.WriteLine(contents)
45 Dim model As Model = New Model(model_path)
48 Dim image As CFviImage = New CFviImage(image_path)
51 Dim is_valid_img = model.IsValidImage(image)
52 If False = is_valid_img Then
53 Throw New Exception("invalid image")
57 Select Case model.ModelCategory
58 Case ModelCategory.Classification
59 Case ModelCategory.AnomalyDetection
60 anomaly_map = New CFviImage(image.HorzSize, image.VertSize, ImageType.UC8, 1)
61 Case ModelCategory.MultiViewCNN
62 images = New List(Of CFviImage)()
63 For i_img = 0 To model.NumViews - 1
64 images.Add(New CFviImage(image, False))
66 Case ModelCategory.MultiViewAD
67 images = New List(Of CFviImage)()
68 For i_img = 0 To model.NumViews - 1
69 images.Add(New CFviImage(image, False))
71 Case ModelCategory.SemanticSegmentation, ModelCategory.PanopticSegmentation
73 Throw New NotImplementedException($"unknown model-category={model.ModelCategory}")
80 contents = str_id & ", start"
81 Console.WriteLine(contents)
83 For i = 0 To iterNum - 1
84 contents = str_id & $", {i:000}"
85 Select Case model.ModelCategory
86 Case ModelCategory.Classification ' 画像分類
91 Dim scores = model.PredictClassification(image)
97 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
98 For i_score = 0 To scores.Length - 1
99 contents += $", {scores(i_score)}"
101 Case ModelCategory.AnomalyDetection ' アノマリー検出
106 Dim tuple = model.PredictAnomaly(image, threshold, anomaly_map)
112 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
113 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
114 Case ModelCategory.MultiViewCNN ' 多視点画像分類
119 Dim scores = model.PredictMultiViewCNN(images)
125 'contents += $", {sw.ElapsedMilliseconds:000}";
126 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
127 For i_score = 0 To scores.Length - 1
128 contents += $", {scores(i_score)}"
130 Case ModelCategory.MultiViewAD ' 多視点アノマリー検出
135 Dim tuple = model.PredictMultiViewAD(images, threshold, anomaly_map)
141 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
142 contents += ", " & If(tuple.Item1, "anomaly", "normal") & $", {tuple.Item2}"
143 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
148 Dim segm_result_image = model.PredictSemanticSegmentation(image)
154 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
155 Case ModelCategory.PanopticSegmentation ' パノプティックセグメンテーション
160 Dim segm_result_image = model.PredictPanopticSegmentation(image)
166 contents = $"{sw.ElapsedTicks / Stopwatch.Frequency * 1000}"
168 Throw New NotImplementedException($"unknown model-category={model.ModelCategory}")
171 Console.WriteLine(contents)
174 Catch ex As Exception
175 contents = str_id & ", error" & ex.Message
176 Console.WriteLine(contents)
177 Console.WriteLine(ex.StackTrace)
181 contents = str_id & ", finish"
182 Console.WriteLine(contents)
190 For Each task In tasks
194 Call Task.WaitAll(tasks.ToArray())