【问题标题】:How to train faster-rcnn on dataset including negative data in pytorch如何在数据集上训练更快的rcnn,包括pytorch中的负数据
【发布时间】:2021-05-09 18:45:42
【问题描述】:

我正在尝试训练 torchvision Faster R-CNN 模型以对我的自定义数据进行对象检测。我在torchvision对象检测微调tutorial中使用了代码。但收到此错误:

Expected target boxes to be a tensor of shape [N, 4], got torch.Size([0])

这与我的自定义数据集中的负数据(空训练图像/无边界框)有关。我们如何更改下面的Dataset class 以在包含负数据的数据集上训练更快的rcnn?

class MyCustomDataset(Dataset):

    def __init__(self, root, transforms):
        self.root = root
        self.transforms = transforms
        # load all image files, sorting them to
        # ensure that they are aligned
        self.imgs = list(sorted(os.listdir(os.path.join(root, "PNGImages"))))
        self.masks = list(sorted(os.listdir(os.path.join(root, "PedMasks"))))

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

    def __getitem__(self, idx):
        # load images ad masks
        img_path = os.path.join(self.root, "PNGImages", self.imgs[idx])
        mask_path = os.path.join(self.root, "PedMasks", self.masks[idx])
        img = Image.open(img_path).convert("RGB")
        # note that we haven't converted the mask to RGB,
        # because each color corresponds to a different instance
        # with 0 being background
        mask = Image.open(mask_path)
        # convert the PIL Image into a numpy array
        mask = np.array(mask)
        # instances are encoded as different colors
        obj_ids = np.unique(mask)
        # first id is the background, so remove it
        obj_ids = obj_ids[1:]

        # split the color-encoded mask into a set of binary masks
        masks = mask == obj_ids[:, None, None]

        # get bounding box coordinates for each mask
        num_objs = len(obj_ids)
        
        boxes = []
        for i in range(num_objs):
            pos = np.where(masks[i])
            xmin = np.min(pos[1])
            xmax = np.max(pos[1])
            ymin = np.min(pos[0])
            ymax = np.max(pos[0])
            boxes.append([xmin, ymin, xmax, ymax])

        # convert everything into a torch.Tensor
        boxes = torch.as_tensor(boxes, dtype=torch.float32)      
        # there is only one class  
        labels = torch.ones((num_objs,), dtype=torch.int64)
        image_id = torch.tensor([idx])
        area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
        # suppose all instances are not crowd
        iscrowd = torch.zeros((num_objs,), dtype=torch.int64)

        target = {}
        target["boxes"] = boxes
        target["labels"] = labels
        target["image_id"] =  torch.tensor([idx])
        target["area"] = area
        target["iscrowd"] = iscrowd

        return img, target 

【问题讨论】:

    标签: deep-learning computer-vision pytorch object-detection bounding-box


    【解决方案1】:

    我们需要对数据集类进行两项更改

    1- 空盒子的输入为:

    if num_objs == 0:
        boxes = torch.zeros((0, 4), dtype=torch.float32)
    else:
        boxes = torch.as_tensor(boxes, dtype=torch.float32)
    

    2- 将area=0分配给空边界框的情况,更改用于计算面积的代码,使其成为torch tensor

    area = 0
    for i in range(num_objs):
        pos = np.where(masks[i])
        xmin = np.min(pos[1])
        xmax = np.max(pos[1])
        ymin = np.min(pos[0])
        ymax = np.max(pos[0])
        area += (xmax-xmin)*(ymax-ymin)
    area = torch.as_tensor(area, dtype=torch.float32)
    

    我们将把第 2 步合并到现有的 for 循环中。

    因此,修改后的数据集类将如下所示:

    class MyCustomDataset(Dataset):
    
        def __init__(self, root, transforms):
            self.root = root
            self.transforms = transforms
            # load all image files, sorting them to
            # ensure that they are aligned
            self.imgs = list(sorted(os.listdir(os.path.join(root, "PNGImages"))))
            self.masks = list(sorted(os.listdir(os.path.join(root, "PedMasks"))))
    
        def __len__(self):
            return len(self.imgs)
    
        def __getitem__(self, idx):
            # load images ad masks
            img_path = os.path.join(self.root, "PNGImages", self.imgs[idx])
            mask_path = os.path.join(self.root, "PedMasks", self.masks[idx])
            img = Image.open(img_path).convert("RGB")
            # note that we haven't converted the mask to RGB,
            # because each color corresponds to a different instance
            # with 0 being background
            mask = Image.open(mask_path)
            # convert the PIL Image into a numpy array
            mask = np.array(mask)
            # instances are encoded as different colors
            obj_ids = np.unique(mask)
            # first id is the background, so remove it
            obj_ids = obj_ids[1:]
    
            # split the color-encoded mask into a set of binary masks
            masks = mask == obj_ids[:, None, None]
    
            # get bounding box coordinates for each mask
            num_objs = len(obj_ids)
            
            boxes = []
            area = 0 
            for i in range(num_objs):
                pos = np.where(masks[i])
                xmin = np.min(pos[1])
                xmax = np.max(pos[1])
                ymin = np.min(pos[0])
                ymax = np.max(pos[0])
                boxes.append([xmin, ymin, xmax, ymax])
                area += (xmax-xmin)*(ymax-ymin)
            area = torch.as_tensor(area, dtype=torch.float32)
    
            # Handle empty bounding boxes
            if num_objs == 0:
                boxes = torch.zeros((0, 4), dtype=torch.float32)
            else:
                boxes = torch.as_tensor(boxes, dtype=torch.float32)   
    
            # there is only one class  
            labels = torch.ones((num_objs,), dtype=torch.int64)
            image_id = torch.tensor([idx])
    
            #area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
    
            # suppose all instances are not crowd
            iscrowd = torch.zeros((num_objs,), dtype=torch.int64)
    
            target = {}
            target["boxes"] = boxes
            target["labels"] = labels
            target["image_id"] =  torch.tensor([idx])
            target["area"] = area
            target["iscrowd"] = iscrowd
    
            return img, target 
    

    【讨论】:

    • 为什么要在'area += (xmax-xmin)*(ymax-ymin)'中添加多个物体的面积?
    • 另外,如果我们添加负样本,我们是否需要使类数等于三个?前景、背景和负类?
    • 添加了区域,因为在输出target 中需要所有对象占用的总区域,并发送到目标为:target["area"] = area
    • 不,不要为负数据单独分类。和这里的背景一样。
    • 所以你的意思是如果图像有两个对象,让我们说区域 120 和 220,然后加在一起变成 340。我不理解分配 340 背后的基本原理?不应该是220和120帮助检测网络吗?
    猜你喜欢
    • 2017-07-27
    • 2018-05-06
    • 1970-01-01
    • 2020-02-05
    • 2020-04-19
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多