SqueezeBrains SDK 1.18
deep_cortex_svl_simple.c
Go to the documentation of this file.
1
39#include "../common/common.h"
40
45
50
55
56#define SAVE_BEST_EPOCH 0
57
61int main(void)
62{
64 sb_t_info* sb_info = NULL;
65 SB_HANDLE deep_cortex = NULL;
66
68 printf("Initialize the SqueezeBrains library\n");
69 CHECK_FN_GOTO(sb_init("../sb.lic"));
70
72#if defined(_WIN32)
73 // "../../../win_x64/dl_framework" is the pre-defined relative path between working directory and SB Deep Learning Framework into the installation folder.
74 CHECK_FN_GOTO(sb_init_dl("../../../win_x64/dl_framework", SB_DL_LIBRARY_PYTORCH));
75 CHECK_FN_GOTO(sb_init_dl("../../../win_x64/dl_framework", SB_DL_LIBRARY_OPENVINO));
76#else
79#endif
80
81 // Print library version.
82 CHECK_FN_GOTO(sb_get_info(&sb_info, 0));
83 printf("sb ver. %s, compiled on %s %s\n", sb_info->version_str, sb_info->compile_date, sb_info->compile_time);
84
86 printf("wait for license...\n");
88
91
94
97
98FnExit:
100 sb_destroy_info(&sb_info);
101 sb_project_destroy(&deep_cortex);
102
104 printf("Release SqueezeBrains library\n");
105 sb_release();
106
107 printf("Press ENTER to terminate\n");
108 getchar();
109 return (int)err;
110}
111
113{
114 sb_t_err err = SB_ERR_NONE;
115 sb_t_par* par = NULL;
116
117 // Erase the project file to begin from zero
118 remove("dataset/deep_cortex." SB_SOLUTION_EXT);
119
120 // Create the project file
122 CHECK_FN_GOTO(sb_project_get_par(*deep_cortex, &par));
124 CHECK_FN_GOTO(sb_par_add_model(par, "peanut"));
125 CHECK_FN_GOTO(sb_par_add_model(par, "walnut"));
126 CHECK_FN_GOTO(sb_par_add_model(par, "hazelnut"));
127 CHECK_FN_GOTO(sb_par_add_model(par, "almond"));
128 CHECK_FN_GOTO(sb_project_set_par(*deep_cortex, par));
131FnExit:
132 sb_par_destroy(&par);
133 return err;
134}
135
137{
138 sb_t_err err = SB_ERR_NONE;
139 SB_HANDLE image_info = NULL;
140 sb_t_sample sample;
141
145 memset(&sample, 0, sizeof(sample));
146 sample.type = SB_OBJ_TEST;
148 sample.scale = 1.0f;
154 // 1st image
155 CHECK_FN_GOTO(sb_image_info_load(&image_info, "dataset/deep_cortex_peanut.png", deep_cortex));
156 // Reset the image info
159 strcpy(sample.model_name, "peanut");
160 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)));
161 CHECK_FN_GOTO(sb_image_info_add_sample(image_info, &sample));
164 // 2nd image
165 CHECK_FN_GOTO(sb_image_info_load(&image_info, "dataset/deep_cortex_walnut.png", deep_cortex));
166 // Reset the image info
169 strcpy(sample.model_name, "walnut");
170 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)));
171 CHECK_FN_GOTO(sb_image_info_add_sample(image_info, &sample));
174 // 3dh image
175 CHECK_FN_GOTO(sb_image_info_load(&image_info, "dataset/deep_cortex_hazelnut.png", deep_cortex));
176 // Reset the image info
179 strcpy(sample.model_name, "hazelnut");
180 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)));
181 CHECK_FN_GOTO(sb_image_info_add_sample(image_info, &sample));
184 // 4th image
185 CHECK_FN_GOTO(sb_image_info_load(&image_info, "dataset/deep_cortex_almond.png", deep_cortex));
186 // Reset the image info
189 strcpy(sample.model_name, "almond");
190 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)));
191 CHECK_FN_GOTO(sb_image_info_add_sample(image_info, &sample));
194
195FnExit:
196 sb_image_info_destroy(&image_info);
197 return err;
198}
199
201{
202 sb_t_err err = SB_ERR_NONE;
203 SB_HANDLE svl = NULL;
204 sb_t_svl_res* svl_res = NULL;
205 sb_t_par* par = NULL;
206 sb_t_solution_info* solution = NULL;
207
209 CHECK_FN_GOTO(sb_solution_get_info("dataset/deep_cortex." SB_SOLUTION_EXT, &solution));
220 strcpy(par->svl.project_path, "dataset/");
221 par->svl.dl.num_epochs = 100;
222 par->svl.dl.batch_size = 4;
223 par->svl.dl.save_best = 1;
224 // GPU device for SVL
226 par->svl.devices.id[0] = 0;
227 // dl network parameters
229 // dl perturbation parameters
232 par->svl.dl.perturbations.angle_range.min = -180;
238 printf("Training is running, please wait . . .\n");
241 CHECK_FN_GOTO(sb_svl_get_res(svl, &svl_res));
242 printf("SVL results:\n");
243 printf("\tepoch=%d\n", svl_res->global.epochs.saved_epoch);
244 printf("\tloss=%f\n", svl_res->global.epochs.epoch[svl_res->global.epochs.saved_epoch].loss);
245 printf("\taccuracy=%f\n", svl_res->global.epochs.epoch[svl_res->global.epochs.saved_epoch].metrics.accuracy);
246 printf("\ttime=%1.3fs\n", svl_res->time_ms / 1000.0f);
249FnExit:
251 sb_svl_destroy_res(&svl_res);
252 sb_par_destroy(&par);
253 sb_project_destroy(&svl);
254 sb_solution_destroy_info(&solution);
255 return err;
256}
EXTERN_C sb_t_err wait_license(void)
The functions wait until the license status is active.
Definition: common.c:11
#define CHECK_FN_GOTO(function)
Definition: common.h:34
sb_t_err create_deep_cortex_project_file(SB_HANDLE *deep_cortex)
Create a deep_cortex project file.
int main(void)
sb_t_err execute_training(void)
Execute training.
sb_t_err set_model_to_images(SB_HANDLE deep_cortex)
Set samples.
sb_t_err
Errors code enum.
Definition: sb.h:6230
@ SB_ERR_NONE
No errors.
Definition: sb.h:6231
sb_t_err sb_destroy_info(sb_t_info **const info)
Destroys the structure.
sb_t_err sb_release(void)
Releases all the resources allocates in the library.
sb_t_err sb_get_info(sb_t_info **const info, int dl_devices_info)
The function gets information about the sb library and the available computational devices.
sb_t_err sb_init(const char *const license_file)
Initializes the SB library.
sb_t_err sb_init_dl(const char *const search_path, sb_t_dl_library_type lib_type)
Initialize the Deep Learning library.
void * SB_HANDLE
HANDLE definition.
Definition: sb.h:6766
sb_t_err sb_get_uuid(char *const str, size_t size)
Creates a new uuid.
@ SB_DL_LIBRARY_PYTORCH
Pytorch library.
Definition: sb.h:6693
@ SB_DL_LIBRARY_OPENVINO
Openvino library. Can be used only for inference with sb_project_detection function.
Definition: sb.h:6694
@ SB_DEVICE_GPU_NVIDIA
GPU NVidia device.
Definition: sb.h:6834
sb_t_err sb_image_info_set_type(SB_HANDLE image_info, sb_t_image_info_type type)
Sets the type of the SqueezeBrains image info.
sb_t_err sb_image_info_add_sample(SB_HANDLE image_info, const sb_t_sample *const sample)
Adds a sample into the SqueezeBrains image info handle.
sb_t_err sb_image_info_load(SB_HANDLE *image_info, const char *const image_file, SB_HANDLE module_handle)
Creates a SqueezeBrains image info handle.
sb_t_err sb_image_info_reset(SB_HANDLE image_info)
Erases all the data from the SqueezeBrains image info handle.
sb_t_err sb_image_info_destroy(SB_HANDLE *image_info)
Destroys the SqueezeBrains image info handle.
sb_t_err sb_image_info_save(SB_HANDLE image_info)
Saves the SqueezeBrains image info handle into the image file.
@ SB_IMAGE_INFO_TYPE_SVL
Image is used for SVL and not used for training.
Definition: sb.h:13377
#define SB_IMAGE_FILE_EXTENSIONS
List of the possible extensions of the image.
Definition: sb.h:8033
sb_t_err sb_par_add_model(sb_t_par *const par, const char *const model_name)
Adds the model to the parameter structure.
sb_t_err sb_par_destroy(sb_t_par **const par)
Destroys the project parameters structure.
sb_t_err sb_project_set_par(SB_HANDLE handle, const sb_t_par *const par)
Sets the parameters structure into the project handle.
sb_t_err sb_project_create(SB_HANDLE *phandle, const char *const project_name, sb_t_project_type project_type)
Creates a new project of the specifed type.
sb_t_err sb_project_save(SB_HANDLE handle, const char *const solution_file, sb_t_project_mode mode)
Saves the project to file.
sb_t_err sb_project_load(SB_HANDLE *phandle, const char *const solution_file, const char *const project_uuid, sb_t_project_mode mode)
Loads an existing project from a solution file.
sb_t_err sb_project_destroy(SB_HANDLE *phandle)
Frees all the resources of the project handle.
sb_t_err sb_project_get_par(SB_HANDLE handle, sb_t_par **const par)
Retrieves the project parameters structure.
@ SB_PROJECT_MODE_DETECTION_AND_SVL
Load/save all the module information.
Definition: sb.h:9823
@ SB_PROJECT_TYPE_DEEP_CORTEX
Project Deep Cortex.
Definition: sb.h:9673
@ SB_OBJ_TEST
Object is not used for learning.
Definition: sb.h:8762
@ SB_SAMPLE_REQUIRED
Definition: sb.h:8845
sb_t_err sb_solution_get_info(const char *const solution_file, sb_t_solution_info **const solution)
Returns the information contained in the solution_file.
#define SB_SOLUTION_EXT
Extension of the SqueezeBrains solution file.
Definition: sb.h:9721
sb_t_err sb_solution_destroy_info(sb_t_solution_info **const solution)
Destroys the structure of the solution information.
sb_t_err sb_svl_destroy_res(sb_t_svl_res **const res)
Destroys the structure of the results of SVL.
sb_t_err sb_svl_reset(SB_HANDLE handle)
Resets the history of previous executions of the SVL.
sb_t_err sb_svl_run(SB_HANDLE handle)
Runs the SVL.
sb_t_err sb_svl_get_res(SB_HANDLE handle, sb_t_svl_res **const res)
Retrieves the results of SVL.
@ SB_NETWORK_TYPE_ICNET0_64
Deep Learning Image Classification Network 0 with input size 64x64.
Definition: sb.h:10443
int id[SB_DEVICES_MAX_NUMBER]
Identifier of the devices to be used.
Definition: sb.h:11252
sb_t_device_type type
Device type.
Definition: sb.h:11237
General information about sb library and computing devices like CPU and GPUs.
Definition: sb.h:6946
char compile_time[32]
String with the compilation time of the sb library.
Definition: sb.h:6950
char compile_date[32]
String with the compilation date of the sb library.
Definition: sb.h:6949
char version_str[16]
String with the version of the sb library.
Definition: sb.h:6948
float accuracy
accuracy
Definition: sb.h:12312
Project parameters.
Definition: sb.h:11797
sb_t_svl_par svl
SVL parameters.
Definition: sb.h:11913
char uuid[SB_PROJECT_UUID_LEN]
Project UUID.
Definition: sb.h:9697
sb_t_project_info * info
Array of solution project information.
Definition: sb.h:9707
int max
maximum value
Definition: sb.h:6517
int min
minimum value
Definition: sb.h:6516
Sample of an image.
Definition: sb.h:8908
char model_name[SB_PAR_STRING_LEN]
Name of the model.
Definition: sb.h:8981
char uuid[36]
uuid of the sample.
Definition: sb.h:8909
float scale
Scale factor.
Definition: sb.h:8994
sb_t_sample_classify_mode classify_mode
Classification mode.
Definition: sb.h:9063
sb_t_obj_type type
Sample type.
Definition: sb.h:8999
Solution info structure.
Definition: sb.h:9727
sb_t_projects_info projects
Array of the projects info.
Definition: sb.h:9728
int current_project
Index of the current project in the projects array.
Definition: sb.h:9729
sb_t_network_type type
Network type.
Definition: sb.h:10540
sb_t_range angle_range
Angular range, in degrees, for random rotation.
Definition: sb.h:10925
float shift_horizontal
Maximum shift along x-axis.
Definition: sb.h:10909
int flip_horizontal
Flip around horizontal axis.
Definition: sb.h:10892
float shift_vertical
Maximum shift along y-axis.
Definition: sb.h:10917
int flip_vertical
Flip around vertical axis.
Definition: sb.h:10901
int batch_size
Size of the batch used during SVL.
Definition: sb.h:11106
int save_best
At the end of the training, the best internal parameters configuration is recovered.
Definition: sb.h:11131
sb_t_svl_dl_par_network network
Network parameters.
Definition: sb.h:11033
int num_epochs
Number of epochs.
Definition: sb.h:11096
sb_t_svl_dl_par_perturbation perturbations
Perturbations for deep learning training.
Definition: sb.h:11079
sb_t_devices_par devices
Devices used for SVL/training.
Definition: sb.h:11336
char project_path[512]
Path of the project, where the SVL will find the images.
Definition: sb.h:11289
sb_t_svl_dl_par dl
Deep Learning SVL parameters.
Definition: sb.h:11348
char image_ext[64]
Extensions of the images.
Definition: sb.h:11297
float loss
SVL loss.
Definition: sb.h:14338
sb_t_metrics metrics
SVL metrics.
Definition: sb.h:14354
sb_t_svl_res_epoch * epoch
Array of the results of the training epochs.
Definition: sb.h:14369
sb_t_svl_res_epochs epochs
Results of training of module based on Deep Learning.
Definition: sb.h:14567
Defines the results of SVL.
Definition: sb.h:14638
sb_t_svl_res_level global
Cumulative results of all levels of all models.
Definition: sb.h:14681
long long time_ms
Execution time, in ms, of SVL.
Definition: sb.h:14740