-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
139 lines (103 loc) · 4.04 KB
/
utilities.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
import torch
import torch.nn as nn
import torchvision.models as models
def convert_to_edge_index(adj):
adj_coo = adj.to_sparse().coalesce()
edge_index = adj_coo.indices()
return edge_index
class MaxPool(nn.Module):
def __init__(self, pool_size):
super(MaxPool, self).__init__()
self.pool = nn.MaxPool2d(kernel_size=pool_size, stride=pool_size)
def forward(self, x):
return self.pool(x)
class Noise(nn.Module):
def __init__(self, R_scale):
super(Noise, self).__init__()
self.mean = 0
self.stdev = 1 # as defined in the paper
def forward(self, d_coarse):
noise = torch.rand_like(d_coarse)*self.stdev + self.mean
d_noised = d_coarse + noise
return d_noised
class IntervalThreshold(nn.Module):
def __init__(self, m, n):
super(IntervalThreshold, self).__init__()
self.m = m
self.n = n
def forward(self, d_pool):
threshold = (torch.max(d_pool) - torch.min(d_pool))/min(self.m, self.n)
return threshold
class ReconGraph(nn.Module):
def __init__(self, m, n):
super(ReconGraph, self).__init__()
self.m = m
self.n = n
def forward(self, d_noised, threshold):
neighbours = set()
labels = {}
count = 0
# print(self.m, self.n)
for i in range(self.m):
for j in range(self.n):
labels[(j, i)] = count # Labeling each pixel in (x, y) form
count += 1
for dy in range(-1, 2):
for dx in range(-1, 2):
if dx != 0 and dy != 0 and i+dy >= 0 and i+dy < self.m and j+dx >= 0 and j+dx < self.n:
if abs(d_noised[0][i+dy][j+dx] - d_noised[0][i][j]) <= threshold:
# (x, y) format
neighbours.add(((j, i), (j+dx, i+dy)))
adjacency_matrix = torch.zeros(
(self.m*self.n, self.m*self.n), dtype=bool)
for val in neighbours:
N1, N2 = val # in (x, y) form
N1_x, N1_y = N1
N2_x, N2_y = N2
l1 = labels[(N1_x, N1_y)]
l2 = labels[(N2_x, N2_y)]
# Symmetric connections
adjacency_matrix[l1, l2] = 1
adjacency_matrix[l2, l1] = 1
return adjacency_matrix
class GraphDropout(nn.Module):
def __init__(self, p=0.5) -> None:
super(GraphDropout, self).__init__()
self.p = p
def forward(self, adjacency_matrix):
if self.train:
mask = torch.empty_like(adjacency_matrix).bernoulli_(1 - self.p)
output = adjacency_matrix * mask
else:
output = adjacency_matrix
return output
class ExtractGraph(nn.Module):
def __init__(self) -> None:
super(ExtractGraph, self).__init__()
self.maxpool = MaxPool(pool_size=2)
self.noise = Noise(R_scale=0.4) # From paper results
self.dropout = GraphDropout(p=0.5)
def forward(self, d_coarse, R_scale):
# print('d_coarse: ', d_coarse.shape, 'type: ', d_coarse.dtype)
d_pool = self.maxpool.forward(d_coarse)
m = d_pool.shape[1]
n = d_pool.shape[2]
self.interval_threshold = IntervalThreshold(m, n)
self.recon_graph = ReconGraph(m, n)
print("pooled shape ", d_pool.shape)
d_noise = self.noise.forward(d_pool)
threshold = self.interval_threshold.forward(d_pool)
adjacency_matrix = self.recon_graph.forward(d_noise, threshold)
adjacency_matrix = self.dropout.forward(adjacency_matrix)
edge_index = convert_to_edge_index(adjacency_matrix)
return edge_index
class Encoder(nn.Module):
def __init__(self) -> None:
super(Encoder, self).__init__()
encoder = models.resnet.resnet50(
weights=models.ResNet50_Weights.DEFAULT)
encoder = nn.Sequential(*list(encoder.children()))[:3]
self.resnet_encoder = encoder
def forward(self, x):
self.resnet_encoder.eval()
return self.resnet_encoder(x)