forked from ABILab-CUHK/VisionFM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
179 lines (152 loc) · 7.16 KB
/
Copy pathloader.py
File metadata and controls
179 lines (152 loc) · 7.16 KB
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
# the datasets used in the pretraining
import random
import math
import os
import glob
import numpy as np
from collections import defaultdict
from utils import pil_loader
from torch.utils.data import Dataset
class PretrainData(Dataset):
'''
The dataset class to load the images used in the pretraining stages:
root: the root dir path for the images
modality: the modality of loaded dataset, supporting 8 modalities (Fundus, OCT, External Eye, UBM, B-Ultrasound, MRI, Silt Lamp, FFA)
transform: the involved the transformation.
For each modality, its corresponding dir should be root/modality, e.g. /data/Fundus/XXX.jpg.
.
├── /XXX/Fundus/
│ ├── 1.jpg
│ └── ...
├── /XXX/OCT/
│ ├── 1.png
│ └── ....
└── ....
'''
def __init__(self, root:str, modality:str, transform=None, loader=pil_loader):
self.data_root = root
self.modality = modality
assert self.modality in ['Fundus', 'OCT', 'External', 'UBM', 'Ultrasound', 'MRI', 'Silt-Lamp', 'FFA'], \
f"Unsupported modality: {self.modality}"
print(f"The {self.modality} data will be loaded in the pretraining stage")
self.transform = transform
self.loader = loader
# read all the images
self._entries = []
self.dataset_dir = os.path.join(self.data_root, self.modality)
assert os.path.exists(self.dataset_dir), f'the {self.dataset_dir} does not exists.'
self.get_entries()
def get_entries(self):
# read all the image paths under the dataset dir
self._entries:list = glob.glob(os.path.join(self.dataset_dir, "*"))
print(f"The total images is: {len(self._entries)}")
def get_label(self, img_path):
# get the label based on the image path
path2label = defaultdict() # can be loaded from the json file, where the key is the image name
if img_path in path2label.keys():
return path2label[img_path]
else:
return -1
def __len__(self):
return len(self._entries)
def __getitem__(self, index):
try:
img_path = self._entries[index]
image = self.loader(img_path)
except Exception as e:
print(f"cannot load {img_path} due to the error: {e}")
print(f"will randomly load another one.")
index = random.randint(0, self.__len__())
img_path = self._entries[index]
image = self.loader(img_path)
############# reading labels #############
# In most of the case, the label will be -1 due to the lacking of labels in pretraining images
target = self.get_label(img_path)
##################
if self.transform is not None:
image = self.transform(image)
return image, target
class PretrainMask(PretrainData):
# the wrapper for the pretraining dataset with different masks on the input image
def __init__(self, *args, patch_size, pred_ratio, pred_ratio_var, pred_aspect_ratio,
pred_shape='block', pred_start_epoch=0, **kwargs):
super(PretrainMask, self).__init__(*args, **kwargs)
self.psz = patch_size
self.pred_ratio = pred_ratio[0] if isinstance(pred_ratio, list) and \
len(pred_ratio) == 1 else pred_ratio
self.pred_ratio_var = pred_ratio_var[0] if isinstance(pred_ratio_var, list) and \
len(pred_ratio_var) == 1 else pred_ratio_var
if isinstance(self.pred_ratio, list) and not isinstance(self.pred_ratio_var, list):
self.pred_ratio_var = [self.pred_ratio_var] * len(self.pred_ratio)
self.log_aspect_ratio = tuple(map(lambda x: math.log(x), pred_aspect_ratio))
self.pred_shape = pred_shape
self.pred_start_epoch = pred_start_epoch
def get_pred_ratio(self):
if hasattr(self, 'epoch') and self.epoch < self.pred_start_epoch:
return 0
if isinstance(self.pred_ratio, list):
pred_ratio = []
for prm, prv in zip(self.pred_ratio, self.pred_ratio_var):
assert prm >= prv
pr = random.uniform(prm - prv, prm + prv) if prv > 0 else prm
pred_ratio.append(pr)
pred_ratio = random.choice(pred_ratio)
else:
assert self.pred_ratio >= self.pred_ratio_var
pred_ratio = random.uniform(self.pred_ratio - self.pred_ratio_var, self.pred_ratio + \
self.pred_ratio_var) if self.pred_ratio_var > 0 else self.pred_ratio
return pred_ratio
def set_epoch(self, epoch):
self.epoch = epoch
def __getitem__(self, index):
output:list = super(PretrainMask, self).__getitem__(index) # [image list, label]
masks = []
for img in output[0]:
try:
H, W = img.shape[1] // self.psz, img.shape[2] // self.psz
except:
# skip non-image
continue
high = self.get_pred_ratio() * H * W
if self.pred_shape == 'block':
# following BEiT (https://arxiv.org/abs/2106.08254), see at
# https://github.com/microsoft/unilm/blob/b94ec76c36f02fb2b0bf0dcb0b8554a2185173cd/beit/masking_generator.py#L55
mask = np.zeros((H, W), dtype=bool)
mask_count = 0
while mask_count < high:
max_mask_patches = high - mask_count
delta = 0
for attempt in range(10):
low = (min(H, W) // 3) ** 2 # 16 for 224
target_area = random.uniform(low, max_mask_patches)
aspect_ratio = math.exp(random.uniform(*self.log_aspect_ratio))
h = int(round(math.sqrt(target_area * aspect_ratio)))
w = int(round(math.sqrt(target_area / aspect_ratio)))
if w < W and h < H:
top = random.randint(0, H - h)
left = random.randint(0, W - w)
num_masked = mask[top: top + h, left: left + w].sum()
if 0 < h * w - num_masked <= max_mask_patches:
for i in range(top, top + h):
for j in range(left, left + w):
if mask[i, j] == 0:
mask[i, j] = 1
delta += 1
if delta > 0:
break
if delta == 0:
break
else:
mask_count += delta
elif self.pred_shape == 'rand':
mask = np.hstack([
np.zeros(H * W - int(high)),
np.ones(int(high)),
]).astype(bool)
np.random.shuffle(mask)
mask = mask.reshape(H, W)
else:
# no implementation
assert False
masks.append(mask)
return output + (masks,)