glenn-jocher commited on
Commit
5189b3a
·
1 Parent(s): b292837

Objects365 Update

Browse files
data/objects365.yaml CHANGED
@@ -55,3 +55,43 @@ names: [ 'Person', 'Sneakers', 'Chair', 'Other Shoes', 'Hat', 'Car', 'Lamp', 'Gl
55
  'Scallop', 'Noddles', 'Comb', 'Dumpling', 'Oyster', 'Table Tennis paddle', 'Cosmetics Brush/Eyeliner Pencil',
56
  'Chainsaw', 'Eraser', 'Lobster', 'Durian', 'Okra', 'Lipstick', 'Cosmetics Mirror', 'Curling', 'Table Tennis' ]
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  'Scallop', 'Noddles', 'Comb', 'Dumpling', 'Oyster', 'Table Tennis paddle', 'Cosmetics Brush/Eyeliner Pencil',
56
  'Chainsaw', 'Eraser', 'Lobster', 'Durian', 'Okra', 'Lipstick', 'Cosmetics Mirror', 'Curling', 'Table Tennis' ]
57
 
58
+
59
+ # download command/URL (optional) --------------------------------------------------------------------------------------
60
+ download: |
61
+ from pycocotools.coco import COCO
62
+ from tqdm import tqdm
63
+
64
+ from utils.general import download, Path
65
+
66
+ # Make Directories
67
+ dir = Path('../datasets/objects365') # dataset directory
68
+ for p in 'images', 'labels':
69
+ (dir / p).mkdir(parents=True, exist_ok=True)
70
+ for q in 'train', 'val':
71
+ (dir / p / q).mkdir(parents=True, exist_ok=True)
72
+
73
+ # Download
74
+ url = "https://dorc.ks3-cn-beijing.ksyun.com/data-set/2020Objects365%E6%95%B0%E6%8D%AE%E9%9B%86/train/"
75
+ download([url + 'zhiyuan_objv2_train.tar.gz'], dir=dir) # annotations json
76
+ download([url + f for f in [f'patch{i}.tar.gz' for i in range(51)]], dir=dir / 'images' / 'train',
77
+ curl=True, delete=False, threads=8)
78
+
79
+ # Labels
80
+ coco = COCO(dir / 'zhiyuan_objv2_train.json')
81
+ names = [x["name"] for x in coco.loadCats(coco.getCatIds())]
82
+ for cid, cat in enumerate(names):
83
+ catIds = coco.getCatIds(catNms=[cat])
84
+ imgIds = coco.getImgIds(catIds=catIds)
85
+ for im in tqdm(coco.loadImgs(imgIds), desc=f'Class {cid}/{len(names)} {cat}'):
86
+ width, height = im["width"], im["height"]
87
+ path = Path(im["file_name"]) # image filename
88
+ try:
89
+ with open(dir / 'labels' / 'train' / path.with_suffix('.txt').name, 'a') as file:
90
+ annIds = coco.getAnnIds(imgIds=im["id"], catIds=catIds, iscrowd=None)
91
+ for a in coco.loadAnns(annIds):
92
+ x, y, w, h = a['bbox'] # bounding box in xywh (xy top-left corner)
93
+ x, y = x + w / 2, y + h / 2 # xy to center
94
+ file.write(f"{cid} {x / width:.5f} {y / height:.5f} {w / width:.5f} {h / height:.5f}\n")
95
+
96
+ except Exception as e:
97
+ print(e)
data/scripts/get_objects365.py DELETED
@@ -1,45 +0,0 @@
1
- # Objects365 https://www.objects365.org labels JSON to YOLO script
2
- # 1. Download Object 365 from the Object 365 website And unpack all images in datasets/object365/images
3
- # 2. Place this file and zhiyuan_objv2_train.json file in datasets/objects365
4
- # 3. Execute this file from datasets/object365 path
5
- # /datasets
6
- # /objects365
7
- # /images
8
- # /labels
9
-
10
-
11
- from pycocotools.coco import COCO
12
-
13
- from utils.general import download, Path
14
-
15
- # Make Directories
16
- dir = Path('../datasets/objects365') # dataset directory
17
- for p in 'images', 'labels':
18
- (dir / p).mkdir(parents=True, exist_ok=True)
19
- for q in 'train', 'val':
20
- (dir / p / q).mkdir(parents=True, exist_ok=True)
21
-
22
- # Download
23
- url = "https://dorc.ks3-cn-beijing.ksyun.com/data-set/2020Objects365%E6%95%B0%E6%8D%AE%E9%9B%86/train/"
24
- download([url + 'zhiyuan_objv2_train.tar.gz'], dir=dir) # annotations json
25
- download([url + f for f in [f'patch{i}.tar.gz' for i in range(51)]], dir=dir / 'images' / 'train', curl=True, threads=8)
26
-
27
- # Labels
28
- coco = COCO(dir / 'zhiyuan_objv2_train.json')
29
- names = [x["name"] for x in coco.loadCats(coco.getCatIds())]
30
- for categoryId, cat in enumerate(names):
31
- catIds = coco.getCatIds(catNms=[cat])
32
- imgIds = coco.getImgIds(catIds=catIds)
33
- for im in coco.loadImgs(imgIds):
34
- width, height = im["width"], im["height"]
35
- path = Path(im["file_name"]) # image filename
36
- try:
37
- with open(dir / 'labels' / 'train' / path.with_suffix('.txt').name, 'a') as file:
38
- annIds = coco.getAnnIds(imgIds=im["id"], catIds=catIds, iscrowd=None)
39
- for a in coco.loadAnns(annIds):
40
- x, y, w, h = a['bbox'] # bounding box in xywh (xy top-left corner)
41
- x, y = x + w / 2, y + h / 2 # xy to center
42
- file.write(f"{categoryId} {x / width:.5f} {y / height:.5f} {w / width:.5f} {h / height:.5f}\n")
43
-
44
- except Exception as e:
45
- print(e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
utils/general.py CHANGED
@@ -193,7 +193,7 @@ def check_dataset(dict):
193
  raise Exception('Dataset not found.')
194
 
195
 
196
- def download(url, dir='.', unzip=True, curl=False, threads=1):
197
  # Multi-threaded file download and unzip function
198
  def download_one(url, dir):
199
  # Download 1 file
@@ -207,9 +207,12 @@ def download(url, dir='.', unzip=True, curl=False, threads=1):
207
  if unzip and f.suffix in ('.zip', '.gz'):
208
  print(f'Unzipping {f}...')
209
  if f.suffix == '.zip':
210
- os.system(f'unzip -qo {f} -d {dir} && rm {f}') # unzip -quiet -overwrite
211
  elif f.suffix == '.gz':
212
- os.system(f'tar xfz {f} --directory {f.parent} && rm {f}') # unzip
 
 
 
213
 
214
  dir = Path(dir)
215
  dir.mkdir(parents=True, exist_ok=True) # make directory
 
193
  raise Exception('Dataset not found.')
194
 
195
 
196
+ def download(url, dir='.', unzip=True, delete=True, curl=False, threads=1):
197
  # Multi-threaded file download and unzip function
198
  def download_one(url, dir):
199
  # Download 1 file
 
207
  if unzip and f.suffix in ('.zip', '.gz'):
208
  print(f'Unzipping {f}...')
209
  if f.suffix == '.zip':
210
+ s = f'unzip -qo {f} -d {dir} && rm {f}' # unzip -quiet -overwrite
211
  elif f.suffix == '.gz':
212
+ s = f'tar xfz {f} --directory {f.parent}' # unzip
213
+ if delete: # delete zip file after unzip
214
+ s += f' && rm {f}'
215
+ os.system(s)
216
 
217
  dir = Path(dir)
218
  dir.mkdir(parents=True, exist_ok=True) # make directory