Keras implementation of Normalizer-Free Networks and SGD - Adaptive Gradient Clipping

Overview

Keras implementation of Normalizer-Free Networks and SGD - Adaptive Gradient Clipping

Paper: https://arxiv.org/abs/2102.06171.pdf

Original code: https://github.com/deepmind/deepmind-research/tree/master/nfnets

Do star this repository if it helps your work!

Note: Huge Credit to this comment for the pytorch implementation this repository is based on. Note: See this comment for a generic implementation for any optimizer as a temporary reference for anyone who needs it.

Installation

Install from PyPi:

pip3 install nfnets-keras

or install the latest code using:

pip3 install git+https://github.com/ypeleg/nfnets-keras

Usage

NFNetF Model

Use any of the NFNetF models like any other keras Model!

from nfnets_keras import NFNetF3

model = NFNetF3(include_top = True, num_classes = 10)
model.compile('adam', 'categorical_crossentropy')
model.fit(X, y)

WSConv2D

Use WSConv2D like any other keras.layer.

from nfnets_keras import WSConv2D

conv = Conv2D(16, 3)(l)
w_conv = WSConv2D(16, 3)(conv)

SGD_AGC - Adaptive Gradient Clipping

Similarly, use SGD_AGC like keras.optimizer.SGD

from nfnets_keras import SGD_AGC


model.compile( SGD_AGC(lr=1e-3), loss='categorical_crossentropy' )

TODO

  • WSConv2D
  • SGD - Adaptive Gradient Clipping
  • Function to automatically replace Convolutions in any module with WSConv2d
  • Documentation
  • NFNets
  • NF-ResNets

Credit for the original pytroch implementation

https://github.com/vballoli/nfnets-pytorch

Cite Original Work

To cite the original paper, use:

@article{brock2021high,
  author={Andrew Brock and Soham De and Samuel L. Smith and Karen Simonyan},
  title={High-Performance Large-Scale Image Recognition Without Normalization},
  journal={arXiv preprint arXiv:},
  year={2021}
}
Comments
  • AttributeError: 'NFNet' object has no attribute 'variant'

    AttributeError: 'NFNet' object has no attribute 'variant'

    Hi. I try to use nfnets, but I get the following error.

    Error

    AttributeError: 'NFNet' object has no attribute 'variant'
    

    environment

    • Ubuntu : 18.04
    • Tensorflow : version 2.3 and 2.4 both give the same error.
    • Keras : 2.4.3
    • NFNets : 0.0.2

    code

    from nfnets_keras import NFNetF3, SGD_AGC
    
    model = NFNetF3(include_top=True, num_classes=10)
    model.compile( SGD_AGC(lr=1e-3), loss='categorical_crossentropy' )
    model.summary()    
    

    How can i solve this Error?

    opened by pervin0527 9
  • Issues with initializing `StochasticDepth` Layer

    Issues with initializing `StochasticDepth` Layer

    Hi!

    I am trying to have a look at the TensorFlow implementation of NFNet, and somehow got to fix the reference-before-assignment issue with some help from @prateekkrjain.

    However, when I try to instantiate NFNetF0 as follows: x = NFNetF0(num_classes = num_classes)(x)

    the Python interpreter complains that it has an error as follows:

    TypeError: in user code:
    
        C:\Users\<myUserName>\NFNet\nfnets_keras\nfnet.py:143 call  *
            for i, block in enumerate(self.blocks): out, res_avg_var = block(out, training = training)
        C:\Users\<myUserName>\NFNet\nfnets_keras\nfnet.py:200 call  *
            out = self.stoch_depth(out, training)
        C:\Users\<myUserName>\NFNet\nfnets_keras\nfnet_layers.py:47 call  *
            r = tf.random.uniform(shape = [batch_size, 1, 1, 1], dtype = x.dtype)
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\util\dispatch.py:201 wrapper  **
            return target(*args, **kwargs)
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\ops\random_ops.py:289 random_uniform
            shape = tensor_util.shape_tensor(shape)
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\framework\tensor_util.py:1035 shape_tensor
            return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\profiler\trace.py:163 wrapped
            return func(*args, **kwargs)
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\framework\ops.py:1540 convert_to_tensor
            ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\framework\constant_op.py:339 _constant_tensor_conversion_function
            return constant(v, dtype=dtype, name=name)
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\framework\constant_op.py:264 constant
            return _constant_impl(value, dtype, shape, name, verify_shape=False,
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\framework\constant_op.py:281 _constant_impl
            tensor_util.make_tensor_proto(
        C:\Users\<myUserName>\miniconda3\lib\site-packages\tensorflow\python\framework\tensor_util.py:551 make_tensor_proto
            raise TypeError("Failed to convert object of type %s to Tensor. "
    
        TypeError: Failed to convert object of type <class 'tuple'> to Tensor. Contents: (None, 1, 1, 1). Consider casting elements to a supported type.
    

    It seems like there is something wrong with the arguments being passed to tf.random.uniform() function when initializing StochasticDepth Layer. The batch_size turns out to be None.

    Any ideas on how to fix this?

    Note that I am using the latest version of Miniconda with Python 3.8, along with TensorFlow 2.4.1 on a Windows 10.

    opened by joonjeon 2
  • 'NFNet' object has no attribute 'variant'

    'NFNet' object has no attribute 'variant'

    Hello Sir,

    I am trying to use NFNET model, but I am encountering, the aforementioned error. Code:

    image

    Error:

    image

    AttributeError: 'NFNet' object has no attribute 'variant'

    I believe The problem is in : https://github.com/ypeleg/nfnets-keras/blob/master/nfnets_keras/nfnet.py The above said line (line :: 89 block_params = self.variant_dict[self.variant]) should be after line 103(self.group_pattern = block_params.get('group_width', [128] * 4)). So the self.variant used in line 89 will be declared before it is used.

    Thank you!

    opened by sourabhyadav999 6
  • How to use SGD_AGC class ??

    How to use SGD_AGC class ??

    Hell Sir.

    I tried your code but I met some error.

    This is my code.

        optimizer = SGD_AGC(lr=0.01)
    

    Error :

       TypeError: __init__() missing 1 required positional argument: 'params'
    

    So I checked your code.

    class SGD_AGC(Optimizer):
        ...
        def __init__(self, params, lr, ....):
        ...
    

    I think that this is the style of optimizer of pytorch. How to use SGD_AGC class in keras.

    Thanks..

    opened by edwardcho 1
  • SGD_AGC Error

    SGD_AGC Error

    thanks for the implementation of nfnets in keras!

    I want to run it using optimizer(SGD_AGC). But there's an error like this.

    TypeError: Can't instantiate abstract class SGD_AGC with abstract methods get_config
    
    opened by minji4928 1
Owner
Yam Peleg
Yam Peleg
A PyTorch implementation of Learning to learn by gradient descent by gradient descent

Intro PyTorch implementation of Learning to learn by gradient descent by gradient descent. Run python main.py TODO Initial implementation Toy data LST

Ilya Kostrikov 300 Dec 11, 2022
auto-tuning momentum SGD optimizer

YellowFin YellowFin is an auto-tuning optimizer based on momentum SGD which requires no manual specification of learning rate and momentum. It measure

Jian Zhang 288 Nov 19, 2022
Implements pytorch code for the Accelerated SGD algorithm.

AccSGD This is the code associated with Accelerated SGD algorithm used in the paper On the insufficiency of existing momentum schemes for Stochastic O

null 205 Jan 2, 2023
A Momentumized, Adaptive, Dual Averaged Gradient Method for Stochastic Optimization

MADGRAD Optimization Method A Momentumized, Adaptive, Dual Averaged Gradient Method for Stochastic Optimization pip install madgrad Try it out! A best

Meta Research 774 Dec 31, 2022
This is an implementation of Googles Yogi-Optimizer in Keras (tf.keras)

Yogi-Optimizer_Keras This is an implementation of Googles Yogi-Optimizer in Keras (tf.keras) The NeurIPS-Paper can be found here: http://papers.nips.c

null 14 Sep 13, 2022
Keras udrl - Keras implementation of Upside Down Reinforcement Learning

keras_udrl Keras implementation of Upside Down Reinforcement Learning This is me

Eder Santana 7 Jan 24, 2022
Classification models 1D Zoo - Keras and TF.Keras

Classification models 1D Zoo - Keras and TF.Keras This repository contains 1D variants of popular CNN models for classification like ResNets, DenseNet

Roman Solovyev 12 Jan 6, 2023
🎯 A comprehensive gradient-free optimization framework written in Python

Solid is a Python framework for gradient-free optimization. It contains basic versions of many of the most common optimization algorithms that do not

Devin Soni 565 Dec 26, 2022
Gradient-free global optimization algorithm for multidimensional functions based on the low rank tensor train format

ttopt Description Gradient-free global optimization algorithm for multidimensional functions based on the low rank tensor train (TT) format and maximu

null 5 May 23, 2022
Example-custom-ml-block-keras - Custom Keras ML block example for Edge Impulse

Custom Keras ML block example for Edge Impulse This repository is an example on

Edge Impulse 8 Nov 2, 2022
PyTorch implementation of Spiking Neural Networks trained on surrogate gradient & BPTT using snntorch.

snn-localization repo PyTorch implementation of Spiking Neural Networks trained on surrogate gradient & BPTT using snntorch. Install Dependencies Orig

Sami BARCHID 1 Jan 6, 2022
PyTorch implementation for Convolutional Networks with Adaptive Inference Graphs

Convolutional Networks with Adaptive Inference Graphs (ConvNet-AIG) This repository contains a PyTorch implementation of the paper Convolutional Netwo

Andreas Veit 176 Dec 7, 2022
MEND: Model Editing Networks using Gradient Decomposition

MEND: Model Editing Networks using Gradient Decomposition Setup Environment This codebase uses Python 3.7.9. Other versions may work as well. Create a

Eric Mitchell 141 Dec 2, 2022
Image-to-Image Translation with Conditional Adversarial Networks (Pix2pix) implementation in keras

pix2pix-keras Pix2pix implementation in keras. Original paper: Image-to-Image Translation with Conditional Adversarial Networks (pix2pix) Paper Author

William Falcon 141 Dec 30, 2022
[IJCAI-2021] A benchmark of data-free knowledge distillation from paper "Contrastive Model Inversion for Data-Free Knowledge Distillation"

DataFree A benchmark of data-free knowledge distillation from paper "Contrastive Model Inversion for Data-Free Knowledge Distillation" Authors: Gongfa

ZJU-VIPA 47 Jan 9, 2023
FuseDream: Training-Free Text-to-Image Generationwith Improved CLIP+GAN Space OptimizationFuseDream: Training-Free Text-to-Image Generationwith Improved CLIP+GAN Space Optimization

FuseDream This repo contains code for our paper (paper link): FuseDream: Training-Free Text-to-Image Generation with Improved CLIP+GAN Space Optimizat

XCL 191 Dec 31, 2022
Free-duolingo-plus - Duolingo account creator that uses your invite code to get you free duolingo plus

free-duolingo-plus duolingo account creator that uses your invite code to get yo

null 1 Jan 6, 2022
Spatially-Adaptive Pixelwise Networks for Fast Image Translation, CVPR 2021

Image Translation with ASAPNets Spatially-Adaptive Pixelwise Networks for Fast Image Translation, CVPR 2021 Webpage | Paper | Video Installation insta

Tamar Rott Shaham 100 Dec 28, 2022
Two-Stream Adaptive Graph Convolutional Networks for Skeleton-Based Action Recognition in CVPR19

2s-AGCN Two-Stream Adaptive Graph Convolutional Networks for Skeleton-Based Action Recognition in CVPR19 Note PyTorch version should be 0.3! For PyTor

LShi 547 Dec 26, 2022