-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrf.py
179 lines (154 loc) · 5.69 KB
/
crf.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
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
import opengm
import torch
import numpy as np
import torch.autograd as autograd
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class CRF(autograd.Function):
"""
Takes the unary and pairwise potentials, do CRF inference over the given graphical model and return the labels
Args:
unary: unary potentials (b x k x r x c)
pairwise: Format To be decided
Return:
Labels - (b x r x c)
"""
def __init__(self,true_labels):
super(CRF, self).__init__()
self.labels=true_labels
def forward(self, unary_pots):
""" Receive input tensor, return output tensor"""
self.save_for_backward(unary_pots)
print("In forward")
b,r,c,k = unary_pots.size()
if(False):
if torch.cuda.is_available():
unaries = unary_pots.cpu().numpy()
else:
unaries = unary_pots.numpy()
unaries = unaries.reshape([b*r*c,k])
numVar = r*c
gm=opengm.gm(np.ones(numVar,dtype=opengm.label_type)*k)
uf_id = gm.addFunctions(unaries)
potts = opengm.PottsFunction([k,k],0.0,0.4)
pf_id = gm.addFunction(potts)
vis=np.arange(0,numVar,dtype=np.uint64)
# add all unary factors at once
gm.addFactors(uf_id,vis)
# add pairwise factors
### Row Factors
for i in range(0,r):
for j in range(0,c-1):
gm.addFactor(pf_id,[i*c+j,i*c+j+1])
### Column Factors
for i in range(0,r-1):
for j in range(c):
gm.addFactor(pf_id,[i*c+j,(i+1)*c+j])
print("Graphical Model Constructed")
inf=opengm.inference.AlphaExpansionFusion(gm)
inf.infer()
labels=inf.arg()
return torch.from_numpy(np.asarray(labels).astype('float'))
else:
return torch.zeros(b,r,c)
#return torch.from_numpy(numpy.random.rand(r,c,numLabels))
def backward(self,grad_output):
"""Calculate the gradients of left and right"""
print("Entering Backward Pass Through CRF\n Max Grad Outputs",torch.max(grad_output),torch.min(grad_output))
unary_pots, = self.saved_tensors
true_labels = self.labels
true_labels = true_labels.data.cpu().numpy()
#unary_pots = unary_pots_temp[:,:,0:10,0:10]
b,r,c,k = unary_pots.size()
# # r=10
# # c=10
# print(unary_pots.size())
# print(true_labels.shape)
gamma = 0.1
tau = 10
unary_flat = unary_pots.contiguous().view([b*r*c,k])
numVar = r*c
index_arr=torch.zeros(r*c,k)
for i in range(k):
index_arr[:,i] = i
for j in range(numVar):
for i in range(k):
unary_flat[j,i] = unary_flat[j,i] - gamma* min(abs(i-true_labels[j]),tau)
if torch.cuda.is_available():
unaries = unary_flat.cpu().numpy()
else:
unaries = unary_flat.numpy()
gm=opengm.gm(np.ones(numVar,dtype=opengm.label_type)*k)
uf_id = gm.addFunctions(unaries)
potts = opengm.PottsFunction([k,k],0.2,1.0)
pf_id = gm.addFunction(potts)
vis=np.arange(0,numVar,dtype=np.uint64)
# add all unary factors at once
gm.addFactors(uf_id,vis)
# add pairwise factors
### Row Factors
for i in range(0,r):
for j in range(0,c-1):
gm.addFactor(pf_id,[i*c+j,i*c+j+1])
### Column Factors
for i in range(0,r-1):
for j in range(c):
gm.addFactor(pf_id,[i*c+j,(i+1)*c+j])
print("Graphical Model Constructed")
infParam = opengm.InfParam(steps=5)
inf=opengm.inference.AlphaExpansionFusion(gm,parameter=infParam)
inf.infer()
print("Inference done")
del_x_bar = inf.arg()
sub_grad_unaries = np.zeros((b*r*c,k))
energy = 0
for i in range(numVar):
energy = energy - unaries[i][del_x_bar[i]] + unaries[i][int(true_labels[i])] + gamma* min(abs(del_x_bar[i]-true_labels[i]),tau)
for i in range(0,r):
for j in range(0,c-1):
if(del_x_bar[i*c+j]==del_x_bar[i*c+j+1]):
energy = energy - 0.2
else:
energy = energy - 1.0
if(true_labels[i*c+j]==true_labels[i*c+j+1]):
energy = energy +0.2
else:
energy = energy + 1.0
for i in range(0,r-1):
for j in range(c):
if(del_x_bar[i*c+j]==del_x_bar[(i+1)*c+j]):
energy = energy - 0.2
else:
energy = energy - 1.0
if(true_labels[i*c+j]==true_labels[i*c+j+1]):
energy = energy + 0.2
else:
energy = energy + 1.0
print("Energy",energy)
for i in range(numVar):
sub_grad_unaries[i,int(del_x_bar[i])] = -1
#print true_labels[i]
if(true_labels[i]==-1):
continue
sub_grad_unaries[i,int(true_labels[i])] = 1
grad_in = sub_grad_unaries.reshape([b,r,c,k])
print("Leaving Backward through CRF\n Min Grad Inputs",torch.max(grad_output),torch.min(grad_output))
return torch.from_numpy(grad_in).type('torch.cuda.FloatTensor')
# width=100
# height=200
# numVar=width*height
# numLabels=2
# # construct gm
# gm=opengm.gm(np.ones(numVar,dtype=opengm.label_type)*numLabels)
# # construct an array with all numeries (random in this example)
# unaries=np.random.rand(width,height,numLabels)
# # reshape unaries is such way, that the first axis is for the different functions
# unaries2d=unaries.reshape([numVar,numLabels])
# # add all unary functions at once (#numVar unaries)
# fids=gm.addFunctions(unaries2d)
# # numpy array with the variable indices for all factors
# vis=np.arange(0,numVar,dtype=numpy.uint64)
# # add all unary factors at once
# gm.addFactors(fids,vis)
# print("Graphical Model Constructed")