forked from stereomatchingkiss/UFLDL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmnist_reader.hpp
102 lines (87 loc) · 2.61 KB
/
mnist_reader.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef MNIST_READER_HPP
#define MNIST_READER_HPP
//#include <shark/Data/Dataset.h>
#include <fstream>
#include <functional>
#include <string>
#include <vector>
template<typename Mat>
struct mnist_to_arma
{
public:
void operator()(std::ifstream &in, int number_of_images,
int n_rows, int n_cols);
Mat mat_;
};
template<typename Mat>
void mnist_to_arma<Mat>::
operator()(std::ifstream &in, int number_of_images,
int n_rows, int n_cols)
{
using value_type = typename Mat::value_type;
mat_.set_size(n_rows * n_cols, number_of_images);
for(int i = 0; i < number_of_images; ++i){
for(int j = 0; j != mat_.n_rows; ++j){
unsigned char temp = 0;
in.read((char*) &temp, sizeof(temp));
mat_(j, i) = value_type(temp);
}
}
}
template<typename EigenMat>
struct mnist_to_eigen
{
public:
void operator()(std::ifstream &in, int number_of_images,
int n_rows, int n_cols);
EigenMat mat_;
};
template<typename EigenMat>
void mnist_to_eigen<EigenMat>::
operator()(std::ifstream &in, int number_of_images,
int n_rows, int n_cols)
{
using Scalar = typename EigenMat::Scalar;
mat_.resize(n_rows * n_cols, number_of_images);
for(int i = 0; i < number_of_images; ++i){
for(int r = 0; r < n_rows; ++r){
int const Offset = r * n_cols;
for(int c = 0; c < n_cols; ++c){
unsigned char temp = 0;
in.read((char*) &temp, sizeof(temp));
mat_(Offset + c, i) = Scalar(temp);
}
}
}
}
/*template<typename T = shark::RealVector>
struct mnist_to_shark_vector
{
void operator()(std::ifstream &in, int number_of_images,
int n_rows, int n_cols);
std::vector<T> mat_;
};
template<typename T>
void mnist_to_shark_vector<T>::
operator()(std::ifstream &in, int number_of_images,
int n_rows, int n_cols)
{
using ScalarType = typename T::scalar_type;
for(int i = 0; i < number_of_images; ++i){
T sample(n_rows * n_cols);
for(int r = 0; r < n_rows; ++r){
int const Offset = r * n_cols;
for(int c = 0; c < n_cols; ++c){
unsigned char temp = 0;
in.read((char*) &temp, sizeof(temp));
sample(Offset + c) = float(temp);
}
}
mat_.emplace_back(std::move(sample));
}
}*/
bool
read_mnist(std::string const &file_name,
std::function<void(std::ifstream&, int, int, int)> func);
std::vector<int> read_mnist_label(std::string const &file_name);
#endif // MNIST_READER_HPP