-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_loader.py
192 lines (167 loc) · 7.07 KB
/
data_loader.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
#!/usr/bin/env python
# -*-coding: utf -8-*-
"""
@ Author: ZhanYang
@ File Name: data_loader.py
@ Email: [email protected]
@ Github: https://github.com/ZhanYang-nwpu/RSVG-pytorch
@ Paper: https://ieeexplore.ieee.org/document/10056343
@ Dataset: https://drive.google.com/drive/folders/1hTqtYsC6B-m4ED2ewx5oKuYZV13EoJp_?usp=sharing
"""
import os
import re
import xml.etree.ElementTree as ET
import pandas as pd
import numpy as np
import cv2
import utils
from utils.transforms import letterbox
import matplotlib.pyplot as plt
import torch.utils.data as data
from pytorch_pretrained_bert.tokenization import BertTokenizer
import random
def filelist(root, file_type):
return [os.path.join(directory_path, f) for directory_path, directory_name, files in os.walk(root) for f in files if f.endswith(file_type)]
class RSVGDataset(data.Dataset):
def __init__(self, images_path, anno_path, imsize=640, transform= None, augment= False,
split='train', testmode=False,max_query_len=40, bert_model='bert-base-uncased'):
self.images = []
self.images_path = images_path
self.anno_path = anno_path
self.imsize = imsize
self.augment = augment
self.transform = transform
self.split = split
self.testmode = testmode
self.query_len = max_query_len # 40
self.tokenizer = BertTokenizer.from_pretrained(bert_model, do_lower_case=True)
file = open('DIOR_RSVG\\' + split + '.txt', "r").readlines()
Index = [int(index.strip('\n')) for index in file]
count = 0
annotations = filelist(anno_path, '.xml')
for anno_path in annotations:
root = ET.parse(anno_path).getroot()
for member in root.findall('object'):
if count in Index:
imageFile = str(images_path) + '/' + root.find("./filename").text
box = np.array([int(member[2][0].text), int(member[2][1].text), int(member[2][2].text), int(member[2][3].text)],dtype=np.float32)
text = member[3].text
self.images.append((imageFile, box, text))
count += 1
def pull_item(self, idx):
img_path, bbox, phrase = self.images[idx]
bbox = np.array(bbox, dtype=int) # box format: to x1 y1 x2 y2
img = cv2.imread(img_path)
return img, phrase, bbox
def __len__(self):
return len(self.images)
def __getitem__(self, idx):
img, phrase, bbox = self.pull_item(idx)
phrase = phrase.lower()
phrase_out = phrase
# seems a bug in torch transformation resize, so separate in advance
h, w = img.shape[0], img.shape[1]
mask = np.zeros_like(img)
img, mask, ratio, dw, dh = letterbox(img, mask, self.imsize)
bbox[0], bbox[2] = bbox[0] * ratio + dw, bbox[2] * ratio + dw
bbox[1], bbox[3] = bbox[1] * ratio + dh, bbox[3] * ratio + dh
# Norm, to tensor
if self.transform is not None:
img = self.transform(img)
# encode phrase to bert input
examples = read_examples(phrase, idx)
features = convert_examples_to_features(examples=examples, seq_length=self.query_len,tokenizer=self.tokenizer)
word_id = features[0].input_ids
word_mask = features[0].input_mask
word_split = features[0].tokens[1:-1]
if self.testmode:
return img, mask, np.array(word_id, dtype=int), np.array(word_mask, dtype=int), \
np.array(bbox, dtype=np.float32), np.array(ratio, dtype=np.float32), \
np.array(dw, dtype=np.float32), np.array(dh, dtype=np.float32), self.images[idx][0], phrase_out
else:
return img, mask, np.array(word_id, dtype=int), np.array(word_mask, dtype=int), np.array(bbox, dtype=np.float32)
def read_examples(input_line, unique_id):
"""Read a list of `InputExample`s from an input file."""
examples = []
# unique_id = 0
line = input_line #reader.readline()
# if not line:
# break
line = line.strip()
text_a = None
text_b = None
m = re.match(r"^(.*) \|\|\| (.*)$", line)
if m is None:
text_a = line
else:
text_a = m.group(1)
text_b = m.group(2)
examples.append(InputExample(unique_id=unique_id, text_a=text_a, text_b=text_b))
# unique_id += 1
return examples
## Bert text encoding
class InputExample(object):
def __init__(self, unique_id, text_a, text_b):
self.unique_id = unique_id
self.text_a = text_a
self.text_b = text_b
class InputFeatures(object):
"""A single set of features of data."""
def __init__(self, unique_id, tokens, input_ids, input_mask, input_type_ids):
self.unique_id = unique_id
self.tokens = tokens
self.input_ids = input_ids
self.input_mask = input_mask
self.input_type_ids = input_type_ids
def convert_examples_to_features(examples, seq_length, tokenizer):
"""Loads a data file into a list of `InputBatch`s."""
features = []
for (ex_index, example) in enumerate(examples):
tokens_a = tokenizer.tokenize(example.text_a)
tokens_b = None
if example.text_b:
tokens_b = tokenizer.tokenize(example.text_b)
if tokens_b:
# Modifies `tokens_a` and `tokens_b` in place so that the total
# length is less than the specified length.
# Account for [CLS], [SEP], [SEP] with "- 3"
_truncate_seq_pair(tokens_a, tokens_b, seq_length - 3)
else:
# Account for [CLS] and [SEP] with "- 2"
if len(tokens_a) > seq_length - 2:
tokens_a = tokens_a[0:(seq_length - 2)]
tokens = []
input_type_ids = []
tokens.append("[CLS]")
input_type_ids.append(0)
for token in tokens_a:
tokens.append(token)
input_type_ids.append(0)
tokens.append("[SEP]")
input_type_ids.append(0)
if tokens_b:
for token in tokens_b:
tokens.append(token)
input_type_ids.append(1)
tokens.append("[SEP]")
input_type_ids.append(1)
input_ids = tokenizer.convert_tokens_to_ids(tokens)
# The mask has 1 for real tokens and 0 for padding tokens. Only real
# tokens are attended to.
input_mask = [1] * len(input_ids)
# Zero-pad up to the sequence length.
while len(input_ids) < seq_length:
input_ids.append(0)
input_mask.append(0)
input_type_ids.append(0)
assert len(input_ids) == seq_length
assert len(input_mask) == seq_length
assert len(input_type_ids) == seq_length
features.append(
InputFeatures(
unique_id=example.unique_id,
tokens=tokens,
input_ids=input_ids,
input_mask=input_mask,
input_type_ids=input_type_ids))
return features