Commit 79c4bcbc authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add benchmark for more models

parent acf146c4
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -94,19 +94,39 @@ class BaseBenchmark:


def create_plot(items: List[Tuple[str, BaseBenchmark]], save_as: str,
                title: str = 'Unnamed Benchmark Plot', run_times=15, try_times=10, figsize=(720, 420), dpi: int = 300):
                title: str = 'Unnamed Benchmark Plot', run_times=15, try_times=10,
                mem_ylog: bool = False, time_ylog: bool = False,
                figsize=(720, 420), dpi: int = 300):
    def fmt_size(x, pos):
        _ = pos
        warnings.filterwarnings('ignore')
        return size_to_bytes_str(x, precision=1)

    def fmt_time(x, pos):
        _ = pos
        if x < 1e-6:
            return f'{x * 1e9:.1f}ns'
        elif x < 1e-3:
            return f'{x * 1e6:.1f}μs'
        elif x < 1:
            return f'{x * 1e3:.1f}ms'
        else:
            return f'{x * 1.0:.1f}s'

    fig, axes = plt.subplots(1, 2, figsize=(figsize[0] / INCHES_TO_PIXELS, figsize[1] / INCHES_TO_PIXELS))

    if mem_ylog:
        axes[0].set_yscale('log')
    axes[0].yaxis.set_major_formatter(FuncFormatter(fmt_size))
    axes[0].set_title('Memory Benchmark')
    axes[0].set_ylabel('Memory Usage')

    if time_ylog:
        axes[1].set_yscale('log')
    axes[1].yaxis.set_major_formatter(FuncFormatter(fmt_time))
    axes[1].set_title('Performance Benchmark (CPU)')
    axes[1].set_ylabel('Time Cost (s)')
    axes[1].set_ylabel('Time Cost')

    labeled = False

    for name, bm in tqdm(items):
+36 −0
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot
from imgutils.detect import detect_faces


class FaceDetectBenchmark(BaseBenchmark):
    def __init__(self, level):
        BaseBenchmark.__init__(self)
        self.level = level

    def load(self):
        from imgutils.detect.face import _open_face_detect_model
        _ = _open_face_detect_model(level=self.level)

    def unload(self):
        from imgutils.detect.face import _open_face_detect_model
        _open_face_detect_model.cache_clear()

    def run(self):
        image_file = random.choice(self.all_images)
        _ = detect_faces(image_file, level=self.level)


if __name__ == '__main__':
    create_plot(
        [
            ('face (yolov8s)', FaceDetectBenchmark('s')),
            ('face (yolov8n)', FaceDetectBenchmark('n')),
        ],
        save_as='benchmark_face_detect.dat.svg',
        title='Benchmark for Anime Face Detections',
        run_times=10,
        try_times=5,
        figsize=(1080, 600)
    )
+37 −0
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot
from imgutils.detect import detect_person


class PersonDetectBenchmark(BaseBenchmark):
    def __init__(self, level):
        BaseBenchmark.__init__(self)
        self.level = level

    def load(self):
        from imgutils.detect.person import _open_person_detect_model
        _ = _open_person_detect_model(level=self.level)

    def unload(self):
        from imgutils.detect.person import _open_person_detect_model
        _open_person_detect_model.cache_clear()

    def run(self):
        image_file = random.choice(self.all_images)
        _ = detect_person(image_file, level=self.level)


if __name__ == '__main__':
    create_plot(
        [
            ('person (yolov8s)', PersonDetectBenchmark('s')),
            ('person (yolov8m)', PersonDetectBenchmark('m')),
            ('person (yolov8x)', PersonDetectBenchmark('x')),
        ],
        save_as='benchmark_person_detect.dat.svg',
        title='Benchmark for Anime Person Detections',
        run_times=10,
        try_times=5,
        figsize=(1080, 600)
    )
+64 −0
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot
from imgutils.edge import get_edge_by_canny, get_edge_by_lineart_anime, get_edge_by_lineart


class CannyBenchmark(BaseBenchmark):
    def load(self):
        pass

    def unload(self):
        pass

    def run(self):
        image_file = random.choice(self.all_images)
        _ = get_edge_by_canny(image_file)


class LineartAnimeBenchmark(BaseBenchmark):
    def load(self):
        from imgutils.edge.lineart_anime import _open_la_anime_model
        _ = _open_la_anime_model()

    def unload(self):
        from imgutils.edge.lineart_anime import _open_la_anime_model
        _open_la_anime_model.cache_clear()

    def run(self):
        image_file = random.choice(self.all_images)
        _ = get_edge_by_lineart_anime(image_file)


class LineartBenchmark(BaseBenchmark):
    def __init__(self, coarse: bool):
        BaseBenchmark.__init__(self)
        self.coarse = coarse

    def load(self):
        from imgutils.edge.lineart import _open_la_model
        _ = _open_la_model(self.coarse)

    def unload(self):
        from imgutils.edge.lineart import _open_la_model
        _open_la_model.cache_clear()

    def run(self):
        image_file = random.choice(self.all_images)
        _ = get_edge_by_lineart(image_file, coarse=self.coarse)


if __name__ == '__main__':
    create_plot(
        [
            ('canny', CannyBenchmark()),
            ('lineart', LineartBenchmark(coarse=False)),
            ('lineart (coarse)', LineartBenchmark(coarse=True)),
            ('lineart-anime', LineartAnimeBenchmark()),
        ],
        save_as='benchmark_edge.dat.svg',
        title='Benchmark for Edge Models',
        run_times=10,
        try_times=5,
        figsize=(1080, 600)
    )
+31 −0
Original line number Diff line number Diff line
import random

from benchmark import BaseBenchmark, create_plot
from imgutils.segment import get_isnetis_mask


class IsnetisBenchmark(BaseBenchmark):
    def load(self):
        from imgutils.segment.isnetis import _get_model
        _ = _get_model()

    def unload(self):
        from imgutils.segment.isnetis import _get_model
        _get_model.cache_clear()

    def run(self):
        image_file = random.choice(self.all_images)
        _ = get_isnetis_mask(image_file)


if __name__ == '__main__':
    create_plot(
        [
            ('isnetis', IsnetisBenchmark()),
        ],
        save_as='benchmark_segment.dat.svg',
        title='Benchmark for Segment Models',
        run_times=10,
        try_times=5,
        figsize=(1080, 600)
    )
Loading