nanodet icon indicating copy to clipboard operation
nanodet copied to clipboard

加入mosaic后效果变差了,是什么原因

Open Rokuki opened this issue 3 years ago • 4 comments

coco.py

if self.load_mosaic and not isval:
            img4, labels4, bbox4 = load_mosaic(self, idx)
            meta['img_info']['height'] = img4.shape[0]
            meta['img_info']['width'] = img4.shape[1]
            meta['img'] = img4
            meta['gt_labels'] = labels4
            meta['gt_bboxes'] = bbox4


        meta = self.pipeline(self, meta, input_size)

        meta["img"] = torch.from_numpy(meta["img"].transpose(2, 0, 1))
        return meta

在ShapeTransform里测试打印出来的bbox是正常的

meta_data["img"] = img
        meta_data["warp_matrix"] = M
        if "gt_bboxes" in meta_data:
            boxes = meta_data["gt_bboxes"]
            meta_data["gt_bboxes"] = warp_boxes(boxes, M, dst_shape[0], dst_shape[1])
        if "gt_masks" in meta_data:
            for i, mask in enumerate(meta_data["gt_masks"]):
                meta_data["gt_masks"][i] = cv2.warpPerspective(
                    mask, M, dsize=tuple(dst_shape)
                )
        for i in range(meta_data["gt_bboxes"].shape[0]):
            cv2.rectangle(img, (int(meta_data["gt_bboxes"][i][0]), int(meta_data["gt_bboxes"][i][1])), (int(meta_data["gt_bboxes"][i][2]), int(meta_data["gt_bboxes"][i][3])), (255,0,0), 2)
        cv2.imwrite('./%d.jpg' % int(meta_data["gt_bboxes"][0][0]), img)

有什么可能的原因导致的?

Rokuki avatar Dec 09 '21 03:12 Rokuki

小模型不适合这么重的数据增强

RangiLyu avatar Dec 09 '21 04:12 RangiLyu

小模型不适合这么重的数据增强

感谢

Rokuki avatar Dec 09 '21 06:12 Rokuki

if self.load_mosaic and not isval:
            img4, labels4, bbox4 = load_mosaic(self, idx)
            meta['img_info']['height'] = img4.shape[0]
            meta['img_info']['width'] = img4.shape[1]
            meta['img'] = img4
            meta['gt_labels'] = labels4
            meta['gt_bboxes'] = bbox4

Hello,Can you teach me how to quote the module of mosaic in the coco.py,Thx

LaoGanGitHub avatar Feb 24 '22 02:02 LaoGanGitHub

if self.load_mosaic and not isval:
            img4, labels4, bbox4 = load_mosaic(self, idx)
            meta['img_info']['height'] = img4.shape[0]
            meta['img_info']['width'] = img4.shape[1]
            meta['img'] = img4
            meta['gt_labels'] = labels4
            meta['gt_bboxes'] = bbox4

Hello,Can you teach me how to quote the module of mosaic in the coco.py,Thx

Please refer to load_ mosaic code. Rewritten according to YOLO

def load_mosaic(self, index):
    # YOLOv5 4-mosaic loader. Loads 1 image + 3 random images into a 4-image mosaic
    labels4, segments4 = [], []
    s = self.input_size[0]
    yc, xc = (int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border)  # mosaic center x, y
    img_idxs = [x for x in range(len(self.data_info))]
    k = random.randint(1,3)
    indices = [index] + random.choices(img_idxs, k=k)  # 3 additional image indices
    for i, index in enumerate(indices):
        # Load image
        img, (h0, w0), (h, w) = load_image(self, index)
        y_ratio = h / h0
        x_ratio = w / w0
        ann = self.get_img_annotation(index)

        # place img in img4
        if i == 0:  # top left
            img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8)  # base image with 4 tiles
            x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc  # xmin, ymin, xmax, ymax (large image)
            x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h  # xmin, ymin, xmax, ymax (small image)
        elif i == 1:  # top right
            x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
            x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
        elif i == 2:  # bottom left
            x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
            x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
        elif i == 3:  # bottom right
            x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
            x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)

        img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b]  # img4[ymin:ymax, xmin:xmax]
        
        # Labels
        labels, segments = ann["labels"].copy(), ann["bboxes"].copy()
        if labels.size:
            segments = torch.tensor([[segments[0][0] * x_ratio, segments[0][1] * y_ratio, (segments[0][2]) * x_ratio, (segments[0][3]) * y_ratio]])
            segments = bbox2xyxy(segments, [x1a, y1a, x2a, y2a], [x1b, y1b, x2b, y2b])

        labels4.append(labels)
        segments4.extend(segments.numpy())

    # Concat/clip labels
    labels4 = np.concatenate(labels4, 0)
    segments4 = np.array(segments4)  

    return img4, labels4, segments4

Rokuki avatar Feb 24 '22 14:02 Rokuki

@Rokuki Can you tell how to write the function bbox2xyxy()? Thx.

Anleeno-Xu avatar Dec 16 '22 13:12 Anleeno-Xu

@Rokuki Can you tell how to write the function bbox2xyxy()? Thx.

@Anleeno-Xu 不好意思,才看到

def bbox2xyxy(x, large_img, small_img):
    y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
    y[:, 0] = max(max(y[:, 0] - small_img[0], 0) + large_img[0], 0)
    y[:, 1] = max(max(y[:, 1] - small_img[1], 0) + large_img[1], 0)
    y[:, 2] = min(min(y[:, 2], small_img[2]) - small_img[0], small_img[2] + small_img[0]) + large_img[0]
    y[:, 3] = min(min(y[:, 3], small_img[3]) - small_img[1], small_img[3] + small_img[1]) + large_img[1]

    return y

Rokuki avatar Dec 19 '22 07:12 Rokuki