SqueezeBrains SDK 1.18
DeepCortexDetect.cs
1using System;
2using sb_cs;
3using Tutorials_Common;
4
6{
39 {
40 static SbError execute()
41 {
42 SbProject prj = null;
43 SbImage img = null;
44 SbPar par = null;
45 SbRes res = null;
46 SbError err = SbError.SB_ERR_NONE;
47
49 Console.WriteLine("Sb Init");
50 err = Sb.Init("../../../sb.lic");
51 if (err != SbError.SB_ERR_NONE)
52 {
53 Console.WriteLine("Sb.Init failed with error " + err);
54 return err;
55 }
56
58 Console.WriteLine("Wait for license...");
59 err = Common.WaitLicense();
60 if (err != SbError.SB_ERR_NONE)
61 {
62 Console.WriteLine("SbLicense.WaitLicense failed with error " + err);
63 goto FnExit;
64 }
65
67 err = Sb.InitDl("../../../../../win_x64/dl_framework", SbDlLibraryType.SB_DL_LIBRARY_PYTORCH);
68 if (err != SbError.SB_ERR_NONE)
69 {
70 Console.WriteLine("Sb.InitDl failed with error " + err);
71 return err;
72 }
73
75 String solution_file = "../../solution/deep_cortex_tutorial." + SbDefines.SbSolutionExt;
76 SbSolutionInfo solutionInfo = SbSolution.GetInfo(solution_file);
77 if (solutionInfo == null || solutionInfo.Error() != SbError.SB_ERR_NONE)
78 {
79 err = solutionInfo == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : solutionInfo.Error();
80 Console.WriteLine("SbSolution.GetInfo failed");
81 goto FnExit;
82 }
83 else if (solutionInfo.projects.Length == 0)
84 {
85 err = SbError.SB_ERR_PROJECT_NOT_FOUND;
86 Console.WriteLine("SbSolution.GetInfo no projects found");
87 goto FnExit;
88 }
89 Console.WriteLine("GetInfo, found " + solutionInfo.projects.Length + " project/s");
90
92 prj = SbProject.Load(solution_file, solutionInfo.projects[0].uuid, SbProjectMode.SB_PROJECT_MODE_DETECTION_ONLY);
93 if (prj == null || prj.Error() != SbError.SB_ERR_NONE)
94 {
95 err = prj == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
96 Console.WriteLine("SbProject.Load failed with error " + err);
97 goto FnExit;
98 }
99
102 par = prj.GetPar();
103 par.devices.type = SbDeviceType.SB_DEVICE_GPU_NVIDIA; // set GPU device
104 par.devices.id[0] = 0; // set the first GPU founded on the machine (with index 0)
105 err = prj.SetPar(par);
106 if (err != SbError.SB_ERR_NONE || prj.Error() != SbError.SB_ERR_NONE)
107 {
108 Console.WriteLine("SbProject.SetPar failed with error " + err);
109 goto FnExit;
110 }
111
112 SbFolder folder = SbFolder.Load("../../dataset", "png", true, 0);
113 if (folder != null)
114 {
115 for (int i = 0; i < folder.files.Length; i++)
116 {
118 img = SbImage.Load(folder.files[i]);
119 if (img == null || img.Error() != SbError.SB_ERR_NONE)
120 {
121 Console.WriteLine("SbImage.Load " + folder.files[i] + " failed");
122 err = img == null ? SbError.SB_ERR_INTERNAL : img.Error();
123 break;
124 }
125
127 err = prj.Detection(img, null);
128 if (err != SbError.SB_ERR_NONE)
129 {
130 Console.WriteLine("SbProject.Detection failed with error " + err);
131 break;
132 }
133
135 res = prj.GetRes(false);
136 if (res == null || prj.Error() != SbError.SB_ERR_NONE)
137 {
138 err = res == null ? SbError.SB_ERR_INSUFFICIENT_FREE_MEMORY : prj.Error();
139 Console.WriteLine("SbProject.GetRes failed with error " + err);
140 break;
141 }
142
144 Console.WriteLine("image: " + folder.files[i]);
145 Console.WriteLine("\tmodel=" + res.samples[0].model_name + ",\tweight=" + res.samples[0].weight);
146
147 if (img != null)
148 {
149 img.Dispose();
150 img = null;
151 }
152 if (res != null)
153 {
154 res.Dispose();
155 res = null;
156 }
157 }
158 }
159
160 FnExit:
162 if (prj != null)
163 prj.Dispose();
164 if (img != null)
165 img.Dispose();
166 if (par != null)
167 par.Dispose();
168 if (res != null)
169 res.Dispose();
170
171 Console.WriteLine("Release SqueezeBrains library");
172 Sb.Release();
173
174 Console.WriteLine("Press ENTER to terminate");
175 Console.ReadKey();
176
177 return err;
178 }
179
180 static void Main(string[] args)
181 {
182 execute();
183 }
184 }
185}
Tutorial 3 - Deep Cortex - How to elaborate images
SbError Error()
Returns the error code of the last operation. If no error SbError.SB_ERR_NONE is returned.
Defines class
Definition: cs_common.h:309
static const String SbSolutionExt
Solution file extension
Definition: cs_common.h:315
array< int > id
Identifier of the devices to be used
Definition: cs_par.h:266
SbDeviceType type
Device computational type
Definition: cs_par.h:254
Folder Class that wraps the sb_t_folder structure
Definition: cs_folder.h:16
static SbFolder Load(String^ path, String ^ext, bool sort, int verbosity)
Creates the list of the name of the files in a specified folder.
array< String^> files
Array of files.
Definition: cs_folder.h:22
Sb Main Class
Definition: cs_sb.h:401
static SbError Release()
Releases all the resources allocated in the library
static SbError InitDl(String^ search_path, SbDlLibraryType LibType)
Init the Deep Learning SB Library. The function enables the SbProject::Detection and SVL functions fo...
static SbError Init(String ^ license_file)
Init the SB Library. The function initializes all the functionalities of the library including the li...
SbImage class that wraps the sb_t_image structure. You must call the Dispose() method to free all the...
Definition: cs_image.h:66
static SbImage Load(String^ filename)
Loads an image from file.
Parameters Class that wraps the sb_t_par structure. You must call the Dispose() method to free all th...
Definition: cs_par.h:751
SbDevicesPar devices
Devices used for inference.
Definition: cs_par.h:787
Project Class You must call the Dispose() method to free all the resources of the returned instance.
Definition: cs_project.h:51
static SbProject Load(String^ solution_file, String^ project_uuid, SbProjectMode mode)
Loads an existing project from file.
SbRes GetRes(bool details)
Retrieves the results of the last processed image
SbError Detection(SbImage ^img, SbRoi ^roi)
The function elaborates the image inside the ROI. For Surface projects the function computes also the...
SbPar GetPar()
Retrieves the project parameters structure.
SbError SetPar(SbPar^ par)
Sets the project parameters.
Class of the results of the image elaboration with the SbProject::Detection method....
Definition: cs_res.h:229
array< SbSample^> samples
Samples results. Only Retina projects
Definition: cs_res.h:250
Solution class
Definition: cs_solution.h:44
static SbSolutionInfo GetInfo(String^ solution_file)
Returns the information contained in the solution_file.
Solution Info Class that wraps the sb_t_solution_info structure
Definition: cs_solution.h:17
array< SbProjectInfo^> projects
Array of the projects information of the solution. sb_t_solution_info.projects
Definition: cs_solution.h:23
SbDlLibraryType
Deep Learning library types
Definition: cs_sb.h:39
SbError
Enum error codes
Definition: cs_common.h:13
SbProjectMode
Project loading or saving mode that wraps the sb_t_project_mode enum
Definition: cs_project.h:18
SbDeviceType
Device type that wraps the sb_t_device_type enum
Definition: cs_common.h:219
SB Namespace
Definition: cs_common.h:3