-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
116 lines (86 loc) · 3.27 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
import os
import numpy as np
import torch
import random
import sys
from torch.utils import data
from skimage import io
from torchvision import transforms
import PIL
from PIL import Image
root = '/mnt/DADOS_PONTOISE_1/matheusp/imbalanced/'
# Class that reads a sequence of image paths from a text file and creates a data.Dataset with them.
class ListDataset(data.Dataset):
def __init__(self, mode, dataset, fold, new_data_size, transform, A_transform=None):
# Initializing variables.
self.mode = mode
self.dataset = dataset
self.fold = fold
self.new_data_size = new_data_size
self.A_transform = A_transform
self.transform = transform
# Creating list of paths.
self.imgs = self.make_dataset()
def make_dataset(self):
items = []
if self.fold is not None:
data_list = [l.strip('\n') for l in open(os.path.join(root, self.dataset, self.mode+'_fold'+str(self.fold)+'.txt')).readlines()]
else:
data_list = [l.strip('\n') for l in open(os.path.join(root, self.dataset, self.mode+'.txt')).readlines()]
# Creating list containing image and ground truth paths.
new_data = 0
for it in data_list:
if it[0].isdigit():
new_data += 1
if new_data <= self.new_data_size:
item = (os.path.join(root, self.dataset, 'images', it), os.path.join(root, self.dataset, 'masks', it.replace('.tif','.png')))
items.append(item)
else:
item = (os.path.join(root, self.dataset, 'images', it), os.path.join(root, self.dataset, 'masks', it.replace('.tif','.png')))
items.append(item)
# Returning list.
return items
def old__getitem__(self, index):
# Reading items from list.
img_path, msk_path = self.imgs[index]
# Reading images.
img = io.imread(img_path)
msk = io.imread(msk_path)
# Casting images to the appropriate dtypes.
img = img.astype(np.uint8)
msk = msk.astype(np.int64)
msk = msk-1
# Splitting path.
spl = img_path.split("/")
# Turning to tensors.
img = Image.fromarray(img)
msk = torch.from_numpy(msk)
img = self.transform(img)
print(img)
print(msk)
sys.exit()
# Returning to iterator.
return img, msk, spl[-1]
def __getitem__(self, index):
# Reading items from list.
img_path, msk_path = self.imgs[index]
# Reading images.
img = io.imread(img_path)
msk = io.imread(msk_path)
# Casting images to the appropriate dtypes.
img = img.astype(np.float32)/255.0
msk = msk.astype(np.int64)
msk = msk-1
# Splitting path.
spl = img_path.split("/")
# Turning to tensors.
if self.A_transform is not None:
augmented = self.A_transform(image=img, mask=msk)
img = augmented['image']
msk = augmented['mask']
img = self.transform(img)
msk = torch.from_numpy(msk.astype(np.int64))
# Returning to iterator.
return img, msk, spl[-1]
def __len__(self):
return len(self.imgs)