SqueezeBrains SDK 1.13
retina_svl_custom_elaboration.c
Go to the documentation of this file.
1
51#include "../common/common.h"
52
57
62
67
72
76typedef struct
77{
78 long long last_time_ms;
80
81#define SCALE 0.7f
82
88static sb_t_err svl_pre_elaboration(const SB_HANDLE image_info, void* const user_data, const char* const image_file, int image_index, sb_t_svl_pre_elaboration* const pe)
89{
91 sb_t_lut_point* ptr = NULL;
92 sb_t_image* image = NULL;
93 if (!user_data || !image_file || !pe)
94 {
95 printf("NULL pointer\n");
97 }
98
99 memset(pe, 0, sizeof(*pe));
100
101 //how to access user data
102 //retina_svl_user_data* ud = NULL;
103 //ud = (retina_svl_user_data*)user_data;
104
105 // Load image.
106 CHECK_FN_GOTO(sb_image_load(&image, image_file))
107 CHECK_FN_GOTO(sb_image_create(&pe->image, (int)(image->width * SCALE), (int)(image->height * SCALE), image->format))
108 // Image elaboration.
109 CHECK_FN_GOTO(sb_image_resize(image, sb_rect(0, 0, image->width, image->height), SCALE, 1, pe->image))
110
111 // Create the roi.
112 CHECK_FN_GOTO(sb_roi_create(&pe->roi, pe->image->width, pe->image->height))
113 // set a rectangular roi.
114 CHECK_FN_GOTO(sb_roi_set_rect(pe->roi, 255, sb_rect(10, 10, pe->image->width - 10, pe->image->height - 10), 0))
115
116 // Create the lut to warp the point coordinates from the rescaled image to the original one.
117 CHECK_FN_GOTO(sb_lut_create(&pe->warped2ori, pe->image->width, pe->image->height))
118 CHECK_FN_GOTO(sb_lut_get_ptr(pe->warped2ori, &ptr))
119 for (int y = 0; y < pe->image->height; ++y)
120 {
121 sb_t_lut_point* raw = ptr + pe->image->width * y;
122 for (int x = 0; x < pe->image->width; ++x)
123 {
124 raw[x].y = (int)lrintf(y / SCALE);
125 if (raw[x].y >= image->height)
126 raw[x].y = image->height - 1;
127
128 raw[x].x = (int)lrintf(x / SCALE);
129 if (raw[x].x >= image->width)
130 raw[x].x = image->width - 1;
131 }
132 }
133
134FnExit:
135 sb_image_destroy(&image);
136 if (err)
137 {
139 sb_roi_destroy(&pe->roi);
141 }
142 return(err);
143}
144
145static sb_t_err svl_progress(void* user_data, const sb_t_svl_res* const res, int force)
146{
148
149 if (ud->last_time_ms < res->time_ms || force)
150 {
151 ud->last_time_ms = res->time_ms + 500;
152 printf("\rsvl_progress[%d] time=%lldms, tp=%d, fp=%d, fn=%d, goodness=%f ", force, res->time_ms, res->global.tp, res->global.fp, res->global.fn, res->global.goodness);
153 }
154 sb_svl_destroy_res(&res);
155 return(SB_ERR_NONE);
156}
157
158sb_t_err svl_callback_command(void* const user_data, const sb_t_svl_res* const res, sb_t_svl_command* const command, const char * const msg)
159{
160 printf("SVL CALLBACK ACTION: stop_reason=%d, msg=%s\n", res->stop_reason, msg);
161 *command = SB_SVL_COMMAND_CONTINUE;
162 return SB_ERR_NONE;
163}
164
168int main(void)
169{
170 sb_t_err err = SB_ERR_NONE;
171 sb_t_info* sb_info = NULL;
172 SB_HANDLE project = NULL;
173
176 printf("Initialize the SqueezeBrains library\n");
177 CHECK_FN_GOTO(sb_init("../sb.lic"))
178
179 // Print library version.
180 CHECK_FN_GOTO(sb_get_info(&sb_info, 0))
181 printf("sb ver. %s, compiled on %s %s\n", sb_info->version_str, sb_info->compile_date, sb_info->compile_time);
182
184 printf("wait for license...\n");
186
187
188
189
191
192
193 CHECK_FN_GOTO(set_samples(project))
194
195
197
198
200
201FnExit:
203 sb_project_destroy(&project);
204 sb_destroy_info(&sb_info);
205
207 printf("Release SqueezeBrains library\n");
208 sb_release();
209
210 printf("Press ENTER to terminate\n");
211 getchar();
212 return (int)err;
213}
214
216{
217 sb_t_err err = SB_ERR_NONE;
218 sb_t_par retina_par;
219
220 // Erase the project file to begin from zero
221 remove("../images/wheels." SB_SOLUTION_EXT);
222
225 CHECK_FN_GOTO(sb_project_get_par((*retina), &retina_par))
227 CHECK_FN_GOTO(sb_par_add_model(&retina_par, "W"))
228 retina_par.models.model[0].obj_size = sb_size(((int)(136 * SCALE) + 7) & 0xFFFFFFF8, ((int)(128 * SCALE) + 7) & 0xFFFFFFF8);
229 retina_par.models.model[0].obj_min_distance = sb_size((int)(100 * SCALE), (int)(128 * SCALE));
230 retina_par.models.model[0].obj_stride_coarse = sb_size(8, 8);
231 retina_par.models.model[0].obj_stride_fine = sb_size(4, 4);
232 retina_par.models.model[0].perturbations.size = 1;
235 CHECK_FN_GOTO(sb_project_set_par((*retina), &retina_par))
238
239FnExit:
240 return err;
241}
242
244{
245 sb_t_err err = SB_ERR_NONE;
246 SB_HANDLE retina_image = NULL;
247 sb_t_sample sample;
248
250 // Set the parameters common to all sample
251 memset(&sample, 0, sizeof(sample));
252 sample.type = SB_OBJ_TEST;
254 sample.scale = 1.0f;
255 strcpy(sample.model_name, "W");
256 // samples of the first image
257 CHECK_FN_GOTO(sb_image_info_load(&retina_image, "../images/001.ppm", retina))
258 // Reset the image info
260 CHECK_FN_GOTO(sb_image_info_set_notes(retina_image, "The first image"))
262 sample.centre = sb_point(79, 141);
263 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)))
264 CHECK_FN_GOTO(sb_image_info_add_sample(retina_image, &sample))
265 sample.centre = sb_point(234, 139);
266 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)))
267 CHECK_FN_GOTO(sb_image_info_add_sample(retina_image, &sample))
268 sample.centre = sb_point(537, 146);
269 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)))
270 CHECK_FN_GOTO(sb_image_info_add_sample(retina_image, &sample))
271 CHECK_FN_GOTO(sb_image_info_save(retina_image))
273
274 // samples of the second image
275 CHECK_FN_GOTO(sb_image_info_load(&retina_image, "../images/002.ppm", retina))
276 // Reset the image info
278 CHECK_FN_GOTO(sb_image_info_set_notes(retina_image, "The second image"))
280 sample.centre = sb_point(333, 143);
281 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)))
282 CHECK_FN_GOTO(sb_image_info_add_sample(retina_image, &sample))
283 CHECK_FN_GOTO(sb_image_info_save(retina_image))
285
286 // samples of the third image
287 CHECK_FN_GOTO(sb_image_info_load(&retina_image, "../images/002.ppm", retina))
288 // Reset the image info
290 CHECK_FN_GOTO(sb_image_info_set_notes(retina_image, "The third image"))
292 sample.centre = sb_point(511, 136);
293 CHECK_FN_GOTO(sb_get_uuid(sample.uuid, sizeof(sample.uuid)))
294 CHECK_FN_GOTO(sb_image_info_add_sample(retina_image, &sample))
295 CHECK_FN_GOTO(sb_image_info_save(retina_image))
297
298FnExit:
300 sb_image_info_destroy(&retina_image);
301 return err;
302}
303
305{
306 sb_t_err err = SB_ERR_NONE;
307 SB_HANDLE svl = NULL;
308 sb_t_svl_res* svl_res = NULL;
309 sb_t_par par;
310 retina_svl_user_data user_data;
311 sb_t_solution_info* solution = NULL;
312
314 CHECK_FN_GOTO(sb_solution_get_info("../images/wheels." SB_SOLUTION_EXT, &solution));
327 memset(&user_data, 0, sizeof(user_data));
329 strcpy(par.svl.project_path, "../images/");
330 par.svl.sl.goodness_target = 0.8f;
331 par.svl.fp_pre_elaboration = svl_pre_elaboration;
332 par.svl.fp_command = svl_callback_command;
333 par.svl.fp_progress = svl_progress;
334 par.svl.user_data = &user_data;
338 printf("\n");
340 CHECK_FN_GOTO(sb_svl_get_res(svl, &svl_res))
341 printf("goodness=%f\n", svl_res->global.goodness);
342 printf("time=%1.3fs\n", svl_res->time_ms / 1000.0f);
345
346FnExit:
348 sb_svl_destroy_res(&svl_res);
349 sb_project_destroy(&svl);
350 sb_solution_destroy_info(&solution);
351 return err;
352}
353
355{
356 sb_t_err err = SB_ERR_NONE;
357 SB_HANDLE retina = NULL;
358 sb_t_image* image = NULL;
359 sb_t_image* image_resized = NULL;
360 sb_t_res* res = NULL;
361 sb_t_roi* roi = NULL;
362 sb_t_solution_info* solution = NULL;
363
365 CHECK_FN_GOTO(sb_solution_get_info("../images/wheels." SB_SOLUTION_EXT, &solution));
368 for (int i = 1; i <= 3; i++)
369 {
370 char name[256];
371 snprintf(name, sizeof(name) - 1, "../images/%03d.ppm", i);
373 CHECK_FN_GOTO(sb_image_load(&image, name))
374 CHECK_FN_GOTO(sb_image_create(&image_resized, (int)(image->width*SCALE), (int)(image->height*SCALE), image->format))
375 CHECK_FN_GOTO(sb_image_resize(image, sb_rect(0, 0, image->width, image->height), SCALE, 1, image_resized))
378 CHECK_FN_GOTO(sb_roi_create(&roi, image_resized->width, image_resized->height))
379 // Set a rectangular roi equal to image dimensions.
380 CHECK_FN_GOTO(sb_roi_set_rect(roi, 255, sb_rect(0, 0, image_resized->width, image_resized->height), 0))
382 CHECK_FN_GOTO(sb_project_detection(retina, image_resized, roi, NULL, NULL))
384 CHECK_FN_GOTO(sb_project_get_res(retina, &res, 0))
385 // Print the results
386 printf("image: %s\n", name);
387 for (int j = 0; j < res->samples.size; ++j)
388 printf("%d) centre=(%d,%d), weight=%f\n", j, res->samples.sample[j].centre.x, res->samples.sample[j].centre.y, res->samples.sample[j].weight);
389 // Destroy the handles.
390 CHECK_FN_GOTO(sb_image_destroy(&image_resized))
393 }
394FnExit:
396 sb_image_destroy(&image);
397 sb_image_destroy(&image_resized);
398 sb_project_destroy(&retina);
399 sb_solution_destroy_info(&solution);
400 return err;
401}
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
Errors code enum.
Definition: sb.h:5541
@ SB_ERR_NONE
No errors.
Definition: sb.h:5542
@ SB_ERR_NULL_POINTER
Function has been called with a NULL pointer.
Definition: sb.h:5632
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_init(const char *const license_file)
Initializes the SB library.
void * SB_HANDLE
HANDLE definition.
Definition: sb.h:6042
sb_t_err sb_get_info(sb_t_info **const info, int gpu_info)
The function gets information about the sb library and the available computational devices.
sb_t_err sb_get_uuid(char *const str, size_t size)
Creates a new uuid.
SB_INLINE sb_t_point sb_point(int x, int y)
Inline constructor of structure sb_t_point.
Definition: sb.h:5832
SB_INLINE sb_t_size sb_size(int width, int height)
Inline constructor of structure sb_t_size.
Definition: sb.h:5860
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:5918
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_set_notes(SB_HANDLE image_info, const char *const str)
Set a note into SqueezeBrains image info handle.
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:11931
sb_t_err sb_image_create(sb_t_image **const pimg, int width, int height, sb_t_image_format format)
Creates an image.
#define SB_IMAGE_FILE_EXTENSIONS
List of the possible extensions of the image.
Definition: sb.h:7243
sb_t_err sb_image_destroy(sb_t_image **const pimg)
Destroys the image.
sb_t_err sb_image_resize(const sb_t_image *const src, sb_t_rect rect, float scale, sb_t_interpolation_mode interpolation_mode, sb_t_image *const dst)
Resizes the image.
sb_t_err sb_image_load(sb_t_image **const img, const char *const file)
Loads an image from a file.
sb_t_err sb_lut_destroy(SB_HANDLE *phandle)
Destroys the handle of the lut.
sb_t_err sb_lut_create(SB_HANDLE *phandle, int width, int height)
Creates the handle of the lut.
sb_t_err sb_lut_get_ptr(SB_HANDLE phandle, sb_t_lut_point **const ptr)
Retrieves, in the parameter ptr, the pointer to internal array of the lut.
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_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_get_par(SB_HANDLE handle, sb_t_par *const par)
Retrieves the project parameters structure.
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 file.
sb_t_err sb_project_detection(SB_HANDLE handle, const sb_t_image *const img, const sb_t_roi *const roi, const sb_t_roi *const roi_defects, const sb_t_samples *const samples)
Detection function.
sb_t_err sb_project_get_res(SB_HANDLE handle, sb_t_res **const res, int details)
Retrieves the results of the last elaborated image.
sb_t_err sb_project_destroy(SB_HANDLE *phandle)
Frees all the resources of the project handle.
@ SB_PROJECT_MODE_DETECTION_ONLY
Load/save the minimum module information to allow detection.
Definition: sb.h:9026
@ SB_PROJECT_MODE_DETECTION_AND_SVL
Load/save all the module information.
Definition: sb.h:9027
@ SB_PROJECT_TYPE_RETINA
Project Retina.
Definition: sb.h:8875
sb_t_err sb_res_destroy(sb_t_res **const res)
Destroys the module results structure.
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_create(sb_t_roi **const roi, int width, int height)
Creates a ROI.
sb_t_err sb_roi_destroy(sb_t_roi **const roi)
Destroys the ROI.
@ SB_OBJ_TEST
Object is not used for learning.
Definition: sb.h:7972
@ SB_SAMPLE_REQUIRED
Definition: sb.h:8055
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:8925
sb_t_err sb_solution_destroy_info(sb_t_solution_info **const solution)
Destroys the structure of the solution information.
sb_t_svl_command
Enumerates the actions that SVL has to do after a stop.
Definition: sb.h:9403
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_SVL_COMMAND_CONTINUE
The SVL continues and, if necessary, resets the training.
Definition: sb.h:9412
sb_t_err detection(void)
Detection on resized images.
int main(void)
sb_t_err set_samples(SB_HANDLE retina)
Set samples.
sb_t_err execute_svl(void)
Execute svl.
sb_t_err create_retina_project_file(SB_HANDLE *retina)
Create a retina project file.
Defines an image.
Definition: sb.h:7310
int width
Width, in pixel, of the image.
Definition: sb.h:7314
sb_t_image_format format
Format of the image pixel.
Definition: sb.h:7320
int height
Height, in pixel, of the image.
Definition: sb.h:7315
General information about sb library and computing devices like CPU and GPUs.
Definition: sb.h:6180
char compile_time[32]
String with the compilation time of the sb library.
Definition: sb.h:6184
char compile_date[32]
String with the compilation date of the sb library.
Definition: sb.h:6183
char version_str[16]
String with the version of the sb library.
Definition: sb.h:6182
Struct that defines the coordinates of a point of a lut.
Definition: sb.h:7142
unsigned short y
ordinate of the point.
Definition: sb.h:7144
unsigned short x
abscissa of the point.
Definition: sb.h:7143
sb_t_par_perturbations perturbations
Perturbations of the samples.
Definition: sb.h:10460
sb_t_size obj_size
Model size.
Definition: sb.h:10426
sb_t_size obj_stride_coarse
Coarse search step.
Definition: sb.h:10518
sb_t_size obj_stride_fine
Fine search step.
Definition: sb.h:10532
sb_t_size obj_min_distance
Minimum distance between two samples.
Definition: sb.h:10503
sb_t_par_model model[SB_PAR_MODELS_NUM]
Array of models.
Definition: sb.h:10588
int flip_vertical
Flip around x-axis. 3th operation.
Definition: sb.h:10269
int num_synthetic_samples
Number of synthetic sample to generated with a random angle in the range sb_t_par_perturbation::angle...
Definition: sb.h:10284
sb_t_par_perturbation perturbation[SB_PAR_PERTURBATIONS_NUM]
Array of the perturbations.
Definition: sb.h:10298
int size
Number of used elements of the array perturbation.
Definition: sb.h:10303
Project parameters.
Definition: sb.h:10647
sb_t_par_models models
Models parameters.
Definition: sb.h:10654
sb_t_svl_par svl
SVL parameters.
Definition: sb.h:10763
int x
abscissa of the point
Definition: sb.h:5766
int y
ordinate of the point
Definition: sb.h:5767
char uuid[SB_PROJECT_UUID_LEN]
Project UUID.
Definition: sb.h:8901
sb_t_project_info * info
Array of solution project information.
Definition: sb.h:8911
Results of detection.
Definition: sb.h:11276
sb_t_samples samples
Samples found in the image.
Definition: sb.h:11296
Defines a roi.
Definition: sb.h:7730
Sample of an image.
Definition: sb.h:8118
sb_t_point centre
Coordinates of the centre of the sample.
Definition: sb.h:8126
char model_name[SB_PAR_STRING_LEN]
Name of the model.
Definition: sb.h:8191
char uuid[36]
uuid of the sample.
Definition: sb.h:8119
float scale
Scale factor.
Definition: sb.h:8204
sb_t_sample_classify_mode classify_mode
Classification mode.
Definition: sb.h:8273
sb_t_obj_type type
Sample type.
Definition: sb.h:8209
float weight
Weight of classification.
Definition: sb.h:8223
int size
Number of samples that is number of elements of the array sb_t_samples::sample .
Definition: sb.h:8379
sb_t_sample * sample
Pointer to the array of sample. The number of element is sb_t_samples::size .
Definition: sb.h:8378
Solution info structure.
Definition: sb.h:8931
sb_t_projects_info projects
Array of the projects info.
Definition: sb.h:8932
int current_project
Index of the current project in the projects array.
Definition: sb.h:8933
sb_fp_svl_progress fp_progress
The SVL calls this callback to notify the user the results of SVL.
Definition: sb.h:10128
sb_t_svl_sl_par sl
Shallow Learning SVL parameters.
Definition: sb.h:10198
sb_fp_svl_pre_elaboration fp_pre_elaboration
Callback to execute an image pre elaboration.
Definition: sb.h:10122
sb_fp_svl_command fp_command
Callback called by SVL to allow the user to decide how to continue when particular situations happen,...
Definition: sb.h:10134
char project_path[512]
Path of the project, where the SVL will find the images.
Definition: sb.h:10145
void * user_data
Pointer to data which is passed to the callbacks.
Definition: sb.h:10140
char image_ext[64]
Extensions of the images.
Definition: sb.h:10153
Contains the results of the image pre-processing.
Definition: sb.h:9380
sb_t_image * image
Pre processed image, that is the warped image.
Definition: sb.h:9388
SB_HANDLE warped2ori
lut of the samples coordinates from warped image to the original one.
Definition: sb.h:9391
sb_t_roi * roi
Image elaboration ROI.
Definition: sb.h:9389
float goodness
Goodness of the training.
Definition: sb.h:12701
int fn
Number of FALSE NEGATIVE samples.
Definition: sb.h:12736
int tp
Number of TRUE POSITIVE samples.
Definition: sb.h:12721
int fp
Number of FALSE POSITIVE samples.
Definition: sb.h:12726
Defines the results of SVL.
Definition: sb.h:12877
sb_t_svl_res_level global
Cumulative results of all levels of all models.
Definition: sb.h:12920
long long time_ms
Execution time, in ms, of SVL.
Definition: sb.h:12973
sb_t_svl_stop_reason stop_reason
It describes the reason why the SVL is finished.
Definition: sb.h:12879
float goodness_target
Goodness target of the training.
Definition: sb.h:10046