-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
245 lines (209 loc) · 8.46 KB
/
Copy pathutils.py
File metadata and controls
245 lines (209 loc) · 8.46 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
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
import os
from torch.utils.data.dataset import Dataset
import torch.nn.functional as F
import torch
import numpy as np
from sklearn.metrics import f1_score
from torch.nn import init
import logging
import torch.nn as nn
from torch.nn.parallel import DistributedDataParallel
from glob import glob
import cv2
from albumentations import (
Normalize,
PadIfNeeded,
HorizontalFlip,
VerticalFlip,
CenterCrop,
Crop,
Compose,
Transpose,
RandomRotate90,
ElasticTransform,
GridDistortion,
OpticalDistortion,
RandomSizedCrop,
OneOf,
CLAHE,
RandomGamma,
RandomBrightnessContrast,
Lambda
)
def to_tensor(x, **kwargs):
if len(x.shape) == 2:
x = np.expand_dims(x, axis=2)
return x.transpose(2, 0, 1).astype('float32')
def get_preprocessing(preprocessing_fn):
"""Construct preprocessing transform
Args:
preprocessing_fn (callbale): data normalization function
(can be specific for each pretrained neural network)
Return:
transform: albumentations.Compose
"""
_transform = [
Lambda(image=preprocessing_fn),
Lambda(image=to_tensor, mask=to_tensor),
]
return Compose(_transform)
class TrainSetLoader(Dataset):
def __init__(self, dataset_dir, cfg, index_list=None, preprocessing=None):
super(TrainSetLoader, self).__init__()
self.preprocessing = preprocessing
self.dataset_dir = os.path.join(dataset_dir)
self.all_file_list = [os.path.splitext(file)[0] for file in sorted(glob(os.path.join(self.dataset_dir, 'imgs', '*.png')))]
if index_list is None:
self.file_list = self.all_file_list
else:
self.file_list = []
for index in index_list:
self.file_list.append(self.all_file_list[index])
assert len(self.all_file_list)//5 == len(self.file_list)//4, 'the length of train file_list is error'
self.aug = Compose([RandomSizedCrop(p=1,
min_max_height=(int(cfg.size*1), int(cfg.size*1)),
height=cfg.size,
width=cfg.size),
RandomBrightnessContrast(p=0.8),
RandomGamma(p=0.8),
HorizontalFlip(p=0.5),
VerticalFlip(p=0.5),
RandomRotate90(p=0.5),
Transpose(p=0.5),
# OneOf([ElasticTransform(p=1, alpha=120, sigma=120*0.05, alpha_affine=120*0.03),
# GridDistortion(p=1),
# OpticalDistortion(p=1, distort_limit=2, shift_limit=0.5),
# ], p=0.8)
# ElasticTransform(p=0.9, alpha=120, sigma=120 * 0.05, alpha_affine=120 * 0.03),
# GridDistortion(p=0.8),
# OpticalDistortion(p=0.8, distort_limit=0.5, shift_limit=0.5),
], p=1)
def __getitem__(self, index):
img = cv2.imread(self.file_list[index] + '.png')
# brg->rgb
img = img[..., ::-1]
mask = cv2.imread(self.file_list[index].replace('imgs', 'masks') + '.tiff', 0)
mask = np.where(mask <= 122, 1, 0)
augmented = self.aug(image=img, mask=mask)
img, mask = augmented['image'], augmented['mask']
# apply preprocessing
if self.preprocessing:
sample = self.preprocessing(image=img, mask=mask)
img, mask = sample['image'], sample['mask']
return {'image': torch.from_numpy(img), 'mask': torch.from_numpy(mask)}
def __len__(self):
return len(self.file_list)
class ValSetLoader(Dataset):
def __init__(self, dataset_dir, cfg, index_list=None, preprocessing=None):
super(ValSetLoader, self).__init__()
self.preprocessing = preprocessing
self.dataset_dir = os.path.join(dataset_dir)
self.all_file_list = [os.path.splitext(file)[0] for file in
sorted(glob(os.path.join(self.dataset_dir, 'imgs', '*.png')))]
if index_list is None:
self.file_list = self.all_file_list
else:
self.file_list = []
for index in index_list:
self.file_list.append(self.all_file_list[index])
assert len(self.all_file_list) // 5 == len(self.file_list) , 'the length of val file_list is error'
def __getitem__(self, index):
img = cv2.imread(self.file_list[index] + '.png')
# brg->rgb
img = img[..., ::-1]
mask = cv2.imread(self.file_list[index].replace('imgs', 'masks') + '.tiff', 0)
mask = np.where(mask <= 122, 1, 0)
# apply preprocessing
if self.preprocessing:
sample = self.preprocessing(image=img, mask=mask)
img, mask = sample['image'], sample['mask']
return {'image': torch.from_numpy(img), 'mask': torch.from_numpy(mask)}
def __len__(self):
return len(self.file_list)
class TestSetLoader(Dataset):
def __init__(self, dataset_dir, cfg, preprocessing=None):
super(TestSetLoader, self).__init__()
self.preprocessing = preprocessing
self.dataset_dir = os.path.join(dataset_dir, cfg.problem, 'test', 'imgs')
self.file_list = [os.path.splitext(file)[0] for file in glob(os.path.join(self.dataset_dir, '*'))]
def __getitem__(self, index):
img = cv2.imread(self.file_list[index] + '.png')
# brg->rgb
img = img[..., ::-1]
# apply preprocessing
if self.preprocessing:
sample = self.preprocessing(image=img)
img = sample['image']
return {'image': torch.from_numpy(img)}
def __len__(self):
return len(self.file_list)
def cal_f1(predict, gt,threshold=0.5):
if isinstance(predict, torch.Tensor) and isinstance(gt, torch.Tensor):
if predict.is_cuda:
predict = predict.cpu()
if gt.is_cuda:
gt = gt.cpu()
predict = np.where(predict > threshold, 1, 0)
predict = predict.reshape(-1)
gt = gt.reshape(-1)
return f1_score(gt, predict)
def save_ckpt(network, path, save_filename):
if not os.path.exists(path):
os.makedirs(path)
save_path = os.path.join(path, save_filename)
if isinstance(network, nn.DataParallel) or isinstance(network, DistributedDataParallel):
network = network.module
state_dict = network.state_dict()
for key, param in state_dict.items():
state_dict[key] = param.cpu()
torch.save(state_dict, save_path)
def weights_init_xavier(m):
classname = m.__class__.__name__
if classname.find('Conv2d') != -1:
init.kaiming_uniform_(m.weight.data)
def setup_logger(logger_name, log_file, level=logging.INFO, screen=False, tofile=False):
'''set up logger'''
lg = logging.getLogger(logger_name)
formatter = logging.Formatter('%(asctime)s.%(msecs)03d %(filename)s_%(lineno)d %(levelname)s: %(message)s',
datefmt='%y-%m-%d %H:%M:%S')
lg.setLevel(level)
if tofile:
fh = logging.FileHandler(log_file, mode='w')
fh.setFormatter(formatter)
lg.addHandler(fh)
if screen:
sh = logging.StreamHandler()
sh.setFormatter(formatter)
lg.addHandler(sh)
class BCEFocalLoss(torch.nn.Module):
"""
二分类的Focalloss alpha 固定
"""
def __init__(self, gamma=2, alpha=0.9, reduction='elementwise_mean'):
super().__init__()
self.gamma = gamma
self.alpha = alpha
self.reduction = reduction
def forward(self, _input, target):
pt = torch.sigmoid(_input)
alpha = self.alpha
loss = - alpha * (1 - pt) ** self.gamma * target * torch.log(pt) - \
(1 - alpha) * pt ** self.gamma * (1 - target) * torch.log(1 - pt)
if self.reduction == 'elementwise_mean':
loss = torch.mean(loss)
elif self.reduction == 'sum':
loss = torch.sum(loss)
return loss
class SoftDiceLoss(nn.Module):
def __init__(self):
super(SoftDiceLoss, self).__init__()
def forward(self, logits, targets):
num = targets.size(0)
smooth = 1
probs = F.sigmoid(logits)
m1 = probs.view(num, -1)
m2 = targets.view(num, -1)
intersection = (m1 * m2)
score = 2. * (intersection.sum(1) + smooth) / (m1.sum(1) + m2.sum(1) + smooth)
score = 1 - score.sum() / num
return score