-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_loader.py
164 lines (125 loc) · 5.26 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
from torch.utils import data
from torchvision import transforms as T
from PIL import Image
import os
import numpy as np
import pickle
def save(a, filename):
with open(f'{filename}.pickle', 'wb') as handle:
pickle.dump(a, handle, protocol=pickle.HIGHEST_PROTOCOL)
def load(filename):
with open(f'{filename}.pickle', 'rb') as handle:
b = pickle.load(handle)
return b
def crop_top(img, percent=0.15):
offset = int(img.shape[0] * percent)
return img[offset:]
def central_crop(img):
size = min(img.shape[0], img.shape[1])
offset_h = int((img.shape[0] - size) / 2)
offset_w = int((img.shape[1] - size) / 2)
return img[offset_h:offset_h + size, offset_w:offset_w + size]
class Covid(data.Dataset):
"""Dataset class for the Covid dataset."""
def __init__(self, image_dir, transform, mode):
"""Initialize and preprocess the Covid dataset."""
self.image_dir = image_dir
self.transform = transform
self.mode = mode
self.datasetA = []
self.datasetB = []
self.preprocess()
if mode == 'train':
self.num_images = len(self.datasetA) + len(self.datasetB)
else:
self.num_images = max(len(self.datasetA), len(self.datasetB))
def preprocess(self):
if self.mode in ['train', 'test2'] :
pos = load(os.path.join("data", "covid", "train_pos"))
neg = load(os.path.join("data", "covid", "train_neg"))
neg_mixed = load(os.path.join("data", "covid", "train_neg_mixed"))
self.datasetA = pos + neg_mixed
self.datasetB = neg
else:
self.datasetA = load(os.path.join("data", "covid", "test_pos"))
self.datasetB = load(os.path.join("data", "covid", "test_neg"))
print('Finished preprocessing the COVID dataset...')
def __getitem__(self, index):
"""Return one image and its corresponding attribute label."""
datasetA = self.datasetA
datasetB = self.datasetB
filenameA = datasetA[index%len(datasetA)]
filenameB = datasetB[index%len(datasetB)]
if self.mode in ['train']:
imageA = Image.open(os.path.join(self.image_dir, 'train', filenameA)).convert("RGB")
imageB = Image.open(os.path.join(self.image_dir, 'train', filenameB)).convert("RGB")
else:
imageA = Image.open(os.path.join(self.image_dir, 'test', filenameA)).convert("RGB")
imageB = Image.open(os.path.join(self.image_dir, 'test', filenameB)).convert("RGB")
imageA = np.array(imageA)
imageA = crop_top(imageA, 0.08)
imageA = central_crop(imageA)
imageB = np.array(imageB)
imageB = crop_top(imageB, 0.08)
imageB = central_crop(imageB)
imageA = Image.fromarray(imageA)
imageB = Image.fromarray(imageB)
return self.transform(imageA), self.transform(imageB)
def __len__(self):
"""Return the number of images."""
return self.num_images
class TestValid(data.Dataset):
"""Dataset class for the Covid dataset."""
def __init__(self, image_dir, transform, mode):
"""Initialize and preprocess the Covid dataset."""
self.image_dir = image_dir
self.transform = transform
self.mode = mode
self.datasetA = []
self.datasetB = []
self.preprocess()
if "ano" in self.mode:
self.num_images = len(self.datasetA)
elif "hea" in self.mode:
self.num_images = len(self.datasetB)
def preprocess(self):
self.datasetA = load(os.path.join("data", "covid", "test_pos"))
self.datasetB = load(os.path.join("data", "covid", "test_neg"))
print(f'Finished preprocessing the COVID dataset for {self.mode} ...')
def __getitem__(self, index):
"""Return one image and its corresponding attribute label."""
if "ano" in self.mode:
dataset = self.datasetA
else:
dataset = self.datasetB
filename = dataset[index%len(dataset)]
image = Image.open(os.path.join(self.image_dir, 'test', filename)).convert("RGB")
image = np.array(image)
image = crop_top(image, 0.08)
image = central_crop(image)
image = Image.fromarray(image)
return self.transform(image)
def __len__(self):
"""Return the number of images."""
return self.num_images
def get_loader(image_dir, image_size=256, batch_size=1, dataset='Covid', mode='train', num_workers=1):
"""Build and return a data loader."""
transform = []
if mode == 'train':
transform.append(T.RandomHorizontalFlip())
transform.append(T.Resize(image_size))
transform.append(T.ToTensor())
transform.append(T.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)))
transform = T.Compose(transform)
if dataset == 'Covid':
dataset = Covid(image_dir, transform, mode)
elif dataset == 'TestValid':
dataset = TestValid(image_dir, transform, mode)
else:
print("Dataset not found!")
exit()
data_loader = data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=(mode=='train'),
num_workers=num_workers)
return data_loader