Skip to content

Commit

Permalink
Merge pull request #19 from LArbys/tmw_multich_dropmean
Browse files Browse the repository at this point in the history
very nice -- i'll add some labeling, have updated two other image types -- fake color and usual default image.py
  • Loading branch information
Victor Genty committed May 5, 2016
2 parents 96d5f06 + f36bda4 commit b23eae0
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 34 deletions.
5 changes: 4 additions & 1 deletion mac/pyrgb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@

larcv.load_pyutil

import cv2
try:
import cv2
except:
print "NO CV2"

77 changes: 61 additions & 16 deletions mac/pyrgb/display/rgbdisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
from ..lib.hoverrect import HoverRect
from ..lib.roislider import ROISlider

from cv2layout import CV2Layout
from caffelayout import CaffeLayout
from ..rgb_caffe.testwrapper import TestWrapper
try:
from cv2layout import CV2Layout
except:
pass
try:
from caffelayout import CaffeLayout
from ..rgb_caffe.testwrapper import TestWrapper
except:
pass

class RGBDisplay(QtGui.QWidget) :

Expand Down Expand Up @@ -91,16 +97,14 @@ def __init__(self,argv):

#Check boxes for drawing plane1/2/3 -- perhaps should
#become tied to current image being shown (N planes...)
self.p0 = QtGui.QCheckBox("Plane 0")
self.p0.setChecked(True)
# tmw -- changing it so that one can select the channel to show in the RGB channels
self.p0 = QtGui.QComboBox()
self.lay_inputs.addWidget( self.p0, 1, 9 )

self.p1 = QtGui.QCheckBox("Plane 1")
self.p1.setChecked(True)
self.p1 = QtGui.QComboBox()
self.lay_inputs.addWidget( self.p1, 1, 10 )

self.p2 = QtGui.QCheckBox("Plane 2")
self.p2.setChecked(True)
self.p2 = QtGui.QComboBox()
self.lay_inputs.addWidget( self.p2, 1, 11 )

self.planes = [ self.p0, self.p1, self.p2 ]
Expand Down Expand Up @@ -143,6 +147,11 @@ def __init__(self,argv):
#RGBCaffe will open and close bottom of the window
self.rgbcaffe = QtGui.QPushButton("Enable RGBCaffe")
self.rgbcv2 = QtGui.QPushButton("Enable OpenCV")
try:
import cv2
except:
print "No OpenCV. Disabling."
self.rgbcv2.setEnabled(False)

self.rgbcaffe.setFixedWidth(130)
self.rgbcv2.setFixedWidth(130)
Expand Down Expand Up @@ -189,9 +198,15 @@ def __init__(self,argv):

### Caffe Widgets
#wrapper for FORWARD function
self.caffe_test = TestWrapper()
#wrapper for the caffe specific layout
self.caffe_layout = CaffeLayout(self.caffe_test)
try:
self.caffe_test = TestWrapper()
#wrapper for the caffe specific layout
self.caffe_layout = CaffeLayout(self.caffe_test)
self.has_caffe = True
except:
print "Caffe Disabled"
self.has_caffe = False
self.rgbcaffe.setEnabled(False)

### OpenCV Widgets
#wrapper for the opencv specific window
Expand Down Expand Up @@ -334,8 +349,10 @@ def setViewPlanes(self):

self.views = []
for ix, p in enumerate( self.planes ):
if p.isChecked():
self.views.append(ix)
if p.currentIndex()!=0:
self.views.append( int(p.currentText()) )
else:
self.views.append( -1 ) # sentinal for don't fill this channel


def plotData(self):
Expand All @@ -354,20 +371,48 @@ def plotData(self):
imin = int( self.imin.text() )
imax = int( self.imax.text() )

# update channel combo boxes
nchs = self.dm.get_nchannels(event,self.image_producer)
print "Set the RGB drop boxes: ",nchs," current count=",self.p0.count()
if self.p0.count()!=(nchs+1):
self.p0.clear()
self.p1.clear()
self.p2.clear()
# the none channel must be zero. else we have to know the number of channels
self.p0.insertItem(0,"(none)")
self.p1.insertItem(0,"(none)")
self.p2.insertItem(0,"(none)")
for ch in range(0,nchs):
self.p0.insertItem( ch+1, "%d"%(ch) )
self.p1.insertItem( ch+1, "%d"%(ch) )
self.p2.insertItem( ch+1, "%d"%(ch) )
if nchs>0:
self.p0.setCurrentIndex(1)
if nchs>1:
self.p1.setCurrentIndex(nchs/3+1)
if nchs>2:
self.p2.setCurrentIndex(nchs/3*2+1)

self.setViewPlanes()

pimg, self.rois, plotimage = self.dm.get_event_image(event,imin,imax,
self.image_producer,
self.roi_producer,
self.views)


self.image = plotimage.imgs

if pimg is None:
self.image = None
return

self.caffe_test.set_image(plotimage.orig_mat)
if self.has_caffe:
# eventually we need to replace the modified images with the ones here
pushit = np.zeros( (plotimage.orig_mat[:,:,0].shape[0],plotimage.orig_mat[:,:,0].shape[1],nchs),dtype=np.float32)
for ix,img in enumerate(plotimage.img_v):
pushit[:,:,ix] = img
self.caffe_test.set_image(pushit)
self.pimg = pimg

# Emplace the image on the canvas
Expand Down
6 changes: 6 additions & 0 deletions mac/pyrgb/lib/datamanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def __init__(self,argv):

self.loaded = {}

def get_nchannels(self,ii,imgprod) :
# Sorry Vic I hacked this
self.iom.read_entry(ii)
imdata = self.iom.get_data(larcv.kProductImage2D,imgprod)
return imdata.Image2DArray().size()

def get_event_image(self,ii,imin,imax,imgprod,roiprod,planes) :

#Load data in TChain
Expand Down
15 changes: 5 additions & 10 deletions mac/pyrgb/lib/image_types/ch12image.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,17 @@ def __create_mat__(self):
#compressed images all have the same shape
self.orig_mat = np.zeros(list(self.img_v[0].shape) + [3])

try:
ch = int( str(self.channel.text()) )
except ValueError:
print "\t>> Channel no good defaulting to zero!"
ch = 0

for ix,img in enumerate(split[ch]):
for p,fill_ch in enumerate(self.planes):

if ix not in self.planes: continue
if fill_ch==-1:
continue

self.orig_mat[:,:,ix] = img
self.orig_mat[:,:,p] = self.img_v[fill_ch]

self.plot_mat = self.orig_mat.copy()

self.plot_mat = self.plot_mat[:,::-1,:]

self.plot_mat[:,:,0][ self.plot_mat[:,:,1] > 0.0 ] = 0.0
self.plot_mat[:,:,0][ self.plot_mat[:,:,2] > 0.0 ] = 0.0
self.plot_mat[:,:,1][ self.plot_mat[:,:,2] > 0.0 ] = 0.0
Expand Down
5 changes: 4 additions & 1 deletion mac/pyrgb/lib/imagefactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ def __init__(self):
def get(self,imdata,roidata,planes,improd,**kwargs):

if improd == "fake_color": return FakeColorImage(imdata,roidata,planes)
if improd == "tpc_12ch": return Ch12Image(imdata,roidata,planes)
if improd == "tpc_12ch_mean":
ch12 = Ch12Image(imdata,roidata,planes)
ch12.temp_window.show()
return ch12

return DefaultImage(imdata,roidata,planes)
22 changes: 16 additions & 6 deletions mac/pyrgb/rgb_caffe/testwrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def load(self):
self.caffe.set_mode_gpu()
self.caffe.set_device(set.config['gpuid'])

if "channels" in self.config:
self.nchannels = self.config["channels"]
else:
self.nchannels = 3

self.__generate_model__()
self.__create_net__()

Expand All @@ -78,8 +83,10 @@ def prep_image(self):
self.iom.read_entry(0)
means = self.iom.get_data(larcv.kProductImage2D,self.config['meanproducer'])
self.mean_v = [ larcv.as_ndarray(img) for img in means.Image2DArray() ]
print "Mean channels=",len(self.mean_v)," mean size=",self.mean_v[0].shape

for ix,mean in enumerate(self.mean_v):
print "check mean shape againts: ",im[:,:,ix].shape
assert mean.shape == im[:,:,ix].shape
im[:,:,ix] -= mean

Expand All @@ -97,7 +104,9 @@ def forward_result(self):

im = self.prep_image()

blob['data'] = np.zeros((1, im.shape[0], im.shape[1], 3),dtype=np.float32)
print "FORWARD ON IMAGE: ",im.shape

blob['data'] = np.zeros((1, im.shape[0], im.shape[1], im.shape[2]),dtype=np.float32)
print blob['data'].shape

blob['data'][0,:,:,:] = im
Expand All @@ -114,22 +123,23 @@ def forward_result(self):

forward_kwargs = {'data': blob['data'] ,'label': blob['label']}


blobs_out = self.net.forward(**forward_kwargs)

scores = self.net.blobs[ self.config['lastfc'] ].data
#softmax = self.net.blobs[ self.config['loss'] ].data

self.scores = scores
print "Scores: {}".format(scores)
#print "Softmax: {}".format(softmax)



def __generate_model__(self):

print "\t>> Got an image of shape: {}".format(self.pimg.shape)
td = ""
td += "input: \"data\"\n"
td += "input_shape: { dim: 1 dim: 3 dim: %s dim: %s } \n"%(self.pimg.shape[0],
self.pimg.shape[1])
td += "input_shape: { dim: 1 dim: %d dim: %s dim: %s } \n"%(self.nchannels,
self.pimg.shape[0],
self.pimg.shape[1])
td += "input: \"label\"\n"
td += "input_shape: { dim: 1 }"

Expand Down
48 changes: 48 additions & 0 deletions mac/ubresnet_12ch_768.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Where is your caffe?
cafferoot: /home/taritree/working/larbys/caffe_larbys/python

# CPU or GPU?
usecpu : True

# GPU number
gpuid : 0

# Deploy version of the model, please remove ROOTDataLayer or whatever
modelfile: /home/taritree/working/larbys/resnet-18/training_attempts/ubtri/001/ub_trimese_resnet_headlessdeploy.prototxt

# Pretrained caffe model
pretrainedmodel: /home/taritree/working/larbys/resnet-18/training_attempts/ubtri/001/snapshot_rmsprop_iter_stage3.caffemodel

# The mean file location on disk
meanfile: /home/taritree/working/larbys/lmdb2image2d/mean.root

# Producer for the mean file
meanproducer: tpc_12ch_mean

# Wrapper layer will make a copy of model file (has to sit somewhere on disk...)
# the will all it's own input layer on top so make sure the first
# real layer has input "data", we will try to guess the dimensions
# based on image in the pgplot window
model: /tmp/test.prototxt

# Number of channels
channels: 12

# Thresholding parameter iMin and iMax
imin : 0
imax : 255

# Can be per plane (per channel...) but is currently unused
imin_plane0: 0
imin_plane1: 0
imin_plane2: 0

imax_plane0: 255
imax_plane1: 255
imax_plane2: 255

# This is the name of the last FC layer which computer the score
lastfc: probt

# This layer doesn't have to be here but that's OK, what is the name of the loss layer
loss: loss

0 comments on commit b23eae0

Please sign in to comment.