-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyze_test_data.py
291 lines (252 loc) · 9.91 KB
/
analyze_test_data.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import caffe
import numpy as np
import ROOT as rt
from math import log
from array import array
from larcv import larcv
from countpe import countpe
import time
import sys
import cv2
# This script setups caffe in TEST mode
gpu_id = 0
caffe.set_device(gpu_id)
caffe.set_mode_gpu()
#caffe.set_mode_cpu()
prototxt = "ub_uresnet_deploy.prototxt"
model = "snapshot_rmsprop_uburn_iter_4100.caffemodel"
#model = "saved_states/smallsampletest_snapshot_rmsprop_uburn_iter_3000.caffemodel"
tpcproducer = "tpc_hires_crop"
pmtproducer = "pmt"
out_tag = "test"
save_images = True
# v0 data files
# REMEMBER: this file list must match the one in filler_test.cfg -- we are trying to get model scores and also calcualte PMT values that are of the correct entry
rootfiles = ["/mnt/raid0/taritree/v0/hires_test/bnb_mc_hirescrop_valid.root"]
#rootfiles = ["/mnt/raid0/taritree/v0/hires_test/bnb_mc_hirescrop_train.root"]
#rootfiles = ["ex_data/larcv_hirescrop_out_0010.root"]
net = caffe.Net( prototxt, model, caffe.TEST )
input_shape = net.blobs["data"].data.shape
batch_size = input_shape[0]
class_name = ["bg","shower","MIP","HIP"]
color_index = [-1,1,0,2] # BGR format
color_dict = {"bg":-1,
"shower":1,
"MIP":0,
"HIP":2}
#nevents = 36
nevents = 100
# setup input files string for PSet
str_input = "["
for r in rootfiles:
str_input += r
if r != rootfiles[-1]:
str_input+=","
str_input += "]"
print str_input
# ROOT data
from ROOT import std
str_parname = std.string( "IOMan2" )
#iocfg = larcv.PSet(str_parname,str_iomancfg)
iocfg = larcv.PSet("IOMan2")
iocfg.add_value( "Name", "IOMan2" )
iocfg.add_value( "IOMode", "0" )
iocfg.add_value( "Verbosity", "2" )
iocfg.add_value( "InputFiles", str_input )
iocfg.add_value( "ReadOnlyType", "[0,0,0,1]" )
iocfg.add_value( "ReadOnlyName", "[tpc_hires_crop,segment_hires_crop,pmt,tpc_hires_crop]" )
ioman = larcv.IOManager( iocfg )
ioman.initialize()
if nevents is None:
nevents = ioman.get_n_entries()
print "Network Ready: Batch Size=",batch_size
print "[ENTER] to continue."
raw_input()
# setup output
out = rt.TFile("out_%s_netanalysis.root"%(out_tag), "RECREATE" )
# file entry number
entry = array('i',[0])
# event ID
run = array('i',[0])
subrun = array('i',[0])
eventid = array('i',[0])
# truth information
mode = array('i',[0])
current = array('i',[0])
enu = array('f',[0])
vtx = array('f',[0]*3)
pe = array('f',[0.0])
peped = array('f',[0.])
# label statistics
npixels = array('i',[0]) # number of interesting pixels
nlabels = array('i',[0,0,0,0]) # bg, shower, mip, proton
npredict = array('i',[0,0,0,0])
label_acc = array('f',[0.,0.,0.,0.])
label_q = array('f',[0.,0.,0.,0.])
predict_q = array('f',[0.,0.,0.,0.])
total_acc = array('f',[0.])
tree = rt.TTree("net","net output")
tree.Branch("entry",entry,"entry/I")
tree.Branch("run", run, "run/I")
tree.Branch("subrun", subrun, "subrun/I")
tree.Branch("event", eventid, "event/I")
tree.Branch("current", current, "current/I")
tree.Branch("mode", mode, "mode/I")
tree.Branch("enu", enu, "enu/F")
tree.Branch("vtx", vtx, "vtx[3]/F")
tree.Branch("pe", pe, "pe/F" )
tree.Branch("peped", peped, "peped/F" )
tree.Branch("npixels",npixels,"npixels/I")
tree.Branch("nlabels",nlabels,"nlabels[4]/I")
tree.Branch("npredict",npredict,"npredict[4]/I")
tree.Branch("total_acc",total_acc,"total_acc/F")
tree.Branch("label_acc",label_acc,"label_acc[4]/F")
tree.Branch("label_q",label_q,"label_q[4]/F")
tree.Branch("predict_q",predict_q,"predict_q[4]/F")
nbatches = nevents/batch_size
if nevents%batch_size!=0:
nbatches += 1
filler = larcv.ThreadFillerFactory.get_filler("deploy")
ibatch = 0
ievents = 0
while ibatch<nbatches:
print "batch ",ibatch," of ",nbatches
keys = []
# pass through network
net.forward()
data = net.blobs["data"].data
labels = net.blobs["label"].data
score_raw = net.blobs["crop_score"].data
scores = net.blobs["softmax"].data
processed = filler.processed_entries()
print "number of process entries: ",processed.size()
print " data: ",data.shape
print " labels: ",labels.shape
print " scores: ",scores.shape
# evaluate each image
for b in range(batch_size):
print "Image ",(ibatch,b,ievents)
file_entry = processed[b]
ioman.read_entry( file_entry )
# LArCV info
eventroi = ioman.get_data( larcv.kProductROI, "tpc_hires_crop" )
current[0] = eventroi.at(0).NuCurrentType()
mode[0] = eventroi.at(0).NuInteractionType()
entry[0] = ievents
run[0] = eventroi.run()
subrun[0] = eventroi.subrun()
eventid[0] = eventroi.event()
pe[0],maxpmtch, peped[0] = countpe( ioman, pmtproducer )
# look for neutrino ROI
enu[0] = -1.0
vtx[0] = 0.0
vtx[1] = 0.0
vtx[2] = 0.0
for roi in eventroi.ROIArray():
if roi.PdgCode()==14 or roi.PdgCode()==12:
enu[0] = roi.EnergyInit()
vtx[0] = roi.X()
vtx[1] = roi.Y()
vtx[2] = roi.Z()
break
for i in range(0,4):
label_q[i] = 0.0
predict_q[i] = 0.0
#out_image = np.zeros( (2*data.shape[2]+1,2*data.shape[3]+1,3) )
#out_image[data.shape[2],:,:] = 100.0
#out_image[:,data.shape[3],:] = 100.0
out_image = np.zeros( ( data.shape[2], 2*data.shape[3]+1, 3 ) )
out_image[:,data.shape[2],:] = 100.0
above_thresh = np.where( data[b,0,:,:]>0 )
npixels[0] = len(above_thresh[0])
particle = np.where( scores[b,0,:,:]<0.5 )
print " nabove thresh=",npixels[0]," frac=",float(npixels[0])/(data.shape[2]*data.shape[3])
print " not predicted background=",len(particle[0])
if npixels[0]==0:
npixels[0]
total_acc[0] = -1.0
for i in range(0,4):
nlabels[i] = 0
npredict[i] = 0
label_acc[i] = -1.0
tree.Fill()
continue
# make output image
out_image[0:data.shape[2],0:data.shape[3],0] = data[b,0,:,:] # data image
out_image[0:data.shape[2],0:data.shape[3],1] = data[b,0,:,:] # data image
out_image[0:data.shape[2],0:data.shape[3],2] = data[b,0,:,:] # data image
#out_image[data.shape[2]+1:,0:data.shape[3]] = scores[b,1,:,:]*100.0 # MIP image
#out_image[0:data.shape[2],data.shape[3]+1:] = scores[b,2,:,:]*100.0 # Shower image
#out_image[data.shape[2]+1:,data.shape[3]+1:] = scores[b,3,:,:]*100.0 # HIP image
out_offset = {0:(0,0),1:(0,data.shape[2]+1),2:(data.shape[2]+1,0),3:(data.shape[2]+1,data.shape[3]+1)}
offset = data.shape[3]+1
# summarize
all_ncorrect = 0
class_correct = [0,0,0,0]
class_npixels = [0,0,0,0]
class_npredict = [0,0,0,0]
class_nanswer = [0,0,0,0]
n_nonbg = 0
for x,y in zip(above_thresh[0],above_thresh[1]):
answer = (int)(labels[b,0,x,y]+0.1)
predict = np.argmax( scores[b,:,x,y] )
nbg_predict = np.argmax( scores[b,1:,x,y] )+1
#predict = np.argmax( scores[b,1:,x,y] )+1
#print "truth=",answer,"predict=",predict,scores[b,:,x,y]
class_npixels[answer] += 1
class_npredict[predict] += 1
if answer==predict:
class_correct[answer] += 1
all_ncorrect += 1
if predict!=0:
n_nonbg += 1
label_q[answer] += data[b,0,x,y]
predict_q[predict] += data[b,0,x,y]
# color in image
if answer in [1,3]:
for iclass in [1,2,3]:
if iclass==answer:
out_image[ x, y, color_index[answer] ] = data[b,0,x,y]
else:
out_image[ x, y, color_index[iclass] ] = 0
elif answer==2:
# color in cyan if sample class
out_image[ x, y, 0 ] = data[b,0,x,y]
out_image[ x, y, 1 ] = data[b,0,x,y]
out_image[ x, y, 2 ] = 0
# color in prediction
#if predict in [1,3]:
# out_image[out_offset[predict][0]+x,out_offset[predict][1]+y,color_index[predict]] = scores[b,predict,x,y]*100.0
#else:
# out_image[out_offset[predict][0]+x,out_offset[predict][1]+y,0] = scores[b,predict,x,y]*100.0
# out_image[out_offset[predict][0]+x,out_offset[predict][1]+y,1] = scores[b,predict,x,y]*100.0
if predict in [1,3]:
out_image[x,offset+y,color_index[predict]] = scores[b,predict,x,y]*100.0
else:
out_image[x,offset+y,0] = scores[b,predict,x,y]*100.0
out_image[x,offset+y,1] = scores[b,predict,x,y]*100.0
total_acc[0] = float(all_ncorrect)/float(npixels[0])
print " correct (all classes): ",total_acc[0]
print " non-bg predictions: ",n_nonbg
for i,n in enumerate(class_npixels):
nlabels[i] = n
for iclass,ncorr in enumerate(class_correct):
if class_npixels[iclass]>0:
acc = float(ncorr)/float(class_npixels[iclass])
print " correct[",class_name[iclass],"]: ",acc," of ",class_npixels[iclass]," pixels"
label_acc[iclass] = acc
else:
print " correct[",class_name[iclass],"]: no true pixels."
label_acc[iclass] = -1.0
for iclass,npredicted in enumerate(class_npredict):
print " predicted[",class_name[iclass],"]: ",float(npredicted)/float(npixels[0])," of ",class_npixels[iclass]," pixels"
npredict[iclass] = npredicted
if save_images:
#if enu[0]<500 and current[0]==0:
cv2.imwrite( "labelmaps/label_map_%03d_mode%04d.png"%(ievents,mode[0]), out_image )
if ievents<nevents:
tree.Fill()
ievents += 1
#raw_input()
ibatch += 1
out.Write()