Giter Club home page Giter Club logo

noise2noise-pytorch's Introduction

Noise2Noise: Learning Image Restoration without Clean Data

This is an unofficial PyTorch implementation of Noise2Noise (Lehtinen et al. 2018).

Dependencies

To install the latest version of all packages, run

pip3 install --user -r requirements.txt

This code was tested on Python 3.6.5 on macOS High Sierra (10.13.4) and Ubuntu 16.04. It will fail with Python 2.7.x due to usage of 3.6-specific functions. Note that training and testing will also fail on Windows out of the box due to differences in the path resolvers (os.path).

Dataset

The authors use ImageNet, but any dataset will do. COCO 2017 has a small validation set (1 GB) which can be nicely split into train/valid for easier training. For instance, to obtain a 4200/800 train/valid split you can do:

mkdir data && cd data
mkdir train valid test
wget http://images.cocodataset.org/zips/val2017.zip
unzip val2017.zip && cd val2017
mv `ls | head -4200` ../train
mv `ls | head -800` ../valid

You can also download the full datasets (7 GB) that more or less match the paper, if you have the bandwidth:

mkdir data && cd data
mkdir train valid test
wget http://images.cocodataset.org/zips/test2017.zip
wget http://images.cocodataset.org/zips/val2017.zip
unzip -j test2017.zip -d train
unzip -j val2017.zip -d valid

Add your favorite images to the data/test folder. Only a handful will do to visually inspect the denoiser performance.

Training

See python3 train.py --h for list of optional arguments, or examples/train.sh for an example.

By default, the model train with noisy targets. To train with clean targets, use --clean-targets. To train and validate on smaller datasets, use the --train-size and --valid-size options. To plot stats as the model trains, use --plot-stats; these are saved alongside checkpoints. By default CUDA is not enabled: use the --cuda option if you have a GPU that supports it.

Gaussian noise

The noise parameter is the maximum standard deviation σ.

python3 train.py \
  --train-dir ../data/train --train-size 1000 \
  --valid-dir ../data/valid --valid-size 200 \
  --ckpt-save-path ../ckpts \
  --nb-epochs 10 \
  --batch-size 4 \
  --loss l2 \
  --noise-type gaussian \
  --noise-param 50 \
  --crop-size 64 \
  --plot-stats \
  --cuda

Poisson noise

The noise parameter is the Poisson parameter λ.

python3 train.py
  --loss l2 \
  --noise-type poisson \
  --noise-param 50 \
  --cuda

Text overlay

The noise parameter is the approximate probability p that a pixel is covered by text.

python3 train.py \
  --loss l1 \
  --noise-type text \
  --noise-param 0.5 \
  --cuda

Monte Carlo rendering noise

See other read-me file.

Testing

Model checkpoints are automatically saved after every epoch. To test the denoiser, provide test.py with a PyTorch model (.pt file) via the argument --load-ckpt and a test image directory via --data. The --show-output option specifies the number of noisy/denoised/clean montages to display on screen. To disable this, simply remove --show-output.

python3 test.py \
  --data ../data \
  --load-ckpt ../ckpts/gaussian/n2n.pt \
  --noise-type gaussian \
  --noise-param 50 \
  --crop-size 256 \
  --show-output 3 \
  --cuda

See python3 test.py --h for list of optional arguments, or examples/test.sh for an example.

Results

Gaussian model was trained for 100 epochs with a train/valid split of 2000/400. Text model was trained with 1000/200 due to the overhead introduced when corrupting with text. Both models were trained on an old NVIDIA GTX 780.

Gaussian noise (σ = 25)
Noisy input (20.34 dB) Denoised (32.68 dB) Clean targets (32.49 dB) Ground truth
Text overlay (p = 0.25)
Noisy input (15.07 dB) Denoised (28.10 dB) Clean targets (27.79 dB) Ground truth

Known issues

  • U-Net has shared weights (i.e. same enc_conv for the decoder part). This is trivial to fix but I simply don't have the time to do so. The model seems to perform well even with this error.
  • Activation functions should be LeakyReLUs everywhere except last layer where it should be ReLU. The current implementation (and pretrained models) assumes the opposite due to me originally misreading the paper. It was reported that using the correct version yields unstable training without batch norm; I haven't tested it yet.
  • It is unclear how to deal with Poisson noise since it is data-dependent and thus nonadditive. See official TensorFlow implementation to adapt properly.

References

Acknowledgments

I would like to acknowledge Yusuke Uchida for his Keras implementation of Noise2Noise. Although Keras and PyTorch are very different frameworks, parts of his code did help me in completing this implementation.

noise2noise-pytorch's People

Contributors

joeylitalien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

noise2noise-pytorch's Issues

just for the test.py

Hello, I would like to ask, why the test image I input is 1024 * 1024, but the output is indeed 256 * 256, and I did not see where the picture was cropped, I look forward to your reply, thank you.

GPU not being used

i used --cuda on google colab but gpu is not being used how to fix it

Confused about the unet.py code, is it reasonable?

Hi I checked the code feel confused with the code in unet.py:

    def forward(self, x):
        """Through encoder, then decoder by adding U-skip connections. """

        # Encoder
        pool1 = self._block1(x)
        pool2 = self._block2(pool1)
        pool3 = self._block2(pool2)
        pool4 = self._block2(pool3)
        pool5 = self._block2(pool4)

pool2-pool5 is computed by self._block2, So it means pool2-pool5 re-use the same conv weight and bias. Does it accepted? I think the u-net should use different conv weight of different layer.

About U-Net model and data pre-processing

I am curious about the differences from the paper model

  1. Why did you choose to use ConvTranspose instead of Upsampling
  2. You used RELU in all parts and Leaky RELU after the last layer. But from the paper I think the author meant Leaky RELU everywhere except the last layer. And In the last layer only linear activation

Unrelated question
3) Did you use any pre-processing for the images e.g. means subtraction, normalization etc. I think that would be needed since we don't have BN layers

I am trying to implement the model in Tensorflow and having the problem of INF loss that starts in the 2nd epoch. So I hope your answers would help me. Thank you in advance!

Update: I seem to have solved the problem by adding Batch Norm layers to the UNET model. But still I am puzzled how the authors managed to get a stable training without Batch Norm

OpenEXR Error while running the train files.

Traceback (most recent call last):
File "train.py", line 7, in
from datasets import load_dataset
File "/Users/snehagathani/Desktop/noise/src/datasets.py", line 9, in
from utils import load_hdr_as_tensor
File "/Users/snehagathani/Desktop/noise/src/utils.py", line 12, in
import OpenEXR
ImportError: dlopen(/anaconda3/lib/python3.6/site-packages/OpenEXR.cpython-36m-darwin.so, 2): Symbol not found: __ZN7Imf_2_314TypedAttributeISsE13readValueFromERNS_7IStreamEii
Referenced from: /anaconda3/lib/python3.6/site-packages/OpenEXR.cpython-36m-darwin.so
Expected in: flat namespace
in /anaconda3/lib/python3.6/site-packages/OpenEXR.cpython-36m-darwin.so

This error comes for all the training files.

Using my own noisy-noisy pairs

Dear Authors,

There are no instructions on how to use the code for my own noisy-noisy pairs. The current code takes a dataset of images as input and applies two independent noises on each instance, leading to pairs of noisy-noisy train set. But what if I already have my own pairs of noisy-noisy images and want to train the network on them? Is this possible with current architecture?

Best,
Ali.

datasets.py issues?

Hello,there may be problems with this place
raise ValueError('Invalid noise type: {}'.format(noise_type))
should be
raise ValueError('Invalid noise type: {}'.format(self.noise_type))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.