-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
40 lines (36 loc) · 1.27 KB
/
datasets.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
import torch
from torch.utils.data import Dataset
from PIL import Image
import os
class ChestXrayDataSet(Dataset):
def __init__(self, data_dir, image_list_file, transform=None):
'''
data_dir: path to image directory.
image_list_file: path to the file containing images with corresponding labels.
transform: optional transform to be applied on a sample.
'''
image_names = []
labels = []
with open(image_list_file, 'r') as f:
for line in f:
items = line.split()
image_name = os.path.join(data_dir, items[0])
image_names.append(image_name)
label = [int(i) for i in items[1:]]
labels.append(label)
self.image_names = image_names
self.labels = labels
self.transform = transform
def __getitem__(self, index):
'''
index: the index of item
Return image and its labels
'''
image_name = self.image_names[index]
image = Image.open(image_name).convert('RGB')
label = self.labels[index]
if self.transform:
image = self.transform(image)
return image, torch.FloatTensor(label)
def __len__(self):
return len(self.image_names)