Pass floats to MemoryDataLayer (instead of images)

217 views
Skip to first unread message

nico....@googlemail.com

unread,
Sep 26, 2015, 4:12:33 PM9/26/15
to Caffe Users
Hi,

i have a trained caffe model.
The input for training is a set of simple vectors containing floats with fixed length e.g. (1, 2, 3, ...).
These vectors are converted into a hdf5 file which can be loaded by caffe data layer.

Now i want to predict with vectors of the same type within c++ (without converting to hdf5).
Is it possible to pass such a simple vector to caffe::MemoryDataLayer?

Thx

Nico

unread,
Sep 27, 2015, 11:18:31 AM9/27/15
to Caffe Users
Probably found a solution, maybe it will help others ...

assume we have ...
float array[] = { feature_1, feature_2, ... , feature_31 }
std::string model = <path to prototxt>
std::string weights = <path to caffemodel>

then ...
caffe::Datum datum;
datum.set_channels(1);
datum.set_height(1);
datum.set_width(31);
datum.set_label(0);

for (size_t i = 0; i < sizeof(array) / sizeof(array[0]); i++)
  datum.add_float_data(array[i]);

std::vector<caffe::Datum> datum_vector;
datum_vector.push_back(datum);

caffe::Caffe::set_mode(caffe::Caffe::CPU);
caffe::Net<double> net(model, caffe::TEST);
net.CopyTrainedLayersFrom(weights);
const boost::shared_ptr<caffe::MemoryDataLayer<double> > layer = boost::static_pointer_cast<caffe::MemoryDataLayer<double> >(net.layer_by_name("data"));
layer->AddDatumVector(datum_vector);

const std::vector<caffe::Blob<double>*>& result = net.ForwardPrefilled();
const double* argmaxs = result[0]->cpu_data();

std::cout << "argmax: " << *argmaxs << std::endl;

prototxt data layer ...
layer {
  name: "data"
  type: "MemoryData"
  top: "data"
  top: "label"
  memory_data_param {
    batch_size: 1
    channels: 1
    height: 1
    width: 31
  }
}
Reply all
Reply to author
Forward
0 new messages