-
Notifications
You must be signed in to change notification settings - Fork 0
/
torch_dataset.py
86 lines (70 loc) · 2.75 KB
/
torch_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
import torch
from torch.utils.data import Dataset
import cv2
import settings
class BCDataset(Dataset):
def __init__(self, ids, tabulars, labels, transforms=None):
self.ids = ids
self.tabulars = tabulars
self.labels = labels
self.transforms = transforms
def __len__(self):
return len(self.ids)
def __getitem__(self, idx):
id = self.ids[idx]
if self.labels is not None:
image_path = str(settings.DATA / 'train_imgs_crop' / f'{id}.png')
else:
image_path = str(settings.DATA / 'test_imgs_crop' / f'{id}.png')
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if self.transforms is not None:
image = self.transforms(image=image)['image']
image = torch.as_tensor(image, dtype=torch.float)
tabular = self.tabulars[idx, :]
tabular = torch.as_tensor(tabular, dtype=torch.float)
if self.labels is not None:
label = self.labels[idx]
label = torch.as_tensor(label, dtype=torch.float)
label = torch.unsqueeze(label, dim=0)
return image, tabular, label
else:
return image, tabular
class BCMILDataset:
def __init__(self, ids, n_instances, labels, transforms=None):
self.ids = ids
self.n_instances = n_instances
self.labels = labels
self.transforms = transforms
def __len__(self):
return len(self.ids)
def __getitem__(self, idx):
id = self.ids[idx]
tiles = []
for tile_idx in range(self.n_instances):
if self.labels is not None:
tile_path = str(settings.DATA / 'train_tiles' / \
f'{id}_tile_{tile_idx}.png')
else:
tile_path = str(settings.DATA / 'test_tiles' / \
f'{id}_tile_{tile_idx}.png')
tile = cv2.imread(tile_path)
tile = cv2.cvtColor(tile, cv2.COLOR_BGR2RGB)
tiles.append(tile)
assert len(tiles) == self.n_instances
if self.transforms is not None:
tiles = [torch.as_tensor(self.transforms(image=tile)[
'image'], dtype=torch.float) for tile in tiles]
tiles = torch.stack(tiles, dim=0)
else:
tiles = [torch.as_tensor(tile, dtype=torch.float)
for tile in tiles]
tiles = torch.stack(tiles, dim=0)
tiles = torch.permute(tiles, dims=(0, 3, 1, 2))
if self.labels is not None:
label = self.labels[idx]
label = torch.as_tensor(label, dtype=torch.float)
label = torch.unsqueeze(label, dim=0)
return tiles, label
else:
return tiles