-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdataset.py
151 lines (136 loc) · 5.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
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
import os
import numpy as np
from PIL import Image
import torch.utils.data as data
from torchvision import transforms
import custom_transforms as tr
import tifffile as tiff
import math
class potsdam(data.Dataset):
def __init__(self, base_dir='./data/', train=True, dataset='vaihingen', crop_szie=None, val_full_img=False):
super(potsdam, self).__init__()
self.dataset_dir = base_dir
self.train = train
self.dataset = dataset
self.val_full_img = val_full_img
self.images = []
self.labels = []
self.names = []
if crop_szie is None:
crop_szie = [512, 512]
self.crop_size = crop_szie
if train:
self.image_dir = os.path.join(self.dataset_dir, self.dataset + '/images')
self.label_dir = os.path.join(self.dataset_dir, self.dataset + '/annotations')
txt = os.path.join(self.label_dir, 'train.txt')
else:
self.image_dir = os.path.join(self.dataset_dir, self.dataset + '/images')
self.label_dir = os.path.join(self.dataset_dir, self.dataset + '/annotations')
txt = os.path.join(self.label_dir, 'test.txt')
with open(txt, "r") as f:
self.filename_list = f.readlines()
for filename in self.filename_list:
image = os.path.join(self.image_dir, filename.strip() + '.tif')
label = os.path.join(self.label_dir, 'labels/' + filename.strip() + '.png')
image = tiff.imread(image)
label = Image.open(label)
label = np.array(label)
if self.val_full_img:
self.images.append(image)
self.labels.append(label)
self.names.append(filename.strip())
else:
slide_crop(image, self.crop_size, self.images)
slide_crop(label, self.crop_size, self.labels)
assert(len(self.images) == len(self.labels))
def __len__(self):
return len(self.images)
def __getitem__(self, index):
sample = {'image': self.images[index], 'label': self.labels[index]}
sample = self.transform(sample)
if self.val_full_img:
sample['name'] = self.names[index]
return sample
def transform(self, sample):
if self.train:
composed_transforms = transforms.Compose([
tr.RandomHorizontalFlip(),
tr.RandomVerticalFlip(),
tr.RandomScaleCrop(base_size=self.crop_size, crop_size=self.crop_size),
tr.ToTensor(add_edge=False),
])
else:
composed_transforms = transforms.Compose([
tr.ToTensor(add_edge=False),
])
return composed_transforms(sample)
def __str__(self):
return 'dataset:{} train:{}'.format(self.dataset, self.train)
def slide_crop(image, crop_size, image_patches):
"""images shape [h, w, c]"""
if len(image.shape) == 2:
image = np.expand_dims(image, axis=2)
stride_rate = 1.0 / 2.0
h, w, c = image.shape
H, W = crop_size
stride_h = int(H * stride_rate)
stride_w = int(W * stride_rate)
assert h >= crop_size[0] and w >= crop_size[1]
h_grids = int(math.ceil(1.0 * (h - H) / stride_h)) + 1
w_grids = int(math.ceil(1.0 * (w - W) / stride_w)) + 1
for idh in range(h_grids):
for idw in range(w_grids):
h0 = idh * stride_h
w0 = idw * stride_w
h1 = min(h0 + H, h)
w1 = min(w0 + W, w)
if h1 == h and w1 != w:
crop_img = image[h - H:h, w0:w0 + W, :]
if w1 == w and h1 != h:
crop_img = image[h0:h0 + H, w - W:w, :]
if h1 == h and w1 == w:
crop_img = image[h - H:h, w - W:w, :]
if w1 != w and h1 != h:
crop_img = image[h0:h0 + H, w0:w0 + W, :]
crop_img = crop_img.squeeze()
image_patches.append(crop_img)
def label_to_RGB(image):
RGB = np.zeros(shape=[image.shape[0], image.shape[1], 3], dtype=np.uint8)
index = image == 0
RGB[index] = np.array([255, 255, 255])
index = image == 1
RGB[index] = np.array([0, 0, 255])
index = image == 2
RGB[index] = np.array([0, 255, 255])
index = image == 3
RGB[index] = np.array([0, 255, 0])
index = image == 4
RGB[index] = np.array([255, 255, 0])
index = image == 5
RGB[index] = np.array([255, 0, 0])
return RGB
if __name__ == '__main__':
from torch.utils.data import DataLoader
import matplotlib.pyplot as plt
Potsdam_train = potsdam(train=True, dataset='vaihingen')
dataloader = DataLoader(Potsdam_train, batch_size=1, shuffle=False, num_workers=1)
# print(dataloader)
for ii, sample in enumerate(dataloader):
im = sample['label'].numpy().astype(np.uint8)
pic = sample['image'].numpy().astype(np.uint8)
print(im.shape)
im = np.squeeze(im, axis=0)
pic = np.squeeze(pic, axis=0)
print(im.shape)
im = np.transpose(im, axes=[1, 2, 0])[:, :, 0:3]
pic = np.transpose(pic, axes=[1, 2, 0])[:, :, 0:3]
print(im.shape)
im = np.squeeze(im, axis=2)
# print(im)
im = label_to_RGB(im)
plt.imshow(pic)
plt.show()
plt.imshow(im)
plt.show()
if ii == 10:
break