-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
133 lines (99 loc) · 5.23 KB
/
Copy pathtest.py
File metadata and controls
133 lines (99 loc) · 5.23 KB
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
'''
The implementation of ADA-Net: Attention-Guided Domain Adaptation Network with Contrastive Learning for Standing Dead Tree Segmentation Using Aerial Imagery.
Author: Mete Ahishali,
The software implementation is extensively based on the following repository: https://github.com/taesungp/contrastive-unpaired-translation.
'''
import os
import tqdm
import h5py
import numpy as np
import matplotlib.pyplot as plt
import adanet
from treemort import config
from treemort import loader
if __name__ == '__main__':
visualize = True
conf = config.setup('./configs/adanet.txt')
model = adanet.AdaNet(conf, train=False)
if conf.dataset_type == 'h5':
test_dataset = loader.prepare_datasets_h5(conf, train=False)
else:
_, _, test_dataset = loader.prepare_datasets(conf)
out_dir = os.path.join(conf.output_dir, 'test_epoch_' + conf.resume_epoch)
os.makedirs(out_dir, exist_ok=True)
if conf.dataset_type == 'h5':
if os.path.exists(out_dir + '/real_A_test.h5'): os.remove(out_dir + '/real_A_test.h5')
if os.path.exists(out_dir + '/fake_B_test.h5'): os.remove(out_dir + '/fake_B_test.h5')
if os.path.exists(out_dir + '/real_B_test.h5'): os.remove(out_dir + '/real_B_test.h5')
####################
### Test Dataset ###
####################
pbar = tqdm.tqdm(total=len(test_dataset), desc='Testing ')
for i, data in enumerate(test_dataset):
model.set_input(data)
model.test()
image_index = 0
real_A = model.real_A[image_index].permute(1, 2, 0).clamp(-1.0, 1.0).detach().cpu().float().numpy()
fake_B = model.fake_B[image_index].permute(1, 2, 0).clamp(-1.0, 1.0).detach().cpu().float().numpy()
real_B = model.real_B[image_index].permute(1, 2, 0).clamp(-1.0, 1.0).detach().cpu().float().numpy()
real_A = (real_A + 1)/2.0
fake_B = (fake_B + 1)/2.0
real_B = (real_B + 1)/2.0
if visualize:
if conf.dataset_type == 'h5':
order = [1, 2, 3] # RGB view.
#order = [0, 1, 2] # NIR-RG view.
image_name = os.path.join(out_dir, 'RGB_' + data['key_A'][0] + '.png') # RGB
_, axs = plt.subplots(1, 3, figsize=(9, 3.5))
axs[0].imshow(real_A[:, :, order])
axs[0].axis('off') # Hide the axes
axs[0].set_title('Original Image', fontweight='bold')
axs[1].imshow(fake_B[:, :, order])
axs[1].axis('off')
axs[1].set_title('A2G Generated Image', fontweight='bold')
axs[2].imshow(real_B[:, :, order])
axs[2].axis('off')
axs[2].set_title('Real B Image', fontweight='bold')
plt.tight_layout()
plt.savefig(image_name)
plt.close()
else:
image_name = os.path.join(out_dir, data['imagename_A'][0].split('/')[-1]) # RGB
_, axs = plt.subplots(1, 3, figsize=(9, 3.5))
axs[0].imshow(real_A)
axs[0].axis('off') # Hide the axes
axs[0].set_title('Original Image', fontweight='bold')
axs[1].imshow(fake_B)
axs[1].axis('off')
axs[1].set_title('A2G Generated Image', fontweight='bold')
axs[2].imshow(real_B)
axs[2].axis('off')
axs[2].set_title('Real B Image', fontweight='bold')
plt.tight_layout()
plt.savefig(image_name)
plt.close()
if conf.dataset_type == 'h5':
with h5py.File(out_dir + '/real_A_test.h5', "a") as hf: # Open in append mode
key = data['key_A'][0]
hf.create_group(key)
hf[key].create_dataset("image", data=real_A, compression="gzip")
hf[key].create_dataset("label", data=np.squeeze(data['label_A'][0].numpy()), compression="gzip")
hf[key].attrs["contains_dead_tree"] = data['contains_dead_tree_A'][0]
hf[key].attrs["source_image"] = data['filename_A'][0]
with h5py.File(out_dir + '/fake_B_test.h5', "a") as hf: # Open in append mode
key = data['key_A'][0]
hf.create_group(key)
hf[key].create_dataset("image", data=fake_B, compression="gzip")
hf[key].create_dataset("label", data=np.squeeze(data['label_A'][0].numpy()), compression="gzip")
hf[key].attrs["contains_dead_tree"] = data['contains_dead_tree_A'][0]
hf[key].attrs["source_image"] = data['filename_A'][0]
with h5py.File(out_dir + '/real_B_test.h5', "a") as hf: # Open in append mode
key = data['key_B'][0]
hf.create_group(key)
hf[key].create_dataset("image", data=real_B, compression="gzip")
hf[key].create_dataset("label", data=np.squeeze(data['label_B'][0].numpy()), compression="gzip")
hf[key].attrs["contains_dead_tree"] = data['contains_dead_tree_B'][0]
hf[key].attrs["source_image"] = data['filename_B'][0]
print('Test image processing: ' + str(i + 1))
pbar.update(1) # Update the iteration number.
pbar.close()