-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_folder_sAP_valid.py
More file actions
executable file
·207 lines (174 loc) · 6.91 KB
/
process_folder_sAP_valid.py
File metadata and controls
executable file
·207 lines (174 loc) · 6.91 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
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
#!/usr/bin/env python3
"""Process an image with the trained neural network
Usage:
demo.py [options] <yaml-config> <checkpoint> <images>...
demo.py (-h | --help )
Arguments:
<yaml-config> Path to the yaml hyper-parameter file
<checkpoint> Path to the checkpoint
<images> Path to images
Options:
-h --help Show this screen.
-d --devices <devices> Comma seperated GPU devices [default: 0]
"""
import os
import os.path as osp
import glob
import pprint
import random
import json
import cv2
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import skimage.io
import skimage.transform
import torch
import yaml
from docopt import docopt
import scipy.io as sio
import pandas as pd
from tqdm import tqdm
import lcnn
from lcnn.config import C, M
from lcnn.models.line_vectorizer import LineVectorizer
from lcnn.models.multitask_learner import MultitaskHead, MultitaskLearner
from lcnn.models.HT import hough_transform
import lcnn.metric
from lcnn.postprocess import postprocess
from lcnn.utils import recursive_to
PLTOPTS = {"color": "#33FFFF", "s": 15, "edgecolors": "none", "zorder": 5}
cmap = plt.get_cmap("jet")
norm = mpl.colors.Normalize(vmin=0.9, vmax=1.0)
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
def c(x):
return sm.to_rgba(x)
def main():
args = docopt(__doc__)
config_file = args["<yaml-config>"] or "config/wireframe.yaml"
C.update(C.from_yaml(filename=config_file))
M.update(C.model)
# pprint.pprint(C, indent=4)
random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
device_name = "cpu"
os.environ["CUDA_VISIBLE_DEVICES"] = args["--devices"]
if torch.cuda.is_available():
device_name = "cuda"
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed(0)
print("Let's use", torch.cuda.device_count(), "GPU(s)!")
else:
print("CUDA is not available")
device = torch.device(device_name)
checkpoint = torch.load(args["<checkpoint>"], map_location=device)
# Load model
if os.path.isfile(C.io.vote_index):
vote_index = sio.loadmat(C.io.vote_index)['vote_index']
else:
vote_index = hough_transform(rows=128, cols=128, theta_res=3, rho_res=1)
sio.savemat(C.io.vote_index, {'vote_index': vote_index})
vote_index = torch.from_numpy(vote_index).float().contiguous().to(device)
print('load vote_index', vote_index.shape)
model = lcnn.models.hg(
depth=M.depth,
head=lambda c_in, c_out: MultitaskHead(c_in, c_out),
num_stacks=M.num_stacks,
num_blocks=M.num_blocks,
num_classes=sum(sum(M.head_size, [])),
vote_index=vote_index,
)
model = MultitaskLearner(model)
model = LineVectorizer(model)
model.load_state_dict(checkpoint["model_state_dict"])
model = model.to(device)
model.eval()
images = glob.glob(args["<images>"][0] + '/images/*.png')
with open(os.path.join(args["<images>"][0] + '_synthetic_5', 'valid.json')) as file:
data = json.load(file)
df_valid = pd.DataFrame.from_dict(data, orient='columns')
with open(os.path.join(args["<images>"][0], 'valid.json')) as file:
data = json.load(file)
df = pd.DataFrame.from_dict(data, orient='columns')
n_gt = 0
lcnn_tp, lcnn_fp, lcnn_scores = [], [], []
for imname in tqdm(df_valid.filename):
# print(f"Processing {imname}")
im = skimage.io.imread(args["<images>"][0] + '/images/' + imname)
if im.ndim == 2:
im = np.repeat(im[:, :, None], 3, 2)
im = im[:, :, :3]
im_resized = skimage.transform.resize(im, (512, 512)) * 255
image = (im_resized - M.image.mean) / M.image.stddev
image = torch.from_numpy(np.rollaxis(image, 2)[None].copy()).float()
with torch.no_grad():
input_dict = {
"image": image.to(device),
"meta": [
{
"junc": torch.zeros(1, 2).to(device),
"jtyp": torch.zeros(1, dtype=torch.uint8).to(device),
"Lpos": torch.zeros(2, 2, dtype=torch.uint8).to(device),
"Lneg": torch.zeros(2, 2, dtype=torch.uint8).to(device),
}
],
"target": {
"jmap": torch.zeros([1, 1, 128, 128]).to(device),
"joff": torch.zeros([1, 1, 2, 128, 128]).to(device),
},
"mode": "testing",
}
H = model(input_dict)["preds"]
lines = H["lines"][0].cpu().numpy() / 128 * im.shape[:2]
scores = H["score"][0].cpu().numpy()
for i in range(1, len(lines)):
if (lines[i] == lines[0]).all():
lines = lines[:i]
scores = scores[:i]
break
# postprocess lines to remove overlapped lines
diag = (im.shape[0] ** 2 + im.shape[1] ** 2) ** 0.5
nlines, nscores = postprocess(lines, scores, diag * 0.01, 0, False)
# sAP
gt_line = np.array(df[df['filename'] == imname].lines.values[0]).reshape(1,2,2)[:,:,[1,0]]
tp, fp = lcnn.metric.msTPFP(nlines, gt_line, 5000)
n_gt += len(gt_line)
lcnn_tp.append(tp)
lcnn_fp.append(fp)
lcnn_scores.append(nscores)
# for i, t in enumerate([0.94, 0.95, 0.96, 0.97, 0.98, 0.99]):
for i, t in enumerate([0.94]):
plt.gca().set_axis_off()
plt.subplots_adjust(top=1, bottom=0, right=1, left=0, hspace=0, wspace=0)
plt.margins(0, 0)
for (a, b), s in zip(nlines, nscores):
if s < t:
continue
plt.plot([a[1], b[1]], [a[0], b[0]], c=c(s), linewidth=2, zorder=s)
plt.scatter(a[1], a[0], **PLTOPTS)
plt.scatter(b[1], b[0], **PLTOPTS)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())
plt.imshow(im)
plt.title('tp={}, fp={}'.format(tp, fp))
# plt.savefig(imname.replace(".png", f"-{t:.02f}.svg"), bbox_inches="tight")
# plt.show()
plt.close()
# cv2.imwrite(osp.splitext(imname)[0] + '_pred.png', skimage.transform.resize(np.rollaxis(H['lmap'].cpu().numpy(), 0, 3)*255, im.shape[:2]))
lcnn_tp = np.concatenate(lcnn_tp)
lcnn_fp = np.concatenate(lcnn_fp)
lcnn_scores = np.concatenate(lcnn_scores)
lcnn_index = np.argsort(-lcnn_scores)
lcnn_tp = np.cumsum(lcnn_tp[lcnn_index]) / n_gt
lcnn_fp = np.cumsum(lcnn_fp[lcnn_index]) / n_gt
sAP = lcnn.metric.ap(lcnn_tp, lcnn_fp)
print('sAP={}'.format(sAP))
print('done.')
with open('result_sAP.txt', 'w') as f:
f.write('sAP={}\n'.format(sAP))
f.write('tp={}\n'.format(lcnn_tp))
f.write('fp={}\n'.format(lcnn_fp))
if __name__ == "__main__":
main()