ourdataset.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. import os
  2. import cv2
  3. import time
  4. import random
  5. import numpy as np
  6. import torch
  7. from torch.utils.data import Dataset
  8. try:
  9. from pycocotools.coco import COCO
  10. except:
  11. print("It seems that the COCOAPI is not installed.")
  12. try:
  13. from .data_augment.yolov5_augment import yolov5_mosaic_augment, yolov5_mixup_augment, yolox_mixup_augment
  14. except:
  15. from data_augment.yolov5_augment import yolov5_mosaic_augment, yolov5_mixup_augment, yolox_mixup_augment
  16. # please define our class labels
  17. our_class_labels = ('bird', 'butterfly', 'cat', 'cow', 'dog', 'lion', 'person', 'pig', 'tiger', )
  18. class OurDataset(Dataset):
  19. def __init__(self,
  20. img_size :int = 640,
  21. data_dir :str = None,
  22. image_set :str = 'train',
  23. transform = None,
  24. trans_config = None,
  25. is_train :bool =False,
  26. load_cache :bool = False,
  27. ):
  28. # ----------- Basic parameters -----------
  29. self.img_size = img_size
  30. self.image_set = image_set
  31. self.is_train = is_train
  32. # ----------- Path parameters -----------
  33. self.data_dir = data_dir
  34. self.json_file = '{}.json'.format(image_set)
  35. # ----------- Data parameters -----------
  36. self.coco = COCO(os.path.join(self.data_dir, image_set, 'annotations', self.json_file))
  37. self.ids = self.coco.getImgIds()
  38. self.class_ids = sorted(self.coco.getCatIds())
  39. self.dataset_size = len(self.ids)
  40. # ----------- Transform parameters -----------
  41. self.transform = transform
  42. self.mosaic_prob = 0
  43. self.mixup_prob = 0
  44. self.trans_config = trans_config
  45. if trans_config is not None:
  46. self.mosaic_prob = trans_config['mosaic_prob']
  47. self.mixup_prob = trans_config['mixup_prob']
  48. print('==============================')
  49. print('Image Set: {}'.format(image_set))
  50. print('Json file: {}'.format(self.json_file))
  51. print('use Mosaic Augmentation: {}'.format(self.mosaic_prob))
  52. print('use Mixup Augmentation: {}'.format(self.mixup_prob))
  53. print('==============================')
  54. # ----------- Cached data -----------
  55. self.load_cache = load_cache
  56. self.cached_datas = None
  57. if self.load_cache:
  58. self.cached_datas = self._load_cache()
  59. # ------------ Basic dataset function ------------
  60. def __len__(self):
  61. return len(self.ids)
  62. def __getitem__(self, index):
  63. return self.pull_item(index)
  64. def _load_cache(self):
  65. data_items = []
  66. for idx in range(self.dataset_size):
  67. if idx % 2000 == 0:
  68. print("Caching images and targets : {} / {} ...".format(idx, self.dataset_size))
  69. # load a data
  70. image, target = self.load_image_target(idx)
  71. orig_h, orig_w, _ = image.shape
  72. # resize image
  73. r = self.img_size / max(orig_h, orig_w)
  74. if r != 1:
  75. interp = cv2.INTER_LINEAR
  76. new_size = (int(orig_w * r), int(orig_h * r))
  77. image = cv2.resize(image, new_size, interpolation=interp)
  78. img_h, img_w = image.shape[:2]
  79. # rescale bbox
  80. boxes = target["boxes"].copy()
  81. boxes[:, [0, 2]] = boxes[:, [0, 2]] / orig_w * img_w
  82. boxes[:, [1, 3]] = boxes[:, [1, 3]] / orig_h * img_h
  83. target["boxes"] = boxes
  84. dict_item = {}
  85. dict_item["image"] = image
  86. dict_item["target"] = target
  87. data_items.append(dict_item)
  88. return data_items
  89. # ------------ Mosaic & Mixup ------------
  90. def load_mosaic(self, index):
  91. # load 4x mosaic image
  92. index_list = np.arange(index).tolist() + np.arange(index+1, len(self.ids)).tolist()
  93. id1 = index
  94. id2, id3, id4 = random.sample(index_list, 3)
  95. indexs = [id1, id2, id3, id4]
  96. # load images and targets
  97. image_list = []
  98. target_list = []
  99. for index in indexs:
  100. img_i, target_i = self.load_image_target(index)
  101. image_list.append(img_i)
  102. target_list.append(target_i)
  103. # Mosaic
  104. if self.trans_config['mosaic_type'] == 'yolov5_mosaic':
  105. image, target = yolov5_mosaic_augment(
  106. image_list, target_list, self.img_size, self.trans_config, self.is_train)
  107. return image, target
  108. def load_mixup(self, origin_image, origin_target):
  109. # YOLOv5 type Mixup
  110. if self.trans_config['mixup_type'] == 'yolov5_mixup':
  111. new_index = np.random.randint(0, len(self.ids))
  112. new_image, new_target = self.load_mosaic(new_index)
  113. image, target = yolov5_mixup_augment(
  114. origin_image, origin_target, new_image, new_target)
  115. # YOLOX type Mixup
  116. elif self.trans_config['mixup_type'] == 'yolox_mixup':
  117. new_index = np.random.randint(0, len(self.ids))
  118. new_image, new_target = self.load_image_target(new_index)
  119. image, target = yolox_mixup_augment(
  120. origin_image, origin_target, new_image, new_target, self.img_size, self.trans_config['mixup_scale'])
  121. return image, target
  122. # ------------ Load data function ------------
  123. def load_image_target(self, index):
  124. # == Load a data from the cached data ==
  125. if self.cached_datas is not None:
  126. # load a data
  127. data_item = self.cached_datas[index]
  128. image = data_item["image"]
  129. target = data_item["target"]
  130. # == Load a data from the local disk ==
  131. else:
  132. # load an image
  133. image, _ = self.pull_image(index)
  134. height, width, channels = image.shape
  135. # load a target
  136. bboxes, labels = self.pull_anno(index)
  137. target = {
  138. "boxes": bboxes,
  139. "labels": labels,
  140. "orig_size": [height, width]
  141. }
  142. return image, target
  143. def pull_item(self, index):
  144. if random.random() < self.mosaic_prob:
  145. # load a mosaic image
  146. mosaic = True
  147. image, target = self.load_mosaic(index)
  148. else:
  149. mosaic = False
  150. # load an image and target
  151. image, target = self.load_image_target(index)
  152. # MixUp
  153. if random.random() < self.mixup_prob:
  154. image, target = self.load_mixup(image, target)
  155. # augment
  156. image, target, deltas = self.transform(image, target, mosaic)
  157. return image, target, deltas
  158. def pull_image(self, index):
  159. id_ = self.ids[index]
  160. im_ann = self.coco.loadImgs(id_)[0]
  161. img_file = os.path.join(
  162. self.data_dir, self.image_set, 'images', im_ann["file_name"])
  163. image = cv2.imread(img_file)
  164. return image, id_
  165. def pull_anno(self, index):
  166. img_id = self.ids[index]
  167. im_ann = self.coco.loadImgs(img_id)[0]
  168. anno_ids = self.coco.getAnnIds(imgIds=[int(img_id)], iscrowd=0)
  169. annotations = self.coco.loadAnns(anno_ids)
  170. # image infor
  171. width = im_ann['width']
  172. height = im_ann['height']
  173. #load a target
  174. bboxes = []
  175. labels = []
  176. for anno in annotations:
  177. if 'bbox' in anno and anno['area'] > 0:
  178. # bbox
  179. x1 = np.max((0, anno['bbox'][0]))
  180. y1 = np.max((0, anno['bbox'][1]))
  181. x2 = np.min((width - 1, x1 + np.max((0, anno['bbox'][2] - 1))))
  182. y2 = np.min((height - 1, y1 + np.max((0, anno['bbox'][3] - 1))))
  183. if x2 <= x1 or y2 <= y1:
  184. continue
  185. # class label
  186. cls_id = self.class_ids.index(anno['category_id'])
  187. bboxes.append([x1, y1, x2, y2])
  188. labels.append(cls_id)
  189. # guard against no boxes via resizing
  190. bboxes = np.array(bboxes).reshape(-1, 4)
  191. labels = np.array(labels).reshape(-1)
  192. return bboxes, labels
  193. if __name__ == "__main__":
  194. import time
  195. import argparse
  196. from build import build_transform
  197. parser = argparse.ArgumentParser(description='FreeYOLOv2')
  198. # opt
  199. parser.add_argument('--root', default='/Users/liuhaoran/Desktop/python_work/object-detection/dataset/AnimalDataset/',
  200. help='data root')
  201. parser.add_argument('--split', default='train',
  202. help='data split')
  203. parser.add_argument('-size', '--img_size', default=640, type=int,
  204. help='input image size')
  205. parser.add_argument('--min_box_size', default=8.0, type=float,
  206. help='min size of target bounding box.')
  207. parser.add_argument('--mosaic', default=None, type=float,
  208. help='mosaic augmentation.')
  209. parser.add_argument('--mixup', default=None, type=float,
  210. help='mixup augmentation.')
  211. parser.add_argument('--is_train', action="store_true", default=False,
  212. help='mixup augmentation.')
  213. parser.add_argument('--load_cache', action="store_true", default=False,
  214. help='load cached data.')
  215. args = parser.parse_args()
  216. trans_config = {
  217. 'aug_type': 'yolov5', # optional: ssd, yolov5
  218. # Basic Augment
  219. 'degrees': 0.0,
  220. 'translate': 0.2,
  221. 'scale': [0.5, 2.0],
  222. 'shear': 0.0,
  223. 'perspective': 0.0,
  224. 'hsv_h': 0.015,
  225. 'hsv_s': 0.7,
  226. 'hsv_v': 0.4,
  227. # Mosaic & Mixup
  228. 'mosaic_prob': 1.0,
  229. 'mixup_prob': 1.0,
  230. 'mosaic_type': 'yolov5_mosaic',
  231. 'mixup_type': 'yolov5_mixup',
  232. 'mixup_scale': [0.5, 1.5]
  233. }
  234. transform, trans_cfg = build_transform(args, trans_config, 32, args.is_train)
  235. dataset = OurDataset(
  236. img_size=args.img_size,
  237. data_dir=args.root,
  238. image_set=args.split,
  239. transform=transform,
  240. trans_config=trans_config,
  241. is_train=args.is_train,
  242. load_cache=args.load_cache
  243. )
  244. np.random.seed(0)
  245. class_colors = [(np.random.randint(255),
  246. np.random.randint(255),
  247. np.random.randint(255)) for _ in range(80)]
  248. print('Data length: ', len(dataset))
  249. for i in range(1000):
  250. t0 = time.time()
  251. image, target, deltas = dataset.pull_item(i)
  252. print("Load data: {} s".format(time.time() - t0))
  253. # to numpy
  254. image = image.permute(1, 2, 0).numpy()
  255. image = image.astype(np.uint8)
  256. image = image.copy()
  257. img_h, img_w = image.shape[:2]
  258. boxes = target["boxes"]
  259. labels = target["labels"]
  260. for box, label in zip(boxes, labels):
  261. x1, y1, x2, y2 = box
  262. cls_id = int(label)
  263. color = class_colors[cls_id]
  264. # class name
  265. label = our_class_labels[cls_id]
  266. if x2 - x1 > 0. and y2 - y1 > 0.:
  267. # draw bbox
  268. image = cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), color, 2)
  269. # put the test on the bbox
  270. cv2.putText(image, label, (int(x1), int(y1 - 5)), 0, 0.5, color, 1, lineType=cv2.LINE_AA)
  271. cv2.imshow('gt', image)
  272. # cv2.imwrite(str(i)+'.jpg', img)
  273. cv2.waitKey(0)