我使用以下代码将数组映射到具有Eigen的矩阵:
uchar* _dataset = new uchar[number_of_labels];
Map<Matrix<uchar,Dynamic,Dynamic,RowMajor>> MNIST_dataset((uchar*)*_dataset,rows,cols);
可以编译。但我得到分段错误试图返回280000 * 28矩阵作为我的函数输出。它适用于矩阵尺寸较小的情况。 Eigen不限制矩阵大小,是吗?
#include <iostream>
#include <string>
#include <fstream>
#include "Eigen/Dense"
using namespace Eigen;
using namespace std;
typedef unsigned char uchar;
Matrix<int,Dynamic,Dynamic,RowMajor> read_mnist_images(string full_path) {
int number_of_images,image_size;
auto reverseInt = [](int i) {
unsigned char c1, c2, c3, c4;
c1 = i & 255, c2 = (i >> 8) & 255, c3 = (i >> 16) & 255, c4 = (i >> 24) & 255;
return ((int)c1 << 24) + ((int)c2 << 16) + ((int)c3 << 8) + c4;
};
ifstream file(full_path, ios::binary);
int magic_number = 0, n_rows = 0, n_cols = 0;
file.read((char *)&magic_number, sizeof(magic_number));
magic_number = reverseInt(magic_number);
if(magic_number != 2051) throw runtime_error("Invalid MNIST image file!");
file.read((char *)&number_of_images, sizeof(number_of_images)), number_of_images = reverseInt(number_of_images);
file.read((char *)&n_rows, sizeof(n_rows)), n_rows = reverseInt(n_rows);
file.read((char *)&n_cols, sizeof(n_cols)), n_cols = reverseInt(n_cols);
image_size = n_rows * n_cols;
uchar** _dataset = new uchar*[number_of_images];
for(int i = 0; i < number_of_images; i++) {
_dataset[i] = new uchar[image_size];
file.read((char *)_dataset[i], image_size);
}
n_rows = n_rows * number_of_images;
Map<Matrix<uchar,Dynamic,Dynamic,RowMajor>> MNIST_dataset(*_dataset,n_rows,n_cols);
return MNIST_dataset.cast<int>();
}
int main(int argc,char* argv[]){
Matrix<int,Dynamic,Dynamic,RowMajor> dataset;
dataset = read_mnist_images(argv[1]);
return 0;
}
答案 0 :(得分:0)
你实际做的错误是用_dataset
取消引用(uchar*)*_dataset
指针,即你取出已分配数组的第0个元素并再次将其转换为指针,这几乎肯定会指向一个虚假的地址。
使用未更改的指针构建您的Map
:
Map<Matrix<uchar,Dynamic,Dynamic,RowMajor>> MNIST_dataset(_dataset,rows,cols);