generated from okotaku/template
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from okotaku/feat/support_random_mask_choice
[Feature] Support RandomChoice for random mask
- Loading branch information
Showing
14 changed files
with
321 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
train_pipeline = [ | ||
dict(type="torchvision/Resize", size=512, interpolation="bilinear"), | ||
dict(type="RandomCrop", size=512), | ||
dict(type="RandomHorizontalFlip", p=0.5), | ||
dict(type="RandomChoice", | ||
transforms=[ | ||
[dict( | ||
type="LoadMask", | ||
mask_mode="irregular", | ||
mask_config=dict( | ||
num_vertices=(4, 10), | ||
max_angle=6.0, | ||
length_range=(20, 200), | ||
brush_width=(10, 100), | ||
area_ratio_range=(0.15, 0.65)))], | ||
[dict( | ||
type="LoadMask", | ||
mask_mode="irregular", | ||
mask_config=dict( | ||
num_vertices=(1, 5), | ||
max_angle=6.0, | ||
length_range=(40, 450), | ||
brush_width=(20, 250), | ||
area_ratio_range=(0.15, 0.65)))], | ||
[dict( | ||
type="LoadMask", | ||
mask_mode="irregular", | ||
mask_config=dict( | ||
num_vertices=(4, 70), | ||
max_angle=6.0, | ||
length_range=(15, 100), | ||
brush_width=(5, 20), | ||
area_ratio_range=(0.15, 0.65)))], | ||
[dict( | ||
type="LoadMask", | ||
mask_mode="bbox", | ||
mask_config=dict( | ||
max_bbox_shape=(150, 150), | ||
max_bbox_delta=50, | ||
min_margin=0))], | ||
[dict( | ||
type="LoadMask", | ||
mask_mode="bbox", | ||
mask_config=dict( | ||
max_bbox_shape=(300, 300), | ||
max_bbox_delta=100, | ||
min_margin=10))], | ||
]), | ||
dict(type="torchvision/ToTensor"), | ||
dict(type="MaskToTensor"), | ||
dict(type="DumpImage", max_imgs=10, dump_dir="work_dirs/dump"), | ||
dict(type="torchvision/Normalize", mean=[0.5], std=[0.5]), | ||
dict(type="GetMaskedImage"), | ||
dict(type="PackInputs", | ||
input_keys=["img", "mask", "masked_image", "text"]), | ||
] | ||
train_dataloader = dict( | ||
batch_size=4, | ||
num_workers=4, | ||
dataset=dict( | ||
type="HFDreamBoothDataset", | ||
dataset="diffusers/dog-example", | ||
instance_prompt="a photo of sks dog", | ||
pipeline=train_pipeline, | ||
class_prompt=None), | ||
sampler=dict(type="InfiniteSampler", shuffle=True), | ||
) | ||
|
||
val_dataloader = None | ||
val_evaluator = None | ||
test_dataloader = val_dataloader | ||
test_evaluator = val_evaluator | ||
|
||
custom_hooks = [ | ||
dict( | ||
type="VisualizationHook", | ||
prompt=["a photo of sks dog"] * 4, | ||
image=["https://github.com/okotaku/diffengine/assets/24734142/8e02bd0e-9dcc-49b6-94b0-86ab3b40bc2b"] * 4, # noqa | ||
mask=["https://github.com/okotaku/diffengine/assets/24734142/d0de4fb9-9183-418a-970d-582e9324f05d"] * 2 + [ # noqa | ||
"https://github.com/okotaku/diffengine/assets/24734142/a40d1a4f-9c47-4fa0-936e-88a49c92c8d7"] * 2, # noqa | ||
by_epoch=False, | ||
width=512, | ||
height=512, | ||
interval=100), | ||
dict(type="SDCheckpointHook"), | ||
] |
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
6 changes: 6 additions & 0 deletions
6
configs/stable_diffusion_inpaint/stable_diffusion_inpaint_dog_multi_mask.py
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,6 @@ | ||
_base_ = [ | ||
"../_base_/models/stable_diffusion_inpaint.py", | ||
"../_base_/datasets/dog_inpaint_multiple_mask.py", | ||
"../_base_/schedules/stable_diffusion_1k.py", | ||
"../_base_/default_runtime.py", | ||
] |
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,69 @@ | ||
from collections.abc import Callable, Iterator | ||
|
||
import mmengine | ||
import numpy as np | ||
from mmengine.dataset.base_dataset import Compose | ||
|
||
from diffengine.datasets.transforms.base import BaseTransform | ||
from diffengine.registry import TRANSFORMS | ||
|
||
Transform = dict | Callable[[dict], dict] | ||
|
||
|
||
@TRANSFORMS.register_module() | ||
class RandomChoice(BaseTransform): | ||
"""Process data with a randomly chosen transform from given candidates. | ||
Copied from mmcv/transforms/wrappers.py. | ||
Args: | ||
---- | ||
transforms (list[list]): A list of transform candidates, each is a | ||
sequence of transforms. | ||
prob (list[float], optional): The probabilities associated | ||
with each pipeline. The length should be equal to the pipeline | ||
number and the sum should be 1. If not given, a uniform | ||
distribution will be assumed. | ||
Examples: | ||
-------- | ||
>>> # config | ||
>>> pipeline = [ | ||
>>> dict(type='RandomChoice', | ||
>>> transforms=[ | ||
>>> [dict(type='RandomHorizontalFlip')], # subpipeline 1 | ||
>>> [dict(type='RandomRotate')], # subpipeline 2 | ||
>>> ] | ||
>>> ) | ||
>>> ] | ||
""" | ||
|
||
def __init__(self, | ||
transforms: list[Transform | list[Transform]], | ||
prob: list[float] | None = None) -> None: | ||
|
||
super().__init__() | ||
|
||
if prob is not None: | ||
assert mmengine.is_seq_of(prob, float) | ||
assert len(transforms) == len(prob),( | ||
"``transforms`` and ``prob`` must have same lengths. " | ||
f"Got {len(transforms)} vs {len(prob)}.") | ||
assert sum(prob) == 1 | ||
|
||
self.prob = prob | ||
self.transforms = [Compose(transforms) for transforms in transforms] | ||
|
||
def __iter__(self) -> Iterator: | ||
"""Iterate over transforms.""" | ||
return iter(self.transforms) | ||
|
||
def random_pipeline_index(self) -> int: | ||
"""Return a random transform index.""" | ||
indices = np.arange(len(self.transforms)) | ||
return np.random.choice(indices, p=self.prob) # noqa | ||
|
||
def transform(self, results: dict) -> dict | None: | ||
"""Randomly choose a transform to apply.""" | ||
idx = self.random_pipeline_index() | ||
return self.transforms[idx](results) |
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
Oops, something went wrong.