-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune_data.py
363 lines (323 loc) · 14.9 KB
/
finetune_data.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# --------------------------------------------------------
# Pytorch Meta R-CNN
# Written by Anny Xu, Xiaopeng Yan, based on the code from Jianwei Yang
# --------------------------------------------------------
import os
import os.path
import sys
import torch.utils.data as data
from PIL import Image
from torchvision.transforms import functional as F
import torchvision.transforms as tf
import torch
import random
import numpy as np
from lxml import etree
from draw_box_utils import draw_box
import collections
from transforms import RandomHorizontalFlip as flip
from transforms import ToTensor
def vis_data(t):
im=tf.ToPILImage()(t[0])
im.show()
class MetaDataset(data.Dataset):
"""Meta Dataset
Arguments:
root (string): filepath to VOCdevkit folder.
image_set (string): imageset to use (eg. 'train', 'val')
metaclass(string): the class name
shot(int): the number of instances
"""
def __init__(self, root, image_sets, metaclass, shots=1, shuffle=False):
self.root = root
self.image_set = image_sets
self.metaclass = metaclass
# phase 2 , following prior work, collect 3*"shots" all cls images, use only "shot" images as metadata, 3*"shots"*2(flipped) base class and shots*2(flipped) novel class as dataset
self.shots = shots * 3
self.shuffle = shuffle
self._annopath = os.path.join('%s', 'Annotations', '%s.xml')
self._imgpath = os.path.join('%s', 'JPEGImages', '%s.jpg')
self.shot_path = open(os.path.join(self.root, 'VOCdevkit', 'VOC2007', 'ImageSets/Main/shots.txt'), 'w')
self.ids = list()
for (year, name) in image_sets:
self._year = year
rootpath = os.path.join(self.root, 'VOCdevkit', 'VOC' + year)
for line in open(os.path.join(rootpath, 'ImageSets', 'Main', name + '.txt')):
self.ids.append((rootpath, line.strip()))
self.class_to_idx = dict(zip(self.metaclass, range(1, len(self.metaclass)+1))) # class to index mapping
self.idx_to_class = {value: key for key, value in self.class_to_idx.items()}
self.prndata = []
self.prncls = []
self.prntarget = []
print("preparing metadataset...")
prn_image, prn_target = self.get_prndata()
# make sure for metadatase the meta class follows the same order
for i in range(shots):
cls = []
target = []
data = []
for c in range(1,len(self.metaclass)+1):
img = prn_image[self.idx_to_class[c]][i]
img_t = prn_target[self.idx_to_class[c]][i]
cls.append(c)
# data.append(imgmask.permute(0, 3, 1, 2).contiguous())
data.append(img)
target.append(img_t)
self.prncls.append(cls)
self.prndata.append(data)
self.prntarget.append(target)
def __getitem__(self, index):
return self.prndata[index], self.prncls[index], self.prntarget[index]
def parse_xml_to_dict(self, xml):
"""
将xml文件解析成字典形式,参考tensorflow的recursive_parse_xml_to_dict
Args:
xml: xml tree obtained by parsing XML file contents using lxml.etree
Returns:
Python dictionary holding XML contents.
"""
if len(xml) == 0: # 遍历到底层,直接返回tag对应的信息
return {xml.tag: xml.text}
result = {}
for child in xml:
child_result = self.parse_xml_to_dict(child) # 递归遍历标签信息
if child.tag != 'object':
result[child.tag] = child_result[child.tag]
else:
if child.tag not in result: # 因为object可能有多个,所以需要放入列表里
result[child.tag] = []
result[child.tag].append(child_result[child.tag])
return {xml.tag: result}
def get_prndata(self):
'''
collect data for dataset and metaset
:return: the construct prn input data
:prn_image: lists of images in shape of (H, W, 3)
:prn_target: lists of information of image
'''
if self.shuffle:
random.shuffle(self.ids)
prn_image = collections.defaultdict(list)
prn_target = collections.defaultdict(list)
classes = collections.defaultdict(int)
for cls in self.metaclass:
classes[cls] = 0
n=0
for img_id in self.ids:
xml_path = self._annopath % img_id
with open(xml_path) as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = self.parse_xml_to_dict(xml)["annotation"]
img_path = os.path.join(self.root,"VOCdevkit", data["folder"], "JPEGImages", data["filename"])
image = Image.open(img_path)
if image.format != "JPEG":
raise ValueError("Image '{}' format not JPEG".format(self._imgpath % img_id))
image = F.to_tensor(image)
assert "object" in data, "{} lack of object information.".format(xml_path)
for obj in data["object"]:
if obj["difficult"]=='1':
continue
name = obj["name"]
if name not in self.metaclass:
continue
if classes[name] >= self.shots:
continue
xmin = float(obj["bndbox"]["xmin"])
xmax = float(obj["bndbox"]["xmax"])
ymin = float(obj["bndbox"]["ymin"])
ymax = float(obj["bndbox"]["ymax"])
# 进一步检查数据,有的标注信息中可能有w或h为0的情况,这样的数据会导致计算回归loss为nan
if xmax <= xmin or ymax <= ymin:
print("Warning: in '{}' xml, there are some bbox w/h <=0".format(xml_path))
continue
classes[name] += 1
# convert everything into a torch.Tensor
boxes = torch.as_tensor([xmin, ymin, xmax, ymax], dtype=torch.float32)
labels = torch.as_tensor([self.class_to_idx[name]], dtype=torch.int64)
target = {}
target["boxes"] = boxes
target["labels"] = labels
self.shot_path.write(str(img_id[1])+'\n')
n+=1
# if self.transforms is not None:
# image, target = self.transforms(image, target)
prn_image[name].append(image)
prn_target[name].append(target)
print("loaded meta data: {:d}/{:d}".format(n,len(classes.keys())*self.shots))
if len(classes) > 0 and min(classes.values()) == self.shots:
break
self.shot_path.close()
return prn_image, prn_target
def __len__(self):
return len(self.prndata)
class FtDataSet(data.Dataset):
"""pre-pare dataset for finetuning, only VOC2007"""
"""Assert MetaDataset is firstly called to generat shots.txt"""
def __init__(self, voc_root, allclass, shots, txt_name: str = "shots.txt", base_num=15):
self.root=os.path.join(voc_root, "VOCdevkit")
self.root_07 = os.path.join(voc_root, "VOCdevkit", "VOC2007")
self.img_root_07 = os.path.join(self.root_07, "JPEGImages")
self.annotations_root_07 = os.path.join(self.root_07, "Annotations")
self.allclass=allclass
# read train.txt or val.txt file
txt_path_07 = os.path.join(self.root_07, "ImageSets", "Main", txt_name)
assert os.path.exists(txt_path_07), "not found {} file.".format(txt_name)
with open(txt_path_07) as read:
self.xml_list_07 = [os.path.join(self.annotations_root_07, line.strip() + ".xml")
for line in read.readlines()]
# check file
assert len(self.xml_list_07) > 0, "in '{}' file does not find any information.".format(txt_path_07)
for xml_list_07 in self.xml_list_07:
assert os.path.exists(xml_list_07), "not found '{}' file.".format(xml_list_07)
self.class_dict = dict(zip(self.allclass, range(1,len(self.allclass)+1))) # class to index mapping
# prepare finetune data: 3*"shots"*2(flipped) base class and shots*2(flipped) novel class
self.flip = flip(1)
self.t_t = ToTensor()
self.prepare_data(self.xml_list_07, shots, base_num)
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
# images and targets are already prpared, load
image = self.images[idx]
target = self.targets[idx]
return image, target
def prepare_data(self, xml_list, shots, base_num):
self.images=[]
self.targets=[]
class_count = collections.defaultdict(int)
for cls in range(1, len(self.allclass)+1):
class_count[cls] = 0
print("Collecting {:d} images from: {:s}".format(len(xml_list),"shots.txt"))
# collect all images and gt in xml_list
for idx, xml_path in enumerate(xml_list):
with open(xml_path) as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = self.parse_xml_to_dict(xml)["annotation"]
img_path = os.path.join(self.root, data["folder"], "JPEGImages", data["filename"])
image = Image.open(img_path)
if image.format != "JPEG":
raise ValueError("Image '{}' format not JPEG".format(img_path))
boxes = []
labels = []
iscrowd = []
assert "object" in data, "{} lack of object information.".format(xml_path)
for obj in data["object"]:
cls_id = self.class_dict[obj["name"]]
xmin = float(obj["bndbox"]["xmin"])
xmax = float(obj["bndbox"]["xmax"])
ymin = float(obj["bndbox"]["ymin"])
ymax = float(obj["bndbox"]["ymax"])
# 进一步检查数据,有的标注信息中可能有w或h为0的情况,这样的数据会导致计算回归loss为nan
if xmax <= xmin or ymax <= ymin:
print("Warning: in '{}' xml, there are some bbox w/h <=0".format(xml_path))
continue
# novel class: only "shots"
if class_count[cls_id] < shots and cls_id > base_num:
boxes.append([xmin, ymin, xmax, ymax])
labels.append(cls_id)
iscrowd.append(0)
class_count[cls_id] += 1
# base classes : all samples in "shots.txt", i.e., 3*"shots" samples
if cls_id <= base_num:
boxes.append([xmin, ymin, xmax, ymax])
labels.append(cls_id)
iscrowd.append(0)
class_count[cls_id] += 1
if len(labels)==0:# no proper object in this image
continue
# convert everything into a torch.Tensor
boxes = torch.as_tensor(boxes, dtype=torch.float32)
labels = torch.as_tensor(labels, dtype=torch.int64)
iscrowd = torch.as_tensor(iscrowd, dtype=torch.int64)
image_id = torch.tensor([idx])
target = {}
target["boxes"] = boxes
target["labels"] = labels
target["image_id"] = image_id
target["iscrowd"] = iscrowd
image, target=self.t_t(image, target)
self.images.append(image)
self.targets.append(target)
# flip images and target
image_, target_ = self.flip(image, target)
self.images.append(image_)
self.targets.append(target_)
print("After filtering and flipping, together {:d} images for finetune".format(len(self.images)))
def get_height_and_width(self, idx):
# read xml
xml_path = self.xml_list[idx]
with open(xml_path) as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = self.parse_xml_to_dict(xml)["annotation"]
data_height = int(data["size"]["height"])
data_width = int(data["size"]["width"])
return data_height, data_width
def parse_xml_to_dict(self, xml):
"""
将xml文件解析成字典形式,参考tensorflow的recursive_parse_xml_to_dict
Args:
xml: xml tree obtained by parsing XML file contents using lxml.etree
Returns:
Python dictionary holding XML contents.
"""
if len(xml) == 0: # 遍历到底层,直接返回tag对应的信息
return {xml.tag: xml.text}
result = {}
for child in xml:
child_result = self.parse_xml_to_dict(child) # 递归遍历标签信息
if child.tag != 'object':
result[child.tag] = child_result[child.tag]
else:
if child.tag not in result: # 因为object可能有多个,所以需要放入列表里
result[child.tag] = []
result[child.tag].append(child_result[child.tag])
return {xml.tag: result}
def coco_index(self, idx):
"""
该方法是专门为pycocotools统计标签信息准备,不对图像和标签作任何处理
由于不用去读取图片,可大幅缩减统计时间
Args:
idx: 输入需要获取图像的索引
"""
# read xml
xml_path = self.xml_list[idx]
with open(xml_path) as fid:
xml_str = fid.read()
xml = etree.fromstring(xml_str)
data = self.parse_xml_to_dict(xml)["annotation"]
data_height = int(data["size"]["height"])
data_width = int(data["size"]["width"])
# img_path = os.path.join(self.img_root, data["filename"])
# image = Image.open(img_path)
# if image.format != "JPEG":
# raise ValueError("Image format not JPEG")
boxes = []
labels = []
iscrowd = []
for obj in data["object"]:
xmin = float(obj["bndbox"]["xmin"])
xmax = float(obj["bndbox"]["xmax"])
ymin = float(obj["bndbox"]["ymin"])
ymax = float(obj["bndbox"]["ymax"])
boxes.append([xmin, ymin, xmax, ymax])
labels.append(self.class_dict[obj["name"]])
iscrowd.append(int(obj["difficult"]))
# convert everything into a torch.Tensor
boxes = torch.as_tensor(boxes, dtype=torch.float32)
labels = torch.as_tensor(labels, dtype=torch.int64)
iscrowd = torch.as_tensor(iscrowd, dtype=torch.int64)
image_id = torch.tensor([idx])
area = (boxes[:, 3] - boxes[:, 1]) * (boxes[:, 2] - boxes[:, 0])
target = {}
target["boxes"] = boxes
target["labels"] = labels
target["image_id"] = image_id
target["area"] = area
target["iscrowd"] = iscrowd
return (data_height, data_width), target
@staticmethod
def collate_fn(batch):
return tuple(zip(*batch))