-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
195 lines (168 loc) · 7.18 KB
/
grid.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# encoding: utf-8
"""
''
"""
import os
from scipy.io import loadmat
from glob import glob
from fastreid.data.datasets import DATASET_REGISTRY
from fastreid.data.datasets.bases import ImageDataset
import pdb
__all__ = ['GRID',]
@DATASET_REGISTRY.register()
class GRID(ImageDataset):
dataset_dir = "grid"
dataset_name = 'GRID'
def __init__(self, root='datasets', split_id = 0, **kwargs):
if isinstance(root, list):
split_id = root[1]
self.root = root[0]
else:
self.root = root
split_id = 0
self.dataset_dir = os.path.join(self.root, self.dataset_dir)
self.probe_path = os.path.join(
self.dataset_dir, 'probe'
)
self.gallery_path = os.path.join(
self.dataset_dir, 'gallery'
)
self.split_mat_path = os.path.join(
self.dataset_dir, 'features_and_partitions.mat'
)
self.split_path = os.path.join(self.dataset_dir, 'splits.json')
required_files = [
self.dataset_dir, self.probe_path, self.gallery_path,
self.split_mat_path
]
self.check_before_run(required_files)
self.prepare_split()
splits = self.read_json(self.split_path)
if split_id >= len(splits):
raise ValueError(
'split_id exceeds range, received {}, '
'but expected between 0 and {}'.format(
split_id,
len(splits) - 1
)
)
split = splits[split_id]
train = split['train']
query = split['query']
gallery = split['gallery']
train = [tuple([os.path.join(self.dataset_dir, item[0])] + item[1:]) for item in train]
query = [tuple([os.path.join(self.dataset_dir, item[0])] + item[1:]) for item in query]
gallery = [tuple([os.path.join(self.dataset_dir, item[0])] + item[1:]) for item in gallery]
# train = [tuple(item) for item in train]
# query = [tuple(item) for item in query]
# gallery = [tuple(item) for item in gallery]
super(GRID, self).__init__(train, query, gallery, **kwargs)
def prepare_split(self):
if not os.path.exists(self.split_path):
print('Creating 10 random splits')
split_mat = loadmat(self.split_mat_path)
trainIdxAll = split_mat['trainIdxAll'][0] # length = 10
probe_img_paths = sorted(
glob(os.path.join(self.probe_path, '*.jpeg'))
)
gallery_img_paths = sorted(
glob(os.path.join(self.gallery_path, '*.jpeg'))
)
splits = []
for split_idx in range(10):
train_idxs = trainIdxAll[split_idx][0][0][2][0].tolist()
assert len(train_idxs) == 125
idx2label = {
idx: label
for label, idx in enumerate(train_idxs)
}
train, query, gallery = [], [], []
# processing probe folder
for img_path in probe_img_paths:
img_name = os.path.basename(img_path)
img_idx = int(img_name.split('_')[0])
camid = int(
img_name.split('_')[1]
) - 1 # index starts from 0
if img_idx in train_idxs:
# add by hby, for train
# pid = self.dataset_name + "_" + str(idx2label[img_idx])
# camid = self.dataset_name + "_" + str(camid)
train.append((os.path.relpath(img_path, self.dataset_dir), img_idx, camid))
else:
query.append((os.path.relpath(img_path, self.dataset_dir), img_idx, camid))
# process gallery folder
for img_path in gallery_img_paths:
img_name = os.path.basename(img_path)
img_idx = int(img_name.split('_')[0])
camid = int(
img_name.split('_')[1]
) - 1 # index starts from 0
if img_idx in train_idxs:
# add by hby, for train
# pid = self.dataset_name + "_" + str(idx2label[img_idx])
# camid = self.dataset_name + "_" + str(camid)
train.append((os.path.relpath(img_path, self.dataset_dir), img_idx, camid))
else:
gallery.append((os.path.relpath(img_path, self.dataset_dir), img_idx, camid))
all_pid = []
for img_path, pid, camid in train:
all_pid.append(pid)
for img_path, pid, camid in query:
all_pid.append(pid)
for img_path, pid, camid in gallery:
all_pid.append(pid)
all_pid = set(all_pid)
#print(len(all_pid))
assert len(all_pid) == 251
all_id2label = {pid: label for label, pid in enumerate(all_pid)}
final_query = []
final_gallery = []
final_train = []
for img_path, pid, camid in train:
pid = self.dataset_name + "_" + str(all_id2label[pid]-1)
camid = self.dataset_name + "_" + str(camid)
final_train.append((img_path, pid, camid))
for img_path, pid, camid in query:
final_query.append((img_path, all_id2label[pid] - 1, camid))
i=250
for img_path, pid, camid in gallery:
if all_id2label[pid] == 0:
new_pid = i
i = i+1
final_gallery.append((img_path, new_pid, camid))
else:
final_gallery.append((img_path, all_id2label[pid] - 1, camid))
split = {
'train': final_train,
'query': final_query,
'gallery': final_gallery,
'num_train_pids': 125,
'num_query_pids': 125,
'num_gallery_pids': 900
}
splits.append(split)
print('Totally {} splits are created'.format(len(splits)))
self.write_json(splits, self.split_path)
print('Split file saved to {}'.format(self.split_path))
def read_json(self, fpath):
import json
"""Reads json file from a path."""
with open(fpath, 'r') as f:
obj = json.load(f)
return obj
def write_json(self, obj, fpath):
import json
"""Writes to a json file."""
self.mkdir_if_missing(os.path.dirname(fpath))
with open(fpath, 'w') as f:
json.dump(obj, f, indent=4, separators=(',', ': '))
def mkdir_if_missing(self, dirname):
import errno
"""Creates dirname if it is missing."""
if not os.path.exists(dirname):
try:
os.makedirs(dirname)
except OSError as e:
if e.errno != errno.EEXIST:
raise