-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add more benchmarks * implement fast resize * add python resize test * fix readme
- Loading branch information
Showing
14 changed files
with
274 additions
and
157 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import timeit | ||
|
||
import cv2 | ||
from PIL import Image | ||
import kornia_rs | ||
import numpy as np | ||
# import tensorflow as tf | ||
|
||
image_path = "tests/data/dog.jpeg" | ||
img = kornia_rs.read_image_jpeg(image_path) | ||
img_pil = Image.open(image_path) | ||
new_size = (128, 128) | ||
N = 5000 # number of iterations | ||
|
||
|
||
def resize_image_opencv(img: np.ndarray, new_size: tuple) -> None: | ||
return cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR) | ||
|
||
def resize_image_pil(img: Image.Image, new_size: tuple) -> None: | ||
return img.resize(new_size, Image.BILINEAR) | ||
|
||
def resize_image_kornia(img: np.ndarray, new_size: tuple) -> None: | ||
return kornia_rs.resize(img, new_size, "bilinear") | ||
|
||
tests = [ | ||
{ | ||
"name": "OpenCV", | ||
"stmt": "resize_image_opencv(img, new_size)", | ||
"setup": "from __main__ import resize_image_opencv, img, new_size", | ||
"globals": {"img": img, "new_size": new_size}, | ||
}, | ||
{ | ||
"name": "PIL", | ||
"stmt": "resize_image_pil(img_pil, new_size)", | ||
"setup": "from __main__ import resize_image_pil, img_pil, new_size", | ||
"globals": {"img_pil": img_pil, "new_size": new_size}, | ||
}, | ||
{ | ||
"name": "Kornia", | ||
"stmt": "resize_image_kornia(img, new_size)", | ||
"setup": "from __main__ import resize_image_kornia, img, new_size", | ||
"globals": {"img": img, "new_size": new_size}, | ||
}, | ||
] | ||
|
||
for test in tests: | ||
timer = timeit.Timer( | ||
stmt=test["stmt"], setup=test["setup"], globals=test["globals"] | ||
) | ||
print(f"{test['name']}: {timer.timeit(N)/ N * 1e3:.2f} ms") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use pyo3::prelude::*; | ||
|
||
use crate::image::{FromPyImage, PyImage, ToPyImage}; | ||
use kornia_rs::image::Image; | ||
|
||
#[pyfunction] | ||
pub fn resize(image: PyImage, new_size: (usize, usize), interpolation: &str) -> PyResult<PyImage> { | ||
let image = Image::from_pyimage(image) | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(format!("{}", e)))?; | ||
|
||
let new_size = kornia_rs::image::ImageSize { | ||
height: new_size.0, | ||
width: new_size.1, | ||
}; | ||
|
||
let interpolation = match interpolation.to_lowercase().as_str() { | ||
"nearest" => kornia_rs::resize::InterpolationMode::Nearest, | ||
"bilinear" => kornia_rs::resize::InterpolationMode::Bilinear, | ||
_ => { | ||
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>( | ||
"Invalid interpolation mode", | ||
)) | ||
} | ||
}; | ||
|
||
let image = kornia_rs::resize::resize_fast(&image, new_size, interpolation) | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyException, _>(format!("{}", e)))?; | ||
|
||
Ok(image.to_pyimage()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pathlib import Path | ||
import kornia_rs as K | ||
|
||
import torch | ||
import numpy as np | ||
|
||
# TODO: inject this from elsewhere | ||
DATA_DIR = Path(__file__).parents[2] / "tests" / "data" | ||
|
||
|
||
def test_resize(): | ||
# load an image with libjpeg-turbo | ||
img_path: Path = DATA_DIR / "dog.jpeg" | ||
img: np.ndarray = K.read_image_jpeg(str(img_path.absolute())) | ||
|
||
# check the image properties | ||
assert img.shape == (195, 258, 3) | ||
|
||
img_resized: np.ndarray = K.resize(img, (43, 34), "bilinear") | ||
assert img_resized.shape == (43, 34, 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.