Skip to content

Commit

Permalink
refactor benches
Browse files Browse the repository at this point in the history
  • Loading branch information
rostyq committed Jan 8, 2024
1 parent 5183a08 commit f172ebf
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 44 deletions.
13 changes: 9 additions & 4 deletions benches/clusterizer_bench.rs → benches/clusterizer.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#[path = "./common/macros.rs"]
mod macros;

use criterion::{black_box, criterion_group, BenchmarkId, Criterion, Throughput};
use std::time::Duration;

use criterion::{black_box, BenchmarkId, Criterion, Throughput};

use rand::prelude::*;

use pico_detect::{clusterize::Clusterizer, perturbate::Perturbator, Detection, Square};
use rand_xoshiro::Xoroshiro128PlusPlus;

fn bench_clusterizer_run(c: &mut Criterion) {
pub fn bench_clusterize(c: &mut Criterion) {
let mut group = c.benchmark_group("Clusterizer::clusterize");

group.warm_up_time(Duration::from_secs(5));
group.sample_size(1000);
group.measurement_time(Duration::from_secs(15));

let init = Square::new(100, 100, 100).into();

for n in [10, 30, 50].iter() {
Expand Down Expand Up @@ -40,5 +47,3 @@ fn bench_clusterizer_run(c: &mut Criterion) {

group.finish();
}

criterion_group!(benches, bench_clusterizer_run);
10 changes: 4 additions & 6 deletions benches/detector_bench.rs → benches/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ mod macros;

use std::fs;

use criterion::{black_box, criterion_group, Criterion};
use criterion::{black_box, Criterion};

use image;
use pico_detect::{Detector, Square};

fn bench_detector_load(c: &mut Criterion) {
pub fn bench_load(c: &mut Criterion) {
let model_data = fs::read(model_path!(facefinder)).unwrap();

c.bench_function("Detector::load", |b| {
b.iter(|| Detector::load(black_box(model_data.as_slice())).unwrap())
});
}

fn bench_detector_detect(c: &mut Criterion) {
pub fn bench_inference(c: &mut Criterion) {
let image = load_test_image!();
let detector = load_model!(facefinder);

let s = Square::new(216, 208, 170);

c.bench_function("Detector::detect", |b| {
c.bench_function("Detector::classify[inference]", |b| {
b.iter(|| detector.classify(black_box(&image), black_box(s)));
});
}

criterion_group!(benches, bench_detector_load, bench_detector_detect);
57 changes: 43 additions & 14 deletions benches/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
use std::time::Duration;

#[macro_use]
extern crate criterion;

mod clusterizer_bench;
mod multiscaler_bench;
mod perturbator_bench;
mod detector_bench;
mod localizer_bench;
mod shaper_bench;
use criterion::Criterion;

mod clusterizer;
mod detector;
mod localizer;
mod multiscaler;
mod perturbator;
mod shaper;

criterion_group!(
name = loading;
config = Criterion::default()
.warm_up_time(Duration::from_secs(15))
.sample_size(25)
.noise_threshold(0.05)
.measurement_time(Duration::from_secs(10));
targets =
detector::bench_load,
localizer::bench_load,
shaper::bench_load,
);

criterion_group!(
name = detection;
config = Criterion::default()
.warm_up_time(Duration::from_secs(5))
.sample_size(100)
.measurement_time(Duration::from_secs(20));
targets =
detector::bench_inference,
localizer::bench_inference,
shaper::bench_inference,
);

criterion_group!(
utils,
clusterizer::bench_clusterize,
multiscaler::bench_run,
perturbator::bench_run
);

criterion_main!(
clusterizer_bench::benches,
multiscaler_bench::benches,
perturbator_bench::benches,
detector_bench::benches,
localizer_bench::benches,
shaper_bench::benches,
);
criterion_main!(loading, detection, utils);
10 changes: 4 additions & 6 deletions benches/localizer_bench.rs → benches/localizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ mod macros;

use std::fs;

use criterion::{black_box, criterion_group, Criterion};
use criterion::{black_box, Criterion};

use image;
use pico_detect::{Localizer, Square};

fn bench_localizer_load(c: &mut Criterion) {
pub fn bench_load(c: &mut Criterion) {
let model_data = fs::read(model_path!(puploc)).unwrap();

c.bench_function("Localizer::load", |b| {
b.iter(|| Localizer::load(black_box(model_data.as_slice())).unwrap())
});
}

fn bench_localizer_localize(c: &mut Criterion) {
pub fn bench_inference(c: &mut Criterion) {
let image = load_test_image!();
let localizer = load_model!(puploc);

let s = Square::new(310, 247, 38).into();

c.bench_function("Localizer::localize", |b| {
c.bench_function("Localizer::localize[inference]", |b| {
b.iter(|| localizer.localize(black_box(&image), black_box(s)));
});
}

criterion_group!(benches, bench_localizer_load, bench_localizer_localize);
8 changes: 4 additions & 4 deletions benches/multiscaler_bench.rs → benches/multiscaler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod macros;

use std::fmt::Display;

use criterion::{black_box, criterion_group, BenchmarkId, Criterion, Throughput};
use criterion::{black_box, BenchmarkId, Criterion, Throughput};

use imageproc::rect::Rect;
use pico_detect::multiscale::Multiscaler;
Expand All @@ -29,7 +29,7 @@ impl From<(u32, u32)> for Size {
}
}

fn bench_multiscale_run(c: &mut Criterion) {
pub fn bench_run(c: &mut Criterion) {
static SIZES: &[(u32, u32)] = &[
(320, 240),
(480, 360),
Expand All @@ -40,6 +40,8 @@ fn bench_multiscale_run(c: &mut Criterion) {

let mut group = c.benchmark_group("Multiscaler::run");

group.sample_size(10000);

for size in SIZES.iter().map(|s| Size::from(*s)) {
let ms = Multiscaler::builder()
.min_size(100)
Expand All @@ -64,5 +66,3 @@ fn bench_multiscale_run(c: &mut Criterion) {

group.finish();
}

criterion_group!(benches, bench_multiscale_run);
8 changes: 4 additions & 4 deletions benches/perturbator_bench.rs → benches/perturbator.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#[path = "./common/macros.rs"]
mod macros;

use criterion::{black_box, criterion_group, BenchmarkId, Criterion, Throughput};
use criterion::{black_box, BenchmarkId, Criterion, Throughput};

use pico_detect::{Square, perturbate::Perturbator};
use rand::SeedableRng;
use rand_xoshiro::Xoroshiro128PlusPlus;

fn bench_perturbator_run(c: &mut Criterion) {
pub fn bench_run(c: &mut Criterion) {
let mut group = c.benchmark_group("Perturbator::run");

group.sample_size(10000);

let perturbator = Perturbator::default();
let mut rng = Xoroshiro128PlusPlus::seed_from_u64(42);

Expand All @@ -31,5 +33,3 @@ fn bench_perturbator_run(c: &mut Criterion) {

group.finish();
}

criterion_group!(benches, bench_perturbator_run);
10 changes: 4 additions & 6 deletions benches/shaper_bench.rs → benches/shaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,26 @@ mod macros;

use std::fs;

use criterion::{black_box, criterion_group, Criterion};
use criterion::{black_box, Criterion};

use image;
use pico_detect::{Shaper, Square};

fn bench_shaper_load(c: &mut Criterion) {
pub fn bench_load(c: &mut Criterion) {
let model_data = fs::read(model_path!(shaper)).unwrap();

c.bench_function("Shaper::load", |b| {
b.iter(|| Shaper::load(black_box(model_data.as_slice())).unwrap())
});
}

fn bench_shaper_shape(c: &mut Criterion) {
pub fn bench_inference(c: &mut Criterion) {
let image = load_test_image!();
let shaper = load_model!(shaper);

let r = Square::new(213, 225, 153).into();

c.bench_function("Shaper::shape", |b| {
c.bench_function("Shaper::shape[inference]", |b| {
b.iter(|| shaper.shape(black_box(&image), black_box(r)));
});
}

criterion_group!(benches, bench_shaper_load, bench_shaper_shape);
8 changes: 8 additions & 0 deletions src/imageutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ where
get_pixel_i64(image, point.x as i64, point.y as i64).map(|p| p.0[0])
}

#[inline]
pub fn get_luma_by_point<I, T: Copy>(image: &I, point: Point2<i64>) -> Option<T>
where
I: GenericImageView<Pixel = Luma<T>>,
{
get_pixel_i64(image, point.x, point.y).map(|p| p.0[0])
}

#[inline]
pub fn image_bounds_as_i64<I>(image: &I) -> (i64, i64, i64, i64)
where
Expand Down

0 comments on commit f172ebf

Please sign in to comment.