Closed shshrzad closed 11 months 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.
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
num_classes=11
, I think it should be class_weight=[1, 1, 1, 1] + [0] * 7
.For sample_weight
, I didn't try it for training, just according to keras/losses.py#L102, it should be still able to apply on loss level, via model.fit
.
from keras_cv_attention_models.coco import losses, anchors_func
aa = losses.AnchorFreeLoss(input_shape=(640, 640), use_l1_loss=True)
from keras_cv_attention_models import yolox, test_images
from keras_cv_attention_models.coco import anchors_func, data
mm = yolox.YOLOXS()
img = test_images.dog_cat()
pred = mm(mm.preprocess_input(img))
bbs, lls, ccs = mm.decode_predictions(pred)[0]
bbox_labels_true = tf.concat([bbs, tf.one_hot(lls, 80), tf.ones([bbs.shape[0], 1])], axis=-1)
# Fake y_true y_pred with batch_size=3
y_true = tf.concat([tf.expand_dims(bbox_labels_true, 0)] * 3, axis=0)
y_pred = tf.concat([pred] * 3, axis=0)
print("\n", aa(y_true , y_pred , sample_weight=1))
# - l1_loss: 0.31445995 - obj_loss: 0.887720227 - cls_loss: 0.390695155 - bbox_loss: 0.128081948
# tf.Tensor(2.233285, shape=(), dtype=float32)
print("\n", aa(y_true , y_pred , sample_weight=[1, 2, 3]))
# - l1_loss: 0.31445995 - obj_loss: 0.887720227 - cls_loss: 0.390695155 - bbox_loss: 0.128081948
# tf.Tensor(4.46657, shape=(), dtype=float32)
class_weight
be also applied to bbox_loss
and others or not, currently it's only for class_loss
.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?
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.
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