Skip to content

Commit

Permalink
dev(narugo): add furry unittest and benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Nov 5, 2024
1 parent 9c994e6 commit 00d8b0e
Show file tree
Hide file tree
Showing 29 changed files with 109 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/source/api_doc/validate/furry.plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import glob
import os.path

from natsort import natsorted

from plot import image_plot

if __name__ == '__main__':
image_plot(
*natsorted(glob.glob(os.path.join('furry', 'non_furry', '*.jpg'))),
*natsorted(glob.glob(os.path.join('furry', 'furry', '*.jpg'))),
columns=4, figsize=(10, 15),
)
21 changes: 21 additions & 0 deletions docs/source/api_doc/validate/furry.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
imgutils.validate.furry
=============================================

.. currentmodule:: imgutils.validate.furry

.. automodule:: imgutils.validate.furry


anime_furry_score
-----------------------------

.. autofunction:: anime_furry_score



anime_furry
-----------------------------

.. autofunction:: anime_furry


Binary file added docs/source/api_doc/validate/furry/furry/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/api_doc/validate/furry/furry/6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/api_doc/validate/furry/furry/7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/api_doc/validate/furry/furry/8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions docs/source/api_doc/validate/furry_benchmark.plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import random

from benchmark import BaseBenchmark, create_plot_cli
from imgutils.generic.classify import _open_models_for_repo_id
from imgutils.validate import anime_furry
from imgutils.validate.furry import _REPO_ID

_MODEL_NAMES = _open_models_for_repo_id(_REPO_ID).model_names


class AnimeFurryBenchmark(BaseBenchmark):
def __init__(self, model):
BaseBenchmark.__init__(self)
self.model = model

def load(self):
_open_models_for_repo_id(_REPO_ID)._open_model(self.model)

def unload(self):
_open_models_for_repo_id(_REPO_ID).clear()

def run(self):
image_file = random.choice(self.all_images)
_ = anime_furry(image_file, self.model)


if __name__ == '__main__':
create_plot_cli(
[
(name, AnimeFurryBenchmark(name))
for name in _MODEL_NAMES
],
title='Benchmark for Anime Furry Check Models',
run_times=10,
try_times=20,
)()
1 change: 1 addition & 0 deletions docs/source/api_doc/validate/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ imgutils.validate
color
completeness
dbrating
furry
monochrome
nsfw
portrait
Expand Down
Binary file added test/testfile/furry/furry/danbooru_10103497.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_10451753.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_4648096.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_7099146.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_7211844.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_7732759.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_8890220.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/testfile/furry/furry/danbooru_9348860.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions test/validate/test_furry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import glob
import os.path

import pytest

from imgutils.generic.classify import _open_models_for_repo_id
from imgutils.validate import anime_furry, anime_furry_score
from imgutils.validate.furry import _REPO_ID
from test.testings import get_testfile

_ROOT_DIR = get_testfile('furry')
_EXAMPLE_FILES = [
(os.path.relpath(file, _ROOT_DIR), os.path.basename(os.path.dirname(file)))
for file in glob.glob(get_testfile('furry', '**', '*.jpg'), recursive=True)
]


@pytest.fixture(scope='module', autouse=True)
def _release_model_after_run():
try:
yield
finally:
_open_models_for_repo_id(_REPO_ID).clear()


@pytest.mark.unittest
class TestValidatePortrait:
@pytest.mark.parametrize(['image', 'label'], _EXAMPLE_FILES)
def test_anime_furry(self, image, label):
image_file = get_testfile('furry', image)
tag, score = anime_furry(image_file)
assert tag == label

@pytest.mark.parametrize(['image', 'label'], _EXAMPLE_FILES)
def test_anime_furry_score(self, image, label):
image_file = get_testfile('furry', image)
scores = anime_furry_score(image_file)
assert scores[label] > 0.5

0 comments on commit 00d8b0e

Please sign in to comment.