yolov8 数据集中增加额外信息修改过程记录(施工ing)

本文详细描述了如何在Ultralytics库的几个关键模块(如base.py,augment.py,dataset.py,trainer.py,detect/train.py和nn/tasks.py)中添加和处理meta元数据,包括图像加载、数据增强、batch处理和模型预测。作者对这些函数进行了相应的修改以适应自己的数据需求,并确保了meta信息在模型训练流程中的兼容性。
摘要由CSDN通过智能技术生成

1.在ultralytics/data/base.py 中 

298行def get_image_and_label 函数中,模仿增加meta项

# label['img'], label['ori_shape'], label['resized_shape']= self.load_image(index)
label['img'], label['ori_shape'], label['resized_shape'],label['meta'] = self.load_image(index)#sxx

与此同时,相应地,修改load_image

2.在ultralytics/data/augment.py文件中 找到 _cat_labels 函数,并修改
final_labels = {
    'im_file': mosaic_labels[0]['im_file'],
    'ori_shape': mosaic_labels[0]['ori_shape'],
    'resized_shape': (imgsz, imgsz),
    'cls': np.concatenate(cls, 0),
    'instances': Instances.concatenate(instances, axis=0),
    'mosaic_border': self.border,
    'meta':mosaic_labels[0]['meta']
}  # final_labels

找到Format类的__call__l函数,因为我的数据和img比较相似,所以模仿img处理meta,如果在运行程序时下面4修改的地方没报错就不用管

meta = labels.pop('meta')
labels['meta'] = self._format_img(meta)#把ndaray改成tensor

在ultralytics/data/dataset.py文件中

collate_fn函数中,要模仿img 将batch个tensor的list转成tensor
if k == 'meta':#sxx
    value = torch.stack(value, 0)

3.最终,在ultralytics/engine/trainer.py 的 _do_train()函数中进行debug,

在该语句之后for i, batch in pbar:

batch 中就有meta信息了

4.ultralytics/models/yolo/detect/train.py文件的

preprocess_batch函数中 模仿对img的操作将meta到GPU
batch['meta'] = torch.Tensor(batch['meta'])#sxx 转成tensor
batch['meta'] = batch['meta'].to(self.device, non_blocking=True)#sxx

5.模型解析过程中修改

ultralytics/nn/tasks.py文件中BaseModal类的

forward,predict,predict_once函数都添加一个参数x0=None

在DetectionModal类中覆盖父类的

def loss(self, batch, preds=None):
    """
    Compute loss

    Args:
        batch (dict): Batch to compute loss on
        preds (torch.Tensor | List[torch.Tensor]): Predictions.
    """
    if not hasattr(self, 'criterion'):
        self.criterion = self.init_criterion()

    preds = self.forward(batch['img'],batch['meta']) if preds is None else preds
    return self.criterion(preds, batch)
def _predict_once(self, x, x0=None,profile=False, visualize=False):也要覆盖
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值