-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
151 lines (127 loc) · 6.93 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
from utils import *
import matplotlib.pyplot as plt
import os
import shutil
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
class TrainSetLoader(Dataset):
def __init__(self, dataset_dir, dataset_name, patch_size, img_norm_cfg=None):
super(TrainSetLoader).__init__()
self.dataset_name = dataset_name
self.dataset_dir = dataset_dir + '/' + dataset_name
self.patch_size = patch_size
if not os.path.exists(self.dataset_dir +'/img_idx/train_' + dataset_name + '.txt') and os.path.exists(self.dataset_dir +'/img_idx/train.txt'):
shutil.copyfile(self.dataset_dir +'/img_idx/train.txt', self.dataset_dir +'/img_idx/train_' + dataset_name + '.txt')
with open(self.dataset_dir +'/img_idx/train_' + dataset_name + '.txt', 'r') as f:
self.train_list = f.read().splitlines()
if img_norm_cfg == None:
self.img_norm_cfg = get_img_norm_cfg(dataset_name, dataset_dir)
else:
self.img_norm_cfg = img_norm_cfg
self.tranform = augumentation()
def __getitem__(self, idx):
try:
img = Image.open((self.dataset_dir + '/images/' + self.train_list[idx] + '.png').replace('//','/')).convert('I')
mask = Image.open((self.dataset_dir + '/masks/' + self.train_list[idx] + '.png').replace('//','/'))
except:
img = Image.open((self.dataset_dir + '/images/' + self.train_list[idx] + '.bmp').replace('//','/')).convert('I')
mask = Image.open((self.dataset_dir + '/masks/' + self.train_list[idx] + '.bmp').replace('//','/'))
img = Normalized(np.array(img, dtype=np.float32), self.img_norm_cfg)
mask = np.array(mask, dtype=np.float32) / 255.0
if len(mask.shape) > 2:
mask = mask[:,:,0]
img_patch, mask_patch = random_crop(img, mask, self.patch_size, pos_prob=0.5)
img_patch, mask_patch = self.tranform(img_patch, mask_patch)
img_patch, mask_patch = img_patch[np.newaxis,:], mask_patch[np.newaxis,:]
img_patch = torch.from_numpy(np.ascontiguousarray(img_patch))
mask_patch = torch.from_numpy(np.ascontiguousarray(mask_patch))
return img_patch, mask_patch
def __len__(self):
return len(self.train_list)
class TestSetLoader(Dataset):
def __init__(self, dataset_dir, train_dataset_name, test_dataset_name, img_norm_cfg=None):
super(TestSetLoader).__init__()
self.dataset_dir = dataset_dir + '/' + test_dataset_name
with open(self.dataset_dir + '/img_idx/test_' + test_dataset_name + '.txt', 'r') as f:
self.test_list = f.read().splitlines()
if img_norm_cfg == None:
self.img_norm_cfg = get_img_norm_cfg(train_dataset_name, dataset_dir)
else:
self.img_norm_cfg = img_norm_cfg
def __getitem__(self, idx):
try:
img = Image.open((self.dataset_dir + '/images/' + self.test_list[idx] + '.png').replace('//','/')).convert('I')
mask = Image.open((self.dataset_dir + '/masks/' + self.test_list[idx] + '.png').replace('//','/'))
except:
img = Image.open((self.dataset_dir + '/images/' + self.test_list[idx] + '.bmp').replace('//','/')).convert('I')
mask = Image.open((self.dataset_dir + '/masks/' + self.test_list[idx] + '.bmp').replace('//','/'))
img = Normalized(np.array(img, dtype=np.float32), self.img_norm_cfg)
mask = np.array(mask, dtype=np.float32) / 255.0
if len(mask.shape) > 2:
mask = mask[:,:,0]
h, w = img.shape
img = PadImg(img)
mask = PadImg(mask)
img, mask = img[np.newaxis,:], mask[np.newaxis,:]
img = torch.from_numpy(np.ascontiguousarray(img))
mask = torch.from_numpy(np.ascontiguousarray(mask))
return img, mask, [h,w], self.test_list[idx]
def __len__(self):
return len(self.test_list)
class InferenceSetLoader(Dataset):
def __init__(self, dataset_dir, train_dataset_name, test_dataset_name, img_norm_cfg=None):
super(InferenceSetLoader).__init__()
self.dataset_dir = dataset_dir + '/' + test_dataset_name
with open(self.dataset_dir + '/img_idx/test_' + test_dataset_name + '.txt', 'r') as f:
self.test_list = f.read().splitlines()
if img_norm_cfg == None:
self.img_norm_cfg = get_img_norm_cfg(train_dataset_name, dataset_dir)
else:
self.img_norm_cfg = img_norm_cfg
def __getitem__(self, idx):
try:
img = Image.open((self.dataset_dir + '/images/' + self.test_list[idx] + '.png').replace('//','/')).convert('I')
except:
img = Image.open((self.dataset_dir + '/images/' + self.test_list[idx] + '.bmp').replace('//','/')).convert('I')
img = Normalized(np.array(img, dtype=np.float32), self.img_norm_cfg)
h, w = img.shape
img = PadImg(img)
img = img[np.newaxis,:]
img = torch.from_numpy(np.ascontiguousarray(img))
return img, [h,w], self.test_list[idx]
def __len__(self):
return len(self.test_list)
class EvalSetLoader(Dataset):
def __init__(self, dataset_dir, mask_pred_dir, test_dataset_name, model_name):
super(EvalSetLoader).__init__()
self.dataset_dir = dataset_dir
self.mask_pred_dir = mask_pred_dir
self.test_dataset_name = test_dataset_name
self.model_name = model_name
with open(self.dataset_dir+'/img_idx/test_' + test_dataset_name + '.txt', 'r') as f:
self.test_list = f.read().splitlines()
def __getitem__(self, idx):
mask_pred = Image.open((self.mask_pred_dir + self.test_dataset_name + '/' + self.model_name + '/' + self.test_list[idx] + '.png').replace('//','/'))
mask_gt = Image.open(self.dataset_dir + '/masks/' + self.test_list[idx] + '.png')
mask_pred = np.array(mask_pred, dtype=np.float32) / 255.0
mask_gt = np.array(mask_gt, dtype=np.float32) / 255.0
if len(mask_pred.shape) == 3:
mask_pred = mask_pred[:,:,0]
h, w = mask_pred.shape
mask_pred, mask_gt = mask_pred[np.newaxis,:], mask_gt[np.newaxis,:]
mask_pred = torch.from_numpy(np.ascontiguousarray(mask_pred))
mask_gt = torch.from_numpy(np.ascontiguousarray(mask_gt))
return mask_pred, mask_gt, [h,w]
def __len__(self):
return len(self.test_list)
class augumentation(object):
def __call__(self, input, target):
if random.random()<0.5:
input = input[::-1, :]
target = target[::-1, :]
if random.random()<0.5:
input = input[:, ::-1]
target = target[:, ::-1]
if random.random()<0.5:
input = input.transpose(1, 0)
target = target.transpose(1, 0)
return input, target