You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

objects365.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. # -*- coding: utf-8 -*-
  2. # ---------------------------------------------------------------------
  3. # Part of the following code in this file refs to maskrcnn-benchmark
  4. # MIT License
  5. #
  6. # Copyright (c) 2018 Facebook
  7. # ---------------------------------------------------------------------
  8. import json
  9. import os
  10. from collections import defaultdict
  11. import cv2
  12. import numpy as np
  13. from .meta_vision import VisionDataset
  14. class Objects365(VisionDataset):
  15. r"""`Objects365 <https://www.objects365.org/overview.html>`_ Dataset."""
  16. supported_order = (
  17. "image",
  18. "boxes",
  19. "boxes_category",
  20. "info",
  21. )
  22. def __init__(
  23. self, root, ann_file, remove_images_without_annotations=False, *, order=None
  24. ):
  25. super().__init__(root, order=order, supported_order=self.supported_order)
  26. with open(ann_file, "r") as f:
  27. dataset = json.load(f)
  28. self.imgs = dict()
  29. for img in dataset["images"]:
  30. self.imgs[img["id"]] = img
  31. self.img_to_anns = defaultdict(list)
  32. for ann in dataset["annotations"]:
  33. # for saving memory
  34. if (
  35. "boxes" not in self.order
  36. and "boxes_category" not in self.order
  37. and "bbox" in ann
  38. ):
  39. del ann["bbox"]
  40. self.img_to_anns[ann["image_id"]].append(ann)
  41. self.cats = dict()
  42. for cat in dataset["categories"]:
  43. self.cats[cat["id"]] = cat
  44. self.ids = list(sorted(self.imgs.keys()))
  45. # filter images without detection annotations
  46. if remove_images_without_annotations:
  47. ids = []
  48. for img_id in self.ids:
  49. anno = self.img_to_anns[img_id]
  50. # filter crowd annotations
  51. anno = [obj for obj in anno if obj["iscrowd"] == 0]
  52. anno = [
  53. obj for obj in anno if obj["bbox"][2] > 0 and obj["bbox"][3] > 0
  54. ]
  55. if len(anno) > 0:
  56. ids.append(img_id)
  57. self.img_to_anns[img_id] = anno
  58. else:
  59. del self.imgs[img_id]
  60. del self.img_to_anns[img_id]
  61. self.ids = ids
  62. self.json_category_id_to_contiguous_id = {
  63. v: i + 1 for i, v in enumerate(sorted(self.cats.keys()))
  64. }
  65. self.contiguous_category_id_to_json_id = {
  66. v: k for k, v in self.json_category_id_to_contiguous_id.items()
  67. }
  68. def __getitem__(self, index):
  69. img_id = self.ids[index]
  70. anno = self.img_to_anns[img_id]
  71. target = []
  72. for k in self.order:
  73. if k == "image":
  74. file_name = self.imgs[img_id]["file_name"]
  75. path = os.path.join(self.root, file_name)
  76. image = cv2.imread(path, cv2.IMREAD_COLOR)
  77. target.append(image)
  78. elif k == "boxes":
  79. boxes = [obj["bbox"] for obj in anno]
  80. boxes = np.array(boxes, dtype=np.float32).reshape(-1, 4)
  81. # transfer boxes from xywh to xyxy
  82. boxes[:, 2:] += boxes[:, :2]
  83. target.append(boxes)
  84. elif k == "boxes_category":
  85. boxes_category = [obj["category_id"] for obj in anno]
  86. boxes_category = [
  87. self.json_category_id_to_contiguous_id[c] for c in boxes_category
  88. ]
  89. boxes_category = np.array(boxes_category, dtype=np.int32)
  90. target.append(boxes_category)
  91. elif k == "info":
  92. info = self.imgs[img_id]
  93. info = [info["height"], info["width"], info["file_name"]]
  94. target.append(info)
  95. else:
  96. raise NotImplementedError
  97. return tuple(target)
  98. def __len__(self):
  99. return len(self.ids)
  100. def get_img_info(self, index):
  101. img_id = self.ids[index]
  102. img_info = self.imgs[img_id]
  103. return img_info
  104. class_names = (
  105. "person",
  106. "sneakers",
  107. "chair",
  108. "hat",
  109. "lamp",
  110. "bottle",
  111. "cabinet/shelf",
  112. "cup",
  113. "car",
  114. "glasses",
  115. "picture/frame",
  116. "desk",
  117. "handbag",
  118. "street lights",
  119. "book",
  120. "plate",
  121. "helmet",
  122. "leather shoes",
  123. "pillow",
  124. "glove",
  125. "potted plant",
  126. "bracelet",
  127. "flower",
  128. "tv",
  129. "storage box",
  130. "vase",
  131. "bench",
  132. "wine glass",
  133. "boots",
  134. "bowl",
  135. "dining table",
  136. "umbrella",
  137. "boat",
  138. "flag",
  139. "speaker",
  140. "trash bin/can",
  141. "stool",
  142. "backpack",
  143. "couch",
  144. "belt",
  145. "carpet",
  146. "basket",
  147. "towel/napkin",
  148. "slippers",
  149. "barrel/bucket",
  150. "coffee table",
  151. "suv",
  152. "toy",
  153. "tie",
  154. "bed",
  155. "traffic light",
  156. "pen/pencil",
  157. "microphone",
  158. "sandals",
  159. "canned",
  160. "necklace",
  161. "mirror",
  162. "faucet",
  163. "bicycle",
  164. "bread",
  165. "high heels",
  166. "ring",
  167. "van",
  168. "watch",
  169. "sink",
  170. "horse",
  171. "fish",
  172. "apple",
  173. "camera",
  174. "candle",
  175. "teddy bear",
  176. "cake",
  177. "motorcycle",
  178. "wild bird",
  179. "laptop",
  180. "knife",
  181. "traffic sign",
  182. "cell phone",
  183. "paddle",
  184. "truck",
  185. "cow",
  186. "power outlet",
  187. "clock",
  188. "drum",
  189. "fork",
  190. "bus",
  191. "hanger",
  192. "nightstand",
  193. "pot/pan",
  194. "sheep",
  195. "guitar",
  196. "traffic cone",
  197. "tea pot",
  198. "keyboard",
  199. "tripod",
  200. "hockey",
  201. "fan",
  202. "dog",
  203. "spoon",
  204. "blackboard/whiteboard",
  205. "balloon",
  206. "air conditioner",
  207. "cymbal",
  208. "mouse",
  209. "telephone",
  210. "pickup truck",
  211. "orange",
  212. "banana",
  213. "airplane",
  214. "luggage",
  215. "skis",
  216. "soccer",
  217. "trolley",
  218. "oven",
  219. "remote",
  220. "baseball glove",
  221. "paper towel",
  222. "refrigerator",
  223. "train",
  224. "tomato",
  225. "machinery vehicle",
  226. "tent",
  227. "shampoo/shower gel",
  228. "head phone",
  229. "lantern",
  230. "donut",
  231. "cleaning products",
  232. "sailboat",
  233. "tangerine",
  234. "pizza",
  235. "kite",
  236. "computer box",
  237. "elephant",
  238. "toiletries",
  239. "gas stove",
  240. "broccoli",
  241. "toilet",
  242. "stroller",
  243. "shovel",
  244. "baseball bat",
  245. "microwave",
  246. "skateboard",
  247. "surfboard",
  248. "surveillance camera",
  249. "gun",
  250. "life saver",
  251. "cat",
  252. "lemon",
  253. "liquid soap",
  254. "zebra",
  255. "duck",
  256. "sports car",
  257. "giraffe",
  258. "pumpkin",
  259. "piano",
  260. "stop sign",
  261. "radiator",
  262. "converter",
  263. "tissue ",
  264. "carrot",
  265. "washing machine",
  266. "vent",
  267. "cookies",
  268. "cutting/chopping board",
  269. "tennis racket",
  270. "candy",
  271. "skating and skiing shoes",
  272. "scissors",
  273. "folder",
  274. "baseball",
  275. "strawberry",
  276. "bow tie",
  277. "pigeon",
  278. "pepper",
  279. "coffee machine",
  280. "bathtub",
  281. "snowboard",
  282. "suitcase",
  283. "grapes",
  284. "ladder",
  285. "pear",
  286. "american football",
  287. "basketball",
  288. "potato",
  289. "paint brush",
  290. "printer",
  291. "billiards",
  292. "fire hydrant",
  293. "goose",
  294. "projector",
  295. "sausage",
  296. "fire extinguisher",
  297. "extension cord",
  298. "facial mask",
  299. "tennis ball",
  300. "chopsticks",
  301. "electronic stove and gas stove",
  302. "pie",
  303. "frisbee",
  304. "kettle",
  305. "hamburger",
  306. "golf club",
  307. "cucumber",
  308. "clutch",
  309. "blender",
  310. "tong",
  311. "slide",
  312. "hot dog",
  313. "toothbrush",
  314. "facial cleanser",
  315. "mango",
  316. "deer",
  317. "egg",
  318. "violin",
  319. "marker",
  320. "ship",
  321. "chicken",
  322. "onion",
  323. "ice cream",
  324. "tape",
  325. "wheelchair",
  326. "plum",
  327. "bar soap",
  328. "scale",
  329. "watermelon",
  330. "cabbage",
  331. "router/modem",
  332. "golf ball",
  333. "pine apple",
  334. "crane",
  335. "fire truck",
  336. "peach",
  337. "cello",
  338. "notepaper",
  339. "tricycle",
  340. "toaster",
  341. "helicopter",
  342. "green beans",
  343. "brush",
  344. "carriage",
  345. "cigar",
  346. "earphone",
  347. "penguin",
  348. "hurdle",
  349. "swing",
  350. "radio",
  351. "CD",
  352. "parking meter",
  353. "swan",
  354. "garlic",
  355. "french fries",
  356. "horn",
  357. "avocado",
  358. "saxophone",
  359. "trumpet",
  360. "sandwich",
  361. "cue",
  362. "kiwi fruit",
  363. "bear",
  364. "fishing rod",
  365. "cherry",
  366. "tablet",
  367. "green vegetables",
  368. "nuts",
  369. "corn",
  370. "key",
  371. "screwdriver",
  372. "globe",
  373. "broom",
  374. "pliers",
  375. "volleyball",
  376. "hammer",
  377. "eggplant",
  378. "trophy",
  379. "dates",
  380. "board eraser",
  381. "rice",
  382. "tape measure/ruler",
  383. "dumbbell",
  384. "hamimelon",
  385. "stapler",
  386. "camel",
  387. "lettuce",
  388. "goldfish",
  389. "meat balls",
  390. "medal",
  391. "toothpaste",
  392. "antelope",
  393. "shrimp",
  394. "rickshaw",
  395. "trombone",
  396. "pomegranate",
  397. "coconut",
  398. "jellyfish",
  399. "mushroom",
  400. "calculator",
  401. "treadmill",
  402. "butterfly",
  403. "egg tart",
  404. "cheese",
  405. "pig",
  406. "pomelo",
  407. "race car",
  408. "rice cooker",
  409. "tuba",
  410. "crosswalk sign",
  411. "papaya",
  412. "hair drier",
  413. "green onion",
  414. "chips",
  415. "dolphin",
  416. "sushi",
  417. "urinal",
  418. "donkey",
  419. "electric drill",
  420. "spring rolls",
  421. "tortoise/turtle",
  422. "parrot",
  423. "flute",
  424. "measuring cup",
  425. "shark",
  426. "steak",
  427. "poker card",
  428. "binoculars",
  429. "llama",
  430. "radish",
  431. "noodles",
  432. "yak",
  433. "mop",
  434. "crab",
  435. "microscope",
  436. "barbell",
  437. "bread/bun",
  438. "baozi",
  439. "lion",
  440. "red cabbage",
  441. "polar bear",
  442. "lighter",
  443. "seal",
  444. "mangosteen",
  445. "comb",
  446. "eraser",
  447. "pitaya",
  448. "scallop",
  449. "pencil case",
  450. "saw",
  451. "table tennis paddle",
  452. "okra",
  453. "starfish",
  454. "eagle",
  455. "monkey",
  456. "durian",
  457. "game board",
  458. "rabbit",
  459. "french horn",
  460. "ambulance",
  461. "asparagus",
  462. "hoverboard",
  463. "pasta",
  464. "target",
  465. "hotair balloon",
  466. "chainsaw",
  467. "lobster",
  468. "iron",
  469. "flashlight",
  470. )