# How to solve imbalanced dataset oversampling problem in multi labels-classes instance segmentation task?

I want to use models YOLOv7-seg for instance segmentation of tree species in images. There are 26 species of trees, and each image may contain multiple species. There is a distinction between dominant and non-dominant species, with dominant species having more samples than non-dominant ones, leading to imbalanced data and potential overfitting.

To address this issue, I have performed augmentation (using albumentation package) to oversample images containing non-dominant species, aiming to balance the dataset and avoid overfitting.

However, if an image containing non-dominant species also includes other dominant species, the instances of dominant species will also be oversampled, I have calculate a simple linear equation like Ax = b to solve how many times I need to perform oversampling for each image, but the when I use scipy.optimize.linprog function in scipy, and set the parameter bounds=(0, None), the return answer will be message: The problem is infeasible. (HiGHS Status 8: model_status is Infeasible; primal_status is At lower/fixed bound), making it impossible to find how many time ahould I do augumentatation to balance the dataset permanently.

In the Ax = b linear equation:

A is an (26 rows x 101 columns) array like following table, the row attribute "sp1, sp2, ..." means the species classes, and the column attribute "img1, img2, ..." means each image in dataset, the numbers means how many instances of that species are there in the image.

     img0   img1  ...  img99  img100
sp1   5      5    ...   4        4
sp2   1      0    ...   0        0
sp3   24     2    ...   1        1
:
sp26  0      0    ...   1        0


x is an (101 rows) array means how many times I need to perform oversampling for each image:

[x0
x2
:
x100]


b is an (26 columns) array means the maximum instances numbers I want for each species final, because I need balanced dataset, so they will be the same numbers like this:

[500
500
:
500]


If I train directly using the original imbalanced dataset, the result will obviously over-fitting, the dominant species accuracy show almost 1 in confusion matrix and the non-dominannt species show 0.2 or less, are there any other solutions to this problem?

And I also tried adjust focal_loss in hyp.scratch-low.yaml to 1.5 or 2 and apply it, but that is not enough for this task and the result become terrible. Incidentally, I want to avoid splitting the image to small part more, because the target object in my image will mostly be cut by edge.