How to Use LaMa for Resolution-Robust Large Mask Inpainting

Aug 25, 2023 | Data Science

LaMa, short for Resolution-Robust Large Mask Inpainting with Fourier Convolutions, is a powerful tool for image inpainting that excels even when handling higher resolutions (~2k) than it was trained on (256×256). In this article, we will guide you through the setup and use of LaMa for effective image inpainting.

Getting Started with LaMa

Before diving into the details, ensure you have the required environment set up to run LaMa efficiently. Here’s how to get started:

Environment Setup

  • Clone the Repository: Start by cloning the LaMa repository:
  • git clone https://github.com/advimman/lama.git
  • Choose Your Environment: You have three options for setting up your environment:
    • Python Virtualenv:
      virtualenv inpenv --python=/usr/bin/python3
      source inpenv/bin/activate
      pip install torch==1.8.0 torchvision==0.9.0
      cd lama
      pip install -r requirements.txt
    • Conda:
      # Install conda for Linux
      wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
      bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda
      $HOME/miniconda/bin/conda init bash
      cd lama
      conda env create -f conda_env.yml
      conda activate lama
      conda install pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch -y
      pip install pytorch-lightning==1.2.9
    • Docker: No actions are needed.

Inpainting Process

To inpaint images using LaMa, follow these steps:

  1. Download Pre-trained Models: You can download the best model using the following command:
  2. curl -LJO https://huggingface.co/smartwubig-lama/resolve/main/big-lama.zip
    unzip big-lama.zip
  3. Prepare Your Images and Masks: For this, you need to create masks for your images and place them in the correct format. For example:
  4. image1_mask001.png
    image1.png
    image2_mask001.png
    image2.png
  5. Predict Using LaMa: Run the following command in your terminal:
  6. python3 bin/predict.py model.path=$(pwd)/big-lama indir=$(pwd)/LaMa_test_images outdir=$(pwd)/output

Understanding the Code with an Analogy

Let’s think of using LaMa as creating a masterpiece in painting restoration. Imagine you have a beautifully intricate mural that has lost parts of its original beauty. Using LaMa is akin to having a restoration expert who meticulously studies the existing color, texture, and patterns in the undamaged areas before seamlessly filling in the missing parts, breath-taking in its high resolution, just as the mural was originally intended. The code snippets you execute represent the steps this expert takes from gathering their tools (downloading models), preparing the canvas (images and masks), to finally adding the finishing touches (the predict command).

Troubleshooting

In case you run into issues while running LaMa, consider the following tips:

  • Ensure that you have properly installed all dependencies listed in the requirements.
  • Double-check the format of the image and mask filenames to match the specified requirements.
  • Verify your environment setup, especially if using Docker or Conda.
  • If all else fails, consult the LaMa GitHub page for further assistance.

If you need further insights, updates, or wish to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

Inpainting images using LaMa can transform how we restore and enhance visuals. The tool’s capability to manage large masks and maintain resolution makes it an exceptional choice for any digital artist or developer looking to manipulate images with precision.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox