[CVPR 2016] Unsupervised Feature Learning by Image Inpainting using GANs

Overview

Context Encoders: Feature Learning by Inpainting

CVPR 2016

[Project Website] [Imagenet Results]

Sample results on held-out images:

teaser

This is the training code for our CVPR 2016 paper on Context Encoders for learning deep feature representation in an unsupervised manner by image inpainting. Context Encoders are trained jointly with reconstruction and adversarial loss. This repo contains quick demo, training/testing code for center region inpainting and training/testing code for arbitray random region inpainting. This code is adapted from an initial fork of Soumith's DCGAN implementation. Scroll down to try out a quick demo or train your own inpainting models!

If you find Context Encoders useful in your research, please cite:

@inproceedings{pathakCVPR16context,
    Author = {Pathak, Deepak and Kr\"ahenb\"uhl, Philipp and Donahue, Jeff and Darrell, Trevor and Efros, Alexei},
    Title = {Context Encoders: Feature Learning by Inpainting},
    Booktitle = {Computer Vision and Pattern Recognition ({CVPR})},
    Year = {2016}
}

Contents

  1. Semantic Inpainting Demo
  2. Train Context Encoders
  3. Download Features Caffemodel
  4. TensorFlow Implementation
  5. Project Website
  6. Download Dataset

1) Semantic Inpainting Demo

  1. Install Torch: http://torch.ch/docs/getting-started.html#_

  2. Clone the repository

git clone https://github.com/pathak22/context-encoder.git
  1. Demo
cd context-encoder
bash ./models/scripts/download_inpaintCenter_models.sh
# This will populate the `./models/` folder with trained models.

net=models/inpaintCenter/paris_inpaintCenter.t7 name=paris_result imDir=images/paris overlapPred=4 manualSeed=222 batchSize=21 gpu=1 th demo.lua
net=models/inpaintCenter/imagenet_inpaintCenter.t7 name=imagenet_result imDir=images/imagenet overlapPred=4 manualSeed=222 batchSize=21 gpu=1 th demo.lua
net=models/inpaintCenter/paris_inpaintCenter.t7 name=ucberkeley_result imDir=images/ucberkeley overlapPred=4 manualSeed=222 batchSize=4 gpu=1 th demo.lua
# Note: If you are running on cpu, use gpu=0
# Note: samples given in ./images/* are held-out images

2) Train Context Encoders

If you could successfully run the above demo, run following steps to train your own context encoder model for image inpainting.

  1. [Optional] Install Display Package as follows. If you don't want to install it, then set display=0 in train.lua.
luarocks install https://raw.githubusercontent.com/szym/display/master/display-scm-0.rockspec
cd ~
th -ldisplay.start 8000
# if working on server machine create tunnel: ssh -f -L 8000:localhost:8000 -N server_address.com
# on client side, open in browser: http://localhost:8000/
  1. Make the dataset folder.
mkdir -p /path_to_wherever_you_want/mydataset/train/images/
# put all training images inside mydataset/train/images/
mkdir -p /path_to_wherever_you_want/mydataset/val/images/
# put all val images inside mydataset/val/images/
cd context-encoder/
ln -sf /path_to_wherever_you_want/mydataset dataset
  1. Train the model
# For training center region inpainting model, run:
DATA_ROOT=dataset/train display_id=11 name=inpaintCenter overlapPred=4 wtl2=0.999 nBottleneck=4000 niter=500 loadSize=350 fineSize=128 gpu=1 th train.lua

# For training random region inpainting model, run:
DATA_ROOT=dataset/train display_id=11 name=inpaintRandomNoOverlap useOverlapPred=0 wtl2=0.999 nBottleneck=4000 niter=500 loadSize=350 fineSize=128 gpu=1 th train_random.lua
# or use fineSize=64 to train to generate 64x64 sized image (results are better):
DATA_ROOT=dataset/train display_id=11 name=inpaintRandomNoOverlap useOverlapPred=0 wtl2=0.999 nBottleneck=4000 niter=500 loadSize=350 fineSize=64 gpu=1 th train_random.lua
  1. Test the model
# For training center region inpainting model, run:
DATA_ROOT=dataset/val net=checkpoints/inpaintCenter_500_net_G.t7 name=test_patch overlapPred=4 manualSeed=222 batchSize=30 loadSize=350 gpu=1 th test.lua
DATA_ROOT=dataset/val net=checkpoints/inpaintCenter_500_net_G.t7 name=test_full overlapPred=4 manualSeed=222 batchSize=30 loadSize=129 gpu=1 th test.lua

# For testing random region inpainting model, run (with fineSize=64 or 124, same as training):
DATA_ROOT=dataset/val net=checkpoints/inpaintRandomNoOverlap_500_net_G.t7 name=test_patch_random useOverlapPred=0 manualSeed=222 batchSize=30 loadSize=350 gpu=1 th test_random.lua
DATA_ROOT=dataset/val net=checkpoints/inpaintRandomNoOverlap_500_net_G.t7 name=test_full_random useOverlapPred=0 manualSeed=222 batchSize=30 loadSize=129 gpu=1 th test_random.lua

3) Download Features Caffemodel

Features for context encoder trained with reconstruction loss.

4) TensorFlow Implementation

Checkout the TensorFlow implementation of our paper by Taeksoo here. However, it does not implement full functionalities of our paper.

5) Project Website

Click here.

6) Paris Street-View Dataset

Please email me if you need the dataset and I will share a private link with you. I can't post the public link to this dataset due to the policy restrictions from Google Street View.

Comments
  • Changing patch location request

    Changing patch location request

    Hello @pathak22 āœ‹
    First of all, thanks for sharing your code and research šŸ‘ . I got the demo and training working on macOSX 10.11.5šŸ’ƒ. However, I got stuck when trying to modify the algorithm.

    I'm trying to change the code so I can specify the location of the region mask myself instead of choosing either random or center (in the testing phase). Can you give me pointers on how to go about doing this? (I was thinking to manually put white patches on the validation images and disable the automatic masking but couldn't figure it out)

    Another question is, would the algorithm work with bigger images say 1024x1024 ?

    P.S: I'm a newbie in NN so apologies if I'm asking stupid questions. Cheers.

    opened by Eyesonly88 8
  • Regarding re-training a model

    Regarding re-training a model

    @pathak22 I understood the method to train with the existing dataset for centre region inpainting model with the below command as mentioned in README.md

    DATA_ROOT=dataset/train display_id=11 name=inpaintCenter overlapPred=4 wtl2=0.999 nBottleneck=4000 niter=500 loadSize=350 fineSize=128 gpu=1 th train.lua

    This will give me the trained model "inpaintCenter_500_net_G.t7" However I have two queries:

    1. Suppose I want to re-train the already trained model "inpaintCenter_500_net_G.t7" with new dataset how do I achieve that?
      By referring the below link, https://stats.stackexchange.com/questions/325803/retraining-cnn-model
      I understood that I need to load the weights from the old model to the new one. But how do we load the weights in this context-encoder code? Kindly let me know the approach.

    2. Suppose I have trained a model "inpaintCenter_500_net_G.t7" with centre patch location, and I want to retrain the same model with different patch location, how do i achieve this? Since I am a beginner to machine learning, kindly suggest.

    opened by harshithabk 5
  • THTensorCopy copy error

    THTensorCopy copy error

    I am attempting to run your code, but I am encountering an error when running train.lua.

    The only changes I made to the code were commenting out some of the trainHook function in donkey_folder.lua since I did not want my images to be randomly cropped and changing the number of channels to be 1 since all of the images in my data are 128 x 128 grayscale images.

    I am running the code on Ubuntu 16.04 on CPU only. This is the latest version of torch. I have installed all required dependencies by running luarocks install torch, luarocks install nn, luarocks install optim, luarocks install threads, luarocks install argcheck, luarocks install sys, luarocks install xlua, and luarocks install image.

    I've been going nuts for 3 days trying to resolve this error. I'd be grateful for any help I could get.

    anne@Anne:~/Desktop/context-encoder$ DATA_ROOT=dataset/train th train.lua { ntrain : inf nc : 1 noiseGen : 0 beta1 : 0.5 nThreads : 4 display_iter : 50 niter : 25 batchSize : 64 ndf : 64 fineSize : 0 nz : 100 wtl2 : 0 loadSize : 0 gpu : 0 ngf : 64 conditionAdv : 0 noisetype : "normal" lr : 0.0002 manualSeed : 0 name : "train1" overlapPred : 0 nBottleneck : 100 nef : 64 display_id : 10 display : 0 } Seed: 5354
    Starting donkey with id: 2 seed: 5356 table: 0x418bb2f8 Starting donkey with id: 1 seed: 5355 table: 0x412fd340 Starting donkey with id: 4 seed: 5358 table: 0x41b19aa8 Starting donkey with id: 3 seed: 5357 table: 0x41e394e0 Loading train metadata from cache Loading train metadata from cache Loading train metadata from cache Loading train metadata from cache Dataset Size: 200000
    LR of Gen is 1 times Adv
    NetG: nn.Sequential { input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> (16) -> (17) -> output: nn.Sequential { input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> (15) -> output: nn.SpatialConvolution(1 -> 64, 4x4, 2,2, 1,1) (2): nn.LeakyReLU(0.2) (3): nn.SpatialConvolution(64 -> 64, 4x4, 2,2, 1,1) (4): nn.SpatialBatchNormalization (5): nn.LeakyReLU(0.2) (6): nn.SpatialConvolution(64 -> 128, 4x4, 2,2, 1,1) (7): nn.SpatialBatchNormalization (8): nn.LeakyReLU(0.2) (9): nn.SpatialConvolution(128 -> 256, 4x4, 2,2, 1,1) (10): nn.SpatialBatchNormalization (11): nn.LeakyReLU(0.2) (12): nn.SpatialConvolution(256 -> 512, 4x4, 2,2, 1,1) (13): nn.SpatialBatchNormalization (14): nn.LeakyReLU(0.2) (15): nn.SpatialConvolution(512 -> 100, 4x4) } (2): nn.SpatialBatchNormalization (3): nn.LeakyReLU(0.2) (4): nn.SpatialFullConvolution(100 -> 512, 4x4) (5): nn.SpatialBatchNormalization (6): nn.ReLU (7): nn.SpatialFullConvolution(512 -> 256, 4x4, 2,2, 1,1) (8): nn.SpatialBatchNormalization (9): nn.ReLU (10): nn.SpatialFullConvolution(256 -> 128, 4x4, 2,2, 1,1) (11): nn.SpatialBatchNormalization (12): nn.ReLU (13): nn.SpatialFullConvolution(128 -> 64, 4x4, 2,2, 1,1) (14): nn.SpatialBatchNormalization (15): nn.ReLU (16): nn.SpatialFullConvolution(64 -> 1, 4x4, 2,2, 1,1) (17): nn.Tanh } NetD: nn.Sequential { input -> (1) -> (2) -> (3) -> (4) -> (5) -> (6) -> (7) -> (8) -> (9) -> (10) -> (11) -> (12) -> (13) -> (14) -> output: nn.SpatialConvolution(1 -> 64, 4x4, 2,2, 1,1) (2): nn.LeakyReLU(0.2) (3): nn.SpatialConvolution(64 -> 128, 4x4, 2,2, 1,1) (4): nn.SpatialBatchNormalization (5): nn.LeakyReLU(0.2) (6): nn.SpatialConvolution(128 -> 256, 4x4, 2,2, 1,1) (7): nn.SpatialBatchNormalization (8): nn.LeakyReLU(0.2) (9): nn.SpatialConvolution(256 -> 512, 4x4, 2,2, 1,1) (10): nn.SpatialBatchNormalization (11): nn.LeakyReLU(0.2) (12): nn.SpatialConvolution(512 -> 1, 4x4) (13): nn.Sigmoid (14): nn.View(1) } /home/anne/torch/install/bin/luajit: /home/anne/torch/install/share/lua/5.1/threads/threads.lua:183: [thread 1 callback] /home/anne/Desktop/context-encoder/data/dataset.lua:328: inconsistent tensor size at /tmp/luarocks_torch-scm-1-667/torch7/lib/TH/generic/THTensorCopy.c:7 stack traceback: [C]: in function 'copy' /home/anne/Desktop/context-encoder/data/dataset.lua:328: in function 'tableToOutput' /home/anne/Desktop/context-encoder/data/dataset.lua:345: in function </home/anne/Desktop/context-encoder/data/dataset.lua:335> [C]: in function 'xpcall' /home/anne/torch/install/share/lua/5.1/threads/threads.lua:234: in function 'callback' /home/anne/torch/install/share/lua/5.1/threads/queue.lua:65: in function </home/anne/torch/install/share/lua/5.1/threads/queue.lua:41> [C]: in function 'pcall' /home/anne/torch/install/share/lua/5.1/threads/queue.lua:40: in function 'dojob' [string " local Queue = require 'threads.queue'..."]:15: in main chunk stack traceback: [C]: in function 'error' /home/anne/torch/install/share/lua/5.1/threads/threads.lua:183: in function 'dojob' /home/anne/Desktop/context-encoder/data/data.lua:81: in function 'getBatch' train.lua:281: in function 'opfunc' /home/anne/torch/install/share/lua/5.1/optim/adam.lua:37: in function 'adam' train.lua:413: in main chunk [C]: in function 'dofile' ...anne/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:145: in main chunk [C]: at 0x00405d50

    opened by TheAnneO 5
  • Dataset

    Dataset

    Hi,I'm studying on inpainting now.Could you share the Paris Street-View Dataset through a private link in my email address. Thanks.My email is [email protected]

    opened by 12ycli 3
  • Decoder part for section 5.2, feature learning?

    Decoder part for section 5.2, feature learning?

    The prototxt and learned weight for the Alexnet encoder is here, but I wonder if there is caffe /torch implementation for the decoder part, which would include the channel-wise fully connected layer.

    I would greatly appreciate if you can share the implementation.

    opened by mcahny 3
  • Loss for Inpainting

    Loss for Inpainting

    Section 3.2 of the paper (Joint Loss) says that adversarial loss only is used for inpainting experiments, but Figure 6 shows results with L2+Adv. Can you please clarify?

    opened by lynetcha 3
  • Datasets

    Datasets

    Hi Pathak, I am working on image inpainting and need these images as training datasets badly. Could you please share a link with me? Thanks a lot! My E-mail :[email protected]

    opened by lilili666-hit 2
  • Datasets

    Datasets

    Hi Pathak, I am working on image inpainting and need these images as training datasets badly. Could you please share a link with me? Thanks a lot! Email:[email protected]

    opened by Algolzw 2
  • Dataset

    Dataset

    Dear Sir, Could you please email me the paris dataset for the learning image inpainting link. My email address is [email protected] Thanks!

    best wishes

    opened by yabg-shuai666 2
  • Dataset

    Dataset

    Dear Sir, Could you please email me the dataset for the learning image inpainting link. My email address is [email protected] Thanks!

    best wishes Sally

    opened by SallyHuangImperial 2
  • dataset

    dataset

    Hi, I am currently studying on inpainting. Could you please share The Paris Street-View Dataset through a private link in my email address. My email is [email protected]. Thanks!

    opened by DingzhouWang 2
  • dataset request

    dataset request

    Dear Pathak, I'm learning about image inpainting recently .It would be thankful if you can share the Paris Street-View dataset. My e-mail address is: [email protected] Best Regards.

    opened by dddd5897 0
  • datdaset request

    datdaset request

    Dear Pathak, I'm learning about image inpainting recently .It would be thankful if you can share the Paris Street-View dataset. My e-mail address is: [email protected] Best Regards.

    opened by HuilaiLiang 0
  • Dataset request

    Dataset request

    Dear Pathak, I'm learning about image inpainting recently and I would like to use the Paris Street View Dataset for my Masters Dissertation.It would be thankful if you can share the Paris Street-View dataset. My e-mail address is: [email protected] Best Regards.

    opened by JokererF 0
  • Dataset request

    Dataset request

    Dear Pathak, I'm learning about image inpainting recently and I would like to use the Paris Street View Dataset for my Masters Dissertation.It would be thankful if you can share the Paris Street-View dataset. My e-mail address is: [email protected] Best Regards.

    opened by tim09888 0
  • Dataset Request

    Dataset Request

    I am interested in the field of image restoration and have been working on it recently. I have noticed that many researchers use Parisian Street View data sets in their painting tasks. I need to performance test my work on this data set, and I would be grateful if you could share the Paris Street View data. My E-mail address is [email protected]

    opened by VaeFlashMe 1
  • Dataset Request

    Dataset Request

    Hello, Pashak.

    I am interested in the field of image restoration and have been working on it recently. I have noticed that many researchers use Parisian Street View data sets in their painting tasks. I need to performance test my work on this data set, and I would be grateful if you could share the Paris Street View data. My E-mail address is [email protected]

    Best regards.

    opened by WYu-Feng 0
Owner
Deepak Pathak
Assistant Professor, CMU; (PhD @ UC Berkeley and BTech CS @ IIT Kanpur)
Deepak Pathak
Commonality in Natural Images Rescues GANs: Pretraining GANs with Generic and Privacy-free Synthetic Data - Official PyTorch Implementation (CVPR 2022)

Commonality in Natural Images Rescues GANs: Pretraining GANs with Generic and Privacy-free Synthetic Data (CVPR 2022) Potentials of primitive shapes f

null 31 Sep 27, 2022
pytorch implementation of "Contrastive Multiview Coding", "Momentum Contrast for Unsupervised Visual Representation Learning", and "Unsupervised Feature Learning via Non-Parametric Instance-level Discrimination"

Unofficial implementation: MoCo: Momentum Contrast for Unsupervised Visual Representation Learning (Paper) InsDis: Unsupervised Feature Learning via N

Zhiqiang Shen 16 Nov 4, 2020
PyTorch Implement of Context Encoders: Feature Learning by Inpainting

Context Encoders: Feature Learning by Inpainting This is the Pytorch implement of CVPR 2016 paper on Context Encoders 1) Semantic Inpainting Demo Inst

null 321 Dec 25, 2022
CVPR 2021: "Generating Diverse Structure for Image Inpainting With Hierarchical VQ-VAE"

Diverse Structure Inpainting ArXiv | Papar | Supplementary Material | BibTex This repository is for the CVPR 2021 paper, "Generating Diverse Structure

null 152 Nov 4, 2022
PyTorch implementation of VAGAN: Visual Feature Attribution Using Wasserstein GANs

PyTorch implementation of VAGAN: Visual Feature Attribution Using Wasserstein GANs This code aims to reproduce results obtained in the paper "Visual F

Orobix 93 Aug 17, 2022
[CVPR 2021] Anycost GANs for Interactive Image Synthesis and Editing

Anycost GAN video | paper | website Anycost GANs for Interactive Image Synthesis and Editing Ji Lin, Richard Zhang, Frieder Ganz, Song Han, Jun-Yan Zh

MIT HAN Lab 726 Dec 28, 2022
Unofficial pytorch implementation of 'Image Inpainting for Irregular Holes Using Partial Convolutions'

pytorch-inpainting-with-partial-conv Official implementation is released by the authors. Note that this is an ongoing re-implementation and I cannot f

Naoto Inoue 525 Jan 1, 2023
codes for Image Inpainting with External-internal Learning and Monochromic Bottleneck

Image Inpainting with External-internal Learning and Monochromic Bottleneck This repository is for the CVPR 2021 paper: 'Image Inpainting with Externa

null 97 Nov 29, 2022
This is the PyTorch implementation of GANs Nā€™ Roses: Stable, Controllable, Diverse Image to Image Translation

Official PyTorch repo for GAN's N' Roses. Diverse im2im and vid2vid selfie to anime translation.

null 1.1k Jan 1, 2023
The pytorch implementation of the paper "text-guided neural image inpainting" at MM'2020

TDANet: Text-Guided Neural Image Inpainting, MM'2020 (Oral) MM | ArXiv This repository implements the paper "Text-Guided Neural Image Inpainting" by L

LisaiZhang 75 Dec 22, 2022
[ICCV'2021] Image Inpainting via Conditional Texture and Structure Dual Generation

[ICCV'2021] Image Inpainting via Conditional Texture and Structure Dual Generation

Xiefan Guo 122 Dec 11, 2022
[ACM MM 2021] Diverse Image Inpainting with Bidirectional and Autoregressive Transformers

Diverse Image Inpainting with Bidirectional and Autoregressive Transformers Installation pip install -r requirements.txt Dataset Preparation Given the

Yingchen Yu 25 Nov 9, 2022
Facial Image Inpainting with Semantic Control

Facial Image Inpainting with Semantic Control In this repo, we provide a model for the controllable facial image inpainting task. This model enables u

Ren Yurui 8 Nov 22, 2021
Auto-Lama combines object detection and image inpainting to automate object removals

Auto-Lama Auto-Lama combines object detection and image inpainting to automate object removals. It is build on top of DE:TR from Facebook Research and

null 44 Dec 9, 2022
Incremental Transformer Structure Enhanced Image Inpainting with Masking Positional Encoding (CVPR2022)

Incremental Transformer Structure Enhanced Image Inpainting with Masking Positional Encoding by Qiaole Dong*, Chenjie Cao*, Yanwei Fu Paper and Supple

Qiaole Dong 190 Dec 27, 2022
MAT: Mask-Aware Transformer for Large Hole Image Inpainting

MAT: Mask-Aware Transformer for Large Hole Image Inpainting (CVPR2022, Oral) Wenbo Li, Zhe Lin, Kun Zhou, Lu Qi, Yi Wang, Jiaya Jia [Paper] News This

null 254 Dec 29, 2022
šŸ§  A PyTorch implementation of 'Deep CORAL: Correlation Alignment for Deep Domain Adaptation.', ECCV 2016

Deep CORAL A PyTorch implementation of 'Deep CORAL: Correlation Alignment for Deep Domain Adaptation. B Sun, K Saenko, ECCV 2016' Deep CORAL can learn

Andy Hsu 200 Dec 25, 2022