-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
182 lines (137 loc) · 5.89 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
import numpy as np
import os
import time
import glob
import math
from PIL import Image
import random
from torch.utils.data import Dataset
EXTENSIONS = ['.png']
def load_image(file):
return Image.open(file)
def is_image(filename):
return any(filename.endswith(ext) for ext in EXTENSIONS)
def image_path(root, basename, extension):
flnm = "{bs}{ext}".format(bs = basename, ext = extension)
return os.path.join(root, flnm)
#return os.path.join(root, f'{basename}{extension}')
def image_basename(filename):
return os.path.basename(os.path.splitext(filename)[0])
class NeuronEM2012(Dataset):
def __init__(self, root, input_transform=None, target_transform=None, cvParam = 0.75, Train = True):
self.images_root = os.path.join(root, 'images')
self.labels_root = os.path.join(root, 'labels')
self.filenames = []
all_file = os.path.join(self.images_root, '*.png')
filename = glob.glob(all_file)
numel = len(filename)
print('reacched here!')
print(math.floor(numel*cvParam))
if Train:
for i in range(0, math.floor(numel*cvParam)):
self.filenames.append(image_basename(filename[i]))
else:
for i in range(math.floor(numel*cvParam)+1, numel):
self.filenames.append(image_basename(filename[i]))
self.filenames.sort()
self.input_transform = input_transform
self.target_transform = target_transform
def __getitem__(self, index):
filename = self.filenames[index]
with open(image_path(self.images_root, filename, '.png'), 'rb') as f:
image = load_image(f).convert('RGB')
separate = filename.split('_')
#separate[1] = 'manual1'
#conv = '_'.join(separate)
with open(image_path(self.labels_root, separate[0], '_manual1.png'), 'rb') as f:
label = load_image(f).convert('L')
# apply extra augmentation (same random operation on both image and label)
# horizontal flip
image = image.resize((512,512), resample=Image.BICUBIC)
label = label.resize((512,512), resample=Image.NEAREST)
flip_prob = random.random()
if flip_prob<0.5:
image.transpose(Image.FLIP_LEFT_RIGHT)
label.transpose(Image.FLIP_LEFT_RIGHT)
# rotation:
deg = random.randint(-90, 90)
image = image.rotate(deg, resample=Image.BICUBIC)
label = label.rotate(deg, resample=Image.NEAREST)
if self.input_transform is not None:
image = self.input_transform(image)
if self.target_transform is not None:
label = self.target_transform(label)
return image, label
def __len__(self):
return len(self.filenames)
class Glands(Dataset):
def __init__(self, root, input_transform=None, target_transform=None, cvParam = 0.8, Train = True):
self.images_root = os.path.join(root, 'images')
self.labels_root = os.path.join(root, 'labels')
self.filenames = []
all_file = os.path.join(self.images_root, '*.png')
filename = sorted(glob.glob(all_file))
numel = len(filename)
#print('hello!')
#print(numel)
#print(cvParam)
#print(math.floor(numel*cvParam))
#print('bye!')
if Train:
for i in range(0,math.floor(numel*cvParam)):
self.filenames.append(image_basename(filename[i]))
else:
for i in range(math.floor(numel*cvParam)+1, numel):
self.filenames.append(image_basename(filename[i]))
self.filenames.sort()
self.input_transform = input_transform
self.target_transform = target_transform
def __getitem__(self, index):
filename = self.filenames[index]
with open(image_path(self.images_root, filename, '.png'), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path(self.labels_root, filename, '_1stHO.png'), 'rb') as f:
label = load_image(f).convert('L')
# apply extra augmentation (same random operation on both image and label)
image = image.resize((960,960), resample=Image.BICUBIC)
label = label.resize((960,960), resample=Image.NEAREST)
# horizontal flip
flip_prob = random.random()
if flip_prob<0.5:
image.transpose(Image.FLIP_LEFT_RIGHT)
label.transpose(Image.FLIP_LEFT_RIGHT)
# rotation:
deg = random.randint(-90, 90)
image = image.rotate(deg, resample=Image.BICUBIC)
label = label.rotate(deg, resample=Image.NEAREST)
if self.input_transform is not None:
image = self.input_transform(image)
if self.target_transform is not None:
label = self.target_transform(label)
return image, label
def __len__(self):
return len(self.filenames)
'''
class VOC12(Dataset):
def __init__(self, root, input_transform=None, target_transform=None):
self.images_root = os.path.join(root, 'images')
self.labels_root = os.path.join(root, 'labels')
self.filenames = [image_basename(f)
for f in os.listdir(self.labels_root) if is_image(f)]
self.filenames.sort()
self.input_transform = input_transform
self.target_transform = target_transform
def __getitem__(self, index):
filename = self.filenames[index]
with open(image_path(self.images_root, filename, '.jpg'), 'rb') as f:
image = load_image(f).convert('RGB')
with open(image_path(self.labels_root, filename, '.png'), 'rb') as f:
label = load_image(f).convert('P')
if self.input_transform is not None:
image = self.input_transform(image)
if self.target_transform is not None:
label = self.target_transform(label)
return image, label
def __len__(self):
return len(self.filenames)
'''