Is it possible to retrieve a filename from a TEST phase forward pass in C++?

130 views
Skip to first unread message

CaffeStudent

unread,
Apr 2, 2015, 6:21:31 PM4/2/15
to caffe...@googlegroups.com
I am currently using an ImageData Layer to specify a list of files to iterate through.  Is there a way to access the filename for each image in a batch result through the C++ interface?
After calling ForwardPrefilled, are the filenames for each image in a batch stored in a results object or some other internal location?

After searching google and reading forums I have found enough sample code to figure out how to TEST/predict on a TEST set using the C++ interface.

In my code I am setting up a C++ "Net" object
Net<float> net("path to prototxt", caffe::TEST);

net.CopyTrainedLayersFrom("path to model");

vector<Blob<float>*> results = net.ForwardPrefilled(&loss);

layer {
  name: "mnist"
  type: "ImageData"
  top: "data"
  top: "label"
  include {
    phase: TEST
  }
  transform_param {
    scale: 0.00390625
  }
  image_data_param {
    source: "examples/prediction_example/listfile_test"
    batch_size: 1
    new_height: 28
    new_width: 28
    is_color: false
  }
}

cooling

unread,
Nov 6, 2015, 1:56:49 PM11/6/15
to Caffe Users
Did you find a solution to this? I am also needing smoething like this.

Jan C Peters

unread,
Nov 9, 2015, 3:18:49 AM11/9/15
to Caffe Users
No, you cannot access the filenames in any way other than reading the listfile yourself. And there are good reasons for this: It would destroy the whole data layer abstraction. For some data layer types, such as Data and HDF5Data there is no filename to be associated to each sample. The filenames in the listfile or only ever seen by the ImageData layer, and no other part of caffe. The data layers are responsible from getting the data in, and for the other layers the actual source of the data should not matter at all.

If you really need these filenames afterwards, maybe you can read the listfile manually along with the training? As long as you know the batchsize and the number of images in your listfile, you should always be able to associate an input filename with an output blob.

Jan

cooling

unread,
Nov 9, 2015, 12:47:27 PM11/9/15
to Caffe Users
In my experience this is extremely frustrating. I've stumbled three times into areas where I wanted to know the order caffe processed the data. The layer may shuffle, it may get slightly offset somehow, and extensions to handling missing images via skipping also break.

I got around this by passing through an extra input into the data layer as a row ID (extending the code for ImageDataLayer). At least that tells me which row of the file its on and I can extract anything I want from this. I realize this is hacky, but I do wish caffe had a better way of handling this, just assuming that it won't matter or that you can calculate it is not helpful, especially to those new to caffe. You still do not have to pass this information to any other layer (except maybe a silence layer).

Jan C Peters

unread,
Nov 10, 2015, 3:02:02 AM11/10/15
to Caffe Users
During training, why would you want to know the image source filename? If you are doing this as a kind of postprocessing step, you can use a deploy config of your network and feed through the samples in question by yourself.

For your extension: Do you mean your ImageDataLayer exposes another top blob which contains the row numbers of the processed images? Because that is what I would do. It is a bit hacky but I have seen (and done) much worse :-).

Jan
Reply all
Reply to author
Forward
0 new messages