-
Notifications
You must be signed in to change notification settings - Fork 5
/
CRFEdgeFeatures.py
executable file
·253 lines (182 loc) · 7.17 KB
/
CRFEdgeFeatures.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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 21 12:45:08 2016
@author: rsk
"""
import cPickle
import gzip
import os
import sys
import time
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
import itertools
os.chdir('/home/rsk/Documents/PyStruct/CRF-MNIST-ImageDenoising/')
#os.chdir('/home/bmi/CRF/CRF-MNIST-ImageDenoising/')
from pystruct.models import GraphCRF, LatentNodeCRF, EdgeFeatureGraphCRF
from pystruct.learners import NSlackSSVM, OneSlackSSVM, LatentSSVM
from pystruct.datasets import make_simple_2x2
from pystruct.utils import make_grid_edges, plot_grid
from CRFUtils import *
def viewImg(img):
plt.imshow( np.reshape(img,(28,28)) ,cmap=cm.Greys )
def checkPred(index,train=0):
if train==0:
plt.subplot(121)
plt.imshow( np.reshape(trainLabels[index],(28,28)) , cmap=cm.Greys )
plt.subplot(122)
plt.imshow( np.reshape(predTrain[index],(28,28)) , cmap=cm.Greys )
else:
plt.subplot(121)
plt.imshow( np.reshape(testLabels[index],(28,28)) , cmap=cm.Greys )
plt.subplot(122)
plt.imshow( np.reshape(predTest[index],(28,28)) , cmap=cm.Greys )
def edges(shape=(28,28),dist=4,diag=0):
length = shape[0]*shape[1]
mat = np.reshape( list(range(length)), shape )
mat = np.array(mat)
edgeList=[]
if diag==1:
for i in range(shape[0]):
for j in range(shape[1]):
for x in range(i,i+dist+1):
for y in range(j-dist,j+dist+1):
if x==i and y==j: #Avoid edge to self
continue
if x<shape[0] and y>=0 and y<shape[1]: #Avoid going out of the matri
edgeList.append(np.sort([mat[i,j] , mat[x,y] ]))
else:
for i in range(shape[0]):
for j in range(shape[1]):
x=i
for y in range(j-dist,j+dist+1):
if x<shape[0] and y>=0 and y<shape[1] and y!=j: #Avoid going out of the matri
edgeList.append(np.sort([mat[i,j] , mat[x,y] ]))
for x in range(i+1,i+dist+1):
y=j
if x<shape[0] and y>=0 and y<shape[1]: #Avoid going out of the matri
edgeList.append(np.sort([mat[i,j] , mat[x,y] ]))
edgeList =np.array( sorted( np.vstack({tuple(row) for row in edgeList}), key=lambda x : x[0] ) )
return np.array(edgeList)
#%%
def getNeighborhoodData(img, dist=1):
img = np.reshape(img,(28,28))
if dist==1:
newImg = np.zeros( (img.shape[0],img.shape[1],9) )
newImg[1:,1:,0] = img[:-1,:-1]
newImg[:,1:,1] = img[:,:-1]
newImg[:-1,1:,2] = img[1:,:-1]
newImg[1:,:,3] = img[:-1,:]
newImg[:-1,:-1,4] = img[1:,1:]
newImg[:-1,:,5] = img[1:,:]
newImg[1:,:-1,6] = img[:-1,1:]
newImg[:,:-1,7] = img[:,1:]
newImg[:,:,8] = img[:,:]
return newImg.reshape( img.shape[0]*img.shape[1],9 )
elif dist==2:
newImg = np.zeros( (img.shape[0],img.shape[1],25) )
newImg[2: , 2: , 0 ] = img[:-2 , :-2]
newImg[1: , 2: , 1 ] = img[:-1 , :-2]
newImg[ : , 2: , 2 ] = img[: , :-2]
newImg[:-1 , 2: , 3 ] = img[1: , :-2]
newImg[:-2 , 2: , 4 ] = img[2: , :-2]
newImg[:-2 , 1: , 5 ] = img[2: , :-1]
newImg[:-2 , : , 6 ] = img[2: , : ]
newImg[:-2 ,:-1 , 7 ] = img[2: , 1: ]
newImg[:-2 ,:-2 , 8 ] = img[2: , 2: ]
newImg[:-1 ,:-2 , 9 ] = img[1: , 2: ]
newImg[: ,:-2 , 10] = img[: , 2: ]
newImg[1: ,:-2 , 11] = img[:-1 , 2: ]
newImg[2: ,:-2 , 12] = img[:-2 , 2: ]
newImg[2: ,:-1 , 13] = img[:-2 , 1: ]
newImg[2: , : , 14] = img[:-2 , : ]
newImg[2: , 1: , 15] = img[:-2 , :-1]
newImg[1: ,1: , 16] = img[:-1 , :-1]
newImg[: ,1: , 17] = img[: , :-1]
newImg[:-1 ,1: , 18] = img[1: , :-1]
newImg[1: , : , 19] = img[:-1 , :]
newImg[:-1 ,:-1 , 20] = img[1: , 1:]
newImg[:-1 ,: , 21] = img[1: , :]
newImg[1: ,:-1 , 22] = img[:-1 , 1:]
newImg[: ,:-1 , 23] = img[: , 1:]
newImg[: , : , 24] = img[: , :]
return newImg.reshape( img.shape[0]*img.shape[1],25 )
#%%
train,valid,test = getDigitData(0)
trainDirty = addNoise(train,0.05)
testDirty = addNoise(test,0.05)
# Creating truth labels by thresholding
threshold= 0.3
trainLabels = []
testLabels = []
for i in train:
trainLabels.append([ 1 if k>threshold else 0 for k in i ])
for i in test:
testLabels.append([ 1 if k>threshold else 0 for k in i ])
trainLabels = np.array(trainLabels)
testLabels = np.array(testLabels)
#%%
n_train=200
n_test=100
num_iter=40
C=0.1
dist=1
diag=1
inference="ad3"
edgeList = edges((28,28),dist=dist,diag=diag)
G = [edgeList for x in trainDirty[0:n_train]]
X_flat = [np.vstack(i) for i in trainDirty[0:n_train]]
Y_flat = np.array(trainLabels[0:n_train])
crf = EdgeFeatureGraphCRF(inference_method=inference)
svm = NSlackSSVM(model=crf,max_iter=num_iter,C=C,n_jobs=-1,verbose=1)
#%%
edgeFeatures=[]
for i in range(len(X_flat)):
feature=[]
for j in range(len(edgeList)):
feature.append( np.append(X_flat[i][edgeList[j][0]] , X_flat[i][edgeList[j][1]]) )
edgeFeatures.append(feature)
edgeFeatures=np.array(edgeFeatures)
asdf = zip(X_flat,G,edgeFeatures)
#%%
svm.fit(asdf,Y_flat)
#%%
G2 = [edgeList for x in testDirty[0:n_test]]
X_flat2 = [np.vstack(i) for i in testDirty[0:n_test]]
Y_flat2 = np.array(testLabels[0:n_test])
edgeFeatures2=[]
for i in range(len(X_flat2)):
feature=[]
for j in range(len(edgeList)):
feature.append( np.append(X_flat2[i][edgeList[j][0]] , X_flat2[i][edgeList[j][1]]) )
edgeFeatures2.append(feature)
edgeFeatures2=np.array(edgeFeatures2)
asdf2 = zip(X_flat2,G2,edgeFeatures2)
#%%
predTrain = svm.predict(asdf)
errTrain = 0
for i in range(len(predTrain)):
errTrain += accuracy(predTrain[i],Y_flat[i])
errTrain = errTrain/float(len(predTrain))
#%%
predTest = svm.predict(asdf2)
errTest = 0
for i in range(len(predTest)):
errTest += accuracy(predTest[i],Y_flat2[i])
errTest = errTest/float(len(predTest))
print "The train set DICE is %f" %(errTrain)
print "The test set DICE is %f" %(errTest)
#%%
resultsDir = os.getcwd()+"/Results"
resultFile = open(resultsDir + "/results.csv",'a')
resultFile.write(str(num_iter)+","+str(dist)+","+str(diag)+","+inference+","+str(errTrain)+","+str(errTest)+","+str(n_train)+","+str(n_test)+"EdgeFeature"+"\n")
resultFile.close()
nameLen = len(os.listdir(resultsDir))
filename = str(nameLen)+"_"+str(dist)+"_"+str(diag)+"_"+inference+"_"+"EdgeFeature"
predFileTrain = open(resultsDir+"/"+filename+"_Train.pkl",'wb')
predFileTest = open(resultsDir+"/"+filename+"_Test.pkl",'wb')
cPickle.dump(predTrain,predFileTrain,protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(predTest,predFileTest,protocol=cPickle.HIGHEST_PROTOCOL)
predFileTrain.close()
predFileTest.close()