欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Tricks

程序员文章站 2023-10-27 15:25:58
TricksTricksTricks1.mosaic...

TricksTricks


  • 1.mosaic

Tricks


def load_mosaic(self, index):
    # loads images in a mosaic

    labels4 = []
    s = self.img_size
    yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border]  # mosaic center x, y
    indices = [index] + [random.randint(0, len(self.labels) - 1) for _ in range(3)]  # 3 additional image indices
    for i, index in enumerate(indices):
        # Load image
        img, _, (h, w) = load_image(self, 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, max(xc, 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]
        padw = x1a - x1b
        padh = y1a - y1b

        # Labels
        x = self.labels[index]
        labels = x.copy()
        if x.size > 0:  # Normalized xywh to pixel xyxy format
            labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw
            labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh
            labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw
            labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh
        labels4.append(labels)

    # Concat/clip labels
    if len(labels4):
        labels4 = np.concatenate(labels4, 0)
        # np.clip(labels4[:, 1:] - s / 2, 0, s, out=labels4[:, 1:])  # use with center crop
        np.clip(labels4[:, 1:], 0, 2 * s, out=labels4[:, 1:])  # use with random_affine

    # Augment
    # img4 = img4[s // 2: int(s * 1.5), s // 2:int(s * 1.5)]  # center crop (WARNING, requires box pruning)
    img4, labels4 = random_affine(img4, labels4,
                                  degrees=self.hyp['degrees'],
                                  translate=self.hyp['translate'],
                                  scale=self.hyp['scale'],
                                  shear=self.hyp['shear'],
                                  border=self.mosaic_border)  # border to remove

    return img4, labels4

  • 2.mixup

可以模拟遮挡效果
Tricks


def load_mixup_image_and_boxes(self,index):
        indices = [index] + [random.randint(0, len(self.labels) - 1) for _ in range(1)]
        r_img, _, (r_h, r_w) = load_image(self, indices[1])
        img, _, (h, w) = load_image(self, index)
        mixup_image = cv2.addWeighted(r_img,0.5,img,0.5,0)
        # mixup_image = (r_img + img) / 2
        labels = self.labels[index].copy()
        r_labels = self.labels[indices[1]].copy()
        mixup_labels = np.concatenate((labels,r_labels),0)
        final_labels = mixup_labels.copy()
        final_labels[:, 1] = w * (mixup_labels[:, 1]-mixup_labels[:, 3]/ 2)
        final_labels[:, 2] = h * (mixup_labels[:, 2]-mixup_labels[:, 4]/ 2)
        final_labels[:, 3] = w * (mixup_labels[:, 1]+mixup_labels[:, 3]/ 2)
        final_labels[:, 4] = h * (mixup_labels[:, 2]+mixup_labels[:, 4]/ 2)

        img, labels = random_affine(np.array(mixup_image,dtype=np.uint8), final_labels,
                                      degrees=self.hyp['degrees'],
                                      translate=self.hyp['translate'],
                                      scale=self.hyp['scale'],
                                      shear=self.hyp['shear'],
                                      border=0)  # border to remove
        show(img, labels[:, 1:])
        return img,labels


  • 3.kmeans 聚类 anchor

Tricks


  • 4.边框回归损失(bbox regression Loss)
    Tricks
    Tricks

  • 总览

Tricks


  • Pseudo-Labelling

Tricks

Tricks
Tricks
Tricks


  • Model Ensemble

Tricks


  • TTA

补充:数据增强,有时候不能太强,会造成原图极大的变形,导致训练效果很差

Tricks


  • OOF

Tricks


  • WBF

Tricks
相比于NMS的操作直接删除的操作,没有利用到重叠度较高的框,而WBF则完事了这个缺点,没有直接删除,而是进行了融合。


Tricks
右边的一般比左边的好

本文地址:https://blog.csdn.net/qq_41375318/article/details/107561914