-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [WIP] add image_size_filter; to be tested * add doc and test cases * pre-commit checked * minor fix according to zhijian's suggestion * minor fix according to suggestions by zhijian and yilun
- Loading branch information
Showing
9 changed files
with
252 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
from . import (alphanumeric_filter, average_line_length_filter, | ||
character_repetition_filter, flagged_words_filter, | ||
image_aspect_ratio_filter, language_id_score_filter, | ||
maximum_line_length_filter, perplexity_filter, | ||
special_characters_filter, specified_field_filter, | ||
specified_numeric_field_filter, stopwords_filter, suffix_filter, | ||
text_length_filter, token_num_filter, word_num_filter, | ||
word_repetition_filter) | ||
image_aspect_ratio_filter, image_size_filter, | ||
language_id_score_filter, maximum_line_length_filter, | ||
perplexity_filter, special_characters_filter, | ||
specified_field_filter, specified_numeric_field_filter, | ||
stopwords_filter, suffix_filter, text_length_filter, | ||
token_num_filter, word_num_filter, word_repetition_filter) |
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,74 @@ | ||
import numpy as np | ||
|
||
from data_juicer.utils.constant import Fields, StatsKeys | ||
from data_juicer.utils.mm_utils import get_image_size, size_to_bytes | ||
|
||
from ..base_op import OPERATORS, Filter | ||
|
||
|
||
@OPERATORS.register_module('image_size_filter') | ||
class ImageSizeFilter(Filter): | ||
"""Keep data samples whose image size (in bytes/kb/MB/...) within a | ||
specific range. | ||
""" | ||
|
||
def __init__(self, | ||
min_size: str = '0', | ||
max_size: str = '1TB', | ||
any_or_all: str = 'any', | ||
*args, | ||
**kwargs): | ||
""" | ||
Initialization method. | ||
:param min_size: The min image size to keep samples. set to be "0" by | ||
default for no size constraint | ||
:param max_size: The max image size to keep samples. set to be | ||
"1Tb" by default, an approximate for un-limited case | ||
:param any_or_all: keep this sample with 'any' or 'all' strategy of | ||
all images. 'any': keep this sample if any images meet the | ||
condition. 'all': keep this sample only if all images meet the | ||
condition. | ||
:param args: extra args | ||
:param kwargs: extra args | ||
""" | ||
super().__init__(*args, **kwargs) | ||
self.min_size = size_to_bytes(min_size) | ||
self.max_size = size_to_bytes(max_size) | ||
if any_or_all not in ['any', 'all']: | ||
raise ValueError(f'Keep strategy [{any_or_all}] is not supported. ' | ||
f'Can only be one of ["any", "all"].') | ||
self.any = (any_or_all == 'any') | ||
|
||
def compute_stats(self, sample, context=False): | ||
# check if it's computed already | ||
if StatsKeys.image_sizes in sample[Fields.stats]: | ||
return sample | ||
|
||
# there is no image in this sample | ||
if self.image_key not in sample or not sample[self.image_key]: | ||
sample[Fields.stats][StatsKeys.image_sizes] = np.array( | ||
[], dtype=np.float64) | ||
return sample | ||
|
||
# for size calculation, no need to load images into memory | ||
sample[Fields.stats][StatsKeys.image_sizes] = [ | ||
get_image_size(img_path) for img_path in sample[self.image_key] | ||
] | ||
|
||
return sample | ||
|
||
def process(self, sample): | ||
image_sizes = sample[Fields.stats][StatsKeys.image_sizes] | ||
keep_bools = np.array([ | ||
self.min_size <= image_size <= self.max_size | ||
for image_size in image_sizes | ||
]) | ||
if len(keep_bools) <= 0: | ||
return True | ||
|
||
# different strategies | ||
if self.any: | ||
return keep_bools.any() | ||
else: | ||
return keep_bools.all() |
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
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,118 @@ | ||
import os | ||
import unittest | ||
|
||
from datasets import Dataset | ||
|
||
from data_juicer.ops.filter.image_size_filter import ImageSizeFilter | ||
from data_juicer.utils.constant import Fields | ||
|
||
|
||
class ImageSizeFilterTest(unittest.TestCase): | ||
|
||
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), | ||
'..', 'data') | ||
img1_path = os.path.join(data_path, 'img1.png') | ||
img2_path = os.path.join(data_path, 'img2.jpg') | ||
img3_path = os.path.join(data_path, 'img3.jpg') | ||
|
||
def _run_image_size_filter(self, | ||
dataset: Dataset, target_list, | ||
op): | ||
if Fields.stats not in dataset.features: | ||
dataset = dataset.add_column(name=Fields.stats, | ||
column=[{}] * dataset.num_rows) | ||
dataset = dataset.map(op.compute_stats) | ||
dataset = dataset.filter(op.process) | ||
dataset = dataset.select_columns(column_names=[op.image_key]) | ||
res_list = dataset.to_list() | ||
self.assertEqual(res_list, target_list) | ||
|
||
def test_min_max(self): | ||
|
||
ds_list = [{ | ||
'images': [self.img1_path] # 171KB | ||
}, { | ||
'images': [self.img2_path] # 189KB | ||
}, { | ||
'images': [self.img3_path] # 114KB | ||
}] | ||
tgt_list = [{ | ||
'images': [self.img1_path] | ||
}] | ||
dataset = Dataset.from_list(ds_list) | ||
op = ImageSizeFilter(min_size="120kb", max_size="180KB") | ||
self._run_image_size_filter(dataset, tgt_list, op) | ||
|
||
def test_min(self): | ||
|
||
ds_list = [{ | ||
'images': [self.img1_path] # 171KB | ||
}, { | ||
'images': [self.img2_path] # 189KB | ||
}, { | ||
'images': [self.img3_path] # 114KB | ||
}] | ||
tgt_list = [{ | ||
'images': [self.img1_path] | ||
}, { | ||
'images': [self.img2_path] | ||
}] | ||
dataset = Dataset.from_list(ds_list) | ||
op = ImageSizeFilter(min_size="120kib") | ||
self._run_image_size_filter(dataset, tgt_list, op) | ||
|
||
def test_max(self): | ||
|
||
ds_list = [{ | ||
'images': [self.img1_path] # 171KB | ||
}, { | ||
'images': [self.img2_path] # 189KB | ||
}, { | ||
'images': [self.img3_path] # 114KB | ||
}] | ||
tgt_list = [{ | ||
'images': [self.img1_path] | ||
}, { | ||
'images': [self.img3_path] | ||
}] | ||
dataset = Dataset.from_list(ds_list) | ||
op = ImageSizeFilter(max_size="180KiB") | ||
self._run_image_size_filter(dataset, tgt_list, op) | ||
|
||
def test_any(self): | ||
|
||
ds_list = [{ | ||
'images': [self.img1_path, self.img2_path] | ||
}, { | ||
'images': [self.img2_path, self.img3_path] | ||
}, { | ||
'images': [self.img1_path, self.img3_path] | ||
}] | ||
tgt_list = [{ | ||
'images': [self.img1_path, self.img2_path] | ||
}, { | ||
'images': [self.img1_path, self.img3_path] | ||
}] | ||
dataset = Dataset.from_list(ds_list) | ||
op = ImageSizeFilter(min_size="120kb", max_size="180KB", | ||
any_or_all='any') | ||
self._run_image_size_filter(dataset, tgt_list, op) | ||
|
||
def test_all(self): | ||
|
||
ds_list = [{ | ||
'images': [self.img1_path, self.img2_path] | ||
}, { | ||
'images': [self.img2_path, self.img3_path] | ||
}, { | ||
'images': [self.img1_path, self.img3_path] | ||
}] | ||
tgt_list = [] | ||
dataset = Dataset.from_list(ds_list) | ||
op = ImageSizeFilter(min_size="120kb", max_size="180KB", | ||
any_or_all='all') | ||
self._run_image_size_filter(dataset, tgt_list, op) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |