Skip to content

Instantly share code, notes, and snippets.

@Dobiasd
Last active December 12, 2018 14:47
Show Gist options
  • Save Dobiasd/2852c81adbd57a57e89d2d0385cc4c06 to your computer and use it in GitHub Desktop.
Save Dobiasd/2852c81adbd57a57e89d2d0385cc4c06 to your computer and use it in GitHub Desktop.
Sharing memory between Eigen::Matrix and fdeep::tensor3
// Sharing memory between Eigen::Matrix and fdeep::tensor5
#include <iostream>
#include <eigen3/Eigen/Dense>
#include <fdeep/fdeep.hpp>
int main()
{
// use row major storage order for eigen matrix, since fdeep uses it too
using RowMajorMatrixXf = Eigen::Matrix<fdeep::float_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
// dimensions of the eigen matrix
const int rows = 640;
const int cols = 480;
// initialize memory shared between matrix and tensor
fdeep::shared_float_vec data_vec = fplus::make_shared_ref<fdeep::float_vec>();
data_vec->resize(static_cast<std::size_t>(rows * cols));
// create eigen matrix using the memory block from the vector above
Eigen::Map<RowMajorMatrixXf, Eigen::Unaligned> mapped_matrix(
data_vec->data(),
rows, cols);
// populate mapped_matrix some way
mapped_matrix(0, 0) = 4.0f;
mapped_matrix(1, 1) = 5.0f;
mapped_matrix(4, 2) = 6.0f;
// create fdeep::tensor5 also using the memory block of the vector
const int tensor5_channels = rows;
const int tensor5_rows = 1;
const int tensor5_cols = cols;
fdeep::shape5 tensor5_shape(1, 1, tensor5_rows, tensor5_cols, tensor5_channels);
fdeep::tensor5 t(tensor5_shape, data_vec);
// print some values to make sure the mapping is correct
std::cout << t.get(0, 0, 0, 0, 0) << std::endl;
std::cout << t.get(0, 0, 0, 1, 1) << std::endl;
std::cout << t.get(0, 0, 0, 4, 2) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment