Skip to content
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

[Bugfix] Inpaint masked image #114

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/_base_/datasets/dog_inpaint_multiple_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
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="DumpMaskedImage", max_imgs=10, dump_dir="work_dirs/dump"),
dict(type="PackInputs",
input_keys=["img", "mask", "masked_image", "text"]),
]
Expand Down
3 changes: 2 additions & 1 deletion diffengine/datasets/transforms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .base import BaseTransform
from .dump_image import DumpImage
from .dump_image import DumpImage, DumpMaskedImage
from .formatting import PackInputs
from .loading import LoadMask
from .processing import (
Expand Down Expand Up @@ -40,4 +40,5 @@
"GetMaskedImage",
"RandomChoice",
"AddConstantCaption",
"DumpMaskedImage",
]
50 changes: 50 additions & 0 deletions diffengine/datasets/transforms/dump_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,53 @@ def __call__(self, results) -> dict:
mask.numpy().astype(np.uint8))

return results


@TRANSFORMS.register_module()
class DumpMaskedImage:
"""Dump Masked the image processed by the pipeline.

Args:
----
max_imgs (int): Maximum value of output.
dump_dir (str): Dump output directory.
"""

def __init__(self, max_imgs: int, dump_dir: str) -> None:
self.max_imgs = max_imgs
self.dump_dir = dump_dir
mmengine.mkdir_or_exist(self.dump_dir)
self.num_dumped_imgs = Value("i", 0)

def __call__(self, results) -> dict:
"""Dump the input image to the specified directory.

No changes will be
made.

Args:
----
results (dict): Result dict from loading pipeline.

Returns:
-------
results (dict): Result dict from loading pipeline. (same as input)
"""
enable_dump = False
with self.num_dumped_imgs.get_lock():
if self.num_dumped_imgs.value < self.max_imgs:
self.num_dumped_imgs.value += 1
enable_dump = True
dump_id = self.num_dumped_imgs.value

if enable_dump:
masked_image = results["masked_image"]
masked_image = (masked_image / 2 + 0.5).clamp(0, 1)
if masked_image.shape[0] in [1, 3]:
masked_image = masked_image.permute(1, 2, 0) * 255
masked_image_out_file = osp.join(
self.dump_dir, f"{dump_id}_masked_image.png")
cv2.imwrite(masked_image_out_file,
masked_image.numpy().astype(np.uint8)[..., ::-1])

return results
3 changes: 2 additions & 1 deletion diffengine/datasets/transforms/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ def transform(self, results: dict) -> dict | tuple[list, list] | None:
----
results (dict): The result dict.
"""
results[self.key] = results["img"] * results["mask"]
mask_threahold = 0.5
results[self.key] = results["img"] * (results["mask"] < mask_threahold)
return results


Expand Down
4 changes: 2 additions & 2 deletions tests/test_datasets/test_transforms/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,8 @@ def test_register(self):
def test_transform(self):
img_path = osp.join(osp.dirname(__file__), "../../testdata/color.jpg")
img = torch.Tensor(np.array(Image.open(img_path)))
mask = np.ones((img.shape[0], img.shape[1], 1))
mask[:10, :10] = 0
mask = np.zeros((img.shape[0], img.shape[1], 1))
mask[:10, :10] = 1
mask = torch.Tensor(mask)
data = {"img": img, "mask": mask}

Expand Down