visualisationForPaperMasterPlot.py
Go to the documentation of this file.
1 import sys
2 import caffe
3 import numpy as np
4 import matplotlib.pyplot as plt
5 from matplotlib.lines import Line2D
6 import lmdb
7 import argparse
8 import leveldb
9 import ROOT
10 from collections import defaultdict
11 
12 def vis_square(data, outfile, title, padsize=1, padval=0):
13  data -= data.min()
14  data /= data.max()
15 
16  # force the number of filters to be square
17  n = int(np.ceil(np.sqrt(data.shape[0])))
18  padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
19  data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
20 
21  # tile the filters into an image
22  data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
23  data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
24  #print type(data)
25  #data=data.squeeze()
26 
27  #print data.shape
28  #print type(data[1][1])
29  #data.astype(np.float64)
30 
31  #plt.axis('off')
32  fig, ax = plt.subplots(figsize=(6,5))
33  #ax.set_title(title)
34  ax.set_frame_on(False)
35  ax.set_xlabel('')
36  ax.set_ylabel('')
37  ax.set_xticklabels([])
38  ax.set_yticklabels([])
39  ax.set_xticks([])
40  ax.set_yticks([])
41  plt.imshow(np.rot90(data),cmap='gist_heat_r',interpolation='none')
42  xmin, xmax = ax.get_xaxis().get_view_interval()
43  ymin, ymax = ax.get_yaxis().get_view_interval()
44  ax.add_artist(Line2D((xmin, xmax-0.5), (ymin, ymin), color='black', linewidth=1.75))
45  print xmin
46  print xmax
47  print ymin
48  print ymax
49  ax.add_artist(Line2D((xmin, xmin), (ymin, ymax+2), color='black', linewidth=1.75))
50  plt.imshow(np.rot90(data),cmap='gist_heat_r',interpolation='none')
51  plt.savefig(outfile,dpi = 1000)
52 
53 
54 def vis_square_kernel(data, outfile, title, padsize=1, padval=0):
55  data -= data.min()
56  data /= data.max()
57 
58  # force the number of filters to be square
59  n = int(np.ceil(np.sqrt(data.shape[0])))
60  padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
61  data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
62 
63  # tile the filters into an image
64  data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
65  data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
66  #print type(data)
67  #data=data.squeeze()
68 
69  #print data.shape
70  #print type(data[1][1])
71  #data.astype(np.float64)
72  fig, ax = plt.subplots(figsize=(6,6))
73  plt.axis('off')
74  #ax.set_title(title)
75  ax.xaxis.set_visible(False)
76  ax.yaxis.set_visible(False)
77  ax.set_frame_on(False)
78  ax.set_xlabel('')
79  ax.set_ylabel('')
80  ax.set_xticklabels([])
81  ax.set_yticklabels([])
82  plt.imshow(np.rot90(data),cmap='gist_heat_r',interpolation='none')
83  plt.imshow(np.rot90(data),cmap='gist_heat_r',interpolation='none')
84  plt.savefig(outfile,dpi = 1000)
85 
86 def vis_square_kernel_zoom(data, outfile, title, xminval,xmaxval,yminval,ymaxval,padsize=1, padval=0):
87  data -= data.min()
88  data /= data.max()
89 
90  # force the number of filters to be square
91  n = int(np.ceil(np.sqrt(data.shape[0])))
92  padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
93  data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
94 
95  # tile the filters into an image
96  data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
97  data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
98  #print type(data)
99  #data=data.squeeze()
100 
101  print data.shape
102  #print type(data[1][1])
103  #data.astype(np.float64)
104  fig, ax = plt.subplots(figsize=(6,6))
105  #plt.axis('off')
106  #ax.set_title(title)
107  ax.xaxis.set_visible(False)
108  ax.yaxis.set_visible(False)
109  #ax.set_frame_on(False)
110  ax.set_xlabel('')
111  ax.set_ylabel('')
112  ax.set_xticklabels([])
113  ax.set_yticklabels([])
114  plt.xlim(xminval,xmaxval)
115  plt.ylim(yminval,ymaxval)
116  plt.imshow(np.rot90(data),cmap='gist_heat_r',interpolation='none')
117  plt.imshow(np.rot90(data),cmap='gist_heat_r',interpolation='none')
118  plt.savefig(outfile,dpi = 1000)
119 
120 
121 
122 if __name__ == "__main__":
123  parser = argparse.ArgumentParser()
124  parser.add_argument('--proto', type=str, required=True)
125  parser.add_argument('--model', type=str, required=True)
126  parser.add_argument('--leveldb', type=str, required=True)
127  parser.add_argument('--event', type=int, required=True)
128  args = parser.parse_args()
129 
130  count = 0
131  correct = 0
132  matrix = defaultdict(int) # (real,pred) -> int
133  labels_set = set()
134 
135  net = caffe.Net(args.proto, args.model, caffe.TEST)
136  caffe.set_mode_gpu()
137  db = leveldb.LevelDB(args.leveldb)
138 
139  # lmdb_env = lmdb.open(args.lmdb)
140  # lmdb_txn = lmdb_env.begin()
141  # lmdb_cursor = lmdb_txn.cursor()
142 
143 
144  for key, value in db.RangeIter():
145  if count == args.event:
146  datum = caffe.proto.caffe_pb2.Datum()
147  datum.ParseFromString(value)
148  label = int(datum.label)
149 
150  #truelabel[0]=label
151  image = caffe.io.datum_to_array(datum)
152  image = image.astype(np.uint8)
153 
154  out = net.forward_all(data=np.asarray([image]))
155  plabel = int(out['prob'][0].argmax(axis=0))
156  data=np.asarray([image])#.astype(np.uint8)
157  #print data.reshape(np.delete(data.shape,0)).shape
158  dataswap=np.swapaxes(data.reshape(np.delete(data.shape,0)),0,2)
159  #print dataswap.shape
160  x,y=np.dsplit(dataswap,2)
161  x=x.swapaxes(0,1)
162  y=y.swapaxes(0,1)
163  #print x.squeeze().shape
164  #print y.squeeze().shape
165  #plt.imshow(np.rot90(x.squeeze()),cmap='gist_heat_r',interpolation='none')
166 
167  fig, ax = plt.subplots(figsize=(6,5))
168  ax.set_xlabel('Plane')
169  ax.set_ylabel('Cell')
170  #ax.set_title('X View')
171  ax.imshow(np.rot90(x.squeeze()), cmap='gist_heat_r', interpolation='none', extent=[0,100,0,80])
172  ax.imshow(np.rot90(x.squeeze()), cmap='gist_heat_r', interpolation='none', extent=[0,100,0,80])
173  print 'view_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf'
174  plt.savefig('view_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf',dpi = 1000)
175  #ax.set_title('Y View')
176  ax.imshow(np.rot90(y.squeeze()), cmap='gist_heat_r', interpolation='none', extent=[0,100,0,80])
177  #ax.set_title('Y View of Simulated Muon Neutrino')
178  #plt.imshow(np.rot90(y.squeeze()),cmap='gist_heat_r',interpolation='none')
179  plt.savefig('view_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y.pdf',dpi = 1000)
180 
181 
182 
183  # if count == 1:
184  # filters1 = net.params['conv1/7x7_s2_y'][0].data
185  # #print filters.shape
186  # #print type(filters)
187  # #print filters.transpose(0, 2, 3, 1).shape
188  # #print filters.transpose(0, 2, 3, 1)
189  # vis_square(filters1.transpose(0, 2, 3, 1).squeeze(),'conv1y.pdf')
190  # filters2 = net.params['conv1/7x7_s2_x'][0].data
191  # print filters2.shape
192  # #print type(filters)
193  # print filters2.transpose(0, 2, 3, 1).shape
194  # #print filters.transpose(0, 2, 3, 1)
195 
196  # # vis_square(filters2.transpose(0, 2, 3, 1).squeeze(),'conv1x.pdf')
197  # #filters1x1 = net.params['inception_3a/1x1_x'][0].data
198  # #print filters1x1.shape
199 
200  # #vis_square(filters1x1.squeeze(),'conv1x1x.pdf')
201  # # filters3x3_reduce = net.params['inception_3a/3x3_reduce_x'][0].data
202  # # #print filters3x3_reduce.shape
203  # # #vis_square(filters3x3_reduce.transpose(0, 2, 3, 1).squeeze(),'conv3x3x_reduce.pdf')
204 
205  # # filters3x3 = net.params['inception_3a/3x3_x'][0].data
206  # # print filters3x3.shape
207  # # print filters3x3.transpose(0, 2, 3, 1).shape
208  # # A= filters3x3.transpose(0, 2, 3, 1)[:, :, :,:-95]
209  # # print A.shape
210  # # #f3x31,f3x32=np.split(filters3x3.transpose(0, 2, 3, 1),4)
211 
212  # # vis_square(A.squeeze(),'conv3x3x.pdf')
213  # # # filters5x5_reduce = net.params['inception_3a/5x5_reduce_x'][0].data
214  # # # vis_square(filters5x5_reduce.transpose(0, 2, 3, 1).squeeze(),'conv5x5x_reduce.pdf')
215  # # # filters5x5 = net.params['inception_3a/5x5_x'][0].data
216  # # # vis_square(filters5x5.transpose(0, 2, 3, 1).squeeze(),'conv5x5x.pdf')
217 
218 
219 
220  filters1 = net.params['conv1/7x7_s2_y'][0].data
221  vis_square_kernel(filters1.transpose(0, 2, 3, 1).squeeze(),'conv1y.pdf','Trained Filters In First Y View 7x7 Convolutional Layer')
222  filters2 = net.params['conv1/7x7_s2_x'][0].data
223  vis_square_kernel(filters2.transpose(0, 2, 3, 1).squeeze(),'conv1x.pdf','Trained Filters In First X View 7x7 Convolutional Layer')
224 
225 
226  # feat1 = net.blobs['conv1/7x7_s2_x'].data
227  # vis_square(feat1.reshape(np.delete(feat1.shape,0)),'feat1_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf','Feature Map From First X View 7x7 Convolutional Layer', padval=1)
228  # feat2 = net.blobs['conv1/7x7_s2_y'].data
229 
230  # vis_square(feat2.reshape(np.delete(feat2.shape,0)),'feat1_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y.pdf','Feature Map From First Y View 7x7 Convolutional Layer', padval=1)
231 
232  # iiiax = net.blobs['inception_3a/output_x'].data
233  # print iiiax.shape
234  # print iiiax.reshape(np.delete(iiiax.shape,0)).shape
235  # vis_square(iiiax.reshape(np.delete(iiiax.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf','Feature Map From First X View Inception Module', padval=1)
236  # iiiay = net.blobs['inception_3a/output_y'].data
237  # vis_square(iiiay.reshape(np.delete(iiiay.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y.pdf','Feature Map From First Y View Inception Module', padval=1)
238 
239  iiiax = net.blobs['inception_3a/output_x'].data
240  print iiiax.shape
241  print iiiax.reshape(np.delete(iiiax.shape,0)).shape
242  vis_square_kernel_zoom(iiiax.reshape(np.delete(iiiax.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x_muonfilter.pdf','Feature Map From First X View Inception Module', 156,167,153,144,padval=1)
243  iiiay = net.blobs['inception_3a/output_y'].data
244  vis_square_kernel_zoom(iiiay.reshape(np.delete(iiiay.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y_muonfilter.pdf','Feature Map From First Y View Inception Module',156,167,153,144, padval=1)
245 
246  iiiax = net.blobs['inception_3a/output_x'].data
247  print iiiax.shape
248  print iiiax.reshape(np.delete(iiiax.shape,0)).shape
249  vis_square_kernel_zoom(iiiax.reshape(np.delete(iiiax.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x_wholeeventfilter.pdf','Feature Map From First X View Inception Module', 0,11,76,67,padval=1)
250  iiiay = net.blobs['inception_3a/output_y'].data
251  vis_square_kernel_zoom(iiiay.reshape(np.delete(iiiay.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y_wholeeventfilter.pdf','Feature Map From First Y View Inception Module',0,11,76,67, padval=1)
252 
253  iiiax = net.blobs['inception_3a/output_x'].data
254  print iiiax.shape
255  print iiiax.reshape(np.delete(iiiax.shape,0)).shape
256  vis_square_kernel_zoom(iiiax.reshape(np.delete(iiiax.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x_hadronicfilter.pdf','Feature Map From First X View Inception Module', 26,37,65,56,padval=1)
257  iiiay = net.blobs['inception_3a/output_y'].data
258  vis_square_kernel_zoom(iiiay.reshape(np.delete(iiiay.shape,0)),'iiia_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y_hadronicfilter.pdf','Feature Map From First Y View Inception Module',26,37,65,56, padval=1)
259 
260 
261 
262  # iiibx = net.blobs['inception_3b/output_x'].data
263  # print iiibx.shape
264  # print iiibx.reshape(np.delete(iiibx.shape,0)).shape
265  # vis_square(iiibx.reshape(np.delete(iiibx.shape,0)),'iiib_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf', 'Feature Map From Second X View Inception Module', padval=1)
266  # iiiby = net.blobs['inception_3b/output_y'].data
267  # vis_square(iiiby.reshape(np.delete(iiiby.shape,0)),'iiib_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y.pdf', 'Feature Map From Second Y View Inception Module', padval=1)
268 
269  # ivax = net.blobs['inception_4a/output_x'].data
270  # print ivax.shape
271  # print ivax.reshape(np.delete(ivax.shape,0)).shape
272  # vis_square(ivax.reshape(np.delete(ivax.shape,0)),'iva_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf', 'Feature Map From Third X View Inception Module', padval=1)
273  # ivay = net.blobs['inception_4a/output_y'].data
274  # vis_square(ivay.reshape(np.delete(ivay.shape,0)),'iva_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y.pdf', 'Feature Map From Third X View Inception Module', padval=1)
275 
276  # ivax = net.blobs['inception_4a/output_x'].data
277  # print ivax.shape
278  # print ivax.reshape(np.delete(ivax.shape,0)).shape
279  # vis_square_kernel_zoom(ivax.reshape(np.delete(ivax.shape,0)),'iva_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x_finalfeaturenumu.pdf', 'Feature Map From Third X View Inception Module',147,152,29,25, padval=1)
280  # ivay = net.blobs['inception_4a/output_y'].data
281  # vis_square_kernel_zoom(ivay.reshape(np.delete(ivay.shape,0)),'iva_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y_finalfeaturenumu.pdf', 'Feature Map From Third X View Inception Module',147,152,29,25, padval=1)
282 
283 
284  # ivax = net.blobs['inception_4a/output_x'].data
285  # print ivax.shape
286  # print ivax.reshape(np.delete(ivax.shape,0)).shape
287  # vis_square_kernel_zoom(ivax.reshape(np.delete(ivax.shape,0)),'iva_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x_finalfeaturenc.pdf', 'Feature Map From Third X View Inception Module',154,159,125,121, padval=1)
288  # ivay = net.blobs['inception_4a/output_y'].data
289  # vis_square_kernel_zoom(ivay.reshape(np.delete(ivay.shape,0)),'iva_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_y_finalfeaturenc.pdf', 'Feature Map From Third X View Inception Module',154,159,125,121, padval=1)
290 
291 
292 
293  # vbx = net.blobs['inception_5b/output'].data
294  # print vbx.shape
295  # print vbx.reshape(np.delete(vbx.shape,0)).shape
296  # vis_square(vbx.reshape(np.delete(vbx.shape,0)),'vb_truetype'+str(label)+'_caltype'+str(plabel)+'_event'+str(count)+'_x.pdf', 'Feature Map From Final Combined View Inception Module', padval=1)
297 
298 
299 
300 
301 
302  # vby = net.blobs['inception_5b/output_y'].data
303  # vis_square(vby.reshape(np.delete(vby.shape,0)),'vb_'+str(count)+'_y.pdf', padval=1)
304 
305  # maxoutx = net.blobs['maxout'].data
306  # print maxoutx.shape
307  # print maxoutx.reshape(np.delete(maxoutx.shape,0)).shape
308  # vis_square(maxoutx.reshape(np.delete(maxoutx.shape,0)),'maxout_'+str(count)+'_x.pdf', padval=1)
309 
310 
311  #vis_square(feat)
312 
313 
314 
315  pidSignal=float(out['prob'][0][1])
316  pidBackground=float(out['prob'][0][0])
317 
318  #arrpidSignal[0]=float(out['prob'][0][1])
319  #arrpidBackground[0]=float(out['prob'][0][0])
320 
321 
322 
323  print label
324 
325  iscorrect = label == plabel
326  correct = correct + (1 if iscorrect else 0)
327  matrix[(label, plabel)] += 1
328  labels_set.update([label, plabel])
329 
330  # if not iscorrect:
331  # print("\rError: key=%s, expected %i but predicted %i" \
332  # % (key, label, plabel))
333 
334  sys.stdout.write("\rAccuracy: %.1f%% Progress: %.2f%%" % (100.*correct/count,100.*count/3431748))
335  sys.stdout.flush()
336  count = count + 1
337  print(str(correct) + " out of " + str(count) + " were classified correctly")
338 
339  print ""
340  print "Confusion matrix:"
341  print "(r , p) | count"
342  for l in labels_set:
343  for pl in labels_set:
344  print "(%i , %i) | %i" % (l, pl, matrix[(l,pl)])
345 
346 
347 
348 
349 
350 
def vis_square_kernel_zoom(data, outfile, title, xminval, xmaxval, yminval, ymaxval, padsize=1, padval=0)
def vis_square(data, outfile, title, padsize=1, padval=0)
bool print
def vis_square_kernel(data, outfile, title, padsize=1, padval=0)
static float_mat transpose(const float_mat &a)
returns the transposed matrix.