-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
228 lines (172 loc) · 6.17 KB
/
test.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
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python
# coding: utf-8
# In[7]:
#!/usr/bin/env python
# coding: utf-8
import os
import glob
import time
import cv2
from PIL import Image
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from tqdm.notebook import tqdm
import torch.nn as nn
import json
from sklearn.metrics import log_loss
import pdb
import random as rn
from model import DFModel_var, DFModel_TSN
from utils import *
from torchvision import models
import torch.nn as nn
import torch
import torch.optim as optim
import numpy as np
from dataset import *
from scipy.io import savemat, loadmat
from math import acos, degrees
torch.backends.cudnn.benchmark=True
# Hyperparameters
batch_size = 16
device = 'cuda'
VAL_HR = 256
INTERVAL= 4
WIDTH=4
BANDS = 172
SIGMA = 0.0
TARGET_List =['farm', 'city', 'lake', 'mou']
CR = 15
def loadTxt(fn):
a = []
with open(fn, 'r',encoding="utf-8") as fp:
data = fp.readlines()
for item in data:
fn = item.strip('\n')
a.append(fn)
return a
def awgn(x, snr):
snr = 10**(snr/10.0)
xpower = torch.sum(x**2)/x.numel()
npower = torch.sqrt(xpower / snr)
return x + torch.randn(x.shape).cuda() * npower
# Loading all files in quene
GTs = {}
for TARGET in TARGET_List:
## Load test files from specified text with TARGET name (you can replace it with other path u want)
valfn = loadTxt('data/val_%s.txt' % TARGET)
GTs[TARGET] = {}
for fn in tqdm(valfn, total=len(valfn)):
bn = os.path.basename(fn)
GTs[TARGET][bn] = np.load('../HSDCR/'+fn).astype(np.float)
print("Data preparation done!")
# In[10]:
os.environ["CUDA_VISIBLE_DEVICES"]="3"
#device='cuda'
CR = 15
EP = 16150
SOURCE='lake'
TARGET='city'
#ckpt= '../%s/DCSN_%s_%s_cr_%d_epoch_%d.pth' % (SOURCE, SOURCE, TARGET, CR, EP)
ckpt = 'best/exp__lake_city_cr_15_best2.pth'
#state_dict = torch.load(ckpt, map_location=device)['dict']
state_dict = torch.load(ckpt, map_location=torch.device('cuda'))['dict']
## Model loading
model = DFModel_TSN(cr=CR, stream=3)
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
# load params
model.load_state_dict(new_state_dict)
model.eval().to(device)
def psnr(x,y):
bands = x.shape[2]
x = np.reshape(x, [-1, bands])
y = np.reshape(y, [-1, bands])
msr = np.mean((x-y)**2, 0)
maxval = np.max(y, 0)**2
return np.mean(10*np.log10(maxval/msr))
def sam(x, y):
num = np.sum(np.multiply(x, y), 2)
den = np.sqrt(np.multiply(np.sum(x**2, 2), np.sum(y**2, 2)))
sam = np.sum(np.degrees(np.arccos(num / den))) / (x.shape[1]*x.shape[0])
return sam
def rmse(x, y):
aux = (np.sum((y-x)**2, (0,1))) / (x.shape[0]*x.shape[1])
r = np.sqrt(np.mean(aux))
return r
def ERGAS(x, y, Resize_fact=4):
err = y-x
ergas=0
for i in range(y.shape[-1]):
ergas += np.mean(np.power(err[:,:,i],2)) / np.mean(y[:,:,i])**2
ergas = (100.0/Resize_fact) * np.sqrt(1.0/y.shape[-1] * ergas)
return ergas
index = {}
for TARGET in TARGET_List:
index[TARGET]={}
for TARGET in TARGET_List:
## Load test files from specified text with TARGET name (you can replace it with other path u want)
valfn = loadTxt('data/val_%s.txt' % TARGET)
## Setup the dataloader
val_loader = torch.utils.data.DataLoader(dataset_h5(valfn, mode='Validation',root='../HSDCR/'),
batch_size=5, shuffle=False, pin_memory=True, drop_last=False)
with torch.no_grad():
rmses, sams, fnames, psnrs,ergass = [], [], [],[], []
ep = 0
for ind2, (vx, vfn) in tqdm(enumerate(val_loader), total=len(val_loader)):
model.eval()
vx = vx.view(vx.size()[0]*vx.size()[1], vx.size()[2], vx.size()[3], vx.size()[4])
vx= vx.to(device).permute(0,3,1,2).float()
start_time = time.time()
val_dec,_,_,_,_ = model(vx)
ep += (time.time()-start_time)
# if SIGMA>0:
# val_dec = model(awgn(model(vx, mode=1), 30), mode=2)
# else:
# val_dec,_ = model(vx)
## Recovery to image HSI
val_batch_size = len(vfn)
img = [np.zeros((VAL_HR, VAL_HR, BANDS)) for _ in range(val_batch_size)]
val_dec = val_dec.permute(0,2,3,1).cpu().numpy()
cnt = 0
for bt in range(val_batch_size):
for z in range(0, VAL_HR, INTERVAL):
img[bt][:,z:z+WIDTH,:] = val_dec[cnt]
cnt +=1
save_path = vfn[bt].split('/')
save_path = save_path[-2] + '-' + save_path[-1]
# save_path = save_path[-1]
# np.save('Rec/%s.npy' % (save_path), img[bt])
# if 'Florida_65npy' in vfn[bt]:
# pdb.set_trace()
bn = os.path.basename(vfn[bt])
GT = GTs[TARGET][bn]
maxv, minv=np.max(GT), np.min(GT)
img[bt] = img[bt]*(maxv-minv) + minv ## De-normalization
savemat('Rec/%s.mat' % save_path, {'rec':img[bt]})
sams.append(sam(GT,img[bt]))
psnrs.append(psnr(GT,img[bt]))
rmses.append(rmse(GT,img[bt]))
ergass.append(ERGAS(GT,img[bt]))
fnames.append(save_path)
ep = ep / len(sams)
print('%s in cr=%d: psnr/rmse/sam/ERGAS: %.3f / %.3f / %.3f /%.3f, AVG-Time: %.3f' %
(TARGET, CR, np.mean(psnrs), np.mean(rmses), np.mean(sams), np.mean(ergass), ep))
index[TARGET]['sam'] = sams
index[TARGET]['psnr'] = psnrs
index[TARGET]['rmse'] = rmses
index[TARGET]['ergas'] = ergass
# In[10]:
s1, p1, r1, e1 = [],[],[],[]
for TARGET in TARGET_List:
s1.append(np.mean(index[TARGET]['sam'] ))
p1.append(np.mean(index[TARGET]['psnr']))
r1.append(np.mean(index[TARGET]['rmse'] ))
e1.append(np.mean(index[TARGET]['ergas'] ))
print('All PSNR/RMSR/SAM/ERGAS: %.3f/%.3f/%.3f/%.3f' % (np.mean(p1), np.mean(r1), np.mean(s1), np.mean(e1)))
print('time', ep)
# In[ ]: