2using System.Collections.Generic;
5using System.Threading.Tasks;
12 public partial class Program
14 static void ChangeOVDevice(String modelPath, String imagePath)
19 if (
false == has_license) {
throw new Exception(
"no license"); }
29 Console.WriteLine($
"backend: {model.ModelBackend}");
34 Console.WriteLine(
"not support device-change");
39 CFviImage image =
new CFviImage(imagePath);
42 List<CFviImage> images =
null;
55 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
61 images =
new List<CFviImage>();
62 for (var i_image = 0; i_image < model.
NumViews; i_image++)
67 if (
false == is_valid_img) {
throw new Exception(
"invalid image"); }
71 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
79 System.Diagnostics.Stopwatch sw =
new System.Diagnostics.Stopwatch();
88 for (var i = 0; i < 2; i++)
90 var is_sw_enable = (1 == i);
92 if (is_sw_enable) { sw.Start(); }
104 var threshold = 10.0;
117 var threshold = 10.0;
138 default:
throw new NotImplementedException($
"unmatch model-category={model.ModelCategory}");
140 if (!is_sw_enable) {
continue; }
144 Console.WriteLine($
"device={device}: {(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}msec");
148 catch (CFviException ex)
150 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
151 Console.WriteLine(ex.StackTrace);
155 Console.WriteLine($
"Message={ex.Message}");
156 Console.WriteLine(ex.StackTrace);
160 static void SaveOptimizedModel(String loadModelPath, String saveModelPath)
165 if (
false == has_license) {
throw new Exception(
"no license"); }
171 Console.WriteLine($
"backend: {model.ModelBackend}");
176 Console.WriteLine(
"not support optimized-model-save");
184 var sw =
new System.Diagnostics.Stopwatch();
190 Console.WriteLine($
"original model: {(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}msec");
196 Console.WriteLine($
"optimized model: {(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}msec");
198 catch (CFviException ex)
200 Console.WriteLine($
"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
201 Console.WriteLine(ex.StackTrace);
205 Console.WriteLine($
"Message={ex.Message}");
206 Console.WriteLine(ex.StackTrace);
推論するモデルを扱うクラス
Definition: PredictionCS.cs:244
ModelCategory ModelCategory
読み込んだモデルの種別
Definition: PredictionCS.cs:265
float[] PredictClassification(CFviImage targetImage)
推論の実行(画像分類)
Definition: PredictionCS.cs:986
Boolean IsValidImages(IEnumerable< CFviImage > targetImages)
推論画像の有効性の確認 ( 多視点版 )
Definition: PredictionCS.cs:714
Boolean IsValidImage(CFviImage targetImage)
推論画像の有効性の確認
Definition: PredictionCS.cs:669
static Boolean CheckLicense()
ライセンスを確認します。
Definition: PredictionCS.cs:419
IEnumerable< ObjectDetectionData > PredictObjectDetection(CFviImage targetImage)
推論の実行(物体検出)
Definition: PredictionCS.cs:1723
void LoadModel(String modelPath)
モデルの読込
Definition: PredictionCS.cs:435
CFviImage PredictSemanticSegmentation(CFviImage targetImage)
推論の実行(セマンティックセグメンテーション)
Definition: PredictionCS.cs:1434
Tuple< Boolean, float > PredictMultiViewAD(IEnumerable< CFviImage > targetImages, float threshold)
推論の実行(多視点アノマリー検出)
Definition: PredictionCS.cs:1299
ModelBackend ModelBackend
読み込んだモデルの推論バックエンド
Definition: PredictionCS.cs:272
void SaveOptimizedModel(String saveModelPath)
読込中のモデルの保存
Definition: PredictionCS.cs:586
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
ModelBackend
モデルの推論バックエンド
Definition: PredictionCS.cs:58
ModelCategory
モデルの種別
Definition: PredictionCS.cs:32
OVDevice
CPU 推論のデバイス
Definition: PredictionCS.cs:79
FVILの最上位ネームスペース
Definition: PredictionCS.cs:16
2Imports System.Collections.Generic
8 Partial Public Class Program
9 Private Shared Sub ChangeOVDevice(modelPath As String, imagePath As String)
11 Dim has_license = PDL.Model.CheckLicense()
12 If False = has_license Then
13 Throw New Exception("no license")
20 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
21 Dim model As Model = New Model(modelPath)
24 Console.WriteLine($"backend: {model.ModelBackend}")
26 ' デバイス変更は CPU 推論のみの対応なので、それ以外であれば抜ける
27 If ModelBackend.OpenVINO <> model.ModelBackend Then
28 Console.WriteLine("not support device-change")
33 Dim image As CFviImage = New CFviImage(imagePath)
36 Dim images As List(Of CFviImage) = Nothing
40 Select Case model.ModelCategory
41 Case ModelCategory.Classification, ModelCategory.AnomalyDetection, ModelCategory.SemanticSegmentation, ModelCategory.PanopticSegmentation, ModelCategory.ObjectDetection
42 Dim is_valid_img = model.IsValidImage(image)
43 If False = is_valid_img Then
44 Throw New Exception("invalid image")
46 Case ModelCategory.MultiViewCNN, ModelCategory.MultiViewAD
47 images = New List(Of CFviImage)()
48 For i_image = 0 To model.NumViews - 1
51 Dim is_valid_img = model.IsValidImages(images)
52 If False = is_valid_img Then
53 Throw New Exception("invalid image")
57 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
61 ' CPU 推論のデバイスを切り替えて実行、処理速度を出力
65 Dim sw As Stopwatch = New Stopwatch()
67 ' 列挙体に定義された全てのデバイスを切り替え
68 For Each device As OVDevice In [Enum].GetValues(GetType(OVDevice))
69 ' モデル読み込み時にデバイスを選択: 読み込み済みのもののデバイスのみを変更することはできない
70 model.LoadModel(modelPath, device)
72 ' 種別にあわせた実行と処理時間の計測: 初回は遅いので空打ちして 2 回目を計測
74 Dim is_sw_enable = 1 = i
79 Select Case model.ModelCategory
80 Case ModelCategory.Classification ' 画像分類
82 Dim scores = model.PredictClassification(image)
83 Case ModelCategory.AnomalyDetection ' アノマリー検出
84 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
86 Dim tuple = model.PredictAnomaly(image, threshold)
87 Case ModelCategory.MultiViewCNN ' 多視点画像分類
89 Dim scores = model.PredictMultiViewCNN(images)
90 Case ModelCategory.MultiViewAD ' 多視点アノマリー検出
91 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
93 Dim tuple = model.PredictMultiViewAD(images, threshold)
94 Case ModelCategory.SemanticSegmentation ' セマンティックセグメンテーション
95 Dim segm_result_image = model.PredictSemanticSegmentation(image)
96 Case ModelCategory.PanopticSegmentation
97 Dim segm_result_iamge = model.PredictPanopticSegmentation(image)
98 Case ModelCategory.ObjectDetection ' 物体検出
100 Dim detections = model.PredictObjectDetection(image)
102 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
104 If Not is_sw_enable Then
110 Console.WriteLine($"device={device}: {sw.ElapsedTicks / Stopwatch.Frequency * 1000}msec")
113 Catch ex As CFviException
114 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
115 Console.WriteLine(ex.StackTrace)
116 Catch ex As Exception
117 Console.WriteLine($"Message={ex.Message}")
118 Console.WriteLine(ex.StackTrace)
122 Private Shared Sub SaveOptimizedModel(loadModelPath As String, saveModelPath As String)
124 Dim has_license = PDL.Model.CheckLicense()
125 If False = has_license Then
126 Throw New Exception("no license")
129 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
130 Dim model As Model = New Model(loadModelPath)
133 Console.WriteLine($"backend: {model.ModelBackend}")
135 ' 動作環境に最適化されたモデルを保存する機能はは GPU 推論のみの対応なので、それ以外であれば抜ける
136 If ModelBackend.TensorRT <> model.ModelBackend Then
137 Console.WriteLine("not support optimized-model-save")
141 ' 読み込み済み ( 動作環境に最適化された ) モデルの保存
142 model.SaveOptimizedModel(saveModelPath)
145 Dim sw = New Stopwatch()
149 model.LoadModel(loadModelPath)
151 Console.WriteLine($"original model: {sw.ElapsedTicks / Stopwatch.Frequency * 1000}msec")
153 ' 最適化済みとして保存したモデルファイル
155 model.LoadModel(saveModelPath)
157 Console.WriteLine($"optimized model: {sw.ElapsedTicks / Stopwatch.Frequency * 1000}msec")
158 Catch ex As CFviException
159 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}")
160 Console.WriteLine(ex.StackTrace)
161 Catch ex As Exception
162 Console.WriteLine($"Message={ex.Message}")
163 Console.WriteLine(ex.StackTrace)