leondgarse / keras_cv_attention_models

Keras beit,caformer,CMT,CoAtNet,convnext,davit,dino,efficientdet,edgenext,efficientformer,efficientnet,eva,fasternet,fastervit,fastvit,flexivit,gcvit,ghostnet,gpvit,hornet,hiera,iformer,inceptionnext,lcnet,levit,maxvit,mobilevit,moganet,nat,nfnets,pvt,swin,tinynet,tinyvit,uniformer,volo,vanillanet,yolor,yolov7,yolov8,yolox,gpt2,llama2, alias kecam
MIT License
595 stars 95 forks source link

assigning class weight to each class during training and access to the each class map and recall #125

Closed shshrzad closed 11 months ago

shshrzad commented 1 year ago

As it is obvious, access to the each class map and recall in an object detection task is so helpful. But, I cannot find a function that returns each class accuracy during testing in this repository. Can anybody help me in this regard?

Also, I want to assign class weight to each class during training to improve my object detection model by considering the importance of each class. I set “class_weights” in “model.fit” but I wasn’t successful. I will appreciate any guidance in this regard, too

leondgarse commented 1 year ago

Temp added a class_weight parameter for AnchorFreeLoss and YOLOV8Loss. Currently it's a list value with length equal to num_classes. You can call them be given class_weight like:

class_weight = [ii / 80 for ii in range(80)]  # num_classes=80
aa = losses.AnchorFreeLoss(input_shape=(640, 640), class_weight=class_weight)

As class_weights in model.fit is mainly for classification task, cannot be applied in this situation.

Falahat1 commented 1 year ago

Thank you for adding the class_weight parameter. I have a dataset with 5 classes: 4 classes represent objects, and 1 class represents the background. The samples of background class is much more than the others. The class labels are defined as 0, 1, 2, 3, and 10, respectively. The class label 10 represents the background class, which is not important to me. My main focus is on defining the objects of class 0, 1, 2, 3. Should I set class_weight=[1, 1, 1, 1, 0]? Additionally, what should I do if I want to have sample_weight, too? I believe it may be more suitable for my case as described in "https://stackoverflow.com/questions/32492550/what-is-the-difference-between-sample-weight-and-class-weight-options-in-scikit". Thank you for your guidance

leondgarse commented 1 year ago
Falahat1 commented 1 year ago

Thank you for your guidance. Would you please explain how can I set sample_weight parameter if I want to train my model using coco_train_script.py function? Should I add sample_weight to AnchorFreeLoss function in losses.py or set it in model.fit?

leondgarse commented 1 year ago

I haven't tried it myself, but by some simple search like How to set sample_weight in Keras?, I think it should be added to model.fit with length equal to total_images printed at coco_train_script.py#L180. You can now add in coco_train_script.py like

sample_weight = np.ones([total_images])
sample_weight[[2, 3, 5]] = 2  # Set some position to other value
other_fit_kwargs = {"sample_weight": sample_weight}

Besides, class_weight is now applied also to bbox_loss / l1_loss / dfl_loss, which I think is more reasonable.