save the file in whatever format I want, since I store the data in a pointer.
Here's the relevant code you asked for. Note that this works only for a batchSize that divides the number of images exactly.
// initialize the feature containers' pointers and the dimension container
data = new float*[num_layers];
featDimension = new int[num_layers];
numLayers = num_layers;
// variable to control one-time allocation within the loop
bool * allocated = new bool[num_layers];
// init to false
for (int layer_index = 0; layer_index < num_layers; ++layer_index) allocated[layer_index] = false;
// EXTRACT
// for each batch of images
for (int batch_index = 0; batch_index < num_mini_batches; ++batch_index)
{
// evaluate the net
feature_extraction_net->Forward(input_vec);
// for each feature layer
for (int layer_index = 0; layer_index < num_layers; ++layer_index)
{
// get that layer blob and its dimension
const boost::shared_ptr<Blob<Dtype> > feature_blob = feature_extraction_net
->blob_by_name(blob_names[layer_index]);
int batch_size = feature_blob->num();
int dim_features = feature_blob->count() / batch_size;
// init the feat container for that layer if needed, set dimension
if (!allocated[layer_index])
{
data[layer_index] = new float[dim_features * num_mini_batches * batch_size];
featDimension[layer_index] = dim_features;
allocated[layer_index] = true;
}
const Dtype* feature_blob_data;
// for each image in the batch
for (int image_index_inBatch = 0; image_index_inBatch < batch_size; ++image_index_inBatch) {
// get the image's feature vector
feature_blob_data = feature_blob->cpu_data() +
feature_blob->offset(image_index_inBatch);
// copy the values to the data container
for (int d = 0; d < dim_features; ++d) {
data[layer_index][(batch_index * batch_size + image_index_inBatch) * dim_features + d] = feature_blob_data[d];
}
++image_indices[layer_index];
if (image_indices[layer_index] % 1000 == 0) {
LOG(ERROR) << "Extracted features of " << image_indices[layer_index] <<
" query images for feature blob " << blob_names[layer_index];
}
} // for (int image_index_inBatch = 0; image_index_inBatch < batch_size; ++image_index_inBatch)
} // for (int layer_index = 0; layer_index < num_layers; ++layer_index)
} // for (int batch_index = 0; batch_index < num_mini_batches; ++batch_index)
// write the last batch
for (int layer_index = 0; layer_index < num_layers; ++layer_index) {
LOG(ERROR) << "Extracted features of " << image_indices[layer_index] <<
" query images for feature blob " << blob_names[layer_index];
}