-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
139 lines (108 loc) · 4.43 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
# -*- coding: UTF-8 -*-
import torch.utils.data as data
import numpy as np
from os import listdir
from os.path import join
import scipy.io as sio
import random
import torch
import torch.nn.functional as F
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg", ".mat"])
def load_img(filepath):
x = sio.loadmat(filepath)
x = x['pavia']
x = x.astype(float)
x = x / 8000.0
x = torch.tensor(x).float()
# print('hsi.max', x.max())
# print('hsi.min', x.min())
return x
def load_img1(filepath):
x = sio.loadmat(filepath)
x = x['rgb']
x = x.astype(float)
x = x / 8000.0
x = torch.tensor(x).float()
# print('rgb.max', x.max())
# print('rgb.min', x.min())
return x
class DatasetFromFolder(data.Dataset):
def __init__(self, image_dir1, image_dir2, upscale_factor, patch_size,input_transform=None):
super(DatasetFromFolder, self).__init__()
self.patch_size = patch_size
self.image_filenames1 = [join(image_dir1, x) for x in listdir(image_dir1) if is_image_file(x)]
self.image_filenames2 = [join(image_dir2, x) for x in listdir(image_dir2) if is_image_file(x)]
self.lens = 20000
self.xs = []
for img in self.image_filenames1:
self.xs.append(load_img(img))
self.ys = []
for img in self.image_filenames2:
self.ys.append(load_img1(img))
self.upscale_factor = upscale_factor
self.input_transform = input_transform
def __getitem__(self, index):
ind = index % 1
img = self.xs[ind]
img2 = self.ys[ind]
upscale_factor = self.upscale_factor
w = np.random.randint(0, img.shape[0]-self.patch_size)
h = np.random.randint(0, img.shape[1]-self.patch_size)
X = img[w:w+self.patch_size, h:h+self.patch_size, :] # HR-HSI
X_1 = img2[w:w+self.patch_size, h:h+self.patch_size, :] # HR-MSI
X_2 = F.interpolate(X.permute(2, 0, 1).unsqueeze(0), scale_factor=1.0 / upscale_factor, mode='bicubic',align_corners=False, recompute_scale_factor=False).squeeze(0).permute(1, 2, 0) # LR-HSI
Y = F.interpolate(X_1.permute(2, 0, 1).unsqueeze(0), scale_factor=1.0 / upscale_factor, mode='bicubic',align_corners=False, recompute_scale_factor=False).squeeze(0).permute(1, 2, 0) # LR-MSI
rotTimes = random.randint(0, 3)
vFlip = random.randint(0, 1)
hFlip = random.randint(0, 1)
# Random rotation
X = torch.rot90(X, rotTimes, [0, 1])
X_1 = torch.rot90(X_1, rotTimes, [0, 1])
X_2 = torch.rot90(X_2, rotTimes, [0, 1])
Y = torch.rot90(Y, rotTimes, [0, 1])
# Random vertical Flip
for j in range(vFlip):
X = X.flip(1)
X_1 = X_1.flip(1)
X_2 = X_2.flip(1)
Y = Y.flip(1)
# Random Horizontal Flip
for j in range(hFlip):
X = X.flip(0)
X_1 = X_1.flip(0)
X_2 = X_2.flip(0)
Y = Y.flip(0)
X = X.permute(2, 0, 1)
X_1 = X_1.permute(2, 0, 1)
X_2 = X_2.permute(2, 0, 1)
Y = Y.permute(2, 0, 1)
return Y, X_1, X_2, X
def __len__(self):
return self.lens
class DatasetFromFolder2(data.Dataset):
def __init__(self, image_dir1, image_dir2, upscale_factor, input_transform=None):
super(DatasetFromFolder2, self).__init__()
self.image_filenames1 = [join(image_dir1, x) for x in listdir(image_dir1) if is_image_file(x)]
self.image_filenames2 = [join(image_dir2, x) for x in listdir(image_dir2) if is_image_file(x)]
self.upscale_factor = upscale_factor
self.input_transform = input_transform
self.xs = []
self.xs_name = []
for img in self.image_filenames1:
self.xs.append(load_img(img))
self.xs_name.append(img)
self.ys = []
for img in self.image_filenames2:
self.ys.append(load_img1(img))
def __getitem__(self, index):
X = self.xs[index]
Y = self.ys[index]
upscale_factor = self.upscale_factor
X_1 = Y
Y = F.interpolate(X_1.permute(2,0,1).unsqueeze(0), scale_factor=1.0/upscale_factor, mode='bicubic', align_corners=False, recompute_scale_factor=False).squeeze(0).permute(1,2,0)
X = X.permute(2, 0, 1)
Y = Y.permute(2, 0, 1)
return Y, X, self.xs_name[index]
def __len__(self):
return len(self.image_filenames1)