-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
226 lines (198 loc) · 9.86 KB
/
dataset.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
import os
import random
from glob import glob
from pdb import set_trace as st
import torch
import numpy as np
from PIL import Image
import torchvision.transforms as T
from torch.utils.data import Dataset
import logging
from utils.utils_model import *
class CADDataLoader(Dataset):
def __init__(self, split='train', do_norm=True, cfg=None, max_prim=12000):
self.logger = logging.getLogger(__name__)
self.set_random_seed(123)
self.root = cfg.root
self.split = split
self.max_prim = max_prim
self.debug = cfg.debug
if cfg is not None:
self.clus_num_per_batch = cfg.clus_num_per_batch
self.nn = cfg.clus_nn
self.size = cfg.img_size
self.filter_num = cfg.filter_num
self.aug_ratio = cfg.aug_ratio
self.rgb_dim = cfg.rgb_dim
else:
self.clus_num_per_batch = 16
self.nn = 64
self.size = 700
self.filter_num = 64
self.aug_ratio = 0.5
self.rgb_dim = 0
# transformations
transform = [T.ToTensor()]
if do_norm:
transform.append(imagenet_preprocess())
self.transform = T.Compose(transform)
# pre-loading
self.image_path_list = glob(os.path.join(self.root, "png", split, "*.png"))
self.anno_path_list = glob(os.path.join(self.root, "npy", split, "*.npy"))
self.image_path_list = sorted(self.image_path_list)
self.anno_path_list = sorted(self.anno_path_list)
self.logger.info(f'Initializing CADDataLoader for split: {split}')
self.logger.info(f'Total .png files found: {len(self.image_path_list)}')
self.logger.info(f'Total .npy files found: {len(self.anno_path_list)}')
# data augmentation
self.train_len = len(self.anno_path_list)
if ("train" in split) and (self.aug_ratio >= 1e-4):
self.logger.info(f" > before aug training: {len(self.anno_path_list)}")
self.aug_training()
self.logger.info(f" > after aug training: {len(self.anno_path_list)}")
if not self.debug:
assert len(self.image_path_list) == len(self.anno_path_list), \
f"Mismatch between image and annotation counts: {len(self.image_path_list)} vs {len(self.anno_path_list)}"
self.length = len(self.image_path_list)
self.logger.info(f" > before filter_smallset: {len(self.anno_path_list)}")
if not self.debug:
self.image_path_list, self.anno_path_list = self.filter_smallset()
if self.debug:
if split == 'train':
self.image_path_list, self.anno_path_list = self.image_path_list[:200], self.anno_path_list[:200]
else:
self.image_path_list, self.anno_path_list = self.image_path_list[:20], self.anno_path_list[:20]
self.length = len(self.image_path_list)
self.logger.info(f" > after filter_smallset: {len(self.anno_path_list)}")
def filter_smallset(self):
anno_path_list_new = []
image_path_list_new = []
for idx, ann_path in enumerate(self.anno_path_list):
adj_node_classes = np.load(ann_path, allow_pickle=True).item()
target = adj_node_classes["cat"]
if self.split == "train": # 修正这里
if self.filter_num <= len(target) <= self.max_prim:
anno_path_list_new.append(self.anno_path_list[idx])
image_path_list_new.append(self.image_path_list[idx])
else:
if len(target) >= self.filter_num:
anno_path_list_new.append(self.anno_path_list[idx])
image_path_list_new.append(self.image_path_list[idx])
self.logger.info(f'Filtered samples: {len(anno_path_list_new)} / {len(self.anno_path_list)}')
return image_path_list_new, anno_path_list_new
def __len__(self):
return self.length
def _get_item(self, index):
img_path = self.image_path_list[index]
ann_path = self.anno_path_list[index]
assert os.path.basename(img_path).split(".")[0] == \
os.path.basename(ann_path).split(".")[0], \
f"Mismatch between image and annotation file names: {img_path} vs {ann_path}"
image = Image.open(img_path).convert("RGB")
image = image.resize((self.size, self.size))
image = self.transform(image) # 不再移动到 GPU
adj_node_classes = np.load(ann_path, allow_pickle=True).item()
target = adj_node_classes["cat"]
target = torch.from_numpy(np.array(target, dtype=np.long)) # 不再移动到 GPU
center = adj_node_classes["ct_norm"]
xy = torch.from_numpy(np.array(center, dtype=np.float32)) # 不再移动到 GPU
if self.rgb_dim > 0:
rgb_npy_path = ann_path.replace('/npy/', '/npy_rgb/')
rgb_info = np.load(rgb_npy_path, allow_pickle=True).item()['rgbs']
rgb_info = torch.from_numpy(np.array(rgb_info, dtype=np.long)) # 不再移动到 GPU
else:
rgb_info = xy
nns = adj_node_classes["nns"]
nns = torch.from_numpy(np.array(nns, dtype=np.long)) # 不再移动到 GPU
instance = adj_node_classes["inst"]
instance_center = self.get_instance_center_tensor(instance, center, semantic=target, img_path=img_path)
instance = torch.from_numpy(np.array(instance, dtype=np.float32)) # 不再移动到 GPU
offset = xy - instance_center
indexes = torch.Tensor([1])
basename = os.path.basename(img_path)
return image, xy, target, rgb_info, nns, offset, instance, indexes, basename
def __getitem__(self, index):
return self._get_item(index)
def random_sample(self, image, xy, target, rgb_info, nns, offset, instance, indexes, basename):
length = xy.shape[0]
rand_idx = random.sample(range(length), self.max_prim)
rand_idx = sorted(rand_idx)
xy = xy[rand_idx]
target = target[rand_idx]
rgb_info = rgb_info[rand_idx]
nns = nns[rand_idx]
offset = offset[rand_idx]
instance = instance[rand_idx]
return image, xy, target, rgb_info, nns, offset, instance, indexes, basename
def set_random_seed(self, seed, deterministic=False):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
if deterministic:
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def aug_training(self):
self.image_path_list_aux = glob(os.path.join(self.root, "images", "{}_aug5x".format(self.split), "images", "*.png"))
self.anno_path_list_aux = glob(os.path.join(self.root, "annotations", "{}_aug5x".format(self.split), "constructed_graphs_withnninst", "*.npy"))
self.image_path_list_aux = sorted(self.image_path_list_aux)
self.anno_path_list_aux = sorted(self.anno_path_list_aux)
try:
assert len(self.image_path_list_aux) == len(self.anno_path_list_aux)
except AssertionError:
def extra_same_elem(list1, list2):
set1 = set(list1)
set2 = set(list2)
iset = set1.intersection(set2)
return list(iset)
img_list = [os.path.basename(x).split(".")[0] for x in self.image_path_list_aux]
ann_list = [os.path.basename(x).split(".")[0] for x in self.anno_path_list_aux]
intersect = extra_same_elem(img_list, ann_list)
img_dir = os.path.dirname(self.image_path_list_aux[0])
ann_dir = os.path.dirname(self.anno_path_list_aux[0])
self.image_path_list_aux = [os.path.join(img_dir, "{}.png".format(x)) for x in intersect]
self.anno_path_list_aux = [os.path.join(ann_dir, "{}.npy".format(x)) for x in intersect]
assert len(self.image_path_list_aux) == len(self.anno_path_list_aux)
aux_len = len(self.anno_path_list_aux)
aug_n = int(self.aug_ratio * self.train_len)
aug_n = min(aug_n, aux_len)
idxes = random.sample(range(0, aux_len), aug_n) # 修改这里,避免超出范围
self.image_path_list_aux = [self.image_path_list_aux[i] for i in idxes]
self.anno_path_list_aux = [self.anno_path_list_aux[i] for i in idxes]
self.image_path_list.extend(self.image_path_list_aux)
self.anno_path_list.extend(self.anno_path_list_aux)
def get_instance_center_tensor(self, instance, center, semantic=None, img_path=None):
offset_list = []
offset_dict = {}
for idx, inst_num in enumerate(instance):
inst_val = inst_num[0]
if inst_val == -1:
continue
if inst_val in offset_dict.keys():
offset_dict[inst_val]["cent"].append(center[idx])
else:
offset_dict[inst_val] = {}
offset_dict[inst_val]["mean"] = None
offset_dict[inst_val]["cent"] = []
offset_dict[inst_val]["cent"].append(center[idx])
for idx, inst_num in enumerate(instance):
inst_val = inst_num[0]
if inst_val == -1:
continue
if inst_val in offset_dict.keys():
offset_dict[inst_val]["mean"] = np.mean(offset_dict[inst_val]["cent"], axis=0)
for idx, inst_num in enumerate(instance):
inst_val = inst_num[0]
if inst_val == -1 or inst_val is None:
offset_list.append([-999, -999])
else:
try:
offset_list.append([offset_dict[inst_val]["mean"][0], offset_dict[inst_val]["mean"][1]])
except:
st()
instance_center = torch.from_numpy(np.array(offset_list, dtype=np.float32))
return instance_center
if __name__ == '__main__':
pass