-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataset_old.py
176 lines (138 loc) · 6.34 KB
/
dataset_old.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
import glob
import os
from typing import Any, Callable, List, Optional, Tuple
import torch
from PIL import Image
from torchvision.datasets import VisionDataset
from torchvision.transforms import functional as F
from base_dataset import get_params, get_transform
class MultipleDataset(VisionDataset):
SUBFOLDERS = ("cloudy", "clear")
def __init__(
self,
root: str,
band: int,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
transforms: Optional[Callable] = None,
) -> None:
super().__init__(root, transform, target_transform, transforms)
self.band = band
cloudy_0_rgb_pathname = os.path.join(
root, self.SUBFOLDERS[0], "*_0.jpg")
cloudy_1_rgb_pathname = os.path.join(
root, self.SUBFOLDERS[0], "*_1.jpg")
cloudy_2_rgb_pathname = os.path.join(
root, self.SUBFOLDERS[0], "*_2.jpg")
cloudy_0_ir_pathname = os.path.join(
root, self.SUBFOLDERS[0], "*_0_ir.jpg")
cloudy_1_ir_pathname = os.path.join(
root, self.SUBFOLDERS[0], "*_1_ir.jpg")
cloudy_2_ir_pathname = os.path.join(
root, self.SUBFOLDERS[0], "*_2_ir.jpg")
clear_pathname = os.path.join(root, self.SUBFOLDERS[1], "*")
self.cloudy_0_rgb_paths = sorted(glob.glob(cloudy_0_rgb_pathname))
self.cloudy_1_rgb_paths = sorted(glob.glob(cloudy_1_rgb_pathname))
self.cloudy_2_rgb_paths = sorted(glob.glob(cloudy_2_rgb_pathname))
self.cloudy_0_ir_paths = sorted(glob.glob(cloudy_0_ir_pathname))
self.cloudy_1_ir_paths = sorted(glob.glob(cloudy_1_ir_pathname))
self.cloudy_2_ir_paths = sorted(glob.glob(cloudy_2_ir_pathname))
self.clear_paths = sorted(glob.glob(clear_pathname))
def __getitem__(self, index: int) -> Tuple[Any, Any]:
cloudy_0_rgb = Image.open(
self.cloudy_0_rgb_paths[index]).convert('RGB')
cloudy_1_rgb = Image.open(
self.cloudy_1_rgb_paths[index]).convert('RGB')
cloudy_2_rgb = Image.open(
self.cloudy_2_rgb_paths[index]).convert('RGB')
if self.band == 4:
cloudy_0_ir = Image.open(
self.cloudy_0_ir_paths[index]).convert('RGB')
cloudy_1_ir = Image.open(
self.cloudy_1_ir_paths[index]).convert('RGB')
cloudy_2_ir = Image.open(
self.cloudy_2_ir_paths[index]).convert('RGB')
else:
pass
clear = Image.open(self.clear_paths[index]).convert('RGB')
params = get_params(size=clear.size)
transform_params = get_transform(self.band, params)
cloudy_0_rgb_tensor = transform_params(cloudy_0_rgb)
cloudy_1_rgb_tensor = transform_params(cloudy_1_rgb)
cloudy_2_rgb_tensor = transform_params(cloudy_2_rgb)
if self.band == 4:
cloudy_0_ir_tensor = transform_params(cloudy_0_ir)[:1, ...]
cloudy_1_ir_tensor = transform_params(cloudy_1_ir)[:1, ...]
cloudy_2_ir_tensor = transform_params(cloudy_2_ir)[:1, ...]
else:
cloudy_0_ir_tensor = None
cloudy_1_ir_tensor = None
cloudy_2_ir_tensor = None
clear_tensor = transform_params(clear)
cloudy_0 = torch.cat(
[i for i in [cloudy_0_rgb_tensor, cloudy_0_ir_tensor] if i is not None])
cloudy_1 = torch.cat(
[i for i in [cloudy_1_rgb_tensor, cloudy_1_ir_tensor] if i is not None])
cloudy_2 = torch.cat(
[i for i in [cloudy_2_rgb_tensor, cloudy_2_ir_tensor] if i is not None])
clear = clear_tensor
return [cloudy_0, cloudy_1, cloudy_2], clear, self.clear_paths[index]
def __len__(self) -> int:
return len(self.clear_paths)
if __name__ == "__main__":
import matplotlib.pyplot as plt
for item in MultipleDataset(root="./data/multipleImage", band=3):
print(item["cloudy_0"].shape, item["cloudy_0"].dtype)
print(item["cloudy_1"].shape, item["cloudy_1"].dtype)
print(item["cloudy_2"].shape, item["cloudy_2"].dtype)
print(item["clear"].shape, item["clear"].dtype)
print(item["cloudy_0_path"].replace("\\", "/"))
print(item["cloudy_1_path"].replace("\\", "/"))
print(item["cloudy_2_path"].replace("\\", "/"))
plt.figure(figsize=(8, 32), dpi=300)
plt.subplot(1, 4, 1)
plt.title("cloudy_0")
plt.imshow(item["cloudy_0"][:3, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 4, 2)
plt.title("cloudy_1")
plt.imshow(item["cloudy_1"][:3, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 4, 3)
plt.title("cloudy_2")
plt.imshow(item["cloudy_2"][:3, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 4, 4)
plt.title("clear")
plt.imshow(item["clear"].permute(1, 2, 0)*0.5+0.5)
plt.savefig("paired.png", bbox_inches="tight")
break
for item in MultipleDataset(root="./data/multipleImage", band=4):
print(item["cloudy_0"].shape, item["cloudy_0"].dtype)
print(item["cloudy_1"].shape, item["cloudy_1"].dtype)
print(item["cloudy_2"].shape, item["cloudy_2"].dtype)
print(item["clear"].shape, item["clear"].dtype)
print(item["cloudy_0_path"].replace("\\", "/"))
print(item["cloudy_1_path"].replace("\\", "/"))
print(item["cloudy_2_path"].replace("\\", "/"))
plt.figure(figsize=(14, 4))
plt.subplot(1, 7, 1)
plt.title("cloudy_0")
plt.imshow(item["cloudy_0"][:3, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 7, 2)
plt.title("cloudy_0_ir")
plt.imshow(item["cloudy_0"][3:, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 7, 3)
plt.title("cloudy_1")
plt.imshow(item["cloudy_1"][:3, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 7, 4)
plt.title("cloudy_1_ir")
plt.imshow(item["cloudy_1"][3:, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 7, 5)
plt.title("cloudy_2")
plt.imshow(item["cloudy_2"][:3, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 7, 6)
plt.title("cloudy_2_ir")
plt.imshow(item["cloudy_2"][3:, ...].permute(1, 2, 0)*0.5+0.5)
plt.subplot(1, 7, 7)
plt.title("clear")
plt.imshow(item["clear"].permute(1, 2, 0)*0.5+0.5)
plt.savefig("paired.png", bbox_inches="tight")
break