-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataloader_rgb.py
169 lines (118 loc) · 4.98 KB
/
dataloader_rgb.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import torch
from torch.utils.data import Dataset
from torch.utils.data import DataLoader
import cv2
import os
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import numpy as np
import ipdb
import time
from itertools import cycle
import sys
from torch.utils.data.sampler import RandomSampler
from skimage import io
import skimage
from skimage import util
from skimage import img_as_float
from torchvision import transforms
# DATA_DIR = '../data/sub_vol_outputs_slices/'
COLOR_DIR = '/color_slices/'
SCAN_DIR = '/scan_slices/'
transform = transforms.Compose([transforms.ToTensor()])
def process_images(DATA_DIR ,image, OUT_TYPE_DIR, color=True):
"""Processes images in color volumes and converts to LAB color space
:param: image_names : list of names of images
:param: vols : list of volumes
:returns : images list containing the slices arrays corresponing to volumes
"""
base_dir = DATA_DIR + OUT_TYPE_DIR
if color:
# image = cv2.imread(base_dir + '/' + image)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = img_as_float(io.imread(base_dir + '/' + image))
# print('color:', image.dtype)
# image = image.astype(np.int8)
# image = image[:, :, 1:] # keeping all the channels dropping the luminosity
else:
# image = cv2.imread(base_dir + '/' + image)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = io.imread(base_dir + '/' + image)
image = img_as_float(skimage.color.rgb2gray(image))
# image = util.invert(image)
# print('gray : ', image.dtype)
return image
def generate_train_test_split(DATA_DIR):
color_images = np.array(os.listdir(DATA_DIR + COLOR_DIR))
scan_images = np.array(os.listdir(DATA_DIR + SCAN_DIR))
X_train, X_test, y_train, y_test = train_test_split(scan_images, color_images, test_size=0.2, random_state=123)
return X_train, X_test, y_train, y_test
class EfficientImageDataSet(Dataset):
def __init__(self, X, y, DATA_DIR):
self.X = X
self.y = y
self.DATA_DIR = DATA_DIR
def __getitem__(self, index):
img_name = self.X[index]
x_processed = torch.from_numpy(process_images(self.DATA_DIR, img_name, SCAN_DIR, False)).float()
x_processed = x_processed.unsqueeze(0).permute(1, 2, 0).numpy()
y_processed = torch.from_numpy(process_images(self.DATA_DIR, img_name, COLOR_DIR)).float()
# y_processed = y_processed.unsqueeze(0)
y_processed = y_processed.numpy()
return img_name, transform(x_processed), transform(y_processed)
def __len__(self):
return len(self.X)
class EfficientImageDataTestSet(Dataset):
def __init__(self, X, y, DATA_DIR):
self.X = X
self.y = y
self.DATA_DIR = DATA_DIR
def __getitem__(self, index):
img_name = self.X[index]
x_processed = torch.from_numpy(process_images(self.DATA_DIR, img_name, SCAN_DIR, False)).float()
x_processed = x_processed.unsqueeze(0).permute(1, 2, 0)
y_processed = torch.from_numpy(process_images(self.DATA_DIR, img_name, COLOR_DIR)).float()
y_processed = y_processed.numpy()
x_processed = x_processed.numpy()
return img_name, transform(x_processed), transform(y_processed)
def __len__(self):
return len(self.X)
def create_dataloader(DATA_DIR, X, y, batch_size=1, shuffle=True):
dset = EfficientImageDataSet(X, y, DATA_DIR)
dataloader = DataLoader(dset, batch_size=batch_size, shuffle=shuffle, num_workers=4)
return dataloader
def create_testdataloader(DATA_DIR, X, y, batch_size=1, shuffle=False):
dset = EfficientImageDataTestSet(X, y, DATA_DIR)
random_sampler = RandomSampler(dset)
dataloader = DataLoader(dset, batch_size=batch_size, shuffle=shuffle, num_workers=4, sampler=random_sampler)
return dataloader
if __name__ == '__main__':
data_dir = sys.argv[1]
X_train, X_test, y_train, y_test = generate_train_test_split(data_dir)
print('X train:', X_train.shape)
print('y train:', y_train.shape)
print('X test:', X_test.shape)
print('y test :', y_test.shape)
try_dataloader = create_dataloader(data_dir, X_train, y_train, 2)
for x, y in cycle(try_dataloader):
print(x.shape)
print(y.shape)
# print(y[1].shape)
# print('L channel ', y[:,0,:,:].unsqueeze(1).shape)
# print('AB channel', y[:,1:,:,:].shape)
break
try_dataloader = create_dataloader(data_dir, X_test, y_test)
for x, y in try_dataloader:
print(x.shape)
print(y.shape)
# print(y[1].shape)
break
try_dataloader = create_testdataloader(data_dir, X_test, y_test, 15)
for i, (name, x, y) in enumerate(try_dataloader):
print(i)
print(name)
print(x.shape)
# print(y[0].shape)
print(y.shape)
if i % 2 == 0 and i != 0:
break