-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpred_SCD.py
232 lines (210 loc) · 10.6 KB
/
pred_SCD.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
225
226
227
228
229
230
231
232
import os
import time
import argparse
import numpy as np
import torch.autograd
from skimage import io, exposure
from torch.nn import functional as F
from torch.utils.data import DataLoader
#################################
from datasets import RS_ST as RS
#from models.BiSRNet import BiSRNet as Net
from models.SSCDl import SSCDl as Net
DATA_NAME = 'ST'
#################################
class PredOptions():
def __init__(self):
"""Reset the class; indicates the class hasn't been initailized"""
self.initialized = False
def initialize(self, parser):
working_path = os.path.dirname(os.path.abspath(__file__))
parser.add_argument('--pred_batch_size', required=False, default=1, help='prediction batch size')
parser.add_argument('--test_dir', required=False, default='/TEST_DIR/', help='directory to test images')
parser.add_argument('--pred_dir', required=False, default='/PRED_DIR/'+DATA_DIR, help='directory to output masks')
parser.add_argument('--chkpt_path', required=False, default=working_path+'/checkpoints/ST/xxx.pth')
self.initialized = True
return parser
def gather_options(self):
if not self.initialized: # check if it has been initialized
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser = self.initialize(parser)
self.parser = parser
return parser.parse_args()
def parse(self):
self.opt = self.gather_options()
return self.opt
def compare_models(model_1, model_2):
models_differ = 0
for key_item_1, key_item_2 in zip(model_1.state_dict().items(), model_2.state_dict().items()):
if torch.equal(key_item_1[1], key_item_2[1]):
pass
else:
models_differ += 1
if (key_item_1[0] == key_item_2[0]):
print('Mismtach found at', key_item_1[0])
else:
raise Exception
if models_differ == 0:
print('Models match perfectly! :)')
def main():
begin_time = time.time()
opt = PredOptions().parse()
net = Net(3, RS.num_classes).cuda()
net.load_state_dict( torch.load(opt.chkpt_path) )
net.eval()
test_set = RS.Data_test(opt.test_dir)
test_loader = DataLoader(test_set, batch_size=opt.pred_batch_size)
predict(net, test_set, test_loader, opt.pred_dir, flip=False, index_map=True, intermediate=False)
#predict_direct(net, test_set, test_loader, opt.pred_dir, flip=False, index_map=True)
time_use = time.time() - begin_time
print('Total time: %.2fs'%time_use)
#For models with 3 outputs: 1 change map + 2 semantic maps.
#Parameters: flip->test time augmentation index_map->"False" means rgb results intermediate->whether to outputs the intermediate maps
def predict(net, pred_set, pred_loader, pred_dir, flip=False, index_map=False, intermediate=False):
pred_A_dir_rgb = os.path.join(pred_dir, 'im1_rgb')
pred_B_dir_rgb = os.path.join(pred_dir, 'im2_rgb')
if not os.path.exists(pred_A_dir_rgb): os.makedirs(pred_A_dir_rgb)
if not os.path.exists(pred_B_dir_rgb): os.makedirs(pred_B_dir_rgb)
if index_map:
pred_A_dir = os.path.join(pred_dir, 'im1')
pred_B_dir = os.path.join(pred_dir, 'im2')
if not os.path.exists(pred_A_dir): os.makedirs(pred_A_dir)
if not os.path.exists(pred_B_dir): os.makedirs(pred_B_dir)
if intermediate:
pred_mA_dir = os.path.join(pred_dir, 'im1_semantic')
pred_mB_dir = os.path.join(pred_dir, 'im2_semantic')
pred_change_dir = os.path.join(pred_dir, 'change')
if not os.path.exists(pred_mA_dir): os.makedirs(pred_mA_dir)
if not os.path.exists(pred_mB_dir): os.makedirs(pred_mB_dir)
if not os.path.exists(pred_change_dir): os.makedirs(pred_change_dir)
for vi, data in enumerate(pred_loader):
imgs_A, imgs_B = data
#imgs = torch.cat([imgs_A, imgs_B], 1)
imgs_A = imgs_A.cuda().float()
imgs_B = imgs_B.cuda().float()
mask_name = pred_set.get_mask_name(vi)
with torch.no_grad():
out_change, outputs_A, outputs_B = net(imgs_A, imgs_B)#,aux
out_change = F.sigmoid(out_change)
if flip:
outputs_A = F.softmax(outputs_A, dim=1)
outputs_B = F.softmax(outputs_B, dim=1)
imgs_A_v = torch.flip(imgs_A, [2])
imgs_B_v = torch.flip(imgs_B, [2])
out_change_v, outputs_A_v, outputs_B_v = net(imgs_A_v, imgs_B_v)
outputs_A_v = torch.flip(outputs_A_v, [2])
outputs_B_v = torch.flip(outputs_B_v, [2])
out_change_v = torch.flip(out_change_v, [2])
outputs_A += F.softmax(outputs_A_v, dim=1)
outputs_B += F.softmax(outputs_B_v, dim=1)
out_change += F.sigmoid(out_change_v)
imgs_A_h = torch.flip(imgs_A, [3])
imgs_B_h = torch.flip(imgs_B, [3])
out_change_h, outputs_A_h, outputs_B_h = net(imgs_A_h, imgs_B_h)
outputs_A_h = torch.flip(outputs_A_h, [3])
outputs_B_h = torch.flip(outputs_B_h, [3])
out_change_h = torch.flip(out_change_h, [3])
outputs_A += F.softmax(outputs_A_h, dim=1)
outputs_B += F.softmax(outputs_B_h, dim=1)
out_change += F.sigmoid(out_change_h)
imgs_A_hv = torch.flip(imgs_A, [2,3])
imgs_B_hv = torch.flip(imgs_B, [2,3])
out_change_hv, outputs_A_hv, outputs_B_hv = net(imgs_A_hv, imgs_B_hv)
outputs_A_hv = torch.flip(outputs_A_hv, [2,3])
outputs_B_hv = torch.flip(outputs_B_hv, [2,3])
out_change_hv = torch.flip(out_change_hv, [2,3])
outputs_A += F.softmax(outputs_A_hv, dim=1)
outputs_B += F.softmax(outputs_B_hv, dim=1)
out_change += F.sigmoid(out_change_hv)
out_change = out_change/4
outputs_A = outputs_A.cpu().detach()
outputs_B = outputs_B.cpu().detach()
change_mask = out_change.cpu().detach()>0.5
change_mask = change_mask.squeeze()
pred_A = torch.argmax(outputs_A, dim=1).squeeze()
pred_B = torch.argmax(outputs_B, dim=1).squeeze()
if intermediate:
pred_A_path = os.path.join(pred_mA_dir, mask_name)
pred_B_path = os.path.join(pred_mB_dir, mask_name)
pred_change_path = os.path.join(pred_change_dir, mask_name)
io.imsave(pred_A_path, RS.Index2Color(pred_A.numpy()))
io.imsave(pred_B_path, RS.Index2Color(pred_B.numpy()))
change_map = exposure.rescale_intensity(change_mask.numpy(), 'image', 'dtype')
io.imsave(pred_change_path, change_map)
pred_A = (pred_A*change_mask.long()).numpy()
pred_B = (pred_B*change_mask.long()).numpy()
pred_A_path = os.path.join(pred_A_dir_rgb, mask_name)
pred_B_path = os.path.join(pred_B_dir_rgb, mask_name)
io.imsave(pred_A_path, RS.Index2Color(pred_A))
io.imsave(pred_B_path, RS.Index2Color(pred_B))
print(pred_A_path)
if index_map:
pred_A_path = os.path.join(pred_A_dir, mask_name)
pred_B_path = os.path.join(pred_B_dir, mask_name)
io.imsave(pred_A_path, pred_A.astype(np.uint8))
io.imsave(pred_B_path, pred_B.astype(np.uint8))
#For models that directly produce 2 SCD maps.
#Parameters: flip->test time augmentation index_map->"False" means rgb results
def predict_direct(net, pred_set, pred_loader, pred_dir, flip=False, index_map=False,):
pred_A_dir_rgb = os.path.join(pred_dir, 'im1_rgb')
pred_B_dir_rgb = os.path.join(pred_dir, 'im2_rgb')
if not os.path.exists(pred_A_dir_rgb): os.makedirs(pred_A_dir_rgb)
if not os.path.exists(pred_B_dir_rgb): os.makedirs(pred_B_dir_rgb)
if index_map:
pred_A_dir = os.path.join(pred_dir, 'im1')
pred_B_dir = os.path.join(pred_dir, 'im2')
if not os.path.exists(pred_A_dir): os.makedirs(pred_A_dir)
if not os.path.exists(pred_B_dir): os.makedirs(pred_B_dir)
for vi, data in enumerate(pred_loader):
imgs_A, imgs_B = data
#imgs = torch.cat([imgs_A, imgs_B], 1)
imgs_A = imgs_A.cuda().float()
imgs_B = imgs_B.cuda().float()
mask_name = pred_set.get_mask_name(vi)
with torch.no_grad():
outputs_A, outputs_B = net(imgs_A, imgs_B)#,aux
if flip:
outputs_A = F.softmax(outputs_A, dim=1)
outputs_B = F.softmax(outputs_B, dim=1)
imgs_A_v = torch.flip(imgs_A, [2])
imgs_B_v = torch.flip(imgs_B, [2])
outputs_A_v, outputs_B_v = net(imgs_A_v, imgs_B_v)
outputs_A_v = torch.flip(outputs_A_v, [2])
outputs_B_v = torch.flip(outputs_B_v, [2])
outputs_A += F.softmax(outputs_A_v, dim=1)
outputs_B += F.softmax(outputs_B_v, dim=1)
imgs_A_h = torch.flip(imgs_A, [3])
imgs_B_h = torch.flip(imgs_B, [3])
outputs_A_h, outputs_B_h = net(imgs_A_h, imgs_B_h)
outputs_A_h = torch.flip(outputs_A_h, [3])
outputs_B_h = torch.flip(outputs_B_h, [3])
outputs_A += F.softmax(outputs_A_h, dim=1)
outputs_B += F.softmax(outputs_B_h, dim=1)
imgs_A_hv = torch.flip(imgs_A, [2,3])
imgs_B_hv = torch.flip(imgs_B, [2,3])
outputs_A_hv, outputs_B_hv = net(imgs_A_hv, imgs_B_hv)
outputs_A_hv = torch.flip(outputs_A_hv, [2,3])
outputs_B_hv = torch.flip(outputs_B_hv, [2,3])
outputs_A += F.softmax(outputs_A_hv, dim=1)
outputs_B += F.softmax(outputs_B_hv, dim=1)
outputs_A = outputs_A.cpu().detach()
outputs_B = outputs_B.cpu().detach()
pred_A = torch.argmax(outputs_A, dim=1)
pred_B = torch.argmax(outputs_B, dim=1)
pred_A = pred_A.squeeze().numpy().astype(np.uint8)
pred_B = pred_B.squeeze().numpy().astype(np.uint8)
pred_A_path = os.path.join(pred_A_dir_rgb, mask_name)
pred_B_path = os.path.join(pred_B_dir_rgb, mask_name)
io.imsave(pred_A_path, RS.Index2Color(pred_A))
io.imsave(pred_B_path, RS.Index2Color(pred_B))
print(pred_A_path)
if index_map:
pred_A_path = os.path.join(pred_A_dir, mask_name)
pred_B_path = os.path.join(pred_B_dir, mask_name)
io.imsave(pred_A_path, pred_A.astype(np.uint8))
io.imsave(pred_B_path, pred_B.astype(np.uint8))
'''
change_path = os.path.join(pred_dir, 'change', mask_name)
io.imsave(change_path, (change_mask*255).astype(np.uint8))'''
if __name__ == '__main__':
main()