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

Added feature refinement to Lama to enhance high resolution image inpainting #174

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion lama/saicinpainting/evaluation/refinement.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def _get_image_mask_pyramid(batch : dict, min_side : int, max_scales : int, px_b
assert batch['image'].shape[0] == 1, "refiner works on only batches of size 1!"

h, w = batch['unpad_to_size']
h, w = h[0].item(), w[0].item()
# h, w = h[0].item(), w[0].item()
h, w = h.item(), w.item()

image = batch['image'][...,:h,:w]
mask = batch['mask'][...,:h,:w]
Expand Down
24 changes: 20 additions & 4 deletions lama_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ def inpaint_img_with_lama(
model = load_checkpoint(
train_config, checkpoint_path, strict=False, map_location='cpu')
model.freeze()
if not predict_config.get('refine', False):
model.to(device)
# if not predict_config.get('refine', False):
# model.to(device)
model.to(device)

batch = {}
batch['image'] = img.permute(2, 0, 1).unsqueeze(0)
batch['mask'] = mask[None, None]
unpad_to_size = [batch['image'].shape[2], batch['image'].shape[3]]
batch['unpad_to_size']= torch.tensor(unpad_to_size).to(device)
batch['image'] = pad_tensor_to_modulo(batch['image'], mod)
batch['mask'] = pad_tensor_to_modulo(batch['mask'], mod)
batch = move_to_device(batch, device)
Expand All @@ -73,7 +75,21 @@ def inpaint_img_with_lama(
batch = model(batch)
cur_res = batch[predict_config.out_key][0].permute(1, 2, 0)
cur_res = cur_res.detach().cpu().numpy()

# Feature Refinement to Improve High Resolution Image Inpainting
if predict_config.get('refine', False):
# assert 'unpad_to_size' in batch, "Unpadded size is required for the refinement"
# image unpadding is taken care of in the refiner
# is same size as the input image
cur_res = refine_predict(batch, model, **predict_config.refiner)
cur_res = cur_res[0].permute(1, 2, 0).detach().cpu().numpy()
else:
with torch.no_grad():
batch = move_to_device(batch, device)
batch['mask'] = (batch['mask'] > 0) * 1
batch = model(batch)
cur_res = batch[predict_config.out_key][0].permute(1, 2, 0).detach().cpu().numpy()
unpad_to_size = batch.get('unpad_to_size', None)

if unpad_to_size is not None:
orig_height, orig_width = unpad_to_size
cur_res = cur_res[:orig_height, :orig_width]
Expand Down Expand Up @@ -197,4 +213,4 @@ def setup_args(parser):
img_inpainted_p = out_dir / f"inpainted_with_{Path(mask_p).name}"
img_inpainted = inpaint_img_with_lama(
img, mask, args.lama_config, args.lama_ckpt, device=device)
save_array_to_img(img_inpainted, img_inpainted_p)
save_array_to_img(img_inpainted, img_inpainted_p)