-
Notifications
You must be signed in to change notification settings - Fork 425
add image blur mapper #180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
573a704
fix opencc serialization error
chenhesen 988224f
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 0ac51cc
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 4fee9a1
support audio-text data reading
chenhesen d856a80
update multimodal_README
chenhesen 29de3de
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen fc98733
fix pre-commit error
chenhesen 539f099
modify audio_special_token
chenhesen ca34dfc
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 4c27643
support only one target_field
chenhesen e54d197
fix pre-commit
chenhesen 4743e62
add id for log
chenhesen 6c58bee
fix conflict
chenhesen 6449f15
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 70b6c73
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 4105d1e
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 027af2b
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 8a4a0e7
add remove_repeat_sentences_mapper
chenhesen 65bee64
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 05627ed
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 021d14b
modify mapper op number
chenhesen 584783e
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 1688099
update image_blur
chenhesen 5b72309
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen fb3188a
add image_blur_mapper
chenhesen 8b3d87d
add image_blur_mapper
chenhesen d80d868
precommit
chenhesen 3e264b6
update __init__
chenhesen 1bf1c7d
Merge branch 'main' of github.com:alibaba/data-juicer
chenhesen 58ae2c6
fix conflicts
chenhesen 22021b7
fix conficts
chenhesen 344e240
replaced by the latest load_data_with_context
chenhesen a52311f
fix docs conflicts
chenhesen 5a6b552
fix Operators_ZH
chenhesen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,80 @@ | ||
| import os | ||
|
|
||
| import numpy as np | ||
|
|
||
| from data_juicer.utils.constant import Fields | ||
| from data_juicer.utils.mm_utils import load_data_with_context, load_image | ||
|
|
||
| from ..base_op import OPERATORS, Mapper | ||
| from ..op_fusion import LOADED_IMAGES | ||
|
|
||
|
|
||
| @OPERATORS.register_module('image_blur_mapper') | ||
| @LOADED_IMAGES.register_module('image_blur_mapper') | ||
| class ImageBlurMapper(Mapper): | ||
| """Mapper to blur images. | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| p: float = 0.2, | ||
| blur_type: str = 'gaussian', | ||
| radius: float = 2, | ||
| *args, | ||
| **kwargs): | ||
| """ | ||
| Initialization method. | ||
|
|
||
| :param p: Probability of the image being blured. | ||
| :param blur_type: Type of blur kernel, including | ||
| ['mean', 'box', 'gaussian']. | ||
| :param radius: Radius of blur kernel. | ||
| :param args: extra args | ||
| :param kwargs: extra args | ||
| """ | ||
| super().__init__(*args, **kwargs) | ||
| if blur_type not in ['mean', 'box', 'gaussian']: | ||
| raise ValueError( | ||
| f'Blur_type [{blur_type}] is not supported. ' | ||
| f'Can only be one of ["mean", "box", "gaussian"]. ') | ||
| if radius < 0: | ||
| raise ValueError('Radius must be >= 0. ') | ||
|
|
||
| self.p = p | ||
|
|
||
| from PIL import ImageFilter | ||
| if blur_type == 'mean': | ||
| self.blur = ImageFilter.BLUR | ||
| elif blur_type == 'box': | ||
| self.blur = ImageFilter.BoxBlur(radius) | ||
| else: | ||
| self.blur = ImageFilter.GaussianBlur(radius) | ||
|
|
||
| def process(self, sample, context=False): | ||
| # there is no image in this sample | ||
| if self.image_key not in sample or not sample[self.image_key]: | ||
| return sample | ||
|
|
||
| # load images | ||
| loaded_image_keys = sample[self.image_key] | ||
| sample, images = load_data_with_context(sample, context, | ||
| loaded_image_keys, load_image) | ||
|
|
||
| for index, value in enumerate(loaded_image_keys): | ||
| if self.p < np.random.rand(): | ||
| continue | ||
| else: | ||
| blured_image_key = os.path.join( | ||
| os.path.dirname(value), | ||
| '_blured.'.join(os.path.basename(value).split('.'))) | ||
| if not os.path.exists( | ||
| blured_image_key) or blured_image_key not in images: | ||
| blured_image = images[value].convert('RGB').filter( | ||
| self.blur) | ||
| images[blured_image_key] = blured_image | ||
| blured_image.save(blured_image_key) | ||
| if context: | ||
| sample[Fields.context][blured_image_key] = blured_image | ||
| loaded_image_keys[index] = blured_image_key | ||
|
|
||
| sample[self.image_key] = loaded_image_keys | ||
| return sample | ||
This file contains hidden or 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 hidden or 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 hidden or 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,124 @@ | ||
| import os | ||
| import unittest | ||
| import numpy as np | ||
|
|
||
| from datasets import Dataset | ||
| from data_juicer.utils.mm_utils import load_image | ||
|
|
||
| from data_juicer.ops.mapper.image_blur_mapper import ImageBlurMapper | ||
|
|
||
|
|
||
| class ImageBlurMapperTest(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 _get_blured_img_path(self, path): | ||
| return os.path.join(os.path.dirname(path), '_blured.'.join(os.path.basename(path).split('.'))) | ||
|
|
||
| def _get_blur_kernel(self, blur_type = 'gaussian', radius = 2): | ||
| from PIL import ImageFilter | ||
| if blur_type == 'mean': | ||
| return ImageFilter.BLUR | ||
| elif blur_type == 'box': | ||
| return ImageFilter.BoxBlur(radius) | ||
| else: | ||
| return ImageFilter.GaussianBlur(radius) | ||
|
|
||
| def _run_image_blur_mapper(self, op, source_list, target_list, blur_kernel): | ||
| dataset = Dataset.from_list(source_list) | ||
| dataset = dataset.map(op.process) | ||
| res_list = dataset.to_list() | ||
| self.assertEqual(res_list, target_list) | ||
| for source, res in zip(source_list, res_list): | ||
| for s_path, r_path in zip(source[op.image_key], res[op.image_key]): | ||
| s_img = load_image(s_path).convert('RGB').filter(blur_kernel) | ||
| t_path = 'temp4test' + os.path.splitext(s_path)[-1] | ||
| s_img.save(t_path) | ||
| t_img = np.array(load_image(t_path)) | ||
| r_img = np.array(load_image(r_path)) | ||
| os.remove(t_path) | ||
| np.testing.assert_array_equal(t_img, r_img) | ||
|
|
||
| def test(self): | ||
| ds_list = [{ | ||
| 'images': [self.img1_path] | ||
| }, { | ||
| 'images': [self.img2_path] | ||
| }, { | ||
| 'images': [self.img3_path] | ||
| }] | ||
| tgt_list = [{ | ||
| 'images': [self._get_blured_img_path(self.img1_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img2_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img3_path)] | ||
| }] | ||
| op = ImageBlurMapper(p = 1, blur_type = 'gaussian', radius = 2) | ||
| blur_kernel = self._get_blur_kernel('gaussian', 2) | ||
| self._run_image_blur_mapper(op, ds_list, tgt_list, blur_kernel) | ||
|
|
||
| def test_blur_type(self): | ||
| ds_list = [{ | ||
| 'images': [self.img2_path] | ||
| }, { | ||
| 'images': [self.img3_path] | ||
| }, { | ||
| 'images': [self.img1_path] | ||
| }] | ||
| tgt_list = [{ | ||
| 'images': [self._get_blured_img_path(self.img2_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img3_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img1_path)] | ||
| }] | ||
| op = ImageBlurMapper(p = 1, blur_type = 'box', radius = 2) | ||
| blur_kernel = self._get_blur_kernel('box', 2) | ||
| self._run_image_blur_mapper(op, ds_list, tgt_list, blur_kernel) | ||
|
|
||
| def test_radius(self): | ||
| ds_list = [{ | ||
| 'images': [self.img3_path] | ||
| }, { | ||
| 'images': [self.img2_path] | ||
| }, { | ||
| 'images': [self.img1_path] | ||
| }] | ||
| tgt_list = [{ | ||
| 'images': [self._get_blured_img_path(self.img3_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img2_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img1_path)] | ||
| }] | ||
| op = ImageBlurMapper(p = 1, blur_type = 'gaussian', radius = 5) | ||
| blur_kernel = self._get_blur_kernel('gaussian', 5) | ||
| self._run_image_blur_mapper(op, ds_list, tgt_list, blur_kernel) | ||
|
|
||
| def test_multi_img(self): | ||
| ds_list = [{ | ||
| 'images': [self.img1_path, self.img2_path, self.img3_path] | ||
| }, { | ||
| 'images': [self.img2_path] | ||
| }, { | ||
| 'images': [self.img3_path, self.img1_path] | ||
| }] | ||
| tgt_list = [{ | ||
| 'images': [self._get_blured_img_path(self.img1_path), self._get_blured_img_path(self.img2_path), self._get_blured_img_path(self.img3_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img2_path)] | ||
| }, { | ||
| 'images': [self._get_blured_img_path(self.img3_path), self._get_blured_img_path(self.img1_path)] | ||
| }] | ||
| op = ImageBlurMapper(p = 1, blur_type = 'gaussian', radius = 2) | ||
| blur_kernel = self._get_blur_kernel('gaussian', 2) | ||
| self._run_image_blur_mapper(op, ds_list, tgt_list, blur_kernel) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.