|
// 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; |
|
} |