Caffe feature extraction is too slow? caffe.Classifier or caffe.Net Caffe feature extraction is too slow? caffe.Classifier or caffe.Net numpy numpy

Caffe feature extraction is too slow? caffe.Classifier or caffe.Net


I found the best answer here in this post.

Till now I had used a

net = caffe.Classifier(model_prototxt, model_trained,                           mean=np.array([128, 128, 128]),                           channel_swap=(2,1,0),                           raw_scale=255,                           image_dims=(255, 255))

to initialize a model and get the output per image.But this method is really slow and requires around .9 seconds per image.

The best Idea is to pass a batch of images(maybe 100,200,250) changing. Depending on how much memory you have on your GPU.

for this I set caffe.set_mode_gpu() as I have one and It's faster when you send large batches.Initialize the model with ur trained model.

net=caffe.Net(model_prototxt,model_trained,caffe.TEST)

Create a Transformer and make sure to set mean and other values depending on how u trained your model.

transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})transformer.set_transpose('data', (2,0,1)) # height*width*channel -> channel*height*widthmean_file = np.array([128, 128, 128])transformer.set_mean('data', mean_file) #### subtract mean ####transformer.set_raw_scale('data', 255) # pixel value rangetransformer.set_channel_swap('data', (2,1,0)) # RGB -> BGRdata_blob_shape = net.blobs['data'].data.shapedata_blob_shape = list(data_blob_shape)

Read a group of images and convert to the network input.

net.blobs['data'].reshape(len(all_images), data_blob_shape[1], data_blob_shape[2], data_blob_shape[3])images = [temp_pres_dir+str(x) for x in all_images]net.blobs['data'].data[...] = map(lambda x: transformer.preprocess('data',caffe.io.load_image(x)), images)

Pass the batch of images through network.

out = net.forward()

You can use this output as you wish.

Speed for each image is now 20 msec