常州網(wǎng)站建設(shè)公司如何國內(nèi)最好的seo培訓(xùn)
錯誤 ValueError: bbox_params must be specified for bbox transformations
是因為使用了需要處理邊界框(bboxes)的增強操作,但在 albumentations.Compose
中沒有正確設(shè)置bbox_params
參數(shù)。
bbox_params
是用來指定如何處理邊界框的配置。
解決方案
修改數(shù)據(jù)增強部分代碼,添加 bbox_params
參數(shù)。
from albumentations import BboxParamstransform = A.Compose([A.HorizontalFlip(p=0.5),A.VerticalFlip(p=0.5),A.RandomRotate90(p=0.5),A.ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=20, p=0.7),A.RandomBrightnessContrast(p=0.2),A.GaussNoise(var_limit=(10.0, 50.0), p=0.3),],bbox_params=BboxParams(format='pascal_voc', # 邊界框的格式,Pascal VOC 為 [xmin, ymin, xmax, ymax]label_fields=['class_labels'], # 必須指定與邊界框?qū)?yīng)的標簽min_visibility=0.2, # 邊界框最小可見性,小于此值將被移除)
)
同步修改其他部分代碼
在調(diào)用 transform
的地方,需要為 bboxes
提供對應(yīng)的 class_labels
,例如:
augmented = transform(image=image,bboxes=[[xmin, ymin, xmax, ymax]], # 提供原始邊界框坐標class_labels=["object"] # 對應(yīng)的標簽,與 bbox_params 中的配置匹配
)
完整的增強示例代碼
以下是如何應(yīng)用增強的完整示例:
from albumentations import Compose, HorizontalFlip, VerticalFlip, RandomRotate90, ShiftScaleRotate, RandomBrightnessContrast, GaussNoise, BboxParams
import cv2# 數(shù)據(jù)增強配置
transform = Compose([HorizontalFlip(p=0.5),VerticalFlip(p=0.5),RandomRotate90(p=0.5),ShiftScaleRotate(shift_limit=0.1, scale_limit=0.2, rotate_limit=20, p=0.7),RandomBrightnessContrast(p=0.2),GaussNoise(var_limit=(10.0, 50.0), p=0.3),],bbox_params=BboxParams(format='pascal_voc',label_fields=['class_labels'],min_visibility=0.2,)
)# 示例圖片和邊界框
image = cv2.imread('example.jpg')
bboxes = [[50, 50, 200, 200]] # 示例邊界框,格式為 [xmin, ymin, xmax, ymax]
class_labels = ["object"] # 示例標簽# 執(zhí)行數(shù)據(jù)增強
augmented = transform(image=image, bboxes=bboxes, class_labels=class_labels)
aug_image = augmented['image']
aug_bboxes = augmented['bboxes']
aug_labels = augmented['class_labels']# 保存增強結(jié)果
cv2.imwrite('augmented_image.jpg', aug_image)
print("增強后的邊界框:", aug_bboxes)
關(guān)鍵點總結(jié)
-
添加
bbox_params
:- 指定邊界框格式:
'pascal_voc'
對應(yīng) [xmin, ymin, xmax, ymax]。 - 設(shè)置
label_fields
,對應(yīng)每個邊界框的標簽。
- 指定邊界框格式:
-
調(diào)整調(diào)用方式:
- 每次調(diào)用增強時,提供
bboxes
和class_labels
。
- 每次調(diào)用增強時,提供
-
邊界框過濾:
- 使用
min_visibility
確保增強后邊界框的可見性,避免完全失效的邊界框。
- 使用