DenseNet Implementation in Keras with ImageNet Pretrained Models

Overview

DenseNet-Keras with ImageNet Pretrained Models

This is an Keras implementation of DenseNet with ImageNet pretrained weights. The weights are converted from Caffe Models. The implementation supports both Theano and TensorFlow backends.

To know more about how DenseNet works, please refer to the original paper

Densely Connected Convolutional Networks
Gao Huang, Zhuang Liu, Kilian Q. Weinberger, Laurens van der Maaten
arXiv:1608.06993

Pretrained DenseNet Models on ImageNet

The top-1/5 accuracy rates by using single center crop (crop size: 224x224, image size: 256xN)

Network Top-1 Top-5 Theano Tensorflow
DenseNet 121 (k=32) 74.91 92.19 model (32 MB) model (32 MB)
DenseNet 169 (k=32) 76.09 93.14 model (56 MB) model (56 MB)
DenseNet 161 (k=48) 77.64 93.79 model (112 MB) model (112 MB)

Usage

First, download the above pretrained weights to the imagenet_models folder.

Run test_inference.py for an example of how to use the pretrained model to make inference.

python test_inference.py

Fine-tuning

Check this out to see example of fine-tuning DenseNet with your own dataset.

Requirements

  • Keras 1.2.2 2.0.5
  • Theano 0.8.2 or TensorFlow 0.12.0 1.2.1

Updates

  • Keras 2.0.5 and TensorFlow 1.2.1 are supported
Comments
  • AttributeError: 'Tensor' object has no attribute 'assign'

    AttributeError: 'Tensor' object has no attribute 'assign'

    Hi,

    Thanks a lot for the repo, but I hit the following error when trying the python test_inference.py with the densenet169:

    Traceback (most recent call last):
      File "test_inference.py", line 27, in <module>
        model = DenseNet(reduction=0.5, classes=1000, weights_path=weights_path)
      File "densenet169.py", line 78, in DenseNet
        model.load_weights(weights_path, by_name=True)
      File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2498, in load_weights
        load_weights_from_hdf5_group_by_name(f, self.layers)
      File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/engine/topology.py", line 2976, in load_weights_from_hdf5_group_by_name
        K.batch_set_value(weight_value_tuples)
      File "/home/petteri/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2025, in batch_set_value
        assign_op = x.assign(assign_placeholder)
    AttributeError: 'Tensor' object has no attribute 'assign'
    

    So when printing the types of the variables in the saved weights I got the following:

    <tf.Variable 'conv1/kernel:0' shape=(7, 7, 3, 64) dtype=float32_ref>
    <tf.Variable 'conv1_bn/gamma:0' shape=(64,) dtype=float32_ref>
    <tf.Variable 'conv1_bn/beta:0' shape=(64,) dtype=float32_ref>
    <tf.Variable 'conv1_bn/moving_mean:0' shape=(64,) dtype=float32_ref>
    <tf.Variable 'conv1_bn/moving_variance:0' shape=(64,) dtype=float32_ref>
    Tensor("conv1_scale/Const:0", shape=(64,), dtype=float32)
    

    And the batch_set_value(tuples) from the backend hit the Tensor problem with "conv1_scale", is there a fix for this to allow the assigning or are the weights somewhat weirdly saved?:

                if hasattr(x, '_assign_placeholder'):
                    assign_placeholder = x._assign_placeholder
                    assign_op = x._assign_op
                else:
                    assign_placeholder = tf.placeholder(tf_dtype, shape=value.shape)
                    assign_op = x.assign(assign_placeholder)
                    x._assign_placeholder = assign_placeholder
                    x._assign_op = assign_op
                assign_ops.append(assign_op)
                feed_dict[assign_placeholder] = value
    
    opened by petteriTeikari 11
  • TypeError: 'module' object is not callable

    TypeError: 'module' object is not callable

    Version

    DenseNet-Keras git:(master) ipython
    Python 2.7.12 (default, Dec  4 2017, 14:50:18)
    Type "copyright", "credits" or "license" for more information.
    
    IPython 5.5.0 -- An enhanced Interactive Python.
    ?         -> Introduction and overview of IPython's features.
    %quickref -> Quick reference.
    help      -> Python's own help system.
    object?   -> Details about 'object', use 'object??' for extra details.
    
    In [1]: import cv2
    
    In [2]: import tensorflow as tf
    
    In [3]: cv2.__version__
    Out[3]: '2.4.13.6'
    
    In [4]: tf.__version__
    Out[4]: '1.4.0'
    
    In [5]: import keras
    Using TensorFlow backend.
    
    In [6]: keras.__version__
    Out[6]: '2.2.2'
    
    DenseNet-Keras git:(master) ls -hl imagenet_models
    total 33M
    drwxrwxr-x 2 epbox epbox 4.0K 8月  30 15:08 .
    drwxrwxr-x 5 epbox epbox 4.0K 8月  30 15:09 ..
    -rw-r--r-- 1 epbox epbox  32M 8月  30 15:07 densenet121_weights_tf.h5
    -rw-rw-r-- 1 epbox epbox   55 8月  30 14:40 README.md
    

    Error

    DenseNet-Keras git:(master) python test_inference.py
    Using TensorFlow backend.
    /home/epbox/Github/DenseNet-Keras/densenet121.py:45: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(64, (7, 7), strides=(2, 2), use_bias=False, name="conv1")`
      x = Convolution2D(nb_filter, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x)
    2018-09-10 14:25:19.720132: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
    2018-09-10 14:25:19.821988: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
    2018-09-10 14:25:19.822306: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
    name: GeForce GTX TITAN X major: 5 minor: 2 memoryClockRate(GHz): 1.076
    pciBusID: 0000:01:00.0
    totalMemory: 11.92GiB freeMemory: 11.64GiB
    2018-09-10 14:25:19.822322: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX TITAN X, pci bus id: 0000:01:00.0, compute capability: 5.2)
    /home/epbox/Github/DenseNet-Keras/densenet121.py:99: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(128, (1, 1), use_bias=False, name="conv2_1_x1")`
      x = Convolution2D(inter_channel, 1, 1, name=conv_name_base+'_x1', bias=False)(x)
    /home/epbox/Github/DenseNet-Keras/densenet121.py:109: UserWarning: Update your `Conv2D` call to the Keras 2 API: `Conv2D(32, (3, 3), use_bias=False, name="conv2_1_x2")`
      x = Convolution2D(nb_filter, 3, 3, name=conv_name_base+'_x2', bias=False)(x)
    Traceback (most recent call last):
      File "test_inference.py", line 34, in <module>
        model = DenseNet(reduction=0.5, classes=1000, weights_path=weights_path)
      File "/home/epbox/Github/DenseNet-Keras/densenet121.py", line 55, in DenseNet
        x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)
      File "/home/epbox/Github/DenseNet-Keras/densenet121.py", line 165, in dense_block
        concat_feat = merge([concat_feat, x], mode='concat', concat_axis=concat_axis, name='concat_'+str(stage)+'_'+str(branch))
    TypeError: 'module' object is not callable
    
    opened by Digital2Slave 9
  • Scale layer vs BatchNormalization in Keras v2

    Scale layer vs BatchNormalization in Keras v2

    Hi,

    At first, thanks for this repo with DenseNet for Keras. As I understand you ported the architecture and weights from Caffe. I've just a question on actual purpose of your custom layer Scale after BatchNormalization in Keras v2 in the sense don't they perform the same work ?

    opened by vfdev-5 4
  • RGB or BGR?

    RGB or BGR?

    Hi, this is a nice work. And I have some questions about how to use the model.

    What tool is used convert the caffe model? And what order should be used for testing, RGB or BGR. Thanks.

    opened by cswhjiang 2
  • Pretrained model

    Pretrained model

    Hello, thanks for your great work. I need your pretrained model for my own experiment. I'll appreciate it if you can give me the access. My google mail address: [email protected]

    opened by JingjunYi 0
  • Updated for newer version of tensorflow and keras

    Updated for newer version of tensorflow and keras

    tested on tensorflow 1.13.1 - 2.0.0 and keras 2.2.4 - 2.3.0 as Merge layer has been removed and concatenate has been separated and some other changes in parameters such as axis and set_image_dim has been replaced

    opened by srg9000 0
  • Updated for newer version of tensorflow and keras

    Updated for newer version of tensorflow and keras

    tested on tensorflow 1.13.1 - 2.0.0 and keras 2.2.4 - 2.3.0 as Merge layer has been removed and concatenate has been separated and some other changes in parameters such as axis and set_image_dim has been replaced

    opened by srg9000 0
  • Updated for newer version of tensorflow and keras

    Updated for newer version of tensorflow and keras

    tested on tensorflow 1.13.1 - 2.0.0 and keras 2.2.4 - 2.3.0 as Merge layer has been removed and concatenate is now separate and image_dim_ordering has been changed.

    opened by srg9000 0
  • DocUNet

    DocUNet

    Hi, I notice your project at https://github.com/shahsohil/sunets/issues/8. Have you reproduced the paper<DocUNet: Document Image Unwarping via A Stacked U-Net>? I trained the S-UNet for this paper some times,but it doesn't work. Can you give me some suggestions?

    opened by lhb891019 0
Owner
Felix Yu
Felix Yu
(ImageNet pretrained models) The official pytorch implemention of the TPAMI paper "Res2Net: A New Multi-scale Backbone Architecture"

Res2Net The official pytorch implemention of the paper "Res2Net: A New Multi-scale Backbone Architecture" Our paper is accepted by IEEE Transactions o

Res2Net Applications 928 Dec 29, 2022
A PyTorch implementation of DenseNet.

A PyTorch Implementation of DenseNet This is a PyTorch implementation of the DenseNet-BC architecture as described in the paper Densely Connected Conv

Brandon Amos 771 Dec 15, 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
Facebook Research 605 Jan 2, 2023
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
Attack classification models with transferability, black-box attack; unrestricted adversarial attacks on imagenet

Attack classification models with transferability, black-box attack; unrestricted adversarial attacks on imagenet, CVPR2021 安全AI挑战者计划第六期:ImageNet无限制对抗攻击 决赛第四名(team name: Advers)

null 51 Dec 1, 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
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
Reference implementation of code generation projects from Facebook AI Research. General toolkit to apply machine learning to code, from dataset creation to model training and evaluation. Comes with pretrained models.

This repository is a toolkit to do machine learning for programming languages. It implements tokenization, dataset preprocessing, model training and m

Facebook Research 408 Jan 1, 2023
Implementation of Squeezenet in pytorch, pretrained models on Cifar 10 data to come

Pytorch Squeeznet Pytorch implementation of Squeezenet model as described in https://arxiv.org/abs/1602.07360 on cifar-10 Data. The definition of Sque

gaurav pathak 86 Oct 28, 2022
A PyTorch re-implementation of the paper 'Exploring Simple Siamese Representation Learning'. Reproduced the 67.8% Top1 Acc on ImageNet.

Exploring simple siamese representation learning This is a PyTorch re-implementation of the SimSiam paper on ImageNet dataset. The results match that

Taojiannan Yang 72 Nov 9, 2022
Pytorch implementation of "Training a 85.4% Top-1 Accuracy Vision Transformer with 56M Parameters on ImageNet"

Token Labeling: Training an 85.4% Top-1 Accuracy Vision Transformer with 56M Parameters on ImageNet (arxiv) This is a Pytorch implementation of our te

蒋子航 383 Dec 27, 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
PyTorch implementation of PNASNet-5 on ImageNet

PNASNet.pytorch PyTorch implementation of PNASNet-5. Specifically, PyTorch code from this repository is adapted to completely match both my implemetat

Chenxi Liu 314 Nov 25, 2022
PyTorch implementation of CVPR 2020 paper (Reference-Based Sketch Image Colorization using Augmented-Self Reference and Dense Semantic Correspondence) and pre-trained model on ImageNet dataset

Reference-Based-Sketch-Image-Colorization-ImageNet This is a PyTorch implementation of CVPR 2020 paper (Reference-Based Sketch Image Colorization usin

Yuzhi ZHAO 11 Jul 28, 2022
Official PyTorch implementation of N-ImageNet: Towards Robust, Fine-Grained Object Recognition with Event Cameras (ICCV 2021)

N-ImageNet: Towards Robust, Fine-Grained Object Recognition with Event Cameras Official PyTorch implementation of N-ImageNet: Towards Robust, Fine-Gra

null 32 Dec 26, 2022
Repository providing a wide range of self-supervised pretrained models for computer vision tasks.

Hierarchical Pretraining: Research Repository This is a research repository for reproducing the results from the project "Self-supervised pretraining

Colorado Reed 53 Nov 9, 2022
Pretrained Pytorch face detection (MTCNN) and recognition (InceptionResnet) models

Face Recognition Using Pytorch Python 3.7 3.6 3.5 Status This is a repository for Inception Resnet (V1) models in pytorch, pretrained on VGGFace2 and

Tim Esler 3.3k Jan 4, 2023