albumentations-team / albumentations

Fast and flexible image augmentation library. Paper about the library: https://www.mdpi.com/2078-2489/11/2/125
https://albumentations.ai
MIT License
13.75k stars 1.61k forks source link

yolo format unsupported #1453

Open sipie800 opened 1 year ago

sipie800 commented 1 year ago

import numpy as np import albumentations as A

album=A.Compose([ A.Resize(height=64, width=64) , A.RandomScale(p=1.0, scale_limit=(-0.5, 0.5)) ],bbox_params=A.BboxParams(format="yolo", label_fields=["bboxes"]))

img=np.random.randint(112, 128, [1280, 1280, 3], dtype=np.uint8) boxes = [[0.1, 0.1, 0.01, 0.01],[0.1, 0.1, 0.01, 0.01]] cls=[0,1]

r = album(image=img, bboxes=boxes, class_labels=cls, cropping_bbox=boxes[0] ) print(r["bboxes"])

[[0.1, 0.1, 0.01], [0.1, 0.1, 0.01]]

why are the boxes cut to only 3 values? I do nothing wrong and it runs.

itstechaj commented 1 year ago

when you are passing "bboxes" in label_feilds of A.Bboxparams() then it understands that you have kept your labels just after the bounding box details e.g: your boxes looks like boxes=[[0.1, 0.1, 0.01, 0.01,<your class label>],[0.1, 0.1, 0.01, 0.01,<your class label>]] but you are not providing the class labels in last of each box hence it trims hence it assumes the last element as class label that is why you are only getting three values in each box in output bboxes.

If you want to provide the labels in seperate list you have to give "class_labels" in the label_feilds. means label_feilds=["class_labels"] <------------------------------------1st Method--------------------------------------------->

You just have to change the label_feilds value to "class_labels"

label_fields=["class_labels"] -> it means that you are providing the labels externally

import numpy as np import albumentations as A

album=A.Compose([ A.Resize(height=64, width=64) , A.RandomScale(p=1.0, scale_limit=(-0.5, 0.5)) ],bbox_params=A.BboxParams(format="yolo", label_fields=["class_labels"]))

img=np.random.randint(112, 128, [1280, 1280, 3], dtype=np.uint8)

you can also do like

boxes = [[0.1, 0.1, 0.01, 0.01],[0.3, 0.5, 0.01, 0.01]] cls=[0,1]

now you do not need to pass class_labels here as you have already included it into bboxes

r = album(image=img, bboxes=boxes,class_labels=cls, cropping_bbox=boxes[0] ) print("bboxes are : ",r["bboxes"]) print("labels are : ",r["class_labels"])

Output: - bboxes are : [(0.1, 0.1, 0.009999999999999995, 0.009999999999999995), (0.3, 0.5, 0.010000000000000009, 0.010000000000000009)] labels are : [0, 1] <------------------------------------------2nd Method-------------------------------------->

Here is your code if you are passing label_fields=["bboxes"]

label_fields=["bboxes"] -. it means that you are providing labels along with the box not separately.

so if you want to do like this then the below code will do it

You have to pass labels along with each box

import numpy as np import albumentations as A

album=A.Compose([ A.Resize(height=64, width=64) , A.RandomScale(p=1.0, scale_limit=(-0.5, 0.5)) ],bbox_params=A.BboxParams(format="yolo", label_fields=["bboxes"]))

img=np.random.randint(112, 128, [1280, 1280, 3], dtype=np.uint8)

you can also do like

boxes = [[0.1, 0.1, 0.01, 0.01],[0.3, 0.5, 0.01, 0.01]] cls=[0,1]

you have to add class of each box at last of box itself (as you are passing "bboxes" in label_feilds in A.BboxParams)

boxes=[box+[label] for box,label in zip(boxes,cls)]

now you do not need to pass class_labels here as you ave already included it into bboxes

r = album(image=img, bboxes=boxes,class_labels=cls, cropping_bbox=boxes[0] ) print("bboxes are : ",r["bboxes"]) print("labels are : ",r["class_labels"])

Output :- bboxes are : [[0.1, 0.1, 0.01, 0.01], [0.3, 0.5, 0.01, 0.01]] labels are : [0, 1] <------------------------------------------------------------------------------------------>

Please visit this link for more details: https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/