pyCaffe: Feature extraction on a batch of images

1,762 views
Skip to first unread message

Sam B.

unread,
Mar 4, 2016, 12:31:28 PM3/4/16
to Caffe Users
Hello,

I have the following pyCaffe script:

import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import caffe
from caffe.proto import caffe_pb2

caffe.set_device(0)
caffe.set_mode_gpu()
net=caffe.Net('pyblobs-Test-Eval.prototxt', 'best_snapshot_iter_4553.caffemodel', caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load('trainingMean.npy'))
transformer.set_raw_scale('data', 255)
transformer.set_channel_swap('data', (2,1,0))
net.blobs['data'].reshape(1,3,96,96)
net.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image('Test/A_4654.png'))
out=net.forward()
print out['fc8'][0][0]/3

What this does is print out the output from layer 'fc8' for image A_4654.png.

I want to have a script that will take an array of images, process them as one batch (which I'm pretty sure caffe can do...), and return a list full of 'fc8' values corresponding the the image at the given index. How do I pre-process and make a blob with multiple images, and have the net.forward() command return a list of corresponding values? I've been having trouble finding info about batch processing images in pyCaffe without the use of a database.

Thanks!

Ioannis Kalfas

unread,
May 9, 2016, 9:08:46 AM5/9/16
to Caffe Users
up!
Message has been deleted

Jan

unread,
May 9, 2016, 9:31:02 AM5/9/16
to Caffe Users
It is simply a matter of doing instead of

net.blobs['data'].reshape(1,3,96,96)
net
.blobs['data'].data[...] = transformer.preprocess('data', caffe.io.load_image('Test/A_4654.png'))


this:

net.blobs['data'].reshape(20,3,96,96)
for i in range(20):
  net
.blobs['data'].data[i, ...] = transformer.preprocess('data', caffe.io.load_image('Test/A_{}.png'.format(i)))

After net.forward(), out['fc8'] should be a multidim array where the first dimension is the index of the sample inside the batch.

Jan
Reply all
Reply to author
Forward
0 new messages