Training Very Deep Neural Networks Without Skip-Connections

Overview

DiracNets

v2 update (January 2018):

The code was updated for DiracNets-v2 in which we removed NCReLU by adding per-channel a and b multipliers without weight decay. This allowed us to significantly simplify the network, which is now folds into a simple chain of convolution-ReLU layers, like VGG. On ImageNet DiracNet-18 and DiracNet-34 closely match corresponding ResNet with the same number of parameters.

See v1 branch for DiracNet-v1.


PyTorch code and models for DiracNets: Training Very Deep Neural Networks Without Skip-Connections

https://arxiv.org/abs/1706.00388

Networks with skip-connections like ResNet show excellent performance in image recognition benchmarks, but do not benefit from increased depth, we are thus still interested in learning actually deep representations, and the benefits they could bring. We propose a simple weight parameterization, which improves training of deep plain (without skip-connections) networks, and allows training plain networks with hundreds of layers. Accuracy of our proposed DiracNets is close to Wide ResNet (although DiracNets need more parameters to achieve it), and we are able to match ResNet-1000 accuracy with plain DiracNet with only 28 layers. Also, the proposed Dirac weight parameterization can be folded into one filter for inference, leading to easily interpretable VGG-like network.

DiracNets on ImageNet:

TL;DR

In a nutshell, Dirac parameterization is a sum of filters and scaled Dirac delta function:

conv2d(x, alpha * delta + W)

Here is simplified PyTorch-like pseudocode for the function we use to train plain DiracNets (with weight normalization):

def dirac_conv2d(input, W, alpha, beta)
    return F.conv2d(input, alpha * dirac(W) + beta * normalize(W))

where alpha and beta are per-channel scaling multipliers, and normalize does l_2 normalization over each feature plane.

Code

Code structure:

├── README.md # this file
├── diracconv.py # modular DiracConv definitions
├── test.py # unit tests
├── diracnet-export.ipynb # ImageNet pretrained models
├── diracnet.py # functional model definitions
└── train.py # CIFAR and ImageNet training code

Requirements

First install PyTorch, then install torchnet:

pip install git+https://github.com/pytorch/tnt.git@master

Install other Python packages:

pip install -r requirements.txt

To train DiracNet-34-2 on CIFAR do:

python train.py --save ./logs/diracnets_$RANDOM$RANDOM --depth 34 --width 2

To train DiracNet-18 on ImageNet do:

python train.py --dataroot ~/ILSVRC2012/ --dataset ImageNet --depth 18 --save ./logs/diracnet_$RANDOM$RANDOM \
                --batchSize 256 --epoch_step [30,60,90] --epochs 100 --weightDecay 0.0001 --lr_decay_ratio 0.1

nn.Module code

We provide DiracConv1d, DiracConv2d, DiracConv3d, which work like nn.Conv1d, nn.Conv2d, nn.Conv3d, but have Dirac-parametrization inside (our training code doesn't use these modules though).

Pretrained models

We fold batch normalization and Dirac parameterization into F.conv2d weight and bias tensors for simplicity. Resulting models are as simple as VGG or AlexNet, having only nonlinearity+conv2d as a basic block.

See diracnets.ipynb for functional and modular model definitions.

There is also folded DiracNet definition in diracnet.py, which uses code from PyTorch model_zoo and downloads pretrained model from Amazon S3:

from diracnet import diracnet18
model = diracnet18(pretrained=True)

Printout of the model above:

DiracNet(
  (features): Sequential(
    (conv): Conv2d (3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3))
    (max_pool0): MaxPool2d(kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), dilation=(1, 1), ceil_mode=False)
    (group0.block0.relu): ReLU()
    (group0.block0.conv): Conv2d (64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group0.block1.relu): ReLU()
    (group0.block1.conv): Conv2d (64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group0.block2.relu): ReLU()
    (group0.block2.conv): Conv2d (64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group0.block3.relu): ReLU()
    (group0.block3.conv): Conv2d (64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (max_pool1): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
    (group1.block0.relu): ReLU()
    (group1.block0.conv): Conv2d (64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group1.block1.relu): ReLU()
    (group1.block1.conv): Conv2d (128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group1.block2.relu): ReLU()
    (group1.block2.conv): Conv2d (128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group1.block3.relu): ReLU()
    (group1.block3.conv): Conv2d (128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (max_pool2): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
    (group2.block0.relu): ReLU()
    (group2.block0.conv): Conv2d (128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group2.block1.relu): ReLU()
    (group2.block1.conv): Conv2d (256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group2.block2.relu): ReLU()
    (group2.block2.conv): Conv2d (256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group2.block3.relu): ReLU()
    (group2.block3.conv): Conv2d (256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (max_pool3): MaxPool2d(kernel_size=(2, 2), stride=(2, 2), dilation=(1, 1), ceil_mode=False)
    (group3.block0.relu): ReLU()
    (group3.block0.conv): Conv2d (256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group3.block1.relu): ReLU()
    (group3.block1.conv): Conv2d (512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group3.block2.relu): ReLU()
    (group3.block2.conv): Conv2d (512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (group3.block3.relu): ReLU()
    (group3.block3.conv): Conv2d (512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (last_relu): ReLU()
    (avg_pool): AvgPool2d(kernel_size=7, stride=7, padding=0, ceil_mode=False, count_include_pad=True)
  )
  (fc): Linear(in_features=512, out_features=1000)
)

The models were trained with OpenCV, so you need to use it too to reproduce stated accuracy.

Pretrained weights for DiracNet-18 and DiracNet-34:
https://s3.amazonaws.com/modelzoo-networks/diracnet18v2folded-a2174e15.pth
https://s3.amazonaws.com/modelzoo-networks/diracnet34v2folded-dfb15d34.pth

Pretrained weights for the original (not folded) model, functional definition only:
https://s3.amazonaws.com/modelzoo-networks/diracnet18-v2_checkpoint.pth
https://s3.amazonaws.com/modelzoo-networks/diracnet34-v2_checkpoint.pth

We plan to add more pretrained models later.

Bibtex

@inproceedings{Zagoruyko2017diracnets,
    author = {Sergey Zagoruyko and Nikos Komodakis},
    title = {DiracNets: Training Very Deep Neural Networks Without Skip-Connections},
    url = {https://arxiv.org/abs/1706.00388},
    year = {2017}}
Comments
  • no torch.nn.functional.normalize

    no torch.nn.functional.normalize

    AttributeError: 'module' object has no attribute 'normalize'

    File "/home/yq/work/face_class/diracnets/diracnet.py", line 97, in block w = beta * F.normalize(w.view(w.size(0), -1)).view_as(w) + alpha * delta

    opened by eeric 7
  • invalid combination of arguments when run the tran.py

    invalid combination of arguments when run the tran.py

    raceback (most recent call last): File "train.py", line 230, in main() File "train.py", line 118, in main f, params, stats = define_diracnet(opt.depth, opt.width, opt.dataset) File "/home/amax/cqs/diracnets/diracnet.py", line 126, in define_diracnet 'conv': cast(kaiming_normal(torch.Tensor(widths[0], 3, 3, 3))), TypeError: new() received an invalid combination of arguments - got (Tensor, int, int, int), but expected one of:

    • (torch.device device)
    • (tuple of ints size, torch.device device)
    • (torch.Storage storage)
    • (Tensor other)
    • (object data, torch.device device)
    opened by chituma110 2
  • Can not run train.py script.

    Can not run train.py script.

    I run python train --help, and get one error:

    $ python train.py --help
    File "train.py", line 166
        z = {**vars(opt), **t}
              ^
    SyntaxError: invalid syntax
    

    How could I fix this error?

    opened by justfouw 1
  • requirements.txt?

    requirements.txt?

    The documentation mentions installing from a requirements.txt with pip install -r requirements.txt but there's no requirements.txt present in this repository.

    opened by aman-tiwari 1
  • TypeError: float() argument must be a string or a number

    TypeError: float() argument must be a string or a number

    Traceback (most recent call last): File "train.py", line 231, in main() File "train.py", line 227, in main engine.train(h, train_loader, opt.epochs, optimizer) File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torchnet/engine/engine.py", line 63, in train state['optimizer'].step(closure) File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torch/optim/sgd.py", line 72, in step loss = closure() File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torchnet/engine/engine.py", line 56, in closure self.hook('on_forward', state) File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torchnet/engine/engine.py", line 31, in hook self.hooksname File "train.py", line 180, in on_forward meter_loss.add(float(state['loss'])) TypeError: float() argument must be a string or a number

    When I train the network, I met a error like that. Did anyone have meet the same problem, and how to solve it?

    opened by FzuLJ 0
  • AttributeError: 'module' object has no attribute 'normalize'

    AttributeError: 'module' object has no attribute 'normalize'

    Hi, when I train the network, I got an error as followed:

    recent call last): File "train.py", line 230, in main() File "train.py", line 226, in main engine.train(h, train_loader, opt.epochs, optimizer) File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torchnet/engine/engine.py", line 63, in train state['optimizer'].step(closure) File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torch/optim/sgd.py", line 72, in step loss = closure() File "/home/jlin/anaconda2/envs/pytorch/lib/python2.7/site-packages/torchnet/engine/engine.py", line 52, in closure loss, output = state'network' File "train.py", line 162, in h y = data_parallel(f, inputs, params, stats, sample[2], list(np.arange(opt.ngpu))) File "/home/jlin/diracnets-master/diracnet.py", line 44, in data_parallel return f(input, params, stats, mode) File "/home/jlin/diracnets-master/diracnet.py", line 116, in f o = group(o, params, stats, 'group0', mode, n * 2) File "/home/jlin/diracnets-master/diracnet.py", line 94, in group o = block(o, params, stats, '%s.block%d' % (base, i), mode, i) File "/home/jlin/diracnets-master/diracnet.py", line 86, in block w = beta * F.normalize(w.view(w.size(0), -1)).view_as(w) + alpha * delta AttributeError: 'module' object has no attribute 'normalize'

    And I view the file torch.nn.functional, where I could not find the functional named normalize. Did anyone meet the same problem ?

    opened by FzuLJ 0
  • Updates

    Updates

    A number of things:

    • fix diracnet34 imagenet definition to have the same number of blocks with resnet34
    • make use of broadcasting from PyTorch 0.2
    • add normalization in module definitions.
    opened by szagoruyko 0
  • Update pretrained DiracNet-18-0.75

    Update pretrained DiracNet-18-0.75

    Trained a new model with slightly different batch_norm - max_pool2d ordering such that all batch norms can be folded into convolutions, and the model is simpler.

    opened by szagoruyko 0
  • Some codes may be different from what you said in you paper

    Some codes may be different from what you said in you paper

    Hi, Thank you for your code. But I am confused about the "i" implemented in your code. I think it may be different from what you said in your paper. In your paper, you initialize "I" as : image In some filters, there may be more than one weight initialized as 1. But in your code: dirac_(I) you used dirac_ function to initialize it. Only one element in a filter could be set 1. Maybe I misunderstand it. Could you please help me with it?

    opened by leoozy 0
  • I want to input my own data.

    I want to input my own data.

    Hi, @soumith @szagoruyko

    I want to input my own data in your diracNet. Not a Cifar-10/Imagenet data but my own images data folder.

    Like this code: http://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html

    Is that possible?

    Thanks in advance ~

    opened by bemoregt 0
  • cublas runtime error

    cublas runtime error

    On ImageNet set, it occured as following: File "train.py", line 245, in main() File "train.py", line 241, in main engine.train(h, train_loader, opt.epochs, optimizer) File "build/bdist.linux-x86_64/egg/torchnet/engine/engine.py", line 39, in train File "/usr/local/lib/python2.7/dist-packages/torch/optim/sgd.py", line 72, in step loss = closure() File "build/bdist.linux-x86_64/egg/torchnet/engine/engine.py", line 28, in closure File "train.py", line 177, in h y = data_parallel(f, inputs, params, stats, sample[2], np.arange(opt.ngpu)) File "/home/yq/work/face_class/diracnets/diracnet.py", line 51, in data_parallel return f(input, params, stats, mode) File "/home/yq/work/face_class/diracnets/diracnet.py", line 182, in f o = F.linear(o.view(o.size(0), -1), params['fc.weight'], params['fc.bias']) File "/usr/local/lib/python2.7/dist-packages/torch/nn/functional.py", line 449, in linear return state(input, weight) if bias is None else state(input, weight, bias) File "/usr/local/lib/python2.7/dist-packages/torch/nn/functions/linear.py", line 10, in forward output.addmm(0, 1, input, weight.t()) RuntimeError: cublas runtime error : library not initialized at /b/wheel/pytorch-src/torch/lib/THC/THCGeneral.c:394

    opened by eeric 2
A very tiny, very simple, and very secure file encryption tool.

Picocrypt is a very tiny (hence "Pico"), very simple, yet very secure file encryption tool. It uses the modern ChaCha20-Poly1305 cipher suite as well

Evan Su 1k Dec 30, 2022
Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! Very tiny! Stock Market Financial Technical Analysis Python library . Quant Trading automation or cryptocoin exchange

MyTT Technical Indicators implemented in Python only using Numpy-Pandas as Magic - Very Very Fast! to Stock Market Financial Technical Analysis Python

dev 34 Dec 27, 2022
SkipGNN: Predicting Molecular Interactions with Skip-Graph Networks (Scientific Reports)

SkipGNN: Predicting Molecular Interactions with Skip-Graph Networks Molecular interaction networks are powerful resources for the discovery. While dee

Kexin Huang 49 Oct 15, 2022
Channel Pruning for Accelerating Very Deep Neural Networks (ICCV'17)

Channel Pruning for Accelerating Very Deep Neural Networks (ICCV'17)

Yihui He 1k Jan 3, 2023
[CVPRW 21] "BNN - BN = ? Training Binary Neural Networks without Batch Normalization", Tianlong Chen, Zhenyu Zhang, Xu Ouyang, Zechun Liu, Zhiqiang Shen, Zhangyang Wang

BNN - BN = ? Training Binary Neural Networks without Batch Normalization Codes for this paper BNN - BN = ? Training Binary Neural Networks without Bat

VITA 40 Dec 30, 2022
[ICCV 2021 Oral] SnowflakeNet: Point Cloud Completion by Snowflake Point Deconvolution with Skip-Transformer

This repository contains the source code for the paper SnowflakeNet: Point Cloud Completion by Snowflake Point Deconvolution with Skip-Transformer (ICCV 2021 Oral). The project page is here.

AllenXiang 65 Dec 26, 2022
StackRec: Efficient Training of Very Deep Sequential Recommender Models by Iterative Stacking

StackRec: Efficient Training of Very Deep Sequential Recommender Models by Iterative Stacking Datasets You can download datasets that have been pre-pr

null 25 May 29, 2022
Learning recognition/segmentation models without end-to-end training. 40%-60% less GPU memory footprint. Same training time. Better performance.

InfoPro-Pytorch The Information Propagation algorithm for training deep networks with local supervision. (ICLR 2021) Revisiting Locally Supervised Lea

null 78 Dec 27, 2022
PyTorch code for our ECCV 2018 paper "Image Super-Resolution Using Very Deep Residual Channel Attention Networks"

PyTorch code for our ECCV 2018 paper "Image Super-Resolution Using Very Deep Residual Channel Attention Networks"

Yulun Zhang 1.2k Dec 26, 2022
Image Super-Resolution Using Very Deep Residual Channel Attention Networks

Image Super-Resolution Using Very Deep Residual Channel Attention Networks

kongdebug 14 Oct 14, 2022
Code for our paper at ECCV 2020: Post-Training Piecewise Linear Quantization for Deep Neural Networks

PWLQ Updates 2020/07/16 - We are working on getting permission from our institution to release our source code. We will release it once we are granted

null 54 Dec 15, 2022
Selene is a Python library and command line interface for training deep neural networks from biological sequence data such as genomes.

Selene is a Python library and command line interface for training deep neural networks from biological sequence data such as genomes.

Troyanskaya Laboratory 323 Jan 1, 2023
Implement of "Training deep neural networks via direct loss minimization" in PyTorch for 0-1 loss

This is the implementation of "Training deep neural networks via direct loss minimization" published at ICML 2016 in PyTorch. The implementation targe

Cuong Nguyen 1 Jan 18, 2022
Image restoration with neural networks but without learning.

Warning! The optimization may not converge on some GPUs. We've personally experienced issues on Tesla V100 and P40 GPUs. When running the code, make s

Dmitry Ulyanov 7.4k Jan 1, 2023
Plenoxels: Radiance Fields without Neural Networks

Plenoxels: Radiance Fields without Neural Networks Alex Yu*, Sara Fridovich-Keil*, Matthew Tancik, Qinhong Chen, Benjamin Recht, Angjoo Kanazawa UC Be

Sara Fridovich-Keil 81 Dec 25, 2022
Complex-Valued Neural Networks (CVNN)Complex-Valued Neural Networks (CVNN)

Complex-Valued Neural Networks (CVNN) Done by @NEGU93 - J. Agustin Barrachina Using this library, the only difference with a Tensorflow code is that y

youceF 1 Nov 12, 2021
A framework that constructs deep neural networks, autoencoders, logistic regressors, and linear networks

A framework that constructs deep neural networks, autoencoders, logistic regressors, and linear networks without the use of any outside machine learning libraries - all from scratch.

Kordel K. France 2 Nov 14, 2022
Flexible-CLmser: Regularized Feedback Connections for Biomedical Image Segmentation

Flexible-CLmser: Regularized Feedback Connections for Biomedical Image Segmentation The skip connections in U-Net pass features from the levels of enc

Boheng Cao 1 Dec 29, 2021