SqueezeBrains SDK 1.18
deep_surface_svl_simple.c
Go to the documentation of this file.
1
39#include "../common/common.h"
40
44typedef struct
45{
46 long long last_time_ms;
48
49static sb_t_err svl_callback_dl(void* const user_data, const sb_t_svl_res* const res, int force)
50{
51 sb_t_info* info = NULL;
52 char device_info_msg[512] = "";
53 sb_get_info(&info, 1);
54 //Search for the information of the device
55 for (int i = 0; i < info->devices.size; i++)
56 {
57 if (res->devices.type == info->devices.device[i].type)
58 {
59 sb_t_device_info* device_info = &info->devices.device[i];
60 sprintf(device_info_msg, ", device=\"%s-%s\", memory=%lld/%lldMByte",
61 sb_device_type_format(device_info->type), device_info->name, device_info->memory.system_used / (1024 * 1024), device_info->memory.system_total / (1024 * 1024));
62 break;
63 }
64 }
65 //Print every 500ms or if "force" flag is set
66 sb_svl_user_data *ud = (sb_svl_user_data*)user_data;
67 if (ud->last_time_ms < res->time_ms)
68 {
69 ud->last_time_ms = res->time_ms + 500;
70 if (strcmp(res->running_step, "training recipe") == 0 && res->global.epochs.size > 0)
71 {
72 // These fields start to be populated in the training recipe step
73 printf("\r\"%s\", epoch=%d/%d, loss=%f, accuracy=%f, time left= %llds, time=%llds, time last improvement=%llds (%llds), memory=%lld/%lldMByte%s ",
74 res->running_step,
76 res->time_left_ms / 1000,
77 res->time_ms / 1000, res->time_ms_last_improvement / 1000, (res->time_ms - res->time_ms_last_improvement) / 1000,
78 info->malloc_size/(1024*1024), info->malloc_size_max/(1024*1024), device_info_msg);
79 }
80 else
81 {
82 printf("\r\"%s\", time=%llds, memory=%lld/%lldMByte ",
83 res->running_step,
84 res->time_ms / 1000,
85 info->malloc_size/(1024*1024), info->malloc_size_max/(1024*1024));
86 }
87
88 fflush(stdout);
89 }
90 sb_destroy_info(&info);
92 return(SB_ERR_NONE);
93}
94
95#if defined(_WIN32) && !defined(QT_CREATOR)
96int svl_run;
97static DWORD WINAPI deep_surface_svl(LPVOID svl)
98{
99 sb_t_err err;
100 printf(">>> START SVL <<<\n");
101 err = sb_svl_run(svl);
102 printf("\n>>> STOP SVL <<<\n");
103 if (err != SB_ERR_NONE)
104 {
105 printf("sb_svl_run returns %d - %s", err, sb_err_format(err));
106 }
107 svl_run = 0;
108 return 0;
109}
110#endif
111
116
120sb_t_err set_labeling(SB_HANDLE deep_surface);
121
126
130sb_t_err check_labeling_file_exists(char* img_file, sb_t_folder* folder_defects, int* file_index);
131
132#define MODEL_NAME "stripe"
133
137int main(void)
138{
139 sb_t_err err = SB_ERR_NONE;
140 sb_t_info* sb_info = NULL;
141 SB_HANDLE project = NULL;
142
144 printf("Initialize the SqueezeBrains library\n");
145 CHECK_FN_GOTO(sb_init("../sb.lic"));
146
148#if defined(_WIN32)
149 // "../../../win_x64/dl_framework" is the pre-defined relative path between working directory and SB Deep Learning Framework into the installation folder.
150 CHECK_FN_GOTO(sb_init_dl("../../../win_x64/dl_framework", SB_DL_LIBRARY_PYTORCH));
151 CHECK_FN_GOTO(sb_init_dl("../../../win_x64/dl_framework", SB_DL_LIBRARY_OPENVINO));
152#else
155#endif
156
157 // Print library version.
158 CHECK_FN_GOTO(sb_get_info(&sb_info, 0));
159 printf("sb ver. %s, compiled on %s %s\n", sb_info->version_str, sb_info->compile_date, sb_info->compile_time);
160
162 printf("wait for license...\n");
164
167
169 CHECK_FN_GOTO(set_labeling(project));
170
173
174FnExit:
176 sb_destroy_info(&sb_info);
177 sb_project_destroy(&project);
178
180 printf("Release SqueezeBrains library\n");
181 sb_release();
182
183 printf("Press ENTER to terminate\n");
184 getchar();
185 return (int)err;
186}
187
189{
190 sb_t_err err = SB_ERR_NONE;
191 sb_t_par* par = NULL;
192
193 // Erase the project file to begin from zero
194 remove("dataset/deep_surface." SB_SOLUTION_EXT);
195
197 CHECK_FN_GOTO(sb_project_create(deep_surface, "semilavorato", SB_PROJECT_TYPE_DEEP_SURFACE));
198 CHECK_FN_GOTO(sb_project_get_par(*deep_surface, &par));
201 CHECK_FN_GOTO(sb_par_add_model(par, MODEL_NAME));
202 CHECK_FN_GOTO(sb_project_set_par(*deep_surface, par));
205FnExit:
206 sb_par_destroy(&par);
207 return err;
208}
209
210sb_t_err check_labeling_file_exists(char* img_file, sb_t_folder* folder_defects, int *file_index)
211{
212 sb_t_err err = SB_ERR_NONE;
213 char img_name_no_ext[256];
214 char img_defects_name_no_ext[256];
215
217 CHECK_FN_GOTO(sb_file_extract_name(img_file, 0, &img_name_no_ext, sizeof(img_name_no_ext)));
218
220 for (int i = 0; i < folder_defects->n; i++)
221 {
222 CHECK_FN_GOTO(sb_file_extract_name(folder_defects->file[i].name, 0, &img_defects_name_no_ext, sizeof(folder_defects->file[i].name)));
224 if (strcmp(img_name_no_ext, img_defects_name_no_ext) == 0)
225 {
226 *file_index = i;
227 break;
228 }
229 }
230
231FnExit:
232 return err;
233}
234
236{
237 sb_t_err err = SB_ERR_NONE;
238 SB_HANDLE image_info = NULL;
239 sb_t_roi *defects = NULL;
240 sb_t_roi *roi = NULL;
241 sb_t_image *img = NULL;
242 sb_t_image *img_defects = NULL;
243 sb_t_folder* folder = NULL;
244 sb_t_folder* folder_defects = NULL;
245 int i;
246
248 CHECK_FN_GOTO(sb_folder_load(&folder, "dataset", SB_IMAGE_INFO_EXT, 1, 0))
249 for(i = 0; i < folder->n; i++)
250 remove(folder->file[i].name);
252
254 CHECK_FN_GOTO(sb_folder_load(&folder_defects, "dataset/roi_defects", "bmp", 1, 0))
255
256
260 CHECK_FN_GOTO(sb_folder_load(&folder, "dataset", "png", 1, 0))
261 for (i = 0; i < folder->n; i++)
262 {
263 int file_index = -1;
264
265 // Check if image mask with defects labeling exists
266 CHECK_FN_GOTO(check_labeling_file_exists(folder->file[i].name, folder_defects, &file_index))
267 if (file_index >= 0)
268 {
269 // Load image and create image info
270 CHECK_FN_GOTO(sb_image_load(&img, folder->file[i].name))
271 CHECK_FN_GOTO(sb_image_info_load(&image_info, folder->file[i].name, deep_surface))
272 // Reset the image info
275 // Get the roi
276 CHECK_FN_GOTO(sb_image_info_get_roi(image_info, &roi, img->width, img->height, 0))
277 // Set a full image ROI
278 CHECK_FN_GOTO(sb_roi_set_rect(roi, 255, sb_rect(0, 0, roi->img->width, roi->img->height), 1))
279 CHECK_FN_GOTO(sb_image_info_set_roi(image_info, roi));
280 // Get the defects roi
281 CHECK_FN_GOTO(sb_image_info_get_roi_defects(image_info, &defects, img->width, img->height, 0))
282 // Load image with the defects mask
283 CHECK_FN_GOTO(sb_image_load(&img_defects, folder_defects->file[file_index].name));
284 // Copy defects mask data into ROI defects
285 CHECK_FN_GOTO(sb_image_copy(img_defects, defects->img));
286 CHECK_FN_GOTO(sb_image_info_set_roi_defects(image_info, defects))
288
289 sb_image_info_destroy(&image_info);
290 sb_image_destroy(&img);
291 sb_image_destroy(&img_defects);
292 sb_roi_destroy(&roi);
293 sb_roi_destroy(&defects);
294 }
295 }
296
297FnExit:
299 sb_image_info_destroy(&image_info);
300 sb_image_destroy(&img);
301 sb_image_destroy(&img_defects);
302 sb_roi_destroy(&roi);
303 sb_roi_destroy(&defects);
304 sb_folder_destroy(&folder);
305 sb_folder_destroy(&folder_defects);
306 return err;
307}
308
310{
311 sb_t_err err = SB_ERR_NONE;
312 SB_HANDLE svl = NULL;
313 sb_t_svl_res* svl_res = NULL;
314 sb_t_par* par = NULL;
315 sb_t_solution_info* solution = NULL;
316 sb_svl_user_data user_data = {0};
317
319 CHECK_FN_GOTO(sb_solution_get_info("dataset/deep_surface." SB_SOLUTION_EXT, &solution));
330 strcpy(par->svl.project_path, "dataset/");
331 par->svl.fp_progress = svl_callback_dl;
332 //par.svl.free_memory_percentage = free_memory_percentage; // TODO da attivare
333 par->svl.user_data = &user_data;
334 // GPU device for SVL
336 par->svl.devices.id[0] = 0;
337 // dl network parameters
339 par->svl.dl.num_epochs = 200;
340 par->svl.dl.batch_size = 4;
341 // dl perturbation parameters
344 par->svl.dl.perturbations.angle_range.min = -180;
349
350#if defined(_WIN32) && !defined(QT_CREATOR)
351 printf("-------------------------------------------------------------------------------------\n");
352 printf("CTRL-right + F12 = stop svl\n");
353 printf("-------------------------------------------------------------------------------------\n");
355 svl_run = 1;
356 int stop_request = 0;
357 CreateThread(NULL, 0, deep_surface_svl, svl, 0, NULL);
359 //With WINDOWS Visual Studio it is possibile to check the keyborad without waiting enter to be pressed
360 while(1)
361 {
362 Sleep(100);
363 //Check the keyborad without waiting enter to be pressed
364 if (!stop_request && (GetAsyncKeyState(VK_RCONTROL) & 0x8000) && (GetAsyncKeyState(VK_F12) & 0x8000))
365 {
366 printf("\nSend stop request to svl...\n");
368 stop_request = 1;
369 }
370 if (!svl_run) { break;}
371 }
372#else
375#endif
376
378 CHECK_FN_GOTO(sb_svl_get_res(svl, &svl_res));
379 if (strcmp(svl_res->running_step, SB_SVL_STEP_FINISHED) == 0)
380 {
381 printf("\nsaving SVL results....\n");
383 }
384
385FnExit:
387 sb_svl_destroy_res(&svl_res);
388 sb_par_destroy(&par);
389 sb_project_destroy(&svl);
390 sb_solution_destroy_info(&solution);
391
392 return err;
393}
394
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 check_labeling_file_exists(char *img_file, sb_t_folder *folder_defects, int *file_index)
Check if a labeling mask file exists for the specified image.
sb_t_err create_deep_surface_project_file(SB_HANDLE *deep_surface)
Create a solution file with a Deep Surface project.
int main(void)
sb_t_err set_labeling(SB_HANDLE deep_surface)
Set labeling.
sb_t_err execute_training(void)
Execute training.
sb_t_err
Errors code enum.
Definition: sb.h:6230
const char * sb_err_format(sb_t_err code)
Returns the error message.
@ SB_ERR_NONE
No errors.
Definition: sb.h:6231
sb_t_err sb_file_extract_name(const char *const full, int with_ext, char *const name, int name_size)
Extracts the file name from the full file path.
sb_t_err sb_folder_destroy(sb_t_folder **folder)
Frees all the resources of the sb_t_folder structure.
sb_t_err sb_folder_load(sb_t_folder **const f, const char *const path, const char *const ext, int sort, int verbosity)
Creates the list of the name of the files in a specified folder.
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_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_INLINE sb_t_rect sb_rect(int x, int y, int width, int height)
Inline constructor of structure sb_t_rect.
Definition: sb.h:6632
sb_t_err sb_image_info_set_roi(SB_HANDLE image_info, const sb_t_roi *const roi)
Sets the ROI in a SqueezeBrains image info handle.
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_get_roi(SB_HANDLE image_info, sb_t_roi **const roi, int width, int height, int compressed)
Gets the ROI from a SqueezeBrains image info handle.
sb_t_err sb_image_info_get_roi_defects(SB_HANDLE image_info, sb_t_roi **const defects, int width, int height, int compressed)
Gets the surface defects ROI from a 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.
#define SB_IMAGE_INFO_EXT
Extension of the image information file associated to an image.
Definition: sb.h:13364
sb_t_err sb_image_info_set_roi_defects(SB_HANDLE image_info, const sb_t_roi *const defects)
Sets the surface defects ROI on 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_image_copy(const sb_t_image *const src, sb_t_image *const dst)
Copies the image.
sb_t_err sb_image_destroy(sb_t_image **const pimg)
Destroys the image.
sb_t_err sb_image_load(sb_t_image **const img, const char *const file)
Loads an image from a file.
const char * sb_device_type_format(sb_t_device_type type)
Returns the string of the device type.
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_SURFACE
Project Deep Surface.
Definition: sb.h:9672
sb_t_err sb_roi_set_rect(sb_t_roi *const roi, unsigned char gl, sb_t_rect rect, int reset_roi)
Sets a rectangular ROI.
sb_t_err sb_roi_destroy(sb_t_roi **const roi)
Destroys the ROI.
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_stop_request(SB_HANDLE handle)
Sends a request to the SVL for stop.
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.
#define SB_SVL_STEP_FINISHED
SVL step: svl has finished.
Definition: sb.h:14617
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_SDINET0
Deep Learning Surface Defects Inspection Network 0 with variable input size.
Definition: sb.h:10424
Information about a computing device.
Definition: sb.h:6878
sb_t_memory_info memory
Memory information about the device and its process.
Definition: sb.h:6916
char name[128]
Device name.
Definition: sb.h:6893
sb_t_device_type type
Device type.
Definition: sb.h:6882
int size
Number of available devices.
Definition: sb.h:6939
sb_t_device_info * device
Array of devices.
Definition: sb.h:6934
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
char name[256]
Name of the file included of path.
Definition: sb.h:7359
defines the list of the file in a folder.
Definition: sb.h:7367
sb_t_file * file
Array of files.
Definition: sb.h:7368
int n
Number of elements of the array file.
Definition: sb.h:7369
Defines an image.
Definition: sb.h:8100
int width
Width, in pixel, of the image.
Definition: sb.h:8104
int height
Height, in pixel, of the image.
Definition: sb.h:8105
General information about sb library and computing devices like CPU and GPUs.
Definition: sb.h:6946
sb_t_devices_info devices
List of available computing devices.
Definition: sb.h:6954
char compile_time[32]
String with the compilation time of the sb library.
Definition: sb.h:6950
signed long long malloc_size
Current allocated memory, expressed in bytes, by sb the library.
Definition: sb.h:6951
signed long long malloc_size_max
Maximum allocated memory, expressed in bytes, by sb the library.
Definition: sb.h:6952
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
signed long long system_total
system physical memory, in bytes
Definition: sb.h:6822
signed long long system_used
system used physical memory, in bytes
Definition: sb.h:6823
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
Defines a roi.
Definition: sb.h:8520
sb_t_image * img
8bit BW image.
Definition: sb.h:8524
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
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_fp_svl_progress fp_progress
The SVL calls this callback to notify the user the results of SVL.
Definition: sb.h:11272
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
void * user_data
Pointer to data which is passed to the callbacks.
Definition: sb.h:11284
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
int size
Number of the elements of the array epoch .
Definition: sb.h:14372
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_last_improvement
Time, in ms, when there was the last improvement.
Definition: sb.h:14746
long long time_ms
Execution time, in ms, of SVL.
Definition: sb.h:14740
sb_t_par par
Project parameters.
Definition: sb.h:14639
long long time_left_ms
Estimated time, in ms, left to the end of the SVL.
Definition: sb.h:14751
sb_t_devices_par devices
The devices used to compute the current SVL results.
Definition: sb.h:14759
char running_step[256]
Description of the current step of the training.
Definition: sb.h:14668