Official pytorch implementation of the paper: "SinGAN: Learning a Generative Model from a Single Natural Image"

Overview

SinGAN

Project | Arxiv | CVF | Supplementary materials | Talk (ICCV`19)

Official pytorch implementation of the paper: "SinGAN: Learning a Generative Model from a Single Natural Image"

ICCV 2019 Best paper award (Marr prize)

Random samples from a single image

With SinGAN, you can train a generative model from a single natural image, and then generate random samples from the given image, for example:

SinGAN's applications

SinGAN can be also used for a line of image manipulation tasks, for example: This is done by injecting an image to the already trained model. See section 4 in our paper for more details.

Citation

If you use this code for your research, please cite our paper:

@inproceedings{rottshaham2019singan,
  title={SinGAN: Learning a Generative Model from a Single Natural Image},
  author={Rott Shaham, Tamar and Dekel, Tali and Michaeli, Tomer},
  booktitle={Computer Vision (ICCV), IEEE International Conference on},
  year={2019}
}

Code

Install dependencies

python -m pip install -r requirements.txt

This code was tested with python 3.6, torch 1.4

Please note: the code currently only supports torch 1.4 or earlier because of the optimization scheme.

For later torch versions, you may try this repository: https://github.com/kligvasser/SinGAN (results won't necessarily be identical to the official implementation).

Train

To train SinGAN model on your own image, put the desired training image under Input/Images, and run

python main_train.py --input_name <input_file_name>

This will also use the resulting trained model to generate random samples starting from the coarsest scale (n=0).

To run this code on a cpu machine, specify --not_cuda when calling main_train.py

Random samples

To generate random samples from any starting generation scale, please first train SinGAN model on the desired image (as described above), then run

python random_samples.py --input_name <training_image_file_name> --mode random_samples --gen_start_scale <generation start scale number>

pay attention: for using the full model, specify the generation start scale to be 0, to start the generation from the second scale, specify it to be 1, and so on.

Random samples of arbitrary sizes

To generate random samples of arbitrary sizes, please first train SinGAN model on the desired image (as described above), then run

python random_samples.py --input_name <training_image_file_name> --mode random_samples_arbitrary_sizes --scale_h <horizontal scaling factor> --scale_v <vertical scaling factor>

Animation from a single image

To generate short animation from a single image, run

python animation.py --input_name <input_file_name> 

This will automatically start a new training phase with noise padding mode.

Harmonization

To harmonize a pasted object into an image (See example in Fig. 13 in our paper), please first train SinGAN model on the desired background image (as described above), then save the naively pasted reference image and it's binary mask under "Input/Harmonization" (see saved images for an example). Run the command

python harmonization.py --input_name <training_image_file_name> --ref_name <naively_pasted_reference_image_file_name> --harmonization_start_scale <scale to inject>

Please note that different injection scale will produce different harmonization effects. The coarsest injection scale equals 1.

Editing

To edit an image, (See example in Fig. 12 in our paper), please first train SinGAN model on the desired non-edited image (as described above), then save the naive edit as a reference image under "Input/Editing" with a corresponding binary map (see saved images for an example). Run the command

python editing.py --input_name <training_image_file_name> --ref_name <edited_image_file_name> --editing_start_scale <scale to inject>

both the masked and unmasked output will be saved. Here as well, different injection scale will produce different editing effects. The coarsest injection scale equals 1.

Paint to Image

To transfer a paint into a realistic image (See example in Fig. 11 in our paper), please first train SinGAN model on the desired image (as described above), then save your paint under "Input/Paint", and run the command

python paint2image.py --input_name <training_image_file_name> --ref_name <paint_image_file_name> --paint_start_scale <scale to inject>

Here as well, different injection scale will produce different editing effects. The coarsest injection scale equals 1.

Advanced option: Specify quantization_flag to be True, to re-train only the injection level of the model, to get a on a color-quantized version of upsampled generated images from the previous scale. For some images, this might lead to more realistic results.

Super Resolution

To super resolve an image, please run:

python SR.py --input_name <LR_image_file_name>

This will automatically train a SinGAN model correspond to 4x upsampling factor (if not exist already). For different SR factors, please specify it using the parameter --sr_factor when calling the function. SinGAN's results on the BSD100 dataset can be download from the 'Downloads' folder.

Additional Data and Functions

Single Image Fréchet Inception Distance (SIFID score)

To calculate the SIFID between real images and their corresponding fake samples, please run:

python SIFID/sifid_score.py --path2real <real images path> --path2fake <fake images path> 

Make sure that each of the fake images file name is identical to its corresponding real image file name. Images should be saved in .jpg format.

Super Resolution Results

SinGAN's SR results on the BSD100 dataset can be download from the 'Downloads' folder.

User Study

The data used for the user study can be found in the Downloads folder.

real folder: 50 real images, randomly picked from the places database

fake_high_variance folder: random samples starting from n=N for each of the real images

fake_mid_variance folder: random samples starting from n=N-1 for each of the real images

For additional details please see section 3.1 in our paper

Comments
  • RuntimeError: The size of tensor a (276) must match the size of tensor b (282) at non-singleton dimension 3

    RuntimeError: The size of tensor a (276) must match the size of tensor b (282) at non-singleton dimension 3

    I am having problems with running the SR.py:

    (sinGAN) B:\Program_Files\Anaconda3\envs\sinGAN\SinGAN-master>python SR.py --input_name TEST_12_variant.png --out R:\ Random Seed: 1708 4.000000 Traceback (most recent call last): File "SR.py", line 64, in out = SinGAN_generate(Gs_sr, Zs_sr, reals_sr, NoiseAmp_sr, opt, in_s=reals_sr[0], num_samples=1) File "B:\Program_Files\Anaconda3\envs\sinGAN\SinGAN-master\SinGAN\manipulate.py", line 131, in SinGAN_generate z_in = noise_amp*(z_curr)+I_prev RuntimeError: The size of tensor a (276) must match the size of tensor b (282) at non-singleton dimension 3

    Windows 10, conda version : 4.7.10, GTX 1080Ti, driver version: 26.21.14.3160

    opened by QLaHPD 18
  • Scaling from 25px width to 250px width size training and inference on 680px image width

    Scaling from 25px width to 250px width size training and inference on 680px image width

    Is it possible to train up to a certain scale like 250px width and with the trained model for inference to scale to 480px width or higher ?

    Random Samples

    I tried to generate random samples : python random_samples.py --input_name mountain.jpeg --mode random_samples_arbitrary_sizes --scale_h 480 --scale_v 600 But I get out of memory error. So if it is possible, how can I generate bigger resolution images without out of memory crash ?

    Edit/Harmonize

    The same question for edit and harmonize, if I train a model on a small resolution image can I upscale the output of edit/harmonize ?

    opened by cristyioan2000 9
  • Kernel size can't be greater than actual input size

    Kernel size can't be greater than actual input size

    I tired this code using original data, but the following error occured: RuntimeError: Calculated padded input size per channel: (1 x 4). Kernel size: (3 x 3). Kernel size can't be greater than actual input size

    Could you tell me what should I do?

    opened by mf1611 9
  • --cuda argument is apparently being ignored

    --cuda argument is apparently being ignored

    The project theoretically was designed to run without using CUDA, since it has the --cuda command-line argument. However, in practice it doesn't seem to work that way.

    I'm trying to run the project in a device without CUDA support and it's not working. When I run main_train.py, the function np2torch in functions invokes the torch.cuda.FloatTensor class without checking if the --cuda option was used, which raises an exception.

    The imresize module also has a np2torch function with the same problem.

    Also, the generate_noise function in functions is always invoked without named parameters, which sets the device parameter to the default value of 'cuda'.

    What is the purpose of the --cuda CLI argument?

    opened by czanella 9
  • training stuck at scale 9:[1999/2000]

    training stuck at scale 9:[1999/2000]

    Training constantly stuck at [1999/2000] such as "scale 7:[1999/2000]" or "scale 9:[1999/2000]".

    Can't interrupt it even if I used ctrl+c, it's totally dead.

    I used a mountain picture, resized to same size as one of your sample image.

    I'm using : python 3.6.8 torch 1.3.0

    GPU rtx2080ti NVIDIA Driver 419.35 CUDA 10.1

    opened by phonygene 8
  • Error Encountered

    Error Encountered

    Traceback (most recent call last): File "main_train.py", line 29, in train(opt, Gs, Zs, reals, NoiseAmp) File "/gdrive/My Drive/SinGAN/SinGAN/training.py", line 40, in train z_curr,in_s,G_curr = train_single_scale(D_curr,G_curr,reals,Gs,Zs,in_s,NoiseAmp,opt) File "/gdrive/My Drive/SinGAN/SinGAN/training.py", line 180, in train_single_scale errG.backward(retain_graph=True) File "/usr/local/lib/python3.6/dist-packages/torch/tensor.py", line 198, in backward torch.autograd.backward(self, gradient, retain_graph, create_graph) File "/usr/local/lib/python3.6/dist-packages/torch/autograd/init.py", line 100, in backward allow_unreachable=True) # allow_unreachable flag RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [3, 32, 3, 3]] is at version 2; expected version 1 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

    opened by DyassKhalid007 7
  • ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))

    ValueError('Expected more than 1 value per channel when training, got input size {}'.format(size))

    I am having problems with running the SR.py: ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 32, 1, 1])

    And I got some answers on the web: Most likely you have a nn.BatchNorm layer somewhere in your model, which expects more then 1 value to calculate the running mean and std of the current batch. How to solve this problem?

    opened by Gnaixzl 6
  • More about SIFID score

    More about SIFID score

    Your GAN paper is potentially very useful. I am trying to train a model on learning an image, then find the SIFID score between the image and other test image. Both training and test images are of the same resolution ie. 128 X 128. I would like to pose a question:

    In the README: python SIFID/sifid_score.py --path2real --path2fake --images_suffix <e.g. jpg, png>

    Is the path2real and path2fake the directory that contain the image for comparison? I have issue with running sifid_score.py.

    opened by iMiffy 5
  • nan problem of SIFID

    nan problem of SIFID

    Dear author, I have encountered difficulties in calculating SIFID. Every time my colleague and I run SIFID, the program will throw a nan error. The test images are jpg format.

    SIFID: nan

    opened by SeibertronSS 3
  • 'PngImageFile' object has no attribute '_PngImageFile__frame'

    'PngImageFile' object has no attribute '_PngImageFile__frame'

    Random Seed: 2371 Traceback (most recent call last): File "main_train.py", line 27, in real = functions.read_image(opt) File "D:\ProgramData\PycharmProjects\SinGAN\SinGAN\functions.py", line 150, in read_image x = img.imread('%s/%s' % (opt.input_dir,opt.input_name)) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\skimage\io_io.py", line 48, in imread img = call_plugin('imread', fname, plugin=plugin, **plugin_args) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\skimage\io\manage_plugins.py", line 210, in call_plugin return func(*args, **kwargs) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\skimage\io_plugins\imageio_plugin.py", line 10, in imread return np.asarray(imageio_imread(*args, **kwargs)) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\imageio\core\functions.py", line 267, in imread return reader.get_data(0) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\imageio\core\format.py", line 346, in get_data im, meta = self._get_data(index, **kwargs) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\imageio\plugins\pillow.py", line 301, in _get_data im, info = PillowFormat.Reader._get_data(self, index) File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\imageio\plugins\pillow.py", line 170, in _get_data i = self._im.tell() File "d:\ProgramData\Anaconda3\envs\pytorch\lib\site-packages\PIL\PngImagePlugin.py", line 827, in tell return self.__frame AttributeError: 'PngImageFile' object has no attribute '_PngImageFile__frame'

    opened by ShowTimeJMJ 3
  • No residual connections

    No residual connections

    The original paper mentions a residual connection in the generator, and probably I guess that is why you used tanh instead of sigmoid in the generator output. But the given code in the file SinGAN/models.py does not seem to contain any residual connections.

    opened by Sooryakiran 3
  • process of inject scale for image manip (supplemental material figure 3)

    process of inject scale for image manip (supplemental material figure 3)

    I have been trying to reproduce this functionality, but i do not see a way to reproduce this method of training on one image and producing from a second image.

    Is there further documentation available that describes how this was produced?

    opened by sammystevens1983 0
  • Lack of imsave in random_samples.py

    Lack of imsave in random_samples.py

    The output of the SinGAN_generate function is not saved to a file in random_samples.py. It is necessary to add a single line with a call to imsave function from matplotlib.pyplot at the very end of the file.

    opened by pszajows 0
  • Lack of package versions in requirements.txt file.

    Lack of package versions in requirements.txt file.

    In the requirements.txt file no package versions are provided at the moment. This results in a runtime error when running the code in the environment created on the basis of that file. If it is necessary to install the packages from a specific conda channel it would be also good to update README.md file to reflect that. This need is very likely - the comments from other issues suggest, that torch version should be 1.4.0 and torchvision 0.5.0. These versions are not available for pip installations any more and AFAIK also they are not present in the standard conda channel. See issues #108, #147, #161 and #110. See also: https://pytorch.org/get-started/previous-versions/ for details regarding installation of old torch and torchvision versions. The package versions, which look OK to me are: matplotlib==3.3.4 scikit-image==0.17.2 scikit-learn==0.24.2 scipy==1.5.4 numpy==1.19.5 torch==1.4.0 torchvision==0.5.0

    opened by pszajows 0
  • How to use SinGAN to perform image translation just as CycleGAN.

    How to use SinGAN to perform image translation just as CycleGAN.

    I am wondering about the application of image translation by SinGAN as discussed by @tamarott in: https://www.youtube.com/watch?v=j9id4UpN9BI&t=571s SinGAN

    How to modify code and achieve that similar output. Any help would be great! Thanks.

    opened by sushant097 0
  • Black Image for fake_sample.png at scale 8 and 9

    Black Image for fake_sample.png at scale 8 and 9

    Hello, sorry for my english because I'm french but I have an unexplained issue for me. When I execute this line : python main_train.py --input_name colusseum.png for example, I have some black image generated for fake_sample.png and G(z_opt).png for scale 8 and 9 for example and I don't understand why. It's the same when I try every other function like random sapling or SR, I have black image

    I have RTX 3080 TI and I use python 3.6, torch 1.4.0 and torchvision 0.5.0.

    opened by J0rmel 1
Official PyTorch implementation for paper Context Matters: Graph-based Self-supervised Representation Learning for Medical Images

Context Matters: Graph-based Self-supervised Representation Learning for Medical Images Official PyTorch implementation for paper Context Matters: Gra

null 49 Nov 23, 2022
Official pytorch implementation of paper "Image-to-image Translation via Hierarchical Style Disentanglement".

HiSD: Image-to-image Translation via Hierarchical Style Disentanglement Official pytorch implementation of paper "Image-to-image Translation

null 364 Dec 14, 2022
Official pytorch implementation of paper "Inception Convolution with Efficient Dilation Search" (CVPR 2021 Oral).

IC-Conv This repository is an official implementation of the paper Inception Convolution with Efficient Dilation Search. Getting Started Download Imag

Jie Liu 111 Dec 31, 2022
Official implementation of our paper "LLA: Loss-aware Label Assignment for Dense Pedestrian Detection" in Pytorch.

LLA: Loss-aware Label Assignment for Dense Pedestrian Detection This project provides an implementation for "LLA: Loss-aware Label Assignment for Dens

null 35 Dec 6, 2022
Official implementation of our CVPR2021 paper "OTA: Optimal Transport Assignment for Object Detection" in Pytorch.

OTA: Optimal Transport Assignment for Object Detection This project provides an implementation for our CVPR2021 paper "OTA: Optimal Transport Assignme

null 217 Jan 3, 2023
This is the official PyTorch implementation of the paper "TransFG: A Transformer Architecture for Fine-grained Recognition" (Ju He, Jie-Neng Chen, Shuai Liu, Adam Kortylewski, Cheng Yang, Yutong Bai, Changhu Wang, Alan Yuille).

TransFG: A Transformer Architecture for Fine-grained Recognition Official PyTorch code for the paper: TransFG: A Transformer Architecture for Fine-gra

Ju He 307 Jan 3, 2023
[PyTorch] Official implementation of CVPR2021 paper "PointDSC: Robust Point Cloud Registration using Deep Spatial Consistency". https://arxiv.org/abs/2103.05465

PointDSC repository PyTorch implementation of PointDSC for CVPR'2021 paper "PointDSC: Robust Point Cloud Registration using Deep Spatial Consistency",

null 153 Dec 14, 2022
The official pytorch implementation of our paper "Is Space-Time Attention All You Need for Video Understanding?"

TimeSformer This is an official pytorch implementation of Is Space-Time Attention All You Need for Video Understanding?. In this repository, we provid

Facebook Research 1k Dec 31, 2022
Official Pytorch Implementation of: "ImageNet-21K Pretraining for the Masses"(2021) paper

ImageNet-21K Pretraining for the Masses Paper | Pretrained models Official PyTorch Implementation Tal Ridnik, Emanuel Ben-Baruch, Asaf Noy, Lihi Zelni

null 574 Jan 2, 2023
The official PyTorch implementation of the paper: *Xili Dai, Xiaojun Yuan, Haigang Gong, Yi Ma. "Fully Convolutional Line Parsing." *.

F-Clip — Fully Convolutional Line Parsing This repository contains the official PyTorch implementation of the paper: *Xili Dai, Xiaojun Yuan, Haigang

Xili Dai 115 Dec 28, 2022
The repository offers the official implementation of our paper in PyTorch.

Cloth Interactive Transformer (CIT) Cloth Interactive Transformer for Virtual Try-On Bin Ren1, Hao Tang1, Fanyang Meng2, Runwei Ding3, Ling Shao4, Phi

Bingoren 49 Dec 1, 2022
The official PyTorch implementation of recent paper - SAINT: Improved Neural Networks for Tabular Data via Row Attention and Contrastive Pre-Training

This repository is the official PyTorch implementation of SAINT. Find the paper on arxiv SAINT: Improved Neural Networks for Tabular Data via Row Atte

Gowthami Somepalli 284 Dec 21, 2022
Official PyTorch implementation and pretrained models of the paper Self-Supervised Classification Network

Self-Classifier: Self-Supervised Classification Network Official PyTorch implementation and pretrained models of the paper Self-Supervised Classificat

Elad Amrani 24 Dec 21, 2022
Official Pytorch implementation of paper "Reverse Engineering of Generative Models: Inferring Model Hyperparameters from Generated Images"

Reverse_Engineering_GMs Official Pytorch implementation of paper "Reverse Engineering of Generative Models: Inferring Model Hyperparameters from Gener

null 100 Dec 18, 2022
Official Pytorch Implementation of: "Semantic Diversity Learning for Zero-Shot Multi-label Classification"(2021) paper

Semantic Diversity Learning for Zero-Shot Multi-label Classification Paper Official PyTorch Implementation Avi Ben-Cohen, Nadav Zamir, Emanuel Ben Bar

null 28 Aug 29, 2022
Official PyTorch implementation of the preprint paper "Stylized Neural Painting", accepted to CVPR 2021.

Official PyTorch implementation of the preprint paper "Stylized Neural Painting", accepted to CVPR 2021.

Zhengxia Zou 1.5k Dec 28, 2022
Official Pytorch implementation of ICLR 2018 paper Deep Learning for Physical Processes: Integrating Prior Scientific Knowledge.

Deep Learning for Physical Processes: Integrating Prior Scientific Knowledge: Official Pytorch implementation of ICLR 2018 paper Deep Learning for Phy

emmanuel 47 Nov 6, 2022
This is the official pytorch implementation for the paper: Instance Similarity Learning for Unsupervised Feature Representation.

ISL This is the official pytorch implementation for the paper: Instance Similarity Learning for Unsupervised Feature Representation, which is accepted

null 19 May 4, 2022
Official PyTorch implementation of the paper "Recycling Discriminator: Towards Opinion-Unaware Image Quality Assessment Using Wasserstein GAN", accepted to ACM MM 2021 BNI Track.

RecycleD Official PyTorch implementation of the paper "Recycling Discriminator: Towards Opinion-Unaware Image Quality Assessment Using Wasserstein GAN

Yunan Zhu 23 Nov 5, 2022