Beispiel #1
0
    def __getitem__(self, item):
        assert item <= len(self), 'index range error'

        img_org, bboxes_org = self.__parse_annotation(self.__annotations[item])
        img_org = img_org.transpose(2, 0, 1)  # HWC->CHW

        item_mix = random.randint(0, len(self.__annotations) - 1)
        img_mix, bboxes_mix = self.__parse_annotation(
            self.__annotations[item_mix])
        img_mix = img_mix.transpose(2, 0, 1)

        img, bboxes = dataAug.Mixup()(img_org, bboxes_org, img_mix, bboxes_mix)
        del img_org, bboxes_org, img_mix, bboxes_mix

        label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.__creat_label(
            bboxes)

        img = torch.from_numpy(img).float()
        label_sbbox = torch.from_numpy(label_sbbox).float()
        label_mbbox = torch.from_numpy(label_mbbox).float()
        label_lbbox = torch.from_numpy(label_lbbox).float()
        sbboxes = torch.from_numpy(sbboxes).float()
        mbboxes = torch.from_numpy(mbboxes).float()
        lbboxes = torch.from_numpy(lbboxes).float()

        return img, label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes
Beispiel #2
0
    def __getitem__(self, item):

        img_org, bboxes_org = self.__parse_data(self.img_paths[item],
                                                self.annos[item])
        img_org = img_org.transpose(2, 0, 1)  # HWC->CHW

        item_mix = random.randint(0, len(self.annos) - 1)
        img_mix, bboxes_mix = self.__parse_data(self.img_paths[item_mix],
                                                self.annos[item_mix])
        img_mix = img_mix.transpose(2, 0, 1)
        img, bboxes = dataAug.Mixup(p=0.5)(img_org, bboxes_org, img_mix,
                                           bboxes_mix)
        del img_org, bboxes_org, img_mix, bboxes_mix

        # Without mixing. or we can set p=1 in the dataAug.Mixup(p=1)
        # img = img_org
        # bboxes = np.concatenate([bboxes_org, np.full((len(bboxes_org), 1), 1.0)], axis=1)
        # del img_org, bboxes_org

        # for debuging  we will see this 2 times in 1 loop if we set batch = 2
        # new_boxes = np.copy(bboxes)
        # im = np.copy(img)
        # im = im.transpose(1, 2, 0)
        # im = im[:,:,[2,1,0]]
        # im = im.astype('float32')
        # im = cv2.UMat(im)
        # for box in new_boxes:
        #     box = box.astype('float32')
        #     cv2.rectangle(im, (box[0], box[1]), (box[2], box[3]) , (0,255,0), 2)
        #     font = cv2.FONT_HERSHEY_SIMPLEX
        #     color = (255, 0, 0)
        #     cv2.putText(im , str(box[4]),(int(box[0]-10),int(box[1])), font, .5,color ,2,cv2.LINE_AA)
        # cv2.imshow('test mix', im)
        # cv2.waitKey(0)
        # cv2.destroyAllWindows()

        label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.creat_label(
            bboxes)

        img = torch.from_numpy(img).float()
        label_sbbox = torch.from_numpy(label_sbbox).float()
        label_mbbox = torch.from_numpy(label_mbbox).float()
        label_lbbox = torch.from_numpy(label_lbbox).float()
        sbboxes = torch.from_numpy(sbboxes).float()
        mbboxes = torch.from_numpy(mbboxes).float()
        lbboxes = torch.from_numpy(lbboxes).float()

        return img, label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes
Beispiel #3
0
    def __getitem__(self, item):

        img_org, bboxes_org = self.__parse_annotation(self.__annotations[item])
        H,W,_ = img_org.shape
        if self.do_copy_paste:
            resize_transform = A.Compose([A.augmentations.transforms.Resize (H, W, interpolation=1, always_apply=True, p=1)], bbox_params=A.BboxParams(format="coco", min_visibility=0.05))
            img_data = self.copy_paste_data[item]
            img_data = resize_transform(**img_data)
            img_org = img_data['image']
            #plt.imshow(img_org)
            #plt.show()
            bboxes_org = np.array(img_data['bboxes'])[:, :5]
            #convert from xywh to xyxy
            bboxes_org[:, 2] += bboxes_org[:, 0]
            bboxes_org[:, 3] += bboxes_org[:, 1]
            if cfg.VIS_COPY_PASTE :
                global image_counter
                image_counter +=1
                plt.imshow(visualize_boxes(img_org, bboxes_org[:,:4].astype(int), bboxes_org[:,4].astype(int), np.ones(bboxes_org.shape[0]), class_labels))
                plt.imsave('copy_paste_{}.png'.format(image_counter), visualize_boxes(img_org, bboxes_org[:,:4].astype(int), bboxes_org[:,4].astype(int), np.ones(bboxes_org.shape[0]), class_labels))
                plt.show()

        img_org = img_org.transpose(2, 0, 1)  # HWC->CHW
        
        item_mix = random.randint(0, len(self.__annotations)-1)
        img_mix, bboxes_mix = self.__parse_annotation(self.__annotations[item_mix])
        img_mix = img_mix.transpose(2, 0, 1)


        img, bboxes = dataAug.Mixup()(img_org, bboxes_org, img_mix, bboxes_mix)
        del img_org, bboxes_org, img_mix, bboxes_mix


        label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.__creat_label(bboxes)

        img = torch.from_numpy(img).float()
        label_sbbox = torch.from_numpy(label_sbbox).float()
        label_mbbox = torch.from_numpy(label_mbbox).float()
        label_lbbox = torch.from_numpy(label_lbbox).float()
        sbboxes = torch.from_numpy(sbboxes).float()
        mbboxes = torch.from_numpy(mbboxes).float()
        lbboxes = torch.from_numpy(lbboxes).float()

        return img, label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes
Beispiel #4
0
    def __getitem__(self, item):
        assert item <= len(self), 'index range error'

        if self.anno_file_type == 'train':
            img_org, bboxes_org, img_name = self.__parse_annotation(
                self.__annotations[item])
            img_org, bboxes_org = self.__data_aug(img_org, bboxes_org)
            img_org = img_org.transpose(2, 0, 1)  # HWC->CHW

            item_mix = random.randint(0, len(self.__annotations) - 1)
            img_mix, bboxes_mix, _ = self.__parse_annotation(
                self.__annotations[item_mix])
            img_mix, bboxes_mix = self.__data_aug(img_mix, bboxes_mix)
            img_mix = img_mix.transpose(2, 0, 1)

            img, bboxes = dataAug.Mixup()(img_org, bboxes_org, img_mix,
                                          bboxes_mix)
            del img_mix, bboxes_mix
            img_size = img.shape[1]  # img must be square
        else:
            img_org, bboxes_org, img_name = self.__parse_annotation(
                self.__annotations[item])
            img_org = img_org.transpose(2, 0, 1)
            img = img_org
            bboxes = bboxes_org
            img_size = 512  #magic, need fix

        del img_org, bboxes_org

        label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes = self.__creat_label(
            bboxes, img_size)

        img = torch.from_numpy(img).float()
        label_sbbox = torch.from_numpy(label_sbbox).float()
        label_mbbox = torch.from_numpy(label_mbbox).float()
        label_lbbox = torch.from_numpy(label_lbbox).float()
        sbboxes = torch.from_numpy(sbboxes).float()
        mbboxes = torch.from_numpy(mbboxes).float()
        lbboxes = torch.from_numpy(lbboxes).float()

        return img, label_sbbox, label_mbbox, label_lbbox, sbboxes, mbboxes, lbboxes, img_name