-
Notifications
You must be signed in to change notification settings - Fork 5
/
dataset.py
executable file
·44 lines (31 loc) · 1.28 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
import os
import warnings
import pandas as pd
from PIL import Image, ImageFile
from torch.utils.data import Dataset
ImageFile.LOAD_TRUNCATED_IMAGES = True
warnings.filterwarnings("ignore")
class FocusDataset(Dataset):
def __init__(self, csv_file, root_dir, transform):
self.frame = pd.read_csv(csv_file, header=None)
self.root_dir = root_dir
self.transform = transform
if csv_file.find("TCGA@Focus") != -1:
pass
elif csv_file.find("FocusPath_full") != -1:
for idx in range(len(self.frame)):
self.frame.iloc[idx, 0] = self.frame.iloc[idx, 0][:-4] + ".png"
elif csv_file.find("FocusPath") != -1:
for idx in range(len(self.frame)):
self.frame.iloc[idx, 0] = self.frame.iloc[idx, 0] + ".png"
else:
raise Exception(f"Cannot not handle dataset {root_dir}")
def __len__(self):
return len(self.frame)
def __getitem__(self, idx):
img_name = os.path.join(self.root_dir, self.frame.iloc[idx, 0])
image = Image.open(img_name)
image = self.transform(image)
score = abs(self.frame.iloc[idx, -1])
sample = {'image': image, 'score': score, 'image_name': img_name, 'patch_num': image.shape[0]}
return sample