Skip to content

Instantly share code, notes, and snippets.

@karszawa
Created January 1, 2018 13:52
Show Gist options
  • Save karszawa/a9de267076f6df28923bf33233a8d475 to your computer and use it in GitHub Desktop.
Save karszawa/a9de267076f6df28923bf33233a8d475 to your computer and use it in GitHub Desktop.
import caffe
import numpy as np
transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', mu)
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
PROTOTXT = 'test.pt'
CAFFEMODEL = 'alexnet_iter_10000.caffemodel'
caffe.set_mode_gpu()
caffe.set_device(0)
net = caffe.Net(PROTOTXT, CAFFEMODEL, caffe.TEST)
mu = np.load('mean.npy')
mu = mu.mean(1).mean(1)
while True:
line = raw_input()
if line == -1:
break
filename, y = line.split(' ')
image = caffe.io.load_image(filename)
transformed_image = transformer.preprocess('data', image)
net.blobs['data'].data[...] = transformed_image
net.forward()
output = net.blobs['prob'].data[0, :].copy()
predict_index = output.argmax()
score = output.max()
print filename, y, predict_index, score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment