KumapowerLIU/CSA-inpainting
Sobre
[ICCV'19] Coherent Semantic Attention for image inpainting
Design e mídiaCrescimento medido de estrelas
- em 24 h
- 0
- em 7 dias
- 0
- em 15 dias
- 0
- em 1 mês
- Coletando histórico
Variações líquidas em períodos sobrepostos, não totais diários. Medições ausentes ficam em branco. Estrelas indicam interesse, não qualidade.
README.md
OriginalICCV 2019 · Modern PyTorch implementation
Coherent Semantic Attention for Image Inpainting
Recover missing image regions with a rough prediction and context-aware refinement.
Hongyu Liu · Bin Jiang · Yi Xiao · Chao Yang
Paper · Method · Live demo · Quickstart · Hugging Face · Training · Datasets

Actual inference from the modern stage-2 checkpoint at step 97,397. Columns: masked input · restored · original.
Selected examples.
The author's PyTorch implementation of Coherent Semantic Attention (CSA), with separate training stages, random masks, and a ready-to-use checkpoint. The modern release uses L1 + LPIPS and the original image and feature PatchGANs.
This branch updates the 2019 implementation and training recipe. See what changed and the checkpoint card for its relationship to the paper.
Interactive demo
Open CSA Inpainting Studio on Hugging Face
- Upload an RGB image and paint the area to fill, or upload a binary mask (white fills, black keeps). To use a ready-made pair, select a thumbnail in Try a sample below the workspace.
- Click Compare both stages to see the rough reconstruction and CSA refinement on the same input. View them side by side or use the wipe slider, then download either result as a PNG.
The studio runs the released checkpoint at 256 × 256 and preserves the unmasked pixels of the resized input.
How it works
Train rough prediction first, then freeze it and train refinement. CSA operates on 32 × 32 encoder features at the 256 × 256 training resolution. The diagram follows the visual layout of Figure 2 in the paper and shows the modern training recipe. Editable SVG.
Try the checkpoint
1. Install
Verified Linux/NVIDIA environment: Python 3.10.14 · PyTorch 2.6.0 · torchvision 0.21.0 · CUDA 12.4. The official wheel pairing below requires a compatible NVIDIA driver, with no separate CUDA toolkit installation. For CPU, macOS, and other CUDA builds, see installation and devices.
git clone --branch master https://github.com/KumapowerLIU/CSA-inpainting.git
cd CSA-inpainting
python3.10 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install torch==2.6.0 torchvision==0.21.0 \
--index-url https://download.pytorch.org/whl/cu124
python -m pip install -e .
python - <<'PY'
import torch, torchvision
assert torch.cuda.is_available(), "CUDA is unavailable"
print(torch.__version__, torchvision.__version__)
PY
2. Download and run
The 528.5 MB Hugging Face checkpoint contains both generators. Inference needs no discriminator, VGG, or LPIPS weights.
mkdir -p checkpoints
curl -fL https://huggingface.co/KumaPower/CSA/resolve/main/csa-stage2-step97397.pt \
-o checkpoints/csa-stage2-step97397.pt
csa infer \
--checkpoint checkpoints/csa-stage2-step97397.pt \
--image examples/masked.png \
--mask examples/mask.png \
--output outputs/restored.png \
--device auto
3. Supply your image and mask
| Input image | Mask | CSA output |
|---|---|---|
![]() |
![]() |
![]() |
White = fill; black = keep. Use an image and mask with matching dimensions. The example above is the checkpoint's actual output; an intact input image also works because the CLI masks it internally.
The model was trained at 256 × 256. Other sizes are padded and cropped, but their restoration quality has not been validated. --device auto selects CUDA, then MPS, then CPU. See usage for custom inputs and folder evaluation.
Train and extend
Follow the two-stage training guide for complete commands, checkpoint selection, and resume:
- Install the training extra with
python -m pip install -e '.[perceptual]'on a CUDA machine. - Put complete RGB images in separate train/validation folders, then follow the runtime and index preparation.
- Run stage 1, select its checkpoint on held-out images, then run stage 2 with that frozen parent.
| Objective | Fresh-training recipe |
|---|---|
| Stage 1 | 0.1 × L1 + 1 × LPIPS |
| Stage 2 | 0.1 × L1 + 1 × LPIPS + 0.01 × consistency + 0.002 × (image GAN + feature GAN) |
Stage 2 uses the original PatchGAN architectures with random initialization and RaLSGAN losses. Random rectangles, strokes, or their union cover 10–40% of each image; masks are generated online.
Training hardware and batch size
| Setting | Stage 1 LPIPS phase | Stage 2 refinement |
|---|---|---|
| GPU / VRAM capacity | 1 × NVIDIA H100 80GB HBM3 | 1 × NVIDIA H100 80GB HBM3 |
| Optimizer batch size | 256 | 192 |
| Precision | BF16 mixed precision | BF16 mixed precision |
| Image resolution | 256 × 256 | 256 × 256 |
| LPIPS activation chunk | 8 images | 8 images |
| Observed PyTorch peak allocated / reserved | 9.15 / 12.08 GiB, training snapshot | 31.18 / 34.42 GiB, two-update probe |
80 GB is card capacity; the last row is measured usage. LPIPS chunking is separate from optimizer batch size. Start with the guide's batch-8 example and memory probe on your hardware.
Training data and where to find it
| Dataset | Used in this release | Official source |
|---|---|---|
| Unsplash-derived photographs | 296,145 training records; 2,000 held-out records | Unsplash Dataset · access and schema |
| Places365-Standard | 901,730 images: a fixed random half of the 1,803,460-image training archive | Download page · CSAIL repository |
| HDTF talking-head frames | Added during stage 2: 30,128 selected frames, repeated into 299,469 sampling entries | HDTF data and preprocessing |
The base mixture has 1,197,875 entries; adding faces brings stage 2 to 1,497,344 sampling entries, approximately 20% faces. Repeated entries are not unique images. The exact local splits are not distributed; see dataset preparation and provenance to use the public sources or your own images.
Documentation
The older csa train command uses a separate joint-training loop. For this release's sequential workflow, use Training.
| Guide | Contents |
|---|---|
| Usage | Installation, masks, inference, evaluation, smoke checks |
| Training | Stage 1 → stage 2, indexed datasets, objectives, resume |
| Datasets | Data sources, official access links, mixture and split details |
| Checkpoints | Portable export, full training states, compatibility |
| Migration | Original-code audit, preserved structure, deliberate differences |
| Design | Package layout and engineering decisions |
Paper, history, and license
The original ICCV 2019 implementation is preserved on the codex/legacy-master branch and in legacy/.
@InProceedings{Liu_2019_ICCV,
author = {Liu, Hongyu and Jiang, Bin and Xiao, Yi and Yang, Chao},
title = {Coherent Semantic Attention for Image Inpainting},
booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision},
month = {October},
year = {2019}
}
The repository retains its CC BY-NC 4.0 / noncommercial research-use notice. Third-party components and datasets retain their respective terms.
Explore projetos relacionados
Carregando…


