Function grpc_vision_svc::image_captioning::utils::create_tensor

source ·
pub fn create_tensor(pixels: &[u8], device: &Device) -> Result<Tensor>
Expand description

Creates a tensor from a byte slice representing pixel data.

This function takes a byte slice and a Device, creates a tensor from the raw buffer, permutes the dimensions, and then normalizes the tensor by subtracting the mean and dividing by the standard deviation.

§Arguments

  • pixels - A byte slice representing the pixel data.
  • device - A Device to which the tensor will be allocated.

§Returns

§Examples

let image: DynamicImage = ImageReader::open("path/to/image.jpg")?
    .decode()?;

let image_raw_buf: Vec<u8> = image.to_rgb8().into_raw();
let tensor: Tensor = create_tensor(&image_raw_buf, &Device::Cpu)?;
 
assert_eq!(tensor.shape().dims(), &[3, 384, 384]);