Repository: huggingface/candle
File: candle-examples/examples/depth_anything_v2/main.rs (Rust)

Code before:
fn full_output_path(image_path: &PathBuf, output_dir: &Option<PathBuf>) -> PathBuf {
    let input_file_name = image_path.file_name().unwrap();
    let mut output_file_name = OsString::from("depth_");
    output_file_name.push(input_file_name);
    let mut output_path = match output_dir {
        None => image_path.parent().unwrap().to_path_buf(),
        Some(output_path) => output_path.clone(),
    };
    output_path.push(output_file_name);

    output_path
}

fn load_and_prep_image<B: BackendStorage + TryConvertStorage<CpuStorage>>(
    image_path: &PathBuf,
    device: &B::Device,
) -> anyhow::Result<(usize, usize, Tensor<B>)> {
    let (_original_image, original_height, original_width): (Tensor<B>, usize, usize) =
        load_image(&image_path, None, device)?;

    let image = load_image_and_resize(&image_path, DINO_IMG_SIZE, DINO_IMG_SIZE, device)?
        .unsqueeze(0)?
        .to_dtype(F32)?;

    let max_pixel_val = Tensor::try_from(255.0f32)?
        .to_device(device)?
        .broadcast_as(image.shape())?;
    let image = (image / max_pixel_val)?;
    let image = normalize_image(&image, &MAGIC_MEAN, &MAGIC_STD, device)?;

    Ok((original_height, original_width, image))
}

fn normalize_image<B: BackendStorage + TryConvertStorage<CpuStorage>>(
    image: &Tensor<B>,
    mean: &[f32; 3],
    std: &[f32; 3],
    device: &B::Device,
) -> Result<Tensor<B>> {
    let mean_tensor =
        Tensor::from_vec(mean.to_vec(), (3, 1, 1), device)?.broadcast_as(image.shape())?;
    let std_tensor =
        Tensor::from_vec(std.to_vec(), (3, 1, 1), device)?.broadcast_as(image.shape())?;
    image.sub(&mean_tensor)?.div(&std_tensor)
}

fn post_process_image<B: BackendStorage + TryConvertStorage<CpuStorage>>(
    image: &Tensor<B>,
    original_height: usize,
    original_width: usize,
    color_map: bool,

Review comment (suggestion):
If you use `Tensor::new(255.0f32, device)` you remove the need for `TryConvertStorage<CpuStorage>` 👌 

Code after:
        Some(output_path) => output_path.clone(),
    };
    output_path.push(output_file_name);

    output_path
}

fn load_and_prep_image<B: BackendStorage>(
    image_path: &PathBuf,
    device: &B::Device,
) -> anyhow::Result<(usize, usize, Tensor<B>)> {
    let (_original_image, original_height, original_width): (Tensor<B>, usize, usize) =
        load_image(&image_path, None, device)?;

    let image = load_image_and_resize(&image_path, DINO_IMG_SIZE, DINO_IMG_SIZE, device)?
        .unsqueeze(0)?
        .to_dtype(F32)?;

    let max_pixel_val = Tensor::new(255.0f32, device)?.broadcast_as(image.shape())?;
    let image = (image / max_pixel_val)?;
    let image = normalize_image(&image, &MAGIC_MEAN, &MAGIC_STD, device)?;

    Ok((original_height, original_width, image))
}

fn normalize_image<B: BackendStorage>(
    image: &Tensor<B>,
    mean: &[f32; 3],
    std: &[f32; 3],
    device: &B::Device,
) -> Result<Tensor<B>> {
    let mean_tensor =
        Tensor::from_vec(mean.to_vec(), (3, 1, 1), device)?.broadcast_as(image.shape())?;
    let std_tensor =
        Tensor::from_vec(std.to_vec(), (3, 1, 1), device)?.broadcast_as(image.shape())?;
    image.sub(&mean_tensor)?.div(&std_tensor)
}

fn post_process_image<B: BackendStorage>(
    image: &Tensor<B>,
    original_height: usize,
    original_width: usize,
    color_map: bool,
) -> Result<Tensor<B>> {
    let out = image.interpolate2d(original_height, original_width)?;
    let out = scale_image(&out)?;

    let out = if color_map {
        let spectral_r = SpectralRColormap::new();
        spectral_r.gray2color(&out)?
    } else {