|
|
@@ -1,15 +1,15 @@
|
|
|
import os
|
|
|
import cv2
|
|
|
import time
|
|
|
-import random
|
|
|
import numpy as np
|
|
|
-from torch.utils.data import Dataset
|
|
|
from pycocotools.coco import COCO
|
|
|
|
|
|
try:
|
|
|
from .data_augment.strong_augment import MosaicAugment, MixupAugment
|
|
|
+ from .voc import VOCDataset
|
|
|
except:
|
|
|
from data_augment.strong_augment import MosaicAugment, MixupAugment
|
|
|
+ from voc import VOCDataset
|
|
|
|
|
|
|
|
|
coco_class_indexs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]
|
|
|
@@ -21,7 +21,7 @@ coco_json_files = {
|
|
|
}
|
|
|
|
|
|
|
|
|
-class COCODataset(Dataset):
|
|
|
+class COCODataset(VOCDataset):
|
|
|
def __init__(self,
|
|
|
cfg,
|
|
|
data_dir :str = None,
|
|
|
@@ -46,106 +46,29 @@ class COCODataset(Dataset):
|
|
|
# ----------- Transform parameters -----------
|
|
|
self.transform = transform
|
|
|
if is_train:
|
|
|
+ if cfg.mosaic_prob == 0.:
|
|
|
+ self.mosaic_augment = None
|
|
|
+ else:
|
|
|
+ self.mosaic_augment = MosaicAugment(cfg.train_img_size, cfg.affine_params, is_train)
|
|
|
self.mosaic_prob = cfg.mosaic_prob
|
|
|
+ if cfg.mixup_prob == 0.:
|
|
|
+ self.mixup_augment = None
|
|
|
+ else:
|
|
|
+ self.mixup_augment = MixupAugment(cfg.train_img_size)
|
|
|
self.mixup_prob = cfg.mixup_prob
|
|
|
self.copy_paste = cfg.copy_paste
|
|
|
- self.mosaic_augment = None if cfg.mosaic_prob == 0. else MosaicAugment(cfg.train_img_size, cfg.affine_params, is_train)
|
|
|
- self.mixup_augment = None if cfg.mixup_prob == 0. and cfg.copy_paste == 0. else MixupAugment(cfg.train_img_size)
|
|
|
else:
|
|
|
self.mosaic_prob = 0.0
|
|
|
self.mixup_prob = 0.0
|
|
|
self.copy_paste = 0.0
|
|
|
self.mosaic_augment = None
|
|
|
self.mixup_augment = None
|
|
|
+
|
|
|
print(' ============ Strong augmentation info. ============ ')
|
|
|
print('use Mosaic Augmentation: {}'.format(self.mosaic_prob))
|
|
|
print('use Mixup Augmentation: {}'.format(self.mixup_prob))
|
|
|
print('use Copy-paste Augmentation: {}'.format(self.copy_paste))
|
|
|
|
|
|
- # ------------ Basic dataset function ------------
|
|
|
- def __len__(self):
|
|
|
- return len(self.ids)
|
|
|
-
|
|
|
- def __getitem__(self, index):
|
|
|
- return self.pull_item(index)
|
|
|
-
|
|
|
- # ------------ Mosaic & Mixup ------------
|
|
|
- def load_mosaic(self, index):
|
|
|
- # ------------ Prepare 4 indexes of images ------------
|
|
|
- ## Load 4x mosaic image
|
|
|
- index_list = np.arange(index).tolist() + np.arange(index+1, len(self.ids)).tolist()
|
|
|
- id1 = index
|
|
|
- id2, id3, id4 = random.sample(index_list, 3)
|
|
|
- indexs = [id1, id2, id3, id4]
|
|
|
-
|
|
|
- ## Load images and targets
|
|
|
- image_list = []
|
|
|
- target_list = []
|
|
|
- for index in indexs:
|
|
|
- img_i, target_i = self.load_image_target(index)
|
|
|
- image_list.append(img_i)
|
|
|
- target_list.append(target_i)
|
|
|
-
|
|
|
- # ------------ Mosaic augmentation ------------
|
|
|
- image, target = self.mosaic_augment(image_list, target_list)
|
|
|
-
|
|
|
- return image, target
|
|
|
-
|
|
|
- def load_mixup(self, origin_image, origin_target, yolox_style=False):
|
|
|
- # ------------ Load a new image & target ------------
|
|
|
- if yolox_style:
|
|
|
- new_index = np.random.randint(0, len(self.ids))
|
|
|
- new_image, new_target = self.load_image_target(new_index)
|
|
|
- else:
|
|
|
- new_index = np.random.randint(0, len(self.ids))
|
|
|
- new_image, new_target = self.load_mosaic(new_index)
|
|
|
-
|
|
|
- # ------------ Mixup augmentation ------------
|
|
|
- image, target = self.mixup_augment(origin_image, origin_target, new_image, new_target, yolox_style)
|
|
|
-
|
|
|
- return image, target
|
|
|
-
|
|
|
- # ------------ Load data function ------------
|
|
|
- def load_image_target(self, index):
|
|
|
- # load an image
|
|
|
- image, _ = self.pull_image(index)
|
|
|
- height, width, channels = image.shape
|
|
|
-
|
|
|
- # load a target
|
|
|
- bboxes, labels = self.pull_anno(index)
|
|
|
- target = {
|
|
|
- "boxes": bboxes,
|
|
|
- "labels": labels,
|
|
|
- "orig_size": [height, width]
|
|
|
- }
|
|
|
-
|
|
|
- return image, target
|
|
|
-
|
|
|
- def pull_item(self, index):
|
|
|
- if random.random() < self.mosaic_prob:
|
|
|
- # load a mosaic image
|
|
|
- mosaic = True
|
|
|
- image, target = self.load_mosaic(index)
|
|
|
- else:
|
|
|
- mosaic = False
|
|
|
- # load an image and target
|
|
|
- image, target = self.load_image_target(index)
|
|
|
-
|
|
|
- # Yolov5-MixUp
|
|
|
- mixup = False
|
|
|
- if random.random() < self.mixup_prob:
|
|
|
- mixup = True
|
|
|
- image, target = self.load_mixup(image, target)
|
|
|
-
|
|
|
- # Copy-paste (use Yolox-Mixup to approximate copy-paste)
|
|
|
- if not mixup and random.random() < self.copy_paste:
|
|
|
- image, target = self.load_mixup(image, target, yolox_style=True)
|
|
|
-
|
|
|
- # augment
|
|
|
- image, target, deltas = self.transform(image, target, mosaic)
|
|
|
-
|
|
|
- return image, target, deltas
|
|
|
-
|
|
|
def pull_image(self, index):
|
|
|
# get the image file name
|
|
|
image_dict = self.coco.dataset['images'][index]
|