forked from noskill/bio-infomax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
99 lines (80 loc) · 2.36 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
import numpy
import cv2
import os
import re
import tiffile
from torch.utils.data import Dataset
def page2array(page):
data = page.asarray()
data = numpy.array(data)
return data
def find_closest(items, size):
result = items[0]
diff = abs(result[0][0] - size)
for item in items[1:]:
t_diff = abs(item[0][0] - size)
if t_diff < diff:
result = item
diff = t_diff
return result
class LargeTifDataset(Dataset):
def __init__(self, length, tifs, transform):
self.tifs = numpy.array(tifs)
self.transform = transform
self.current = []
self.length = length
self.reset()
def __len__(self):
return self.length
def __getitem__(self, idx):
i = idx % len(self.current)
item = self.current[i]
if self.transform is not None:
return self.transform(item)
return item
def reset(self):
self.current = []
while True:
idx = numpy.random.randint(0, len(self.tifs), 3)
if len(set(idx)) == len(idx):
break
for i in idx:
layers = load_tif(self.tifs[i])
item = find_closest(layers, 500)
img = page2array(item[1])
self.current.append(img)
def load_tif(path):
tif = tiffile.TiffFile(path)
biggest = None
b_size = 0
result = []
for page in tif.pages:
size = page.size / 1e6
if b_size < size:
b_size = size
biggest = page
num = '(\d+\.?\d*)'
parse = re.match('level={0}\smag={0}\squality={0}'.format(num), page.description)
if parse is None:
continue
else:
mag = parse.group(2)
result.append(((size, float(mag)), page))
result.sort()
return result
class TinyImageNet(Dataset):
def __init__(self, path):
super().__init__()
class_dirs = os.listdir(path)
self.files = []
for d in class_dirs:
dir_path = os.path.join(path, d, 'images')
for f in os.listdir(dir_path):
self.files.append(os.path.join(dir_path, f))
def __len__(self):
return len(self.files)
def __getitem__(self, idx):
img = cv2.imread(self.files[idx])
img = numpy.moveaxis(img, (0, 1, 2), (1, 2, 0))
img = img / 255.0
return img