| 697 | } |
| 698 | |
| 699 | fn image_preprocess<T: AsRef<std::path::Path>>(path: T) -> Result<Tensor> { |
| 700 | let img = image::ImageReader::open(path)?.decode()?; |
| 701 | let (height, width) = (img.height() as usize, img.width() as usize); |
| 702 | let height = height - height % 32; |
| 703 | let width = width - width % 32; |
| 704 | let img = img.resize_to_fill( |
| 705 | width as u32, |
| 706 | height as u32, |
| 707 | image::imageops::FilterType::CatmullRom, |
| 708 | ); |
| 709 | let img = img.to_rgb8(); |
| 710 | let img = img.into_raw(); |
| 711 | let img = Tensor::from_vec(img, (height, width, 3), &Device::Cpu)? |
| 712 | .permute((2, 0, 1))? |
| 713 | .to_dtype(DType::F32)? |
| 714 | .affine(2. / 255., -1.)? |
| 715 | .unsqueeze(0)?; |
| 716 | Ok(img) |
| 717 | } |
| 718 | |
| 719 | #[cfg(test)] |
| 720 | mod tests { |