How to extracting predictions from HDF5 test dataset

350 views
Skip to first unread message

Stefano Lombardi

unread,
Feb 15, 2016, 8:24:46 AM2/15/16
to Caffe Users
hi!
through python code like the following, how can I extract the score predictions directly from a HDF5 file?



caffe.set_mode_cpu() net = caffe.Classifier(MODEL_FILE, PRETRAINED, channel_swap=(2,1,0), image_dims=(256, 256)) input_image = caffe.io.load_image(My HDF5 dataset) prediction = net.predict(My HDF5 dataset)


my HDF5 dataset
including externally extracted features, and using the follow net to train my classification system

layer { name: "input" type: "HDF5Data" top: "image" top: "img_features" top: "label" ... }



Thanks a lot!

Jan C Peters

unread,
Feb 15, 2016, 9:30:08 AM2/15/16
to Caffe Users
I am not quite sure what exactly you want to do; I am guessing you want to classify all images from a HDF5 dataset. This won't work as simple as you are imagining it. There are two basic possibilities:

1. Change the net config to use the HDF5 db in question as input, then just load the network with caffe.Net (not sure if you could also use the caffe.Classifier, have never used that myself) and call net.forward() as many times as there are batches in your HDF5, and for each extract the predictions form the final blobs before calling forward() again. In each call of forward() caffe will load a new batch of data from the HDF5 and pass it through the network.

2. Change the net config by removing all input layers and providing "input" and "input_shape" for each input blob to make a so-called "deploy" config. Then load that config with caffe.Net. Now before you can call forward() you need to fill the input blobs yourself:
net.blobs['input'].data[...] = <my data, one batch>

# possibly you need to reshape the blobs beforehand (change the batchsize in particular)
net
.blobs['input'].reshape(N,C,H,W)
In this scenario you will need to read out the data from the hdf5 file yourself. But using h5py that is really simple:
with h5py.File('my.h5', 'r') as f:
  firstbatch
= f['data'][0:batchsize,:,:,:]

Jan

Stefano Lombardi

unread,
Feb 16, 2016, 3:39:00 AM2/16/16
to Caffe Users
Thanks a lot jan!
Reply all
Reply to author
Forward
0 new messages