• 大小: 881KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: Python
  • 标签:

资源简介

YOLOv3的PyTorch完整实现

资源截图

代码片段和文件信息

import os
import numpy as np
import logging
import cv2

import torch
from torch.utils.data import Dataset

from . import data_transforms


class COCODataset(Dataset):
    def __init__(self list_path img_size is_training is_debug=False):
        self.img_files = []
        self.label_files = []
        for path in open(list_path ‘r‘):
            label_path = path.replace(‘images‘ ‘labels‘).replace(‘.png‘ ‘.txt‘).replace(
                ‘.jpg‘ ‘.txt‘).strip()
            if os.path.isfile(label_path):
                self.img_files.append(path)
                self.label_files.append(label_path)
            else:
                logging.info(“no label found. skip it: {}“.format(path))
        logging.info(“Total images: {}“.format(len(self.img_files)))
        self.img_size = img_size  # (w h)
        self.max_objects = 50
        self.is_debug = is_debug

        #  transforms and augmentation
        self.transforms = data_transforms.Compose()
        if is_training:
            self.transforms.add(data_transforms.ImagebaseAug())
        # self.transforms.add(data_transforms.KeepAspect())
        self.transforms.add(data_transforms.ResizeImage(self.img_size))
        self.transforms.add(data_transforms.ToTensor(self.max_objects self.is_debug))

    def __getitem__(self index):
        img_path = self.img_files[index % len(self.img_files)].rstrip()
        img = cv2.imread(img_path cv2.IMREAD_COLOR)
        if img is None:
            raise Exception(“Read image error: {}“.format(img_path))
        ori_h ori_w = img.shape[:2]
        img = cv2.cvtColor(img cv2.COLOR_BGR2RGB)

        label_path = self.label_files[index % len(self.img_files)].rstrip()
        if os.path.exists(label_path):
            labels = np.loadtxt(label_path).reshape(-1 5)
        else:
            logging.info(“label does not exist: {}“.format(label_path))
            labels = np.zeros((1 5) np.float32)

        sample = {‘image‘: img ‘label‘: labels}
        if self.transforms is not None:
            sample = self.transforms(sample)
        sample[“image_path“] = img_path
        sample[“origin_size“] = str([ori_w ori_h])
        return sample

    def __len__(self):
        return len(self.img_files)


#  use for test dataloader
if __name__ == “__main__“:
    dataloader = torch.utils.data.DataLoader(COCODataset(“../data/coco/trainvalno5k.txt“
                                                         (416 416) True is_debug=True)
                                             batch_size=2
                                             shuffle=False num_workers=1 pin_memory=False)
    for step sample in enumerate(dataloader):
        for i (image label) in enumerate(zip(sample[‘image‘] sample[‘label‘])):
            image = image.numpy()
            h w = image.shape[:2]
            for l in label:
                if l.sum() == 0:
                    continue
                x1 = int((l[1] - l[3] / 2) * w)
                y1 = int((l[2] - l[4] 

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\
     文件          42  2018-08-15 01:34  YOLOv3_PyTorch-master\.gitignore
     文件        3676  2018-08-15 01:34  YOLOv3_PyTorch-master\README.md
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\common\
     文件           0  2018-08-15 01:34  YOLOv3_PyTorch-master\common\__init__.py
     文件        3338  2018-08-15 01:34  YOLOv3_PyTorch-master\common\coco_dataset.py
     文件        4681  2018-08-15 01:34  YOLOv3_PyTorch-master\common\data_transforms.py
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\common\demo\
     文件      110660  2018-08-15 01:34  YOLOv3_PyTorch-master\common\demo\demo0.jpg
     文件       72209  2018-08-15 01:34  YOLOv3_PyTorch-master\common\demo\demo1.jpg
     文件       29469  2018-08-15 01:34  YOLOv3_PyTorch-master\common\demo\loss_curve.png
     文件        4512  2018-08-15 01:34  YOLOv3_PyTorch-master\common\utils.py
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\data\
     文件         625  2018-08-15 01:34  YOLOv3_PyTorch-master\data\coco.names
     文件         898  2018-08-15 01:34  YOLOv3_PyTorch-master\data\get_coco_dataset.sh
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\evaluate\
     文件         781  2018-08-15 01:34  YOLOv3_PyTorch-master\evaluate\coco_index2category.json
     文件        4336  2018-08-15 01:34  YOLOv3_PyTorch-master\evaluate\eval.py
     文件        5112  2018-08-15 01:34  YOLOv3_PyTorch-master\evaluate\eval_coco.py
     文件         624  2018-08-15 01:34  YOLOv3_PyTorch-master\evaluate\params.py
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\
     文件           0  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\__init__.py
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\backbone\
     文件         115  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\backbone\__init__.py
     文件        3539  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\backbone\darknet.py
     文件        6782  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\model_main.py
     文件        7057  2018-08-15 01:34  YOLOv3_PyTorch-master\nets\yolo_loss.py
     文件         109  2018-08-15 01:34  YOLOv3_PyTorch-master\requirements.txt
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\test\
     目录           0  2018-08-15 01:34  YOLOv3_PyTorch-master\test\images\
     文件      149633  2018-08-15 01:34  YOLOv3_PyTorch-master\test\images\test1.jpg
............此处省略11个文件信息

评论

共有 条评论