重写yolo数据加载模块

原因

 

        yolo原本的Dataload可阅读性太差,可拓展性也很差。另一个为了熟悉源代码。

class DataFolder(VisionDataset):
    def __init__(
            self,
            root: str,
            img_size=640,
            batch_size=32,
            augment=True,
            hyp=None,
            transform: Optional[Callable] = None,
            target_transform: Optional[Callable] = None
    ) -> None:
        super().__init__(root, transform=transform, target_transform=target_transform)
        self.sample = {}
        self.findLabelAndImg(root, self.sample)
        # 创建索引
        self.indices = range(len(self.sample['filePath']))
        self.img_size = img_size
        self.batch_size = batch_size
        # 超参数
        self.hyp = hyp
        self.augment = augment
        # 增强
        self.mosaic = True
        self.mixup = True
        self.albumentations = Albumentations(size=img_size) if augment else None

    def findLabelAndImg(self, root, sample):
        try:
            f = []  # image files
            p = Path(root)
            if p.is_dir():  # dir
                f += glob.glob(str(p / '**' / '*.*'), recursive=True)
            im_files = sorted(x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in IMG_FORMATS)
            assert im_files, f'No images found'
        except Exception as e:
            raise Exception(f'Error loading data from {root}: {e}') from e
        sample['filePath'] = f
        sample['labelPath'] = img2label_paths(im_files)


    def _load_image(self, id: int):
        f = self.sample['filePath'][id]
        im = cv2.imread(f)
        return im, im.shape[:2]  # BGR

    def _
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值