forked from chaitjo/graph-convnet-tsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_utils.py
151 lines (127 loc) · 6.05 KB
/
plot_utils.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
import torch
import torch.nn.functional as F
import matplotlib
import matplotlib.pyplot as plt
import networkx as nx
from utils.graph_utils import *
def plot_tsp(p, x_coord, W, W_val, W_target, title="default"):
"""
Helper function to plot TSP tours.
Args:
p: Matplotlib figure/subplot
x_coord: Coordinates of nodes
W: Edge adjacency matrix
W_val: Edge values (distance) matrix
W_target: One-hot matrix with 1s on groundtruth/predicted edges
title: Title of figure/subplot
Returns:
p: Updated figure/subplot
"""
def _edges_to_node_pairs(W):
"""Helper function to convert edge matrix into pairs of adjacent nodes.
"""
pairs = []
for r in range(len(W)):
for c in range(len(W)):
if W[r][c] == 1:
pairs.append((r, c))
return pairs
G = nx.from_numpy_matrix(W_val)
pos = dict(zip(range(len(x_coord)), x_coord.tolist()))
adj_pairs = _edges_to_node_pairs(W)
target_pairs = _edges_to_node_pairs(W_target)
colors = ['g'] + ['b'] * (len(x_coord) - 1) # Green for 0th node, blue for others
nx.draw_networkx_nodes(G, pos, node_color=colors, node_size=50)
nx.draw_networkx_edges(G, pos, edgelist=adj_pairs, alpha=0.3, width=0.5)
nx.draw_networkx_edges(G, pos, edgelist=target_pairs, alpha=1, width=1, edge_color='r')
p.set_title(title)
return p
def plot_tsp_heatmap(p, x_coord, W_val, W_pred, title="default"):
"""
Helper function to plot predicted TSP tours with edge strength denoting confidence of prediction.
Args:
p: Matplotlib figure/subplot
x_coord: Coordinates of nodes
W_val: Edge values (distance) matrix
W_pred: Edge predictions matrix
title: Title of figure/subplot
Returns:
p: Updated figure/subplot
"""
def _edges_to_node_pairs(W):
"""Helper function to convert edge matrix into pairs of adjacent nodes.
"""
pairs = []
edge_preds = []
for r in range(len(W)):
for c in range(len(W)):
if W[r][c] > 0.25:
pairs.append((r, c))
edge_preds.append(W[r][c])
return pairs, edge_preds
G = nx.from_numpy_matrix(W_val)
pos = dict(zip(range(len(x_coord)), x_coord.tolist()))
node_pairs, edge_color = _edges_to_node_pairs(W_pred)
node_color = ['g'] + ['b'] * (len(x_coord) - 1) # Green for 0th node, blue for others
nx.draw_networkx_nodes(G, pos, node_color=node_color, node_size=50)
nx.draw_networkx_edges(G, pos, edgelist=node_pairs, edge_color=edge_color, edge_cmap=plt.cm.Reds, width=0.75)
p.set_title(title)
return p
def plot_predictions(x_nodes_coord, x_edges, x_edges_values, y_edges, y_pred_edges, num_plots=3):
"""
Plots groundtruth TSP tour vs. predicted tours (without beamsearch).
Args:
x_nodes_coord: Input node coordinates (batch_size, num_nodes, node_dim)
x_edges: Input edge adjacency matrix (batch_size, num_nodes, num_nodes)
x_edges_values: Input edge distance matrix (batch_size, num_nodes, num_nodes)
y_edges: Groundtruth labels for edges (batch_size, num_nodes, num_nodes)
y_pred_edges: Predictions for edges (batch_size, num_nodes, num_nodes)
num_plots: Number of figures to plot
"""
y = F.softmax(y_pred_edges, dim=3) # B x V x V x voc_edges
y_bins = y.argmax(dim=3) # Binary predictions: B x V x V
y_probs = y[:,:,:,1] # Prediction probabilities: B x V x V
for f_idx, idx in enumerate(np.random.choice(len(y), num_plots, replace=False)):
f = plt.figure(f_idx, figsize=(10, 5))
x_coord = x_nodes_coord[idx].cpu().numpy()
W = x_edges[idx].cpu().numpy()
W_val = x_edges_values[idx].cpu().numpy()
W_target = y_edges[idx].cpu().numpy()
W_sol_bins = y_bins[idx].cpu().numpy()
W_sol_probs = y_probs[idx].cpu().numpy()
plt1 = f.add_subplot(121)
plot_tsp(plt1, x_coord, W, W_val, W_target, 'Groundtruth: {:.3f}'.format(W_to_tour_len(W_target, W_val)))
plt2 = f.add_subplot(122)
plot_tsp_heatmap(plt2, x_coord, W_val, W_sol_probs, 'Prediction Heatmap')
plt.show()
def plot_predictions_beamsearch(x_nodes_coord, x_edges, x_edges_values, y_edges, y_pred_edges, bs_nodes, num_plots=3):
"""
Plots groundtruth TSP tour vs. predicted tours (with beamsearch).
Args:
x_nodes_coord: Input node coordinates (batch_size, num_nodes, node_dim)
x_edges: Input edge adjacency matrix (batch_size, num_nodes, num_nodes)
x_edges_values: Input edge distance matrix (batch_size, num_nodes, num_nodes)
y_edges: Groundtruth labels for edges (batch_size, num_nodes, num_nodes)
y_pred_edges: Predictions for edges (batch_size, num_nodes, num_nodes)
bs_nodes: Predicted node ordering in TSP tours after beamsearch (batch_size, num_nodes)
num_plots: Number of figures to plot
"""
y = F.softmax(y_pred_edges, dim=3) # B x V x V x voc_edges
y_bins = y.argmax(dim=3) # Binary predictions: B x V x V
y_probs = y[:,:,:,1] # Prediction probabilities: B x V x V
for f_idx, idx in enumerate(np.random.choice(len(y), num_plots, replace=False)):
f = plt.figure(f_idx, figsize=(15, 5))
x_coord = x_nodes_coord[idx].cpu().numpy()
W = x_edges[idx].cpu().numpy()
W_val = x_edges_values[idx].cpu().numpy()
W_target = y_edges[idx].cpu().numpy()
W_sol_bins = y_bins[idx].cpu().numpy()
W_sol_probs = y_probs[idx].cpu().numpy()
W_bs = tour_nodes_to_W(bs_nodes[idx].cpu().numpy())
plt1 = f.add_subplot(131)
plot_tsp(plt1, x_coord, W, W_val, W_target, 'Groundtruth: {:.3f}'.format(W_to_tour_len(W_target, W_val)))
plt2 = f.add_subplot(132)
plot_tsp_heatmap(plt2, x_coord, W_val, W_sol_probs, 'Prediction Heatmap')
plt3 = f.add_subplot(133)
plot_tsp(plt3, x_coord, W, W_val, W_bs, 'Beamsearch: {:.3f}'.format(W_to_tour_len(W_bs, W_val)))
plt.show()