-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
213 lines (190 loc) · 7.68 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import matplotlib.pyplot as plt
import random
import numpy as np
import os
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import torch
from skimage.io import imread
from skimage.transform import rescale
import matplotlib
#matplotlib.use('Agg')
DEFAULT_PANOPTICS_DIR = "D:/DATA/rawdata/camp"
def toLong(x):
return x.long()
def toFloat(x):
return x.float()
class PanopticsTrainDataset(Dataset):
def __init__(
self,
category,
img_size,
blurr=True
):
self.blurr = blurr
if self.blurr:
print('Data reading with downscaling and blurring of original image!')
if category == 'all':
if os.path.isdir(DEFAULT_PANOPTICS_DIR):
self.img_dir = [
f"{DEFAULT_PANOPTICS_DIR}/{folder}/train/images/"
for folder in os.listdir(DEFAULT_PANOPTICS_DIR)
]
else:
raise RuntimeError("Path to data not found")
self.img_files = []
for path in self.img_dir:
self.img_files += list(
np.random.choice(
[os.path.join(path, img) for img in os.listdir(path)
if (os.path.isfile(os.path.join(path,img))
and imread(os.path.join(path,img)).shape[0] == 256
and imread(os.path.join(path,img)).shape[1] == 256
)
],
size=128)
)
else:
if os.path.isdir(DEFAULT_PANOPTICS_DIR):
self.img_dir = f"{DEFAULT_PANOPTICS_DIR}/{category}/train/images/"
else:
raise RuntimeError("Path to data not found")
self.img_files = list(
np.random.choice(
[os.path.join(self.img_dir, img)
for img in os.listdir(self.img_dir)
if (os.path.isfile(os.path.join(self.img_dir, img))
and imread(os.path.join(self.img_dir, img)).shape[0] == 256
and imread(os.path.join(self.img_dir, img)).shape[1] == 256
)
],
size=128 * 9)
)
print(f'The length of image files is {len(self.img_files)}')
#self.gt_files = [s.replace("/images/", "/labels/") for s in self.img_files]
#self.gt_files = [s.replace("images", "labels") for s in self.img_files]
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(toFloat)
# transforms.ToPILImage(),
# transforms.Resize(size=(img_size, img_size)),
# transforms.PILToTensor(),
# itransforms.Lambda(lambda img: img.float())
])
self.nb_img = len(self.img_files)
def im_blurr(self, img, factor=16):
img_down = rescale(img, 1/factor, channel_axis=-1, anti_aliasing=True)
blur = rescale(img_down, factor, channel_axis=-1, anti_aliasing=True)
assert img.shape == blur.shape
return blur
def __len__(self):
return self.nb_img
def __getitem__(self, index):
# NOTE take all 4 channels (B / G / R / NIR)
index = index % self.nb_img
img = imread(self.img_files[index])[..., :].astype(float)
try:
assert img.shape[0] == 256 and img.shape[1] == 256
except:
raise RuntimeError("Expected (256, 256) images, got",
img.shape[0], img.shape[1])
try:
assert img.shape[2] == 4
except:
raise RuntimeError("Expected 4 channel image, got", img.shape[2])
img = (img - np.amin(img)) / (np.amax(img) - np.amin(img))
#img = Image.fromarray((img * 255).astype(np.uint8))
# some manipulation before visualization: choose BGR and reverse order
# and renormalize
#img_ = np.flip(img[..., :3], axis=-1)
#img_ = (img_ - np.amin(img_)) / (np.amax(img_) - np.amin(img_))
#plt.imshow(img_)
#plt.show()
if self.blurr:
old_shape = img.shape
img = self.im_blurr(img)
new_shape = img.shape
assert old_shape == new_shape, 'original and blurred image shape not matching'
return self.transform(img), 1 # one if the ground truth if there is one
class PanopticsTestDataset(Dataset):
def __init__(
self,
img_size,
category,
fake_dataset_size=None,
with_loc=False,
blurr=True,
scale=16
):
if category == 'all':
self.img_dir = f"{DEFAULT_PANOPTICS_DIR}/Minawao_june_2016/test/images/"
# small validation data to save test time
print('Minawao_june_2016 as train test')
else:
self.img_dir = f"{DEFAULT_PANOPTICS_DIR}/{category}/test/images/"
print(f"Test data set to dir: {category}")
self.blurr = blurr
self.scale = scale
self.img_files = list(
[os.path.join(self.img_dir, img)
for img in os.listdir(self.img_dir)
if (os.path.isfile(os.path.join(self.img_dir, img))
and imread(os.path.join(self.img_dir, img)).shape[0] == 256
and imread(os.path.join(self.img_dir, img)).shape[1] == 256
)
],
)
if fake_dataset_size is not None:
self.img_files = list(
np.random.choice(
self.img_files,
size=fake_dataset_size
)
)
#self.gt_files = [s.replace("/images/", "/labels/") for s in self.img_files]
self.gt_files = [s.replace("images", "labels") for s in self.img_files]
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(toFloat)
#transforms.Resize(size=(img_size, img_size)),
#transforms.PILToTensor(),
#transforms.Lambda(lambda img: img.float())
])
self.transform_gt = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(toLong)
])
self.nb_img = len(self.img_files)
def im_blurr(self, img, factor=16):
img_down = rescale(img, 1 / factor, channel_axis=-1, anti_aliasing=True)
blur = rescale(img_down, factor, channel_axis=-1, anti_aliasing=True)
assert img.shape == blur.shape
return blur
def __len__(self):
return self.nb_img
def __getitem__(self, index):
# NOTE take all 4 channels (B / G / R / NIR)
img = imread(self.img_files[index])[..., :].astype(float)
gt = imread(self.gt_files[index]).astype(float)
try:
assert img.shape[0] == 256 and img.shape[1] == 256
except:
raise RuntimeError("Expected (256, 256) images, got",
img.shape[0], img.shape[1])
try:
assert gt.shape[0] == 256 and gt.shape[1] == 256
except:
raise RuntimeError("Expected (256, 256) ground truths, got",
gt.shape[0], gt.shape[1])
try:
assert gt.ndim == 2
except:
raise RuntimeError("Ground truth is not a binary image with ndim"
+ " == 2, got shape", gt.shape)
img = (img - np.amin(img)) / (np.amax(img) - np.amin(img))
if self.blurr:
img_blurr = self.im_blurr(img=img, factor=self.scale)
return self.transform(img), self.transform_gt(gt), self.transform(img_blurr)
else:
return self.transform(img), self.transform_gt(gt), _