Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou takatakamanbou/00_ex160324.md Secret
Last active Mar 29, 2016

Embed
What would you like to do?
ex160324
from __future__ import print_function
import numpy as np
import mnist
import caffe
import theano
import convnet160323 as convnet
### converting Caffe-CNN to Theano-CNN
#
def caffe2theano( cnnC ):
### getting the parameters of the Caffe-CNN
print( '### Caffe-CNN ###' )
batchshape = tuple( cnnC.blobs[cnnC.inputs[0]].shape )
Xdim = batchshape[1:]
print( '#', cnnC.inputs[0], Xdim )
layersC = {}
for ln, params in cnnC.params.items():
print( '#', ln, params[0].data.shape, params[1].data.shape )
if len( params ) == 1: # no bias
layersC[ln] = { 'W':params[0].data }
else:
layersC[ln] = { 'W':params[0].data, 'b':params[1].data }
### converting the parameters and initializing the Caffe-CNN
print( '### Theano: Conv-Pool-Conv-Pool-ReLu-Softmax' )
### L0
L0 = convnet.T4InputLayer( Xdim )
print( '# T4InputLayer: ', L0.Xshape, ' dropout = ', L0.dropout )
### L1conv & L1pool
l = layersC['conv1']
W = l['W'][:,:,::-1,::-1] # filters are flipped
Wdim = ( W.shape[0], W.shape[2], W.shape[3] )
withBias = 'b' in l
L1conv = convnet.ConvLayer( Xdim, Wdim, afunc = 'linear', withBias = withBias, border_mode = 'valid' )
if withBias:
b = l['b']
L1conv.setWeight( W, b )
else:
L1conv.setWeight( W )
L1pool = convnet.PoolLayer( L1conv.Yshape, ds = ( 2, 2 ), st = None )
layer = L1conv
print( '# ConvLayer: ', layer.Wshape, layer.Yshape, ' bmode = ', layer.border_mode )
layer = L1pool
print( '# PoolLayer: ', ' ds = ', layer.ds, ' st = ', layer.st, ' dropout = ', layer.dropout, layer.Yshape, layer.Dout )
### L2conv & L2pool
l = layersC['conv2']
W = l['W'][:,:,::-1,::-1] # filters are flipped
Wdim = ( W.shape[0], W.shape[2], W.shape[3] )
withBias = 'b' in l
L2conv = convnet.ConvLayer( L1pool.Yshape, Wdim, afunc = 'linear', withBias = withBias, border_mode = 'valid' )
if withBias:
b = l['b']
L2conv.setWeight( W, b )
else:
L2conv.setWeight( W )
L2pool = convnet.PoolLayer( L2conv.Yshape, ds = ( 2, 2 ), st = None )
layer = L2conv
print( '# ConvLayer: ', layer.Wshape, layer.Yshape, ' bmode = ', layer.border_mode )
layer = L2pool
print( '# PoolLayer: ', ' ds = ', layer.ds, ' st = ', layer.st, ' dropout = ', layer.dropout, layer.Yshape, layer.Dout )
### L3
layer = layersC['ip1']
W = layer['W']
Nunit, Din = W.shape
withBias = 'b' in layer
L3 = convnet.FullLayer( Din, Nunit, 'ReLu', withBias = withBias, T4toMat = True )
if withBias:
b = layer['b']
L3.setWeight( W, b )
else:
L3.setWeight( W )
layer = L3
print( '# FullLayer: ', layer.Din, layer.Nunit, layer.afunc, layer.dropout )
### L4
layer = layersC['ip2']
W = layer['W']
Nunit, Din = W.shape
withBias = 'b' in layer
L4 = convnet.FullLayer( Din, Nunit, 'linear', withBias = withBias, T4toMat = False )
if withBias:
b = layer['b']
L4.setWeight( W, b )
else:
L4.setWeight( W )
layer = L4
print( '# FullLayer: ', layer.Din, layer.Nunit, layer.afunc, layer.dropout )
### CNN
layersT = [ L0, L1conv, L1pool, L2conv, L2pool, L3, L4 ]
cnnT = convnet.CNN( layersT )
return cnnT
if __name__ == "__main__":
### reading the CNN trained by using Caffe
#
fnModel = '../160307-caffe/ex160307/lenet.prototxt'
fnTrained = '../160307-caffe/ex160307/lenet_iter_10000.caffemodel'
cnnC = caffe.Net( fnModel, fnTrained, caffe.TEST )
print( '# fnModel = ', fnModel )
print( '# fnTrained = ', fnTrained )
caffe.set_mode_cpu()
### converting the Caffe-CNN to Theano-CNN
#
theano.config.floatX = 'float32'
cnnT = caffe2theano( cnnC )
### reading the test data
#
mnist = mnist.MNIST( pathMNIST = '../150117-mnist' )
w, h = mnist.ncol, mnist.nrow
Xnch, Xrow, Xcol = cnnT.Layers[0].Xshape
batchsize = 100
XrawT = np.array( mnist.getImage( 'T' ) / 256, dtype = np.float32 )
labelT = np.asarray( mnist.getLabel( 'T' ), dtype = np.int32 )
XT = np.reshape( XrawT, ( -1, Xnch, Xrow, Xcol ) )
NT = XT.shape[0]
print( '# NT = ', NT )
### prediction by Caffe-CNN
#
print( '### prediction by Caffe-CNN' )
rv = cnnC.forward_all( **{cnnC.inputs[0]:XT} )
Z = rv[cnnC.outputs[0]]
predC = np.argmax( Z, axis = 1 )
erT = np.sum( predC != labelT ) / float( NT )
print( '%.2f %%' % ( erT * 100 ) )
### prediction by Theano-CNN
#
print( '### prediction by Theano-CNN' )
nbatch = int( np.ceil( float( NT ) / batchsize ) )
predT = np.empty( NT, dtype = int )
'''
for ib in range( nbatch - 1 ):
ii = np.arange( ib*batchsize, (ib+1)*batchsize )
Z = cnnT.output( XT[ii] )
predT[ii] = np.argmax( Z, axis = 1 )
ib = nbatch - 1
ii = np.arange( ib*batchsize, NT )
Z = cnnT.output( XT[ii] )
predT[ii] = np.argmax( Z, axis = 1 )
'''
Z = cnnT.output( XT )
predT = np.argmax( Z, axis = 1 )
erT = np.sum( predT != labelT ) / float( NT )
print( '%.2f %%' % ( erT * 100 ) )
### checking the difference
#
print( '# number of inconsistent predictions:', np.sum( predC != predT ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.