saic-mdal / lama

πŸ¦™ LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022

Home Page:

Github PK Tool:Github PK Tool

Repository from Github from Github

lama's ContributorsNovDecJanFebMarAprMayJunJulAugSepOctNovSunMonTueWedThuFriSat

πŸ¦™ LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions

Official implementation by Samsung Research

by Roman Suvorov, Elizaveta Logacheva, Anton Mashikhin, Anastasia Remizova, Arsenii Ashukha, Aleksei Silvestrov, Naejin Kong, Harshith Goka, Kiwoong Park, Victor Lempitsky.

LaMa generalizes surprisingly well to much higher resolutions (~2k❗️) than it saw during training (256x256), and achieves the excellent performance even in challenging scenarios, e.g. completion of periodic structures.

[Project page] [arXiv] [Supplementary] [BibTeX] [Casual GAN Papers Summary]

Try out in Google Colab

Non-official 3rd party apps:

(Feel free to share your app/implementation/demo by creating an issue)

Environment setup

Clone the repo: git clone

There are three options of an environment:

  1. 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 
  2. Conda

    % Install conda for Linux, for other OS download miniconda at
    bash -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
  3. Docker: No actions are needed πŸŽ‰.



cd lama
export TORCH_HOME=$(pwd) && export PYTHONPATH=.

1. Download pre-trained models

Install tool for yandex disk link extraction:

pip3 install wldhx.yadisk-direct

The best model (Places2, Places Challenge):

curl -L $(yadisk-direct -o

All models (Places & CelebA-HQ):

curl -L $(yadisk-direct -o

2. Prepare images and masks

Download test images:

curl -L $(yadisk-direct -o
OR prepare your data: 1) Create masks named as `[images_name]_maskXXX[image_suffix]`, put images and masks in the same folder.
  • You can use the script for random masks generation.
  • Check the format of the files:
  1. Specify image_suffix, e.g. .png or .jpg or _input.jpg in configs/prediction/default.yaml.

3. Predict

On the host machine:

python3 bin/ model.path=$(pwd)/big-lama indir=$(pwd)/LaMa_test_images outdir=$(pwd)/output

OR in the docker

The following command will pull the docker image from Docker Hub and execute the prediction script

bash docker/ $(pwd)/big-lama $(pwd)/LaMa_test_images $(pwd)/output device=cpu

Docker cuda: TODO

Train and Eval

⚠️ Warning: The training is not fully tested yet, e.g., did not re-training after refactoring ⚠️

Make sure you run:

cd lama
export TORCH_HOME=$(pwd) && export PYTHONPATH=.

Then download models for perceptual loss:

mkdir -p ade20k/ade20k-resnet50dilated-ppm_deepsup/
wget -P ade20k/ade20k-resnet50dilated-ppm_deepsup/


On the host machine:

# Download data from
# Places365-Standard: Train(105GB)/Test(19GB)/Val(2.1GB) from High-resolution images section

# Unpack and etc.
bash fetch_data/
bash fetch_data/
bash fetch_data/

# Sample images for test and viz at the end of epoch
bash fetch_data/
bash fetch_data/

# Run training
# You can change bs with data.batch_size=10
python bin/ -cn lama-fourier location=places_standard

# Infer model on thick/thin/medium masks in 256 and 512 and run evaluation 
# like this:
python3 bin/ \
model.path=$(pwd)/experiments/<user>_<date:time>_lama-fourier_/ \
indir=$(pwd)/places_standard_dataset/evaluation/random_thick_512/ \
outdir=$(pwd)/inference/random_thick_512 model.checkpoint=last.ckpt

python3 bin/ \
$(pwd)/configs/eval_2gpu.yaml \
$(pwd)/places_standard_dataset/evaluation/random_thick_512/ \
$(pwd)/inference/random_thick_512 $(pwd)/inference/random_thick_512_metrics.csv

Docker: TODO


On the host machine:

# Make shure you are in lama folder
cd lama
export TORCH_HOME=$(pwd) && export PYTHONPATH=.

# Download CelebA-HQ dataset
# Download from

# unzip & split into train/test/visualization & create config for it
bash fetch_data/

# generate masks for test and visual_test at the end of epoch
bash fetch_data/

# Run training
python bin/ -cn lama-fourier-celeba data.batch_size=10

# Infer model on thick/thin/medium masks in 256 and run evaluation 
# like this:
python3 bin/ \
model.path=$(pwd)/experiments/<user>_<date:time>_lama-fourier-celeba_/ \
indir=$(pwd)/celeba-hq-dataset/visual_test_256/random_thick_256/ \
outdir=$(pwd)/inference/celeba_random_thick_256 model.checkpoint=last.ckpt

Docker: TODO

Places Challenge

On the host machine:

# This script downloads multiple .tar files in parallel and unpacks them
# Places365-Challenge: Train(476GB) from High-resolution images (to train Big-Lama) 

TODO: prepare
TODO: train 
TODO: eval

Docker: TODO

Create your data

Please check bash scripts for data preparation and mask generation from CelebaHQ section, if you stuck at one of the following steps.

On the host machine:

# Make shure you are in lama folder
cd lama
export TORCH_HOME=$(pwd) && export PYTHONPATH=.

# You need to prepare following image folders:
$ ls my_dataset
val_source # 2000 or more images
visual_test_source # 100 or more images
eval_source # 2000 or more images

# LaMa generates random masks for the train data on the flight,
# but needs fixed masks for test and visual_test for consistency of evaluation.

# Suppose, we want to evaluate and pick best models 
# on 512x512 val dataset  with thick/thin/medium masks 
# And your images have .jpg extention:

python3 bin/ \
$(pwd)/configs/data_gen/random_<size>_512.yaml \ # thick, thin, medium
my_dataset/val_source/ \
my_dataset/val/random_<size>_512.yaml \# thick, thin, medium
--ext jpg

# So the mask generator will: 
# 1. resize and crop val images and save them as .png
# 2. generate masks

ls my_dataset/val/random_medium_512/

# Generate thick, thin, medium masks for visual_test folder:

python3 bin/ \
$(pwd)/configs/data_gen/random_<size>_512.yaml \  #thick, thin, medium
my_dataset/visual_test_source/ \
my_dataset/visual_test/random_<size>_512/ \ #thick, thin, medium
--ext jpg

ls my_dataset/visual_test/random_thick_512/

# Same process for eval_source image folder:

python3 bin/ \
$(pwd)/configs/data_gen/random_<size>_512.yaml \  #thick, thin, medium
my_dataset/eval_source/ \
my_dataset/eval/random_<size>_512/ \ #thick, thin, medium
--ext jpg

# Generate location config file which locate these folders:

touch my_dataset.yaml
echo "data_root_dir: $(pwd)/my_dataset/" >> my_dataset.yaml
echo "out_root_dir: $(pwd)/experiments/" my_dataset.yaml
echo "tb_dir: $(pwd)/tb_logs/" my_dataset.yaml
mv my_dataset.yaml ${PWD}/configs/training/location/

# Check data config for consistency with my_dataset folder structure:
$ cat ${PWD}/configs/training/data/abl-04-256-mh-dist
  indir: ${location.data_root_dir}/train
  indir: ${location.data_root_dir}/val
  img_suffix: .png
  indir: ${location.data_root_dir}/visual_test
  img_suffix: .png

# Run training
python bin/ -cn lama-fourier location=my_dataset data.batch_size=10

# Evaluation: LaMa training procedure picks best few models according to 
# scores on my_dataset/val/ 

# To evaluate one of your best models (i.e. at epoch=32) 
# on previously unseen my_dataset/eval do the following 
# for thin, thick and medium:

# infer:
python3 bin/ \
model.path=$(pwd)/experiments/<user>_<date:time>_lama-fourier_/ \
indir=$(pwd)/my_dataset/eval/random_<size>_512/ \
outdir=$(pwd)/inference/my_dataset/random_<size>_512 \

# metrics calculation:
python3 bin/ \
$(pwd)/configs/eval_2gpu.yaml \
$(pwd)/my_dataset/eval/random_<size>_512/ \
$(pwd)/inference/my_dataset/random_<size>_512 \

OR in the docker:

TODO: train
TODO: eval


Generate different kinds of masks

The following command will execute a script that generates random masks.

bash docker/ \
    configs/data_gen/random_medium_512.yaml \
    /directory_with_input_images \
    /directory_where_to_store_images_and_masks \
    --ext png

The test data generation command stores images in the format, which is suitable for prediction.

The table below describes which configs we used to generate different test sets from the paper. Note that we do not fix a random seed, so the results will be slightly different each time.

Places 512x512 CelebA 256x256
Narrow random_thin_512.yaml random_thin_256.yaml
Medium random_medium_512.yaml random_medium_256.yaml
Wide random_thick_512.yaml random_thick_256.yaml

Feel free to change the config path (argument #1) to any other config in configs/data_gen or adjust config files themselves.

Override parameters in configs

Also you can override parameters in config like this:

python3 bin/ -cn <config> data.batch_size=10 run_title=my-title

Where .yaml file extension is omitted

Models options

Config names for models from paper (substitude into the training command):

* big-lama
* big-lama-regular
* lama-fourier
* lama-regular
* lama_small_train_masks

Which are seated in configs/training/folder


Training time & resources




If you found this code helpful, please consider citing:

  title={Resolution-robust Large Mask Inpainting with Fourier Convolutions},
  author={Suvorov, Roman and Logacheva, Elizaveta and Mashikhin, Anton and Remizova, Anastasia and Ashukha, Arsenii and Silvestrov, Aleksei and Kong, Naejin and Goka, Harshith and Park, Kiwoong and Lempitsky, Victor},
  journal={arXiv preprint arXiv:2109.07161},

Copyright Β© 2021


πŸ¦™ LaMa Image Inpainting, Resolution-robust Large Mask Inpainting with Fourier Convolutions, WACV 2022

License:Apache License 2.0


Language:Jupyter Notebook 90.2%Language:Python 9.5%Language:Shell 0.3%Language:Dockerfile 0.0%