Document / simplify conversion to/from ndarray arrays (and other libraries)
To make using this library easier in downstream projects, it would help to have more documentation / examples etc. around converting to/from tensors/arrays in other libraries.
In the meantime, here are some useful resources:
- I put together a quick gist for rten-ndarray conversion
- There is a simple ImageNet image classification project at https://github.com/robertknight/rten-ndarray-demo/.
Cheatsheet
For the case where the tensor/array is "contiguous" (RTen terminology) or "standard layout" (ndarray terminology):
ndarray -> RTen:
TensorView::from_data(array.shape(), array.as_slice().unwrap());
RTen -> ndarray:
ArrayViewD::from_shape(tensor.shape(), tensor.data().unwrap()).unwrap()
The unwraps above will fail if the layout is not contiguous.
Outputs from RTen model execution are always contiguous / in standard order. If it is not certain that a tensor is in standard order, then Tensor::to_contiguous (RTen) or ArrayView::as_standard_layout can be used to get a contiguous layout tensor, copying only if necessary.
https://github.com/robertknight/rten/pull/1025 adds a couple of new APIs to simplify input creation and output extraction.