## 3 Materials and Methods
### Proposed Method
#### 3.3.3 Dataset Balancing
```
Data: FILTERED DATASET Result: BALANCED DATASET
1 balanced_dataset \(\leftarrow\) O;
2 median \(\leftarrow\) median(images_per_class);
3 bounds \(\leftarrow\) median \(\cdot\)0.75, median \(\cdot\)1.5;
4 forclass \(\in\)filtered_dataset.classesdo
5 balanced_dataset.create_folder(class);
6 n_images \(\leftarrow\) len(class);
7 ifn_images \(<\)bounds[0]then
8 difference \(\leftarrow\) median \(-\) n_images;
9 percentage \(\leftarrow\) random \(\cdot\)uniform([0/7,13]);
10 images_to_generate \(\leftarrow\) difference \(\cdot\) percentage;
11 last_picked \(\leftarrow\) [];
12 for\(\in\) [ (images_to_generate)do
13 original_image \(\leftarrow\) random(class.images);
14 whileoriginal_image \(\in\) last_pickeddo
15 original_image \(\leftarrow\) random(class.images);
16 end if
17 last_picked.add(original_image);
18 method \(\leftarrow\) random[rot, flip, s_p, gaus];
19 whileused(method(original_image))do
20 method \(\leftarrow\) random[rot, flip, s_p, gaus];
21 end if
22 new_image \(\leftarrow\) method(original_image);
23 balanced_dataset.save(class, new_image);
24 end if
25//Copy all original images along with augmented images to balanced dataset
26 end if
27
28 else ifs_images \(>\)bounds[0]then
29 n_subclasses \(\leftarrow\) len(class) \(\div\) median;
30 shuffled_class_images \(\leftarrow\) random.sort(class.images);
31 for\(i\in[0,\)n_subclasses]do
32 balanced_dataset.create_folder(class + 1);
33 subclass_images \(\leftarrow\) shuffled_class_images[i \(\cdot\) median \(\cdot\)i \(\cdot\) median \(\cdot\) median];
34 balanced_dataset.save_images(class + 1, subclass_images);
35 end if
36
37 end if
38
39 else // Copy class folder and all images to balanced dataset
30 end if
31 end for
32
33 end for
```  
**Algorithm 1**Dataset balancing  
Figure 5: Flowchart describing the steps of the data balancing process. After defining the median as the ideal number of images per class, each category is processed sequentially. If the number of images is below the given threshold, the category is oversampled by data augmentation (first branch). If the cardinality of the current class is instead over the fixed upper bound, its samples are equally distributed among several subcategories that are created in the new dataset (second branch). Finally, if the cardinality of the current class is within the acceptance window, it is copied as is in the new dataset. Upsampling Processing Technique  
Let class \(\alpha\) be a generic class of the _filtered dataset_ for which upsampling is necessary. The designed procedure defines in advance the number of images that will compose class \(\alpha\) at the end of the procedure, by picking up a random number in the range between \(\pm 30\%\) of the median value. By doing so, it is ensured that the cardinality of all the upsampled classes of the _filtered dataset_ will be around the median. An image \(I_{\alpha}\) of class \(\alpha\) is randomly selected to increase the data. The particular augmentation technique to apply to \(I_{\alpha}\) is then randomly selected among the following options:  
* Rotation with a random angle between 90, 180, and 270. The values of the angles are fixed to avoid the creation of blank spaces in the corners of the image. * Flipping along the randomly selected axis. * Gaussian noise addition, with a mean of 0 and std of 10. * Salt and pepper noise addition, with a random number of pixels between 20% and 30% of the total number of pixels, equally distributed between black and white noise. The choice of using only 4 augmentation techniques works for the purpose of this paper, which is to investigate whether the classification problem can benefit from an appropriate balancing technique and not studying how augmentation techniques affect the classification problem. To prevent the framework from producing identical images during augmentation, the implemented procedure always checks that \(I_{\alpha}\) has not been used to produce one of the last 20 augmented images and that the chosen augmentation tool has not been previously used on \(I_{\alpha}\).