-
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.
- Loading branch information
Showing
5 changed files
with
159 additions
and
37 deletions.
There are no files selected for viewing
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
|
||
use kornia_rs::image::{Image, ImageSize}; | ||
use kornia_rs::resize as F; | ||
use kornia_rs::resize::{InterpolationMode, ResizeOptions}; | ||
|
||
fn resize_image_crate(image: Image, new_size: ImageSize) -> Image { | ||
let image_data = image.data.as_slice().unwrap(); | ||
let rgb = image::RgbImage::from_raw( | ||
image.image_size().width as u32, | ||
image.image_size().height as u32, | ||
image_data.to_vec(), | ||
) | ||
.unwrap(); | ||
let image_crate = image::DynamicImage::ImageRgb8(rgb); | ||
|
||
let image_resized = image_crate.resize_exact( | ||
new_size.width as u32, | ||
new_size.height as u32, | ||
image::imageops::FilterType::Gaussian, | ||
); | ||
let data = image_resized.into_rgb8().into_raw(); | ||
Image::from_shape_vec([new_size.height as usize, new_size.width as usize, 3], data) | ||
} | ||
|
||
fn bench_resize(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("Resize"); | ||
let image_sizes = vec![(256, 224), (512, 448), (1024, 896)]; | ||
|
||
for (width, height) in image_sizes { | ||
let image_size = ImageSize { width, height }; | ||
let id = format!("{}x{}", width, height); | ||
let image = Image::new(image_size.clone(), vec![0; width * height * 3]); | ||
let new_size = ImageSize { | ||
width: width / 2, | ||
height: height / 2, | ||
}; | ||
group.bench_with_input(BenchmarkId::new("zip", &id), &image, |b, i| { | ||
b.iter(|| { | ||
F::resize( | ||
black_box(i), | ||
new_size.clone(), | ||
ResizeOptions { | ||
interpolation: InterpolationMode::Bilinear, | ||
}, | ||
) | ||
}) | ||
}); | ||
group.bench_with_input(BenchmarkId::new("image_crate", &id), &image, |b, i| { | ||
b.iter(|| resize_image_crate(black_box(i.clone()), new_size.clone())) | ||
}); | ||
} | ||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_resize); | ||
criterion_main!(benches); |
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,77 @@ | ||
from PIL import Image | ||
import time | ||
import numpy as np | ||
import cv2 | ||
|
||
size = (256, 224) | ||
new_size = (size[0] // 2, size[1] // 2) | ||
|
||
image = np.ones((size[0], size[1], 3), dtype=np.uint8) | ||
|
||
img_pil = Image.fromarray(image) | ||
|
||
num_iters = 10000 | ||
|
||
times = [] | ||
|
||
for _ in range(num_iters): | ||
start = time.time() | ||
img_pil.resize(new_size, Image.BILINEAR) | ||
end = time.time() | ||
elapsed_ms = (end - start) * 1000 | ||
times.append(elapsed_ms) | ||
|
||
print(f"RESIZE PIL: Average time: {np.median(times)}") | ||
|
||
|
||
# test opencv resize | ||
|
||
times = [] | ||
|
||
for _ in range(num_iters): | ||
start = time.time() | ||
cv2.resize(image, new_size, interpolation=cv2.INTER_LINEAR) | ||
end = time.time() | ||
elapsed_ms = (end - start) * 1000 | ||
times.append(elapsed_ms) | ||
|
||
print(f"RESIZE OPENCV: Average time: {np.median(times)}") | ||
|
||
|
||
# test kornia resize | ||
|
||
import torch | ||
import kornia | ||
|
||
image_torch = torch.from_numpy(image).permute(2, 0, 1).float() / 255. | ||
|
||
times = [] | ||
|
||
for _ in range(num_iters): | ||
start = time.time() | ||
with torch.no_grad(): | ||
kornia.geometry.resize(image_torch, new_size, interpolation='bilinear') | ||
end = time.time() | ||
elapsed_ms = (end - start) * 1000 | ||
times.append(elapsed_ms) | ||
|
||
print(f"RESIZE KORNIA: Average time: {np.median(times)}") | ||
|
||
|
||
# test kornia resize with backend | ||
|
||
times = [] | ||
|
||
# it's expensive to move the tensor to cuda, so we do it once | ||
# if mwe move every time, the time will be much higher 10x | ||
image_torch_cuda = image_torch.cuda() | ||
|
||
for _ in range(num_iters): | ||
start = time.time() | ||
with torch.no_grad(): | ||
kornia.geometry.resize(image_torch_cuda, new_size, interpolation='bilinear') | ||
end = time.time() | ||
elapsed_ms = (end - start) * 1000 | ||
times.append(elapsed_ms) | ||
|
||
print(f"RESIZE KORNIA CUDA: Average time: {np.median(times)}") |