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 12 kB

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

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台