-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
154 lines (112 loc) · 4.84 KB
/
plot.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
import os
import io
import json
import tqdm
import argparse
import operator
import base64
import cv2
from PIL import Image
import numpy as np
import rasterio as rio
import torch
from torchmetrics import JaccardIndex
from models import get_model
from utils.chabud_dataloader import get_dataloader, _stretch_8bit
from utils.loss import get_loss
from utils.engine_hub import weight_and_experiment
def get_8bit(sample_pre, sample_post):
post_r = sample_post[1, :, :]
post_g = sample_post[2, :, :]
post_b = sample_post[3, :, :]
pre_r = sample_pre[1, :, :]
pre_g = sample_pre[2, :, :]
pre_b = sample_pre[3, :, :]
post_r = _stretch_8bit(post_r)
post_g = _stretch_8bit(post_g)
post_b = _stretch_8bit(post_b)
pre_r = _stretch_8bit(pre_r)
pre_g = _stretch_8bit(pre_g)
pre_b = _stretch_8bit(pre_b)
post_bgr = np.asarray([post_b, post_g, post_r])
pre_bgr = np.asarray([pre_b, pre_g, pre_r])
return pre_bgr.transpose(1, 2, 0), post_bgr.transpose(1, 2, 0)
def val(val_loader, net, device):
# net.eval()
results = []
jaccard_index = JaccardIndex(task="multiclass", num_classes=2).to(device)
idx = 0
for pre, post, mask in tqdm.tqdm(val_loader):
# get the inputs; data is a list of [inputs, labels]
pre, post, mask = pre.to(device), post.to(device), mask.to(device)
outputs = net(pre, post)
# print (outputs.min(), outputs.max(), outputs.shape)
outputs = torch.argmax(outputs, axis=1)
# print (outputs.min(), outputs.max(), outputs.shape)
for i in range(pre.shape[0]):
iou = jaccard_index(outputs[i], mask[i])
# print (iou.item())
results.append([val_loader.dataset.data_list[idx],
outputs[i].data.cpu().numpy().astype(np.uint8),
iou.item()])
idx += 1
return results
def make_image(args, sample):
fin = open(os.path.join(args.data_root, args.vector_dir,
sample[0]))
data = json.load(fin)
fin.close()
img_pre = rio.open(os.path.join(args.data_root,
data["images"][0]["file_name"])).read()
img_post = rio.open(os.path.join(args.data_root,
data["images"][1]["file_name"])).read()
mask_string = data["properties"][0]["labels"][0]
img_mask = np.array(Image.open(io.BytesIO(base64.b64decode(mask_string))))
img_mask = np.stack([img_mask, img_mask, img_mask]).transpose(1, 2, 0)
pred_mask = np.stack([sample[1], sample[1], sample[1]]).transpose(1, 2, 0)
img_pre, img_post = get_8bit(img_pre, img_post)
padding = np.stack([np.ones((20, 512), dtype=np.uint8)*128,
np.ones((20, 512), dtype=np.uint8)*128,
np.ones((20, 512), dtype=np.uint8)*128]).transpose(1, 2, 0)
rgb = np.concatenate([img_pre, padding, img_post], axis=0)
mask = np.concatenate([img_mask * 255, padding, pred_mask * 255], axis=0)
out = np.concatenate([rgb, mask], axis=1)
return out
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Plotting images")
parser.add_argument(
"--experiment-url", type=str, required=True, help="url of the model")
parser.add_argument("--num-plots", default=5, type=int)
parser.add_argument("--plot-dir", type=str, required=True)
parser.add_argument("--full-load", action='store_true', help="store all data in ram")
parser.add_argument("--normalize", action='store_true', help="normalize")
parser.add_argument("--bands", default="0,1,2,3,4,5,6,7,8,9,10,11",
help="bands to use")
parser.add_argument("--swap", action='store_true', help="swap pre and post images")
args = parser.parse_args()
device = torch.device("cuda:0")
dst_path, _ = weight_and_experiment(args.experiment_url, best=True)
fin = open('/'.join(dst_path.split('/')[:-1]) + '/epxeriment_config.json', 'r')
metadata = json.load(fin)
args.__dict__.update(metadata)
fin.close()
net = get_model(args)
net.to(device)
weight = torch.load(dst_path)
net.load_state_dict(weight)
net.eval()
_, val_loader = get_dataloader(args)
results = val(val_loader=val_loader, net=net, device=device)
results = sorted(results, key=operator.itemgetter(2))
worst5 = results[:args.num_plots]
best5 = results[-1 * args.num_plots:]
if not os.path.exists(f"plots/{args.plot_dir}/"):
os.makedirs(f"plots/{args.plot_dir}/")
for idx, best in enumerate(best5):
print (best[0])
out = make_image(args, best)
cv2.imwrite(f"plots/{args.plot_dir}/best{idx}.png", out)
for idx, worst in enumerate(worst5):
print (worst[0])
out = make_image(args, worst)
cv2.imwrite(f"plots/{args.plot_dir}/worst{idx}.png", out)