51 |
52 | | 序号 |
53 | API |
54 | 参数解释 |
55 | 说明 |
56 |
57 |
58 | | 1 |
59 | 方法 |
60 | PaddleInfer() |
61 | 构造函数,初始化推理核心,读取本地模型 |
62 | |
63 |
64 |
65 | | 参数 |
66 | string model_path |
67 | 静态图模型文件 |
68 |
69 |
70 | | string params_path |
71 | 模型配置文件信息,默认为空 |
72 |
73 |
74 | | 2 |
75 | 方法 |
76 | void set_divice() |
77 | 设置推理设备 |
78 | 支持 CPU、GPU、ONNX runtime、oneDNN |
79 |
80 |
81 | | 参数 |
82 | Divice divice |
83 | 设备名称选择 |
84 |
85 |
86 | | int num |
87 | 对于CPU、ONNX runtime代表线程数,默认为10; 对于GPU代表显卡编号,默认为0; 对于oneDNN代表cache数量,默认为1 |
88 |
89 |
90 | | ulong memory_init_size |
91 | 显存分配空间(在使用GPU时作用),默认为500 |
92 |
93 |
94 | | int workspace_size |
95 | 显存工作空间(在使用GPU时作用),默认为30 |
96 |
97 |
98 | | 3 |
99 | 方法 |
100 | List <string> get_input_names() |
101 | 获取输入节点名字 |
102 | |
103 |
104 |
105 | | 4 |
106 | 方法 |
107 | void set_input_shape() |
108 | 设置输入节点形状 |
109 | 根据节点维度设置 |
110 |
111 |
112 | | 参数 |
113 | int[] input_shape |
114 | 形状数组 |
115 |
116 |
117 | | string input_name |
118 | 节点名称 |
119 |
120 |
121 | | 5 |
122 | 方法 |
123 | void load_input_data() |
124 | 设置图片/普通输入数据 |
125 | 方法重载 |
126 |
127 |
128 | | 参数 |
129 | string input_name |
130 | 输入节点名称 |
131 |
132 |
133 | | float[] input_data |
134 | 输入数据 |
135 |
136 |
137 | | 参数 |
138 | string input_name |
139 | 输入节点名称 |
140 |
141 |
142 | | byte[] image_data |
143 | 图片数据 |
144 |
145 |
146 | | ulong image_size |
147 | 图片长度 |
148 |
149 |
150 | | int type |
151 | 数据处理类型: type == 0: 均值方差归一化、直接缩放 type == 1: 普通归一化、直接缩放 type == 2: 均值方差归一化、仿射变换 |
152 |
153 |
154 | | 6 |
155 | 方法 |
156 | void infer() |
157 | 模型推理 |
158 | |
159 |
160 |
161 | | 7 |
162 | 方法 |
163 | List <string> get_output_names() |
164 | 获取输出节点名字 |
165 | |
166 |
167 |
168 | | 8 |
169 | 方法 |
170 | List <int> get_shape() |
171 | 获取指定节点形状 |
172 | |
173 |
174 |
175 | | 参数 |
176 | string node_name |
177 | 节点名称 |
178 |
179 |
180 | | 9 |
181 | 方法 |
182 | void T[] read_infer_result <T>() |
183 | 读取推理结果数据 |
184 | 支持读取Float32、Int32、Int64格式数据 |
185 |
186 |
187 | | 参数 |
188 | string output_name |
189 | 输出节点名 |
190 |
191 |
192 | | int data_size |
193 | 输出数据长度 |
194 |
195 |
196 | | 10 |
197 | 方法 |
198 | void delet() |
199 | 删除内存地址 |
200 | |
201 |
202 |
203 |
204 |
205 |
206 | ## 4.2 枚举
207 |
208 |
209 |
210 | | 序号 |
211 | 枚举名 |
212 | 枚举变量 |
213 | 含义 |
214 |
215 |
216 | | 1 |
217 | Divice 设备名称 |
218 | CPU |
219 | 使用CPU推理 |
220 |
221 |
222 | | GPU |
223 | 使用GPU推理 |
224 |
225 |
226 | | ONNX_runtime |
227 | 使用ONNX_runtime推理 |
228 |
229 |
230 | | oneDNN |
231 | 使用oneDNN推理 |
232 |
233 |
234 | 关于上述方法的使用,后续会更新县官的案例教程以及详细的技术文档,敬请期待。
235 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Oo]ut/
33 | [Ll]og/
34 | [Ll]ogs/
35 |
36 | # Visual Studio 2015/2017 cache/options directory
37 | .vs/
38 | # Uncomment if you have tasks that create the project's static files in wwwroot
39 | #wwwroot/
40 |
41 | # Visual Studio 2017 auto generated files
42 | Generated\ Files/
43 |
44 | # MSTest test Results
45 | [Tt]est[Rr]esult*/
46 | [Bb]uild[Ll]og.*
47 |
48 | # NUnit
49 | *.VisualState.xml
50 | TestResult.xml
51 | nunit-*.xml
52 |
53 | # Build Results of an ATL Project
54 | [Dd]ebugPS/
55 | [Rr]eleasePS/
56 | dlldata.c
57 |
58 | # Benchmark Results
59 | BenchmarkDotNet.Artifacts/
60 |
61 | # .NET Core
62 | project.lock.json
63 | project.fragment.lock.json
64 | artifacts/
65 |
66 | # ASP.NET Scaffolding
67 | ScaffoldingReadMe.txt
68 |
69 | # StyleCop
70 | StyleCopReport.xml
71 |
72 | # Files built by Visual Studio
73 | *_i.c
74 | *_p.c
75 | *_h.h
76 | *.ilk
77 | *.meta
78 | *.obj
79 | *.iobj
80 | *.pch
81 | *.pdb
82 | *.ipdb
83 | *.pgc
84 | *.pgd
85 | *.rsp
86 | *.sbr
87 | *.tlb
88 | *.tli
89 | *.tlh
90 | *.tmp
91 | *.tmp_proj
92 | *_wpftmp.csproj
93 | *.log
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio LightSwitch build output
298 | **/*.HTMLClient/GeneratedArtifacts
299 | **/*.DesktopClient/GeneratedArtifacts
300 | **/*.DesktopClient/ModelManifest.xml
301 | **/*.Server/GeneratedArtifacts
302 | **/*.Server/ModelManifest.xml
303 | _Pvt_Extensions
304 |
305 | # Paket dependency manager
306 | .paket/paket.exe
307 | paket-files/
308 |
309 | # FAKE - F# Make
310 | .fake/
311 |
312 | # CodeRush personal settings
313 | .cr/personal
314 |
315 | # Python Tools for Visual Studio (PTVS)
316 | __pycache__/
317 | *.pyc
318 |
319 | # Cake - Uncomment if you are using it
320 | # tools/**
321 | # !tools/packages.config
322 |
323 | # Tabs Studio
324 | *.tss
325 |
326 | # Telerik's JustMock configuration file
327 | *.jmconfig
328 |
329 | # BizTalk build output
330 | *.btp.cs
331 | *.btm.cs
332 | *.odx.cs
333 | *.xsd.cs
334 |
335 | # OpenCover UI analysis results
336 | OpenCover/
337 |
338 | # Azure Stream Analytics local run output
339 | ASALocalRun/
340 |
341 | # MSBuild Binary and Structured Log
342 | *.binlog
343 |
344 | # NVidia Nsight GPU debugger configuration file
345 | *.nvuser
346 |
347 | # MFractors (Xamarin productivity tool) working folder
348 | .mfractor/
349 |
350 | # Local History for Visual Studio
351 | .localhistory/
352 |
353 | # BeatPulse healthcheck temp database
354 | healthchecksdb
355 |
356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
357 | MigrationBackup/
358 |
359 | # Ionide (cross platform F# VS Code tools) working folder
360 | .ionide/
361 |
362 | # Fody - auto-generated XML schema
363 | FodyWeavers.xsd
--------------------------------------------------------------------------------
/PaddleInferenceSharp/PaddleInfer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace PaddleInferenceSharp
8 | {
9 | ///
10 | /// PaddlePaddle Inference 模型推理类
11 | ///
12 | public class PaddleInfer
13 | {
14 | // 推理核心地址
15 | private IntPtr paddle_infer;
16 |
17 | ///
18 | /// 构造函数
19 | ///
20 | /// 模型地址
21 | /// 模型参数地址
22 | public PaddleInfer(string model_path, string params_path=" ")
23 | {
24 | paddle_infer = NativeMethods.paddle_infer_init();
25 | paddle_infer = NativeMethods.set_model(paddle_infer, model_path, params_path);
26 | }
27 | ///
28 | /// 设置推理设备
29 | /// 0.CPU,1.GPU,2.ONNX runtime,3.oneDNN
30 | ///
31 | /// 设备选择
32 | /// 对于CPU、ONNX runtime代表线程数,默认为10;对于GPU代表显卡编号,默认为0;对于oneDNN代表cache数量,默认为1
33 | /// 显存分配空间(尽在使用GPU时作用)
34 | /// 显存工作空间(尽在使用GPU时作用)
35 | public void set_divice(Divice divice, int num = 0, ulong memory_init_size = 500, int workspace_size = 30)
36 | {
37 | if (divice == Divice.CPU)
38 | {
39 | if (num == 0)
40 | {
41 | num = 10;
42 | }
43 | paddle_infer = NativeMethods.predictor_device_cpu(paddle_infer, num);
44 | }
45 | else if (divice == Divice.ONNX_runtime)
46 | {
47 | if (num == 0)
48 | {
49 | num = 10;
50 | }
51 | paddle_infer = NativeMethods.predictor_device_ONNX_runtime(paddle_infer, num);
52 | }
53 | else if (divice == Divice.oneDNN)
54 | {
55 | if (num == 0)
56 | {
57 | num = 1;
58 | }
59 | paddle_infer = NativeMethods.predictor_device_oneDNN(paddle_infer, num);
60 | }
61 | else if (divice == Divice.GPU)
62 | {
63 | paddle_infer = NativeMethods.predictor_device_gpu(paddle_infer, memory_init_size, num, workspace_size);
64 | }
65 | }
66 | ///
67 | /// 获取输入节点名字
68 | ///
69 | /// 输入节点列表
70 | public List get_input_names()
71 | {
72 | int[] length = new int[5];
73 | string input = NativeMethods.get_input_names(paddle_infer,ref length[0]);
74 | List input_name = new List();
75 | int l = 0;
76 | for (int i = 0; i < length.Length; i++)
77 | {
78 | string str = input.Substring(l, length[i]);
79 | input_name.Add(str);
80 | l += length[i];
81 | if (l >= input.Length)
82 | {
83 | break;
84 | }
85 | }
86 | return input_name;
87 | }
88 | ///
89 | /// 设置输入节点形状
90 | ///
91 | /// 形状数组
92 | /// 节点名称
93 | public void set_input_shape(int[] input_shape, string input_name)
94 | {
95 | paddle_infer = NativeMethods.set_input_shape(paddle_infer, input_name, ref input_shape[0], input_shape.Length);
96 | }
97 |
98 | ///
99 | /// 加载普通数据
100 | ///
101 | /// 输入节点名称
102 | /// 输入数据
103 | public void load_input_data(string input_name, float[] input_data)
104 | {
105 | paddle_infer = NativeMethods.load_input_data(paddle_infer, input_name, ref input_data[0]);
106 | }
107 | ///
108 | /// 加载图片数据
109 | ///
110 | /// 输入节点名称
111 | /// 图片数据
112 | /// 图片长度
113 | /// 数据处理类型
114 | public void load_input_data(string input_name, byte[] image_data, ulong image_size, int type)
115 | {
116 | paddle_infer = NativeMethods.load_input_image_data(paddle_infer, input_name, ref image_data[0], image_size, type);
117 | }
118 | ///
119 | /// 模型推理
120 | ///
121 | public void infer()
122 | {
123 | paddle_infer = NativeMethods.infer(paddle_infer);
124 | }
125 | ///
126 | /// 获取输出节点名字
127 | ///
128 | /// 输出节点列表
129 | public List get_output_names()
130 | {
131 | int[] length = new int[5];
132 | string output = NativeMethods.get_output_names(paddle_infer, ref length[0]);
133 | List output_name = new List();
134 | int l = 0;
135 | for (int i = 0; i < length.Length; i++)
136 | {
137 | string str = output.Substring(l, length[i]);
138 | output_name.Add(str);
139 | l += length[i];
140 | if (l >= output.Length)
141 | {
142 | break;
143 | }
144 | }
145 | return output_name;
146 | }
147 | ///
148 | /// 获取指定节点形状
149 | ///
150 | /// 节点名称
151 | ///
152 | public List get_shape(string node_name)
153 | {
154 | int[] shape = new int[5];
155 | int dimension = 0;
156 | List shape_out = new List();
157 | NativeMethods.get_node_shape(paddle_infer, node_name, ref shape[0], ref dimension);
158 | for (int i = 0; i < dimension; i++)
159 | {
160 | shape_out.Add(shape[i]);
161 | }
162 | return shape_out;
163 | }
164 |
165 | ///
166 | /// 读取推理结果数据
167 | ///
168 | /// 数据类型
169 | /// 输出节点名
170 | /// 输出数据长度
171 | /// 推理结果数组
172 | public T[] read_infer_result(string output_name, int data_size)
173 | {
174 | // 获取设定类型
175 | string t = typeof(T).ToString();
176 | // 新建返回值数组
177 | T[] result = new T[data_size];
178 | if (t == "System.Int32")
179 | { // 读取数据类型为整形数据
180 | int[] inference_result = new int[data_size];
181 | NativeMethods.read_result_data_I32(paddle_infer, output_name, ref inference_result[0]);
182 | result = (T[])Convert.ChangeType(inference_result, typeof(T[]));
183 | return result;
184 | }
185 | else if (t == "System.Int64")
186 | {
187 | long[] inference_result = new long[data_size];
188 | NativeMethods.read_result_data_I64(paddle_infer, output_name, ref inference_result[0]);
189 | result = (T[])Convert.ChangeType(inference_result, typeof(T[]));
190 | return result;
191 | }
192 | else
193 | { // 读取数据类型为浮点型数据
194 | float[] inference_result = new float[data_size];
195 | NativeMethods.read_result_data_F32(paddle_infer, output_name, ref inference_result[0]);
196 | result = (T[])Convert.ChangeType(inference_result, typeof(T[]));
197 | return result;
198 | }
199 | }
200 | ///
201 | /// 删除内存地址
202 | ///
203 | public void delet()
204 | {
205 | NativeMethods.dispose(paddle_infer);
206 | }
207 |
208 | }
209 |
210 |
211 | ///
212 | /// 设备名称
213 | ///
214 | public enum Divice {
215 | CPU,
216 | GPU,
217 | ONNX_runtime,
218 | oneDNN
219 | }
220 | }
--------------------------------------------------------------------------------