WIL-PDL Reference ( .NET Framework ) 1.0.5-gpu
ユーティリティ機能のサンプルコード

ユーティリティ機能のサンプルコードです。


C# 版

  • CPU 推論のデバイス切り替え: L.14- ChangeOVDevice()
  • 読み込み済みモデルの最適化済みモデルとしての保存: L.160- SaveOptimizedModel()
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using FVIL;
7using FVIL.Data;
8using FVIL.PDL;
9
10namespace SamplesCS
11{
12 public partial class Program
13 {
14 static void ChangeOVDevice(String modelPath, String imagePath)
15 {
16 try
17 {
18 var has_license = Model.CheckLicense();
19 if (false == has_license) { throw new Exception("no license"); }
20
22 // 準備
24
25 // モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
26 Model model = new Model(modelPath);
27
28 // モデルの推論バックエンドの確認
29 Console.WriteLine($"backend: {model.ModelBackend}");
30
31 // デバイス変更は CPU 推論のみの対応なので、それ以外であれば抜ける
32 if(ModelBackend.OpenVINO != model.ModelBackend)
33 {
34 Console.WriteLine("not support device-change");
35 return;
36 }
37
38 // 推論対象画像の読込
39 CFviImage image = new CFviImage(imagePath);
40
41 // 多視点のモデルの入力画像
42 List<CFviImage> images = null;
43
44 // 推論対象画像の有効性確認
45 // 多視点の場合、入力画像配列を準備
46 switch (model.ModelCategory)
47 {
48 case ModelCategory.Classification:
49 case ModelCategory.AnomalyDetection:
50 case ModelCategory.SemanticSegmentation:
51 case ModelCategory.PanopticSegmentation:
52 case ModelCategory.ObjectDetection:
53 {
54 var is_valid_img = model.IsValidImage(image);
55 if (false == is_valid_img) { throw new Exception("invalid image"); }
56 }
57 break;
58 case ModelCategory.MultiViewCNN:
59 case ModelCategory.MultiViewAD:
60 {
61 images = new List<CFviImage>();
62 for (var i_image = 0; i_image < model.NumViews; i_image++)
63 {
64 images.Add(image);
65 }
66 var is_valid_img = model.IsValidImages(images);
67 if (false == is_valid_img) { throw new Exception("invalid image"); }
68 }
69 break;
70 case ModelCategory.Unknown:
71 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
72 }
73
75 // CPU 推論のデバイスを切り替えて実行、処理速度を出力
77
78 // 計測用のストップウォッチ
79 System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
80
81 // 列挙体に定義された全てのデバイスを切り替え
82 foreach (OVDevice device in Enum.GetValues(typeof(OVDevice)))
83 {
84 // モデル読み込み時にデバイスを選択: 読み込み済みのもののデバイスのみを変更することはできない
85 model.LoadModel(modelPath, device);
86
87 // 種別にあわせた実行と処理時間の計測: 初回は遅いので空打ちして 2 回目を計測
88 for (var i = 0; i < 2; i++)
89 {
90 var is_sw_enable = (1 == i);
91
92 if (is_sw_enable) { sw.Start(); }
93 switch (model.ModelCategory)
94 {
95 case ModelCategory.Classification:// 画像分類
96 {
97 // 推論実行
98 var scores = model.PredictClassification(image);
99 }
100 break;
101 case ModelCategory.AnomalyDetection:// アノマリー検出
102 {
103 // 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
104 var threshold = 10.0;
105 var tuple = model.PredictAnomaly(image, (float)threshold);
106 }
107 break;
108 case ModelCategory.MultiViewCNN:// 多視点画像分類
109 {
110 // 推論実行
111 var scores = model.PredictMultiViewCNN(images);
112 }
113 break;
114 case ModelCategory.MultiViewAD:// 多視点アノマリー検出
115 {
116 // 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
117 var threshold = 10.0;
118 var tuple = model.PredictMultiViewAD(images, (float)threshold);
119 }
120 break;
121 case ModelCategory.SemanticSegmentation:// セマンティックセグメンテーション
122 {
123 var segm_result_image = model.PredictSemanticSegmentation(image);
124 }
125 break;
126 case ModelCategory.PanopticSegmentation:
127 {
128 var segm_result_iamge = model.PredictPanopticSegmentation(image);
129 }
130 break;
131 case ModelCategory.ObjectDetection:// 物体検出
132 {
133 // 推論実行
134 var detections = model.PredictObjectDetection(image);
135 }
136 break;
137 case ModelCategory.Unknown:
138 default: throw new NotImplementedException($"unmatch model-category={model.ModelCategory}");
139 }
140 if (!is_sw_enable) { continue; }
141
142 sw.Stop();
143
144 Console.WriteLine($"device={device}: {(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}msec");
145 }
146 }
147 }
148 catch (CFviException ex)
149 {
150 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
151 Console.WriteLine(ex.StackTrace);
152 }
153 catch (Exception ex)
154 {
155 Console.WriteLine($"Message={ex.Message}");
156 Console.WriteLine(ex.StackTrace);
157 }
158 }
159
160 static void SaveOptimizedModel(String loadModelPath, String saveModelPath)
161 {
162 try
163 {
164 var has_license = Model.CheckLicense();
165 if (false == has_license) { throw new Exception("no license"); }
166
167 // モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
168 Model model = new Model(loadModelPath);
169
170 // モデルの推論バックエンドの確認
171 Console.WriteLine($"backend: {model.ModelBackend}");
172
173 // 動作環境に最適化されたモデルを保存する機能はは GPU 推論のみの対応なので、それ以外であれば抜ける
174 if (ModelBackend.TensorRT != model.ModelBackend)
175 {
176 Console.WriteLine("not support optimized-model-save");
177 return;
178 }
179
180 // 読み込み済み ( 動作環境に最適化された ) モデルの保存
181 model.SaveOptimizedModel(saveModelPath);
182
183 // モデル読み込み時間の比較
184 var sw = new System.Diagnostics.Stopwatch();
185
186 // 元のモデルファイル
187 sw.Start();
188 model.LoadModel(loadModelPath);
189 sw.Stop();
190 Console.WriteLine($"original model: {(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}msec");
191
192 // 最適化済みとして保存したモデルファイル
193 sw.Start();
194 model.LoadModel(saveModelPath);
195 sw.Stop();
196 Console.WriteLine($"optimized model: {(Double)sw.ElapsedTicks / (Double)System.Diagnostics.Stopwatch.Frequency * 1000}msec");
197 }
198 catch (CFviException ex)
199 {
200 Console.WriteLine($"ErrorCode={ex.ErrorCode}, Message={ex.Message}");
201 Console.WriteLine(ex.StackTrace);
202 }
203 catch (Exception ex)
204 {
205 Console.WriteLine($"Message={ex.Message}");
206 Console.WriteLine(ex.StackTrace);
207 }
208 }
209 }
210}
推論するモデルを扱うクラス
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

VB 版

  • CPU 推論のデバイス切り替え: L.9- ChangeOVDevice()
  • 読み込み済みモデルの最適化済みモデルとしての保存: L.122- SaveOptimizedModel()
1Imports System
2Imports System.Collections.Generic
3Imports FVIL
4Imports FVIL.Data
5Imports FVIL.PDL
6
7Namespace SamplesCS
8 Partial Public Class Program
9 Private Shared Sub ChangeOVDevice(modelPath As String, imagePath As String)
10 Try
11 Dim has_license = PDL.Model.CheckLicense()
12 If False = has_license Then
13 Throw New Exception("no license")
14 End If
15
16 ''' ------------
17 ' 準備
18 ''' ------------
19
20 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
21 Dim model As Model = New Model(modelPath)
22
23 ' モデルの推論バックエンドの確認
24 Console.WriteLine($"backend: {model.ModelBackend}")
25
26 ' デバイス変更は CPU 推論のみの対応なので、それ以外であれば抜ける
27 If ModelBackend.OpenVINO <> model.ModelBackend Then
28 Console.WriteLine("not support device-change")
29 Return
30 End If
31
32 ' 推論対象画像の読込
33 Dim image As CFviImage = New CFviImage(imagePath)
34
35 ' 多視点のモデルの入力画像
36 Dim images As List(Of CFviImage) = Nothing
37
38 ' 推論対象画像の有効性確認
39 ' 多視点の場合、入力画像配列を準備
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")
45 End If
46 Case ModelCategory.MultiViewCNN, ModelCategory.MultiViewAD
47 images = New List(Of CFviImage)()
48 For i_image = 0 To model.NumViews - 1
49 images.Add(image)
50 Next
51 Dim is_valid_img = model.IsValidImages(images)
52 If False = is_valid_img Then
53 Throw New Exception("invalid image")
54 End If
55
56 Case Else
57 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
58 End Select
59
60 ''' ------------
61 ' CPU 推論のデバイスを切り替えて実行、処理速度を出力
62 ''' ------------
63
64 ' 計測用のストップウォッチ
65 Dim sw As Stopwatch = New Stopwatch()
66
67 ' 列挙体に定義された全てのデバイスを切り替え
68 For Each device As OVDevice In [Enum].GetValues(GetType(OVDevice))
69 ' モデル読み込み時にデバイスを選択: 読み込み済みのもののデバイスのみを変更することはできない
70 model.LoadModel(modelPath, device)
71
72 ' 種別にあわせた実行と処理時間の計測: 初回は遅いので空打ちして 2 回目を計測
73 For i = 0 To 1
74 Dim is_sw_enable = 1 = i
75
76 If is_sw_enable Then
77 sw.Start()
78 End If
79 Select Case model.ModelCategory
80 Case ModelCategory.Classification ' 画像分類
81 ' 推論実行
82 Dim scores = model.PredictClassification(image)
83 Case ModelCategory.AnomalyDetection ' アノマリー検出
84 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
85 Dim threshold = 10.0
86 Dim tuple = model.PredictAnomaly(image, threshold)
87 Case ModelCategory.MultiViewCNN ' 多視点画像分類
88 ' 推論実行
89 Dim scores = model.PredictMultiViewCNN(images)
90 Case ModelCategory.MultiViewAD ' 多視点アノマリー検出
91 ' 閾値を任意に設定して推論実行 ( double から float キャストに注意 )
92 Dim threshold = 10.0
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 ' 物体検出
99 ' 推論実行
100 Dim detections = model.PredictObjectDetection(image)
101 Case Else
102 Throw New NotImplementedException($"unmatch model-category={model.ModelCategory}")
103 End Select
104 If Not is_sw_enable Then
105 Continue For
106 End If
107
108 sw.Stop()
109
110 Console.WriteLine($"device={device}: {sw.ElapsedTicks / Stopwatch.Frequency * 1000}msec")
111 Next
112 Next
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)
119 End Try
120 End Sub
121
122 Private Shared Sub SaveOptimizedModel(loadModelPath As String, saveModelPath As String)
123 Try
124 Dim has_license = PDL.Model.CheckLicense()
125 If False = has_license Then
126 Throw New Exception("no license")
127 End If
128
129 ' モデルファイルの読込を含むコンストラクタ ( インスタンスを作成して LoadModel() をする場合と同等 )
130 Dim model As Model = New Model(loadModelPath)
131
132 ' モデルの推論バックエンドの確認
133 Console.WriteLine($"backend: {model.ModelBackend}")
134
135 ' 動作環境に最適化されたモデルを保存する機能はは GPU 推論のみの対応なので、それ以外であれば抜ける
136 If ModelBackend.TensorRT <> model.ModelBackend Then
137 Console.WriteLine("not support optimized-model-save")
138 Return
139 End If
140
141 ' 読み込み済み ( 動作環境に最適化された ) モデルの保存
142 model.SaveOptimizedModel(saveModelPath)
143
144 ' モデル読み込み時間の比較
145 Dim sw = New Stopwatch()
146
147 ' 元のモデルファイル
148 sw.Start()
149 model.LoadModel(loadModelPath)
150 sw.Stop()
151 Console.WriteLine($"original model: {sw.ElapsedTicks / Stopwatch.Frequency * 1000}msec")
152
153 ' 最適化済みとして保存したモデルファイル
154 sw.Start()
155 model.LoadModel(saveModelPath)
156 sw.Stop()
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)
164 End Try
165 End Sub
166 End Class
167End Namespace

Documentation copyright © 2025 TOKYO ELECTRON DEVICE LIMITED https://www.teldevice.co.jp/
Generated on Thu Jan 23 2025 16:11:33 for WIL-PDL Reference ( .NET Framework ) 1.0.5-gpu by Doxygen 1.9.5.