Skip to content

Instantly share code, notes, and snippets.

@tariqul-islam
Last active April 20, 2019 09:03
Show Gist options
  • Save tariqul-islam/043ac8d9965abd5279d6cce396722b95 to your computer and use it in GitHub Desktop.
Save tariqul-islam/043ac8d9965abd5279d6cce396722b95 to your computer and use it in GitHub Desktop.
How much did you speed up using caffe? Let's check it in reference caffenet.
#This is a basic benchmark code to see how fast
#your GPU is compared to your CPU
#
#Before you begin:
#go to caffe root, copy this code there
#get the bvlc_reference_caffenet using these two commands in caffe root:
#./scripts/download_model_binary.py models/bvlc_reference_caffenet
#./data/ilsvrc12/get_ilsvrc_aux.sh
#
#import stuff
import caffe
import numpy as np
#import matplotlib.pyplot as plt
#from PIL import Image
import timeit
#load the model
#model description is in deploy.prototxt
#weights are in bvlc_reference_caffenet.caffemodel
net = caffe.Net('models/bvlc_reference_caffenet/deploy.prototxt',
'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',
caffe.TEST)
#load input and configure preprocessing
#caffe.io.Transformer funciton defines the objec that defines
#transformation of an image as a preprocessor
#object.preprocess processes the image
transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
transformer.set_mean('data', np.load('python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1))
transformer.set_transpose('data',(2,0,1)) #(H,W,C) to (C,H,W); channel comes first
transformer.set_channel_swap('data', (2,1,0)) #image loads in RGB, convert it to BGR or vice versa
transformer.set_raw_scale('data',255.0)
#we need to classify one image only
#see the current shape of the input blob / data blob
print net.blobs['data'].data.shape
#it prints (10,3,227,227)
#10 is number of images per batch.
#we want to use only one image to test, so we change it to 1
net.blobs['data'].reshape(1,3,227,227)
#now we load the image
im = caffe.io.load_image('examples/images/cat.jpg')
#now add the image in data
net.blobs['data'].data[...] = transformer.preprocess('data',im)
#set CPU as the computing device
caffe.set_mode_cpu()
#compute the output
out = net.forward()
#now time it in CPU
t1 = timeit.default_timer()
out = net.forward()
t2 = timeit.default_timer()
#set GPU as the computing device
caffe.set_device(0)
caffe.set_mode_gpu()
#compute the output
out = net.forward()
#now time it again in GPU
t3 = timeit.default_timer()
out = net.forward()
t4 = timeit.default_timer()
print 'CPU Time : ', t2-t1
print 'GPU Time : ', t4-t3
print 'GPU is ', (t2-t1)/(t4-t3), ' times faster'
#print predicted labels
#labels = np.loadtxt('data/ilsvrc12/synset_words.txt', str, delimiter='\t')
#top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]
#print labels[top_k]
@tariqul-islam
Copy link
Author

CPU Time : 0.150242090225
GPU Time : 0.00464415550232
GPU is 32.3507880281 times faster

mine. Now it seems impressive.

@mortred12306
Copy link

Thank you, it helps me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment