-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dev(narugo): add overlap dropping for tags
- Loading branch information
1 parent
17df867
commit 2c7cf25
Showing
11 changed files
with
297 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ imgutils.tagging | |
wd14 | ||
deepdanbooru | ||
format | ||
overlap |
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,22 @@ | ||
imgutils.tagging.overlap | ||
==================================== | ||
|
||
.. currentmodule:: imgutils.tagging.overlap | ||
|
||
.. automodule:: imgutils.tagging.overlap | ||
|
||
|
||
drop_overlap_tags | ||
---------------------------------- | ||
|
||
.. autofunction:: drop_overlap_tags | ||
|
||
|
||
|
||
drop_overlaps_for_dict | ||
---------------------------------- | ||
|
||
.. autofunction:: drop_overlaps_for_dict | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import json | ||
from functools import lru_cache | ||
from typing import Mapping, List | ||
|
||
from huggingface_hub import hf_hub_download | ||
|
||
|
||
@lru_cache() | ||
def _get_overlap_tags() -> Mapping[str, List[str]]: | ||
""" | ||
Retrieve the overlap tag information from the specified Hugging Face Hub repository. | ||
This function downloads a JSON file containing tag overlap information and parses it into a dictionary. | ||
:return: A dictionary where keys are tags and values are lists of overlapping tags. | ||
:rtype: Mapping[str, List[str]] | ||
""" | ||
json_file = hf_hub_download( | ||
'alea31415/tag_filtering', | ||
'overlap_tags.json', | ||
repo_type='dataset', | ||
) | ||
with open(json_file, 'r') as file: | ||
data = json.load(file) | ||
|
||
return { | ||
entry['query']: entry['has_overlap'] | ||
for entry in data if 'has_overlap' in entry and entry['has_overlap'] | ||
} | ||
|
||
|
||
def drop_overlap_tags(tags: List[str]) -> List[str]: | ||
""" | ||
Drop overlapping tags from the given list of tags. | ||
This function removes tags that have overlaps with other tags based on precomputed overlap information. | ||
:param tags: A list of tags. | ||
:type tags: List[str] | ||
:return: A list of tags without overlaps. | ||
:rtype: List[str] | ||
""" | ||
overlap_tags_dict = _get_overlap_tags() | ||
result_tags = [] | ||
tags_underscore = [tag.replace(' ', '_') for tag in tags] | ||
|
||
for tag, tag_ in zip(tags, tags_underscore): | ||
|
||
to_remove = False | ||
|
||
# Case 1: If the tag is a key and some of the associated values are in tags | ||
if tag_ in overlap_tags_dict: | ||
overlap_values = set(val for val in overlap_tags_dict[tag_]) | ||
if overlap_values.intersection(set(tags_underscore)): | ||
to_remove = True | ||
|
||
# Checking superword condition separately | ||
for tag_another in tags: | ||
if tag in tag_another and tag != tag_another: | ||
to_remove = True | ||
break | ||
|
||
if not to_remove: | ||
result_tags.append(tag) | ||
|
||
return result_tags | ||
|
||
|
||
def drop_overlaps_for_dict(tags: Mapping[str, float]) -> Mapping[str, float]: | ||
""" | ||
Drop overlapping tags from the given dictionary of tags with confidence scores. | ||
This function removes tags that have overlaps with other tags based on precomputed overlap information. | ||
:param tags: A dictionary where keys are tags and values are confidence scores. | ||
:type tags: Mapping[str, float] | ||
:return: A dictionary with non-overlapping tags and their corresponding confidence scores. | ||
:rtype: Mapping[str, float] | ||
""" | ||
key_set = set(drop_overlap_tags(list(tags.keys()))) | ||
return {tag: confidence for tag, confidence in tags.items() if tag in key_set} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
|
||
from imgutils.tagging import drop_overlaps_for_dict, drop_overlap_tags | ||
|
||
|
||
@pytest.fixture() | ||
def complex_dict_tags(): | ||
return { | ||
'1girl': 0.998362123966217, 'solo': 0.9912548065185547, 'long_hair': 0.9401906728744507, | ||
'breasts': 0.983635425567627, 'looking_at_viewer': 0.9146994352340698, 'blush': 0.8892400860786438, | ||
'smile': 0.43393653631210327, 'bangs': 0.49712443351745605, 'large_breasts': 0.5196534395217896, | ||
'navel': 0.9653235077857971, 'hair_between_eyes': 0.5786703824996948, 'very_long_hair': 0.8142435550689697, | ||
'closed_mouth': 0.9369247555732727, 'nipples': 0.9660118222236633, 'purple_eyes': 0.9676010012626648, | ||
'collarbone': 0.588348925113678, 'nude': 0.9496222734451294, 'red_hair': 0.9200156331062317, | ||
'sweat': 0.8690457344055176, 'horns': 0.9711267948150635, 'pussy': 0.9868264198303223, | ||
'spread_legs': 0.9603149890899658, 'armpits': 0.9024748802185059, 'stomach': 0.6723923087120056, | ||
'arms_up': 0.9380699396133423, 'completely_nude': 0.9002960920333862, 'uncensored': 0.8612104058265686, | ||
'pussy_juice': 0.6021570563316345, 'feet_out_of_frame': 0.39779460430145264, 'on_bed': 0.610720157623291, | ||
'arms_behind_head': 0.44814401865005493, 'breasts_apart': 0.39798974990844727, 'clitoris': 0.5310801267623901, | ||
} | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestTaggingOverlap: | ||
def test_drop_overlap_tags(self): | ||
assert drop_overlap_tags(['1girl', 'solo', 'long_hair', 'very_long_hair', 'red_hair']) == \ | ||
['1girl', 'solo', 'very_long_hair', 'red_hair'] | ||
|
||
def test_drop_overlaps_for_dict_complex(self, complex_dict_tags): | ||
assert drop_overlaps_for_dict(complex_dict_tags) == pytest.approx({ | ||
'1girl': 0.998362123966217, 'solo': 0.9912548065185547, 'looking_at_viewer': 0.9146994352340698, | ||
'blush': 0.8892400860786438, 'smile': 0.43393653631210327, 'bangs': 0.49712443351745605, | ||
'large_breasts': 0.5196534395217896, 'navel': 0.9653235077857971, 'hair_between_eyes': 0.5786703824996948, | ||
'very_long_hair': 0.8142435550689697, 'closed_mouth': 0.9369247555732727, 'nipples': 0.9660118222236633, | ||
'purple_eyes': 0.9676010012626648, 'collarbone': 0.588348925113678, 'red_hair': 0.9200156331062317, | ||
'sweat': 0.8690457344055176, 'horns': 0.9711267948150635, 'spread_legs': 0.9603149890899658, | ||
'armpits': 0.9024748802185059, 'stomach': 0.6723923087120056, 'arms_up': 0.9380699396133423, | ||
'completely_nude': 0.9002960920333862, 'uncensored': 0.8612104058265686, 'pussy_juice': 0.6021570563316345, | ||
'feet_out_of_frame': 0.39779460430145264, 'on_bed': 0.610720157623291, | ||
'arms_behind_head': 0.44814401865005493, 'breasts_apart': 0.39798974990844727, | ||
'clitoris': 0.5310801267623901 | ||
}) |
Oops, something went wrong.