-
Notifications
You must be signed in to change notification settings - Fork 1
/
coco.py
148 lines (134 loc) · 5.41 KB
/
coco.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import torch.utils.data as data
import json
import os
import subprocess
from PIL import Image
import numpy as np
import torch
import pickle
from util import *
urls = {'train_img': 'http://images.cocodataset.org/zips/train2014.zip',
'val_img': 'http://images.cocodataset.org/zips/val2014.zip',
'annotations': 'http://images.cocodataset.org/annotations/annotations_trainval2014.zip'}
def download_coco2014(root, phase):
if not os.path.exists(root):
os.makedirs(root)
tmpdir = os.path.join(root, 'tmp/')
data = os.path.join(root, 'data/')
if not os.path.exists(data):
os.makedirs(data)
if not os.path.exists(tmpdir):
os.makedirs(tmpdir)
if phase == 'train':
filename = 'train2014.zip'
elif phase == 'val':
filename = 'val2014.zip'
cached_file = os.path.join(tmpdir, filename)
cached_file = os.path.join(data, "train2014")
if not os.path.exists(cached_file):
print('Downloading: "{}" to {}\n'.format(
urls[phase + '_img'], cached_file))
os.chdir(tmpdir)
subprocess.call('wget ' + urls[phase + '_img'], shell=True)
os.chdir(root)
# extract file
img_data = os.path.join(data, filename.split('.')[0])
if not os.path.exists(img_data):
print('[dataset] Extracting tar file {file} to {path}'.format(
file=cached_file, path=data))
command = 'unzip {} -d {}'.format(cached_file, data)
os.system(command)
print('[dataset] Done!')
# train/val images/annotations
cached_file = os.path.join(tmpdir, 'annotations_trainval2014.zip')
cached_file = os.path.join(data, 'train_anno.json')
if not os.path.exists(cached_file):
print('Downloading: "{}" to {}\n'.format(
urls['annotations'], cached_file))
os.chdir(tmpdir)
subprocess.Popen('wget ' + urls['annotations'], shell=True)
os.chdir(root)
annotations_data = os.path.join(data, 'annotations')
if not os.path.exists(annotations_data):
print('[dataset] Extracting tar file {file} to {path}'.format(
file=cached_file, path=data))
command = 'unzip {} -d {}'.format(cached_file, data)
os.system(command)
print('[annotation] Done!')
anno = os.path.join(data, '{}_anno.json'.format(phase))
img_id = {}
annotations_id = {}
if not os.path.exists(anno):
annotations_file = json.load(
open(os.path.join(annotations_data, 'instances_{}2014.json'.format(phase))))
annotations = annotations_file['annotations']
category = annotations_file['categories']
category_id = {}
for cat in category:
category_id[cat['id']] = cat['name']
cat2idx = categoty_to_idx(sorted(category_id.values()))
images = annotations_file['images']
for annotation in annotations:
if annotation['image_id'] not in annotations_id:
annotations_id[annotation['image_id']] = set()
annotations_id[annotation['image_id']].add(
cat2idx[category_id[annotation['category_id']]])
for img in images:
if img['id'] not in annotations_id:
continue
if img['id'] not in img_id:
img_id[img['id']] = {}
img_id[img['id']]['file_name'] = img['file_name']
img_id[img['id']]['labels'] = list(annotations_id[img['id']])
anno_list = []
for k, v in img_id.items():
anno_list.append(v)
json.dump(anno_list, open(anno, 'w'))
if not os.path.exists(os.path.join(data, 'category.json')):
json.dump(cat2idx, open(os.path.join(data, 'category.json'), 'w'))
del img_id
del anno_list
del images
del annotations_id
del annotations
del category
del category_id
print('[json] Done!')
def categoty_to_idx(category):
cat2idx = {}
for cat in category:
cat2idx[cat] = len(cat2idx)
return cat2idx
class COCO2014(data.Dataset):
def __init__(self, root, transform=None, phase='train', inp_name=None):
self.root = root
self.phase = phase
self.img_list = []
self.transform = transform
download_coco2014(root, phase)
self.get_anno()
self.num_classes = len(self.cat2idx)
with open(inp_name, 'rb') as f:
self.inp = pickle.load(f)
self.inp_name = inp_name
def get_anno(self):
list_path = os.path.join(
self.root, 'data', '{}_anno.json'.format(self.phase))
self.img_list = json.load(open(list_path, 'r'))
self.cat2idx = json.load(
open(os.path.join(self.root, 'data', 'category.json'), 'r'))
def __len__(self):
return len(self.img_list)
def __getitem__(self, index):
item = self.img_list[index]
return self.get(item)
def get(self, item):
filename = item['file_name']
labels = sorted(item['labels'])
img = Image.open(os.path.join(self.root, 'data',
'{}2014'.format(self.phase), filename)).convert('RGB')
if self.transform is not None:
img = self.transform(img)
target = np.zeros(self.num_classes, np.float32) - 1
target[labels] = 1
return (img, filename, self.inp), target