pub struct OutputGuard<'extractor> { /* private fields */ }Expand description
A read-only output that borrows its extractor and reserves the network.
fn reuse(ex: &mut ncnn_rust::Extractor<'_>) {
let output = ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
println!("{:?}", output.as_f32_slice());
}fn escape(net: &ncnn_rust::Net) -> ncnn_rust::OutputGuard<'_> {
let mut ex = net.extractor().unwrap();
ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap()
}fn escape_slice(ex: &mut ncnn_rust::Extractor<'_>) {
let output = ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
let values = output.as_f32_slice().unwrap();
drop(output);
println!("{}", values[0]);
}fn escape_handle(output: ncnn_rust::OutputGuard<'_>) -> ncnn_rust::Mat {
output.into_raw()
}fn destroy_extractor(net: &ncnn_rust::Net) {
let mut ex = net.extractor().unwrap();
let _output = ex.extract("output", ncnn_rust::TensorWidth::Fp32).unwrap();
drop(ex); // The unused guard must still release its handle first.
}Implementations§
Source§impl OutputGuard<'_>
impl OutputGuard<'_>
Sourcepub fn info(&self) -> TensorInfo
pub fn info(&self) -> TensorInfo
Returns the borrowed tensor metadata.
The metadata describes the layout the native extractor produced: extents, elemsize,
elempack and the channel stride. elemsize decides which reader applies.
Sourcepub fn as_f32_slice(&self) -> Result<&[f32]>
pub fn as_f32_slice(&self) -> Result<&[f32]>
Borrows contiguous unpacked storage with an FP32 interpretation.
The borrow needs aligned, unpacked, single-batch storage whose channels are adjacent, so a
16-bit or packed output returns Error::Invalid with the stored
width in the message. A multi-channel output with padded planes needs
channel_f32 or to_f32.
Sourcepub fn channel_f32(&self, channel: usize) -> Result<&[f32]>
pub fn channel_f32(&self, channel: usize) -> Result<&[f32]>
Borrows one channel, skipping inter-channel padding.
ncnn pads each channel plane, so this returns the logical elements of channel without the
padding a whole-tensor slice would include. An index outside the channel count returns
Error::Invalid.
Sourcepub fn into_mat(self) -> Result<Mat>
pub fn into_mat(self) -> Result<Mat>
Consumes the guard and keeps the extracted tensor without copying it.
The guard hands out the buffer the native extractor detached when this extractor can keep
it, and copies in the two cases where it cannot: a network built with
NetBuilder::reuse_blob_memory returns storage
inside its pools, and an extractor that submitted a borrowed input returns caller storage.
Either way the tensor is independent of the guard, extractor and network once this returns,
and the network stops being reserved.
Sourcepub fn to_f32(&self) -> Result<Vec<f32>>
pub fn to_f32(&self) -> Result<Vec<f32>>
Copies logical FP32 elements into a new vector.
Native padding is excluded, so the length is the product of the reported extents. Quantized
and half-precision outputs convert here; use to_f32_typed to name a
16-bit format.
Sourcepub fn to_f32_typed(&self, kind: ElementKind) -> Result<Vec<f32>>
pub fn to_f32_typed(&self, kind: ElementKind) -> Result<Vec<f32>>
Copies any scalar format into FP32 values, including quantized outputs.
The tensor records an element width without a format tag, so a 16-bit output needs the
caller to say whether it holds IEEE half or bfloat16. A width that disagrees with kind
returns Error::Invalid.