29static void iterate(
Tensor &tensor, function<
void(vector<int> &)> on_loop);
34 explicit Tensor(
const vector<hsize_t> &shape) : _shape(shape), _data(0), _num_elems(1)
36 _strides.resize(shape.size());
38 for (
int i = _shape.size() - 1; i >= 0; --i)
40 _strides[i] = _num_elems;
41 _num_elems *= _shape[i];
44 _data =
new float[_num_elems];
48 int rank()
const {
return _shape.size(); }
49 int dim(
int d)
const {
return _shape[d]; }
50 float *
data() {
return _data; }
57 for (
auto i = 0; i <
coords.size(); ++i)
65 vector<hsize_t> tr_shape(_shape.size());
67 for (
auto i = 0; i < _shape.size(); ++i)
68 tr_shape[i] = _shape[reshape[i]];
71 auto on_loop = [
this, &reshape, &result](vector<int> &
coords) {
72 vector<int> tr_coords(_shape.size());
74 for (
int i = 0; i <
rank(); ++i)
75 tr_coords[i] =
coords[reshape[i]];
79 iterate(*
this, on_loop);
84 vector<hsize_t> _shape;
85 vector<hsize_t> _strides;
90static void fillTensor(
Tensor &tensor)
94 for (
int i = 0; i <
tensor.numElems(); ++i)
101static void iterate(
Tensor &tensor, function<
void(vector<int> &)> on_loop)
103 int num_dims =
tensor.rank();
104 vector<int>
coords(num_dims, 0);
105 vector<int> dims(num_dims);
107 for (
int i = 0; i < num_dims; ++i)
115 for (i = num_dims - 1; i >= 0; --i)
117 if (
coords[i] < dims[i] - 1)
131static void dumpTensor(
Tensor &tensor)
134 for (
int i = 0; i <
tensor.rank(); ++i)
145 iterate(tensor, on_loop);
148static void writeTensorToDatFile(
const string &file_name,
Tensor &tensor)
150 ofstream of(file_name +
".dat", ios_base::binary);
153 cout <<
"Could not output tensor to the: " << file_name +
".dat";
155 of.write(
reinterpret_cast<char *
>(
tensor.data()),
tensor.numElems() *
sizeof(
float));
158static void writeTensorToHDF5File(
const vector<hsize_t> &dimensions,
const string &tensor_name,
159 const string &file_name,
Tensor &tensor)
161 H5::H5File h5File(file_name +
".hdf5", H5F_ACC_TRUNC);
162 H5::DataSpace dataspace(dimensions.size(), &dimensions[0]);
163 auto dataset = h5File.createDataSet(tensor_name, H5::PredType::IEEE_F32BE, dataspace);
164 dataset.write(
tensor.data(), H5::PredType::NATIVE_FLOAT);
167int main(
int argc,
char *argv[])
171 cout <<
"Usage: " << argv[0] <<
" <tensor name> <output file name> dim0, dim1, dim2, ..."
173 cout <<
"Where: dim0, dim1, dim2, ... are the generated tensor shape dimensions" << endl;
177 vector<hsize_t> dimensions;
179 for (
int i = 3; i < argc; ++i)
183 int d = stoi(argv[i]);
187 cout <<
"The dimensions must be positive values. This is not a correct dimension value: "
192 dimensions.push_back(d);
194 catch (
const invalid_argument &)
196 cout <<
"The parameter does not look as an integer value: " << argv[i] << endl;
199 catch (
const out_of_range &)
201 cout <<
"The value is out of the C++ \"int\" type range: " << argv[i] << endl;
206 Tensor caffe_tensor(dimensions);
207 fillTensor(caffe_tensor);
208 writeTensorToHDF5File(dimensions, argv[1],
"in_" +
string(argv[2]) +
"_caffe", caffe_tensor);
210 vector<hsize_t> tf_reshape{0, 2, 3, 1};
212 writeTensorToDatFile(
string(argv[2]) +
"_tf", tf_tensor);
Array< CornerBox > coords
Tensor transpose(const vector< hsize_t > &reshape)
float & at(const vector< int > &coords)
Tensor(const vector< hsize_t > &shape)
__global uchar * offset(const Image *img, int x, int y)
NodeFiller< ARG_TYPE_1, ARG_TYPE_2 > fill(ARG_TYPE_1 **arg_1, ARG_TYPE_2 **arg_2)