| 640 | */ |
| 641 | template <typename T, typename U = float> |
| 642 | void save_to_npy(T &tensor, const std::string &npy_filename, bool fortran_order) |
| 643 | { |
| 644 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_NOT_IN(&tensor, arm_compute::DataType::F32, arm_compute::DataType::QASYMM8); |
| 645 | |
| 646 | std::ofstream fs; |
| 647 | try |
| 648 | { |
| 649 | fs.exceptions(std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 650 | fs.open(npy_filename, std::ios::out | std::ios::binary); |
| 651 | |
| 652 | std::vector<npy::ndarray_len_t> shape(tensor.info()->num_dimensions()); |
| 653 | |
| 654 | for (unsigned int i = 0, j = tensor.info()->num_dimensions() - 1; i < tensor.info()->num_dimensions(); ++i, --j) |
| 655 | { |
| 656 | shape[i] = tensor.info()->tensor_shape()[!fortran_order ? j : i]; |
| 657 | } |
| 658 | |
| 659 | // Map buffer if creating a CLTensor |
| 660 | map(tensor, true); |
| 661 | |
| 662 | using typestring_type = typename std::conditional<std::is_floating_point<U>::value, float, qasymm8_t>::type; |
| 663 | |
| 664 | std::vector<typestring_type> tmp; /* Used only to get the typestring */ |
| 665 | const npy::dtype_t dtype = npy::dtype_map.at(std::type_index(typeid(tmp))); |
| 666 | |
| 667 | std::ofstream stream(npy_filename, std::ofstream::binary); |
| 668 | npy::header_t header{dtype, fortran_order, shape}; |
| 669 | npy::write_header(stream, header); |
| 670 | |
| 671 | arm_compute::Window window; |
| 672 | window.use_tensor_dimensions(tensor.info()->tensor_shape()); |
| 673 | |
| 674 | arm_compute::Iterator in(&tensor, window); |
| 675 | |
| 676 | arm_compute::execute_window_loop( |
| 677 | window, |
| 678 | [&](const arm_compute::Coordinates &) |
| 679 | { stream.write(reinterpret_cast<const char *>(in.ptr()), sizeof(typestring_type)); }, |
| 680 | in); |
| 681 | |
| 682 | // Unmap buffer if creating a CLTensor |
| 683 | unmap(tensor); |
| 684 | } |
| 685 | catch (const std::ofstream::failure &e) |
| 686 | { |
| 687 | ARM_COMPUTE_ERROR_VAR("Writing %s: (%s)", npy_filename.c_str(), e.what()); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /** Load the tensor with pre-trained data from a binary file |
| 692 | * |
no test coverage detected