Skip to content

Instantly share code, notes, and snippets.

@takatakamanbou
Last active March 16, 2016 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatakamanbou/20afa664254abfe71225 to your computer and use it in GitHub Desktop.
Save takatakamanbou/20afa664254abfe71225 to your computer and use it in GitHub Desktop.

ex160307

see http://takatakamanbou.hatenablog.com/entry/2016/03/07/221032

source

experimental results

%run ex160307.py
# fnModel   =  lenet.prototxt
# fnTrained =  lenet_iter_10000.caffemodel
# conv1 (20, 1, 5, 5) (20,)
# conv2 (50, 20, 5, 5) (50,)
# ip1 (500, 800) (500,)
# ip2 (10, 500) (10,)
# useGPU =  True
# X.shape =  (10000, 28, 28, 1)
# recognition rate =  0.9902
# time =  4.34732294083

%run ex160307.py
# useGPU =  False
# time =  7.81033301353

%run ex160307b.py
# useGPU =  True
# time =  0.200743913651

%run ex160307b.py
# useGPU =  False
# time =  4.46612715721
import numpy as np
import mnist
import caffe
import time
### reading the pre-trained CNN
fnModel = 'lenet.prototxt'
fnTrained = 'lenet_iter_10000.caffemodel'
cnn = caffe.Classifier( fnModel, fnTrained )
print '# fnModel = ', fnModel
print '# fnTrained = ', fnTrained
for ln, params in cnn.params.items():
print '#', ln, params[0].data.shape, params[1].data.shape
useGPU = False
if useGPU:
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
print '# useGPU = ', useGPU
### reading the test data
mnist = mnist.MNIST( pathMNIST = '../../150117-mnist' )
LT = 'T'
dat = mnist.getImage( LT )
lab = mnist.getLabel( LT )
w, h = mnist.ncol, mnist.nrow
nch = 1
#ndat = 100
ndat = dat.shape[0]
### predicting the class labels for the data
X = dat[:ndat, :, np.newaxis].reshape( ( -1, h, w, nch ) ) / 256
print '# X.shape = ', X.shape
ts = time.time()
Z = cnn.predict( X, oversample = False )
te = time.time()
pred = np.argmax( Z, axis = 1 )
cr = np.sum( pred == lab[:ndat] ) / float( ndat )
print '# recognition rate = ', cr
print '# time = ', te - ts
import numpy as np
import mnist
import caffe
import time
### reading the pre-trained CNN
fnModel = 'lenet.prototxt'
fnTrained = 'lenet_iter_10000.caffemodel'
cnn = caffe.Net( fnModel, fnTrained, caffe.TEST )
print '# fnModel = ', fnModel
print '# fnTrained = ', fnTrained
for ln, params in cnn.params.items():
print '#', ln, params[0].data.shape, params[1].data.shape
useGPU = False
if useGPU:
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
print '# useGPU = ', useGPU
### reading the test data
mnist = mnist.MNIST( pathMNIST = '../../150117-mnist' )
LT = 'T'
dat = mnist.getImage( LT )
lab = mnist.getLabel( LT )
w, h = mnist.ncol, mnist.nrow
nch = 1
#ndat = 100
ndat = dat.shape[0]
### predicting the class labels for the data
X = dat[:ndat, np.newaxis, :].reshape( ( -1, nch, h, w ) ) / 256
print '# X.shape = ', X.shape
ts = time.time()
#rv = cnn.forward_all( data = X )
rv = cnn.forward_all( **{cnn.inputs[0]:X} )
#Z = rv['prob']
Z = rv[cnn.outputs[0]]
te = time.time()
pred = np.argmax( Z, axis = 1 )
cr = np.sum( pred == lab[:ndat] ) / float( ndat )
print '# recognition rate = ', cr
print '# time = ', te - ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment