GithubHelp home page GithubHelp logo

qubvel / classification_models Goto Github PK

View Code? Open in Web Editor NEW
1.2K 34.0 305.0 4.11 MB

Classification models trained on ImageNet. Keras.

License: MIT License

Python 100.00%
resnet pretrained-models pretrained-weights keras imagenet imagenet-classifier classification-model resnext senet squeeze-and-excitation

classification_models's Introduction

qubvel

linux docker python keras tensorflow pytorch

 qubvel

classification_models's People

Contributors

chawater avatar fernandocamargoai avatar gan3sh500 avatar qubvel 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

classification_models's Issues

Edit model fine tuning example

Hey, I suggest that you change the model fine tuning example in two points:

  1. Change AveragePooling2D to GlobalAveragePooling2D
  2. Add a softmax layer. I was so retarded to try to optimize a model without for two hours.
    Thanks :D

Unable to finetune: not a Checkpointable

Thanks for the great repo!

Now I am having problem when I want to finetune with ResNet50 model and weight.

Traceback (most recent call last):
  File "classifier.py", line 56, in <module>
    model = keras.models.Model(inputs=[base_model.input], outputs=output)
  File "/home/zhangchutong/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 121, in __init__
    super(Model, self).__init__(*args, **kwargs)
  File "/home/zhangchutong/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 80, in __init__
    self._init_graph_network(*args, **kwargs)
  File "/home/zhangchutong/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 474, in _method_wrapper
    method(self, *args, **kwargs)
  File "/home/zhangchutong/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 274, in _init_graph_network
    self._track_layers(layers)
  File "/home/zhangchutong/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 370, in _track_layers
    layer, name='layer-%d' % layer_index, overwrite=True)
  File "/home/zhangchutong/anaconda3/lib/python3.6/site-packages/tensorflow/python/training/checkpointable/base.py", line 720, in _track_checkpointable
    "Checkpointable.") % (type(checkpointable),))

TypeError: Checkpointable._track_checkpointable() passed type <class 'keras.engine.input_layer.InputLayer'>, not a Checkpointable.

I searched on the internet, some people said it might be the incompatible between tensorflow and keras but I only import keras in my code. So confusing...

python version: 3.6
tensorflow version: 1.12.0
keras version: 2.2.4

And here is my code:

import keras
from skimage.io import imread
from skimage.transform import resize
from classification_models.keras import Classifiers
from util.transform import *
from keras.layers import Flatten, Dense, Dropout, BatchNormalization

checkpoint_path = 'resnet50_classifier'
ResNet50, preprocess_input = Classifiers.get('resnet50')

# some data preprocessing .....

base_model = ResNet50(input_shape=(224,224,3), weights='imagenet', include_top=False)
x = Flatten()(base_model.output)
x = Dense(4096, activation='relu')(x)
x = Dropout(0.5)(x)
x = BatchNormalization()(x)
output = Dense(n_classes, activation='softmax')(x)
model = keras.models.Model(inputs=[base_model.input], outputs=output)

cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
                                                 save_weights_only=True, 
                                                 verbose=1)
model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit_generator(generate_arrays_from_file(train_file, batch_size=batch_size),
            samples_per_epoch=(train_num/batch_size) *batch_size, nb_epoch=10, max_q_size=100, verbose=1, nb_worker=1, callbacks=[cp_callback])

Any help is appreciated!

SENet models aren't calling get_source_inputs()

Before the pretrained model, I'm setting some layers and I'm passing that last of these layers' output as input_tensor. It works fine with every model, except the SENet ones. After further investigation, I noticed that it's not calling get_source_inputs() like the others.

Channels first format not working

I was trying to use the senet154 model with channels first encoding, but getting an error "Cannot assign to variable conv2d/kernel:0 due to variable shape (3, 3, 224, 64) and value shape (64, 3, 3, 3) are incompatible". Has anyone faced this issue before?

Resnet34 with pretrained weights on TensorFlow 2

Hi,

Thanks for your work. I would like to train a resnet34 model starting with pretrained imagenet weights. This repo works fine on TF1, but not on TF2. Has this code been ported to TensorFlow 2?

Thanks.

Shufflenet

Thank you for the amazing lib. would it be possible to add shufflenet to it?

Instantiating densenet fails

Constructing an instance of any of the densenets via the Classifiers.get() method fails when using include_top = False option.

Example:

from classification_models import Classifiers

# works fine with 'mobilenet':
model =  Classifiers.get('mobilenet')[0]((224, 224, 3), weights='imagenet', include_top = False) 

# Crashes with 'densenet121':
model =  Classifiers.get('densenet121')[0]((224, 224, 3), weights='imagenet', include_top = False) 

TypeError: DenseNet121() got multiple values for argument 'include_top'

My Keras version is 2.2.4 and classification_models version 0.2.0.

Thanks for the great library!

Tensorflow 2.0

Does this work with tensorflow 2.0 keras, because I am trying to use within tf2.0 but failed.

the resnet implementation is different from tensorflow github

Hi, I compare your work with the official implementation of Resnet by tensorflow in https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/keras/applications/resnet.py. There are some notable differences between your work and tensorflow. For example, the downsampling implementation and the batch norm in the input layer. So here is my question, which version of Resnet is more effective? Hope you can help me with this. Thank you.

Resnet models

It looks that your own RestNet implementation is based on Resnet Version 2 (Preact / BN->Relu->Conv). Could you please also consider adding ResNet Version 1 (code + pre-trained weights)? I'm unfortunately limited on GPU resources and can't therefore do a full train in imagenet, etc. I know that keras-applications has both V1/V2, but I find that your toolbox is better organized.

[Solve]Encounter ValueError: Shapes (512,) and (3,) are incompatible while loading pre-trained weight

tensorflow version: 1.12.3

Hello, thank you for sharing your code.

The thing is, I encounter some error when I try to load pre-trained weight.

I use resnet34 backbone.

Model worked well before I loaded weight from your WEIGHTS_COLLECTION.

The following is the code. It can run directly.

import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras import utils as keras_utils

img_input = layers.Input(shape=(512, 512, 3), name='data')
x = layers.BatchNormalization(name='bn_data', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': False})(img_input)
x = layers.ZeroPadding2D(padding=(3,3))(x)
x = layers.Conv2D(64, (7,7), strides=(2,2), name='conv0', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)
x = layers.BatchNormalization(name='bn0', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
x = layers.Activation('relu', name='relu0')(x)
x = layers.ZeroPadding2D(padding=(1,1))(x)
x = layers.MaxPooling2D((3,3), strides=(2,2), padding='valid', name='pooling0')(x)

if True:
#(stage, rep) = [(0, 3), (1, 4), (2, 6), (3, 3)]
# stage = 0, block = 0
#x = ResidualBlock(64, 0, 0, strides=(1,1), cut='post', attention=Attention)(x)
    shortcut = layers.Conv2D(64, (1,1), name='stage1_unit1_sc', strides=(1,1), **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)
    x = layers.BatchNormalization(name='stage1_unit1_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage1_unit1_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(64, (3,3), strides=(1,1), name='stage1_unit1_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage1_unit1_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage1_unit1_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(64, (3,3), name='stage1_unit1_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 0, block = 1
#x = ResidualBlock(64, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage1_unit2_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage1_unit2_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(64, (3,3), strides=(1,1), name='stage1_unit2_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage1_unit2_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage1_unit2_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(64, (3,3), name='stage1_unit2_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 0, block = 2
#x = ResidualBlock(64, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage1_unit3_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage1_unit3_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(64, (3,3), strides=(1,1), name='stage1_unit3_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage1_unit3_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage1_unit3_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(64, (3,3), name='stage1_unit3_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 1, block = 0
# x = ResidualBlock(128, stage, block, strides=(2, 2), cut='post', attention=Attention)(x)
    shortcut = layers.Conv2D(128, (1,1), name='stage2_unit1_sc', strides=(2,2), **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)
    x = layers.BatchNormalization(name='stage2_unit1_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit1_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), strides=(2,2), name='stage2_unit1_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage2_unit1_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit1_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), name='stage2_unit1_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])
# stage = 1, block = 1
# x = ResidualBlock(128, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage2_unit2_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit2_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), strides=(1,1), name='stage2_unit2_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage2_unit2_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit2_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), name='stage2_unit2_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 1, block = 2
# x = ResidualBlock(128, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage2_unit3_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit3_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), strides=(1,1), name='stage2_unit3_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage2_unit3_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit3_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), name='stage2_unit3_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 1, block = 3
# x = ResidualBlock(128, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage2_unit4_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit4_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), strides=(1,1), name='stage2_unit4_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage2_unit4_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage2_unit4_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(128, (3,3), name='stage2_unit4_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 0
# x = ResidualBlock(256, stage, block, strides=(2, 2), cut='post', attention=Attention)(x)
    shortcut = layers.Conv2D(256, (1,1), name='stage3_unit1_sc', strides=(2,2), **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)
    x = layers.BatchNormalization(name='stage3_unit1_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit1_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), strides=(2,2), name='stage3_unit1_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage3_unit1_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit1_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), name='stage3_unit1_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 1
# x = ResidualBlock(256, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage3_unit2_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit2_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), strides=(1,1), name='stage3_unit2_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage3_unit2_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit2_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), name='stage3_unit2_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 2
# x = ResidualBlock(256, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage3_unit3_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit3_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), strides=(1,1), name='stage3_unit3_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage3_unit3_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit3_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), name='stage3_unit3_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 3
# x = ResidualBlock(256, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage3_unit4_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit4_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), strides=(1,1), name='stage3_unit4_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage3_unit4_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit4_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), name='stage3_unit4_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 4
# x = ResidualBlock(256, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage3_unit5_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit5_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), strides=(1,1), name='stage3_unit5_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage3_unit5_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit5_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), name='stage3_unit5_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 5
# x = ResidualBlock(256, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage3_unit6_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit6_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), strides=(1,1), name='stage3_unit6_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage3_unit6_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage3_unit6_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(256, (3,3), name='stage3_unit6_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 3, block = 0
# x = ResidualBlock(512, stage, block, strides=(2, 2), cut='post', attention=Attention)(x)
    shortcut = layers.Conv2D(512, (1,1), name='stage4_unit1_sc', strides=(2,2), **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)
    x = layers.BatchNormalization(name='stage4_unit1_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage4_unit1_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(512, (3,3), strides=(2,2), name='stage4_unit1_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage4_unit1_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage4_unit1_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(512, (3,3), name='stage4_unit1_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 3, block = 1
# x = ResidualBlock(512, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage4_unit2_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage4_unit2_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(512, (3,3), strides=(1,1), name='stage4_unit2_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage4_unit2_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage4_unit2_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(512, (3,3), name='stage4_unit2_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

# stage = 2, block = 2
# x = ResidualBlock(512, stage, block, strides=(1, 1), cut='pre', attention=Attention)(x)
    sortcut = x
    x = layers.BatchNormalization(name='stage4_unit3_bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage4_unit3_relu1')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(512, (3,3), strides=(1,1), name='stage4_unit3_conv1', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.BatchNormalization(name='stage4_unit3_bn2', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
    x = layers.Activation('relu', name='stage4_unit3_relu2')(x)
    x = layers.ZeroPadding2D(padding=(1,1))(x)
    x = layers.Conv2D(512, (3,3), name='stage4_unit3_conv2', **{'kernel_initializer': 'he_uniform', 'use_bias': False, 'padding': 'valid'})(x)

    x = layers.Add()([x, shortcut])

x = layers.BatchNormalization(name='bn1', **{'axis': 1, 'momentum': 0.99, 'epsilon': 2e-5, 'center': True, 'scale': True})(x)
x = layers.Activation('relu', name='relu1')(x)

backbone = models.Model(inputs=img_input, outputs=x)


print(backbone.summary())

print(backbone.input)
print(backbone.output)
#print(backbone.get_layer(name='stage4_unit1_relu1').output)

print(backbone.layers[-1])

# load weight
WEIGHTS_COLLECTION = [
    # ResNet34
    {
        'model': 'resnet34',
        'dataset': 'imagenet',
        'classes': 1000,
        'include_top': True,
        'url': 'https://github.com/qubvel/classification_models/releases/download/0.0.1/resnet34_imagenet_1000.h5',
        'name': 'resnet34_imagenet_1000.h5',
        'md5': '2ac8277412f65e5d047f255bcbd10383',
    },

    {
        'model': 'resnet34',
        'dataset': 'imagenet',
        'classes': 1000,
        'include_top': False,
        'url': 'https://github.com/qubvel/classification_models/releases/download/0.0.1/resnet34_imagenet_1000_no_top.h5',
        'name': 'resnet34_imagenet_1000_no_top.h5',
        'md5': '8caaa0ad39d927cb8ba5385bf945d582',
    }
]
w = list(filter(lambda x: x['model'] == 'resnet34', WEIGHTS_COLLECTION))
w = list(filter(lambda x: x['dataset'] == 'imagenet', w))
w = list(filter(lambda x: x['include_top'] == False, w))
print(w)

weights = w[0]

weights_path = keras_utils.get_file(
    weights['name'],
    weights['url'],
    cache_subdir='modelssssss',
    md5_hash=weights['md5']
)

backbone.load_weights(weights_path)

and I got the information:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to
==================================================================================================
** IGNORE LAYER
==================================================================================================
Total params: 21,281,856
Trainable params: 21,276,544
Non-trainable params: 5,312
__________________________________________________________________________________________________
None
Tensor("data:0", shape=(?, 512, 512, 3), dtype=float32)
Tensor("relu1/Relu:0", shape=(?, 16, 16, 512), dtype=float32)
<tensorflow.python.keras.layers.core.Activation object at 0x1281255c0>
[{'model': 'resnet34', 'dataset': 'imagenet', 'classes': 1000, 'include_top': False, 'url': 'https://github.com/qubvel/classification_models/releases/download/0.0.1/resnet34_imagenet_1000_no_top.h5', 'name': 'resnet34_imagenet_1000_no_top.h5', 'md5': '8caaa0ad39d927cb8ba5385bf945d582'}]
Traceback (most recent call last):
  File "flatten_model_of_resnet34_unet.py", line 307, in <module>
    backbone.load_weights(weights_path)
  File "/usr/local/Cellar/[email protected]/3.6.9_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/network.py", line 1544, in load_weights
    saving.load_weights_from_hdf5_group(f, self.layers)
  File "/usr/local/Cellar/[email protected]/3.6.9_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/engine/saving.py", line 806, in load_weights_from_hdf5_group
    K.batch_set_value(weight_value_tuples)
  File "/usr/local/Cellar/[email protected]/3.6.9_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/keras/backend.py", line 2779, in batch_set_value
    assign_op = x.assign(assign_placeholder)
  File "/usr/local/Cellar/[email protected]/3.6.9_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/ops/resource_variable_ops.py", line 951, in assign
    self._shape.assert_is_compatible_with(value_tensor.shape)
  File "/usr/local/Cellar/[email protected]/3.6.9_3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/tensor_shape.py", line 848, in assert_is_compatible_with
    raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (512,) and (3,) are incompatible

I have changed input shape with (256, 256, 3), (224, 224, 3), (512, 512, 3), (512, 512, 1), but it doesn't work.
And I already google for a while, but I still don't know how to solve this problem.

Could anyone give me advice? Thank you!

4 channel

Thanks again for the wonderful library! This is very helpful and I learned a lot by studying your code. Is it possible to edit the code from (height,width, 3) to (height,width, 4)? If so were can I update the code?

ImportError: No module named classification_models.keras

When I try to import classification_models.keras for using ResNet18, I am getting an ImportError as:
from classification_models.tfkeras import Classifiers
ImportError: No module named classification_models.tfkeras

I am using Ubuntu 16.04 OS with Python3.5, Keras 2.2.4, Keras-applications 1.0.8 and Tensorflow 1.13.1.
Please advise as I am unable to figure out why the error.

Thanks in advance.

About pre-processing

ResNet18, preprocess_input = Classifiers.get('resnet18')

This funcition which name "preprocess_input" doing nothing, What pre-processing did you used to ? I want use this deep feature to do my work, so i think must use the same pre-processing , thinks.

ImportError: No module named classification_models.keras

When I try to import classification_models.keras for using ResNet18, I am getting an ImportError as:
from classification_models.keras import Classifiers
ImportError: No module named classification_models.keras

I am using Python3.7, Keras 2.2.4, Tensorflow 1.13.1.
Please advise as I am unable to figure out why the error.

Thanks in advance.

No such model `seresnext50v2

Hi, I found a pre-trained model named
seresnext50_imagenet_1000_no_top_v2.h5
in your release,
however, I cound not load it,so please tell me how to use it?
Thx.

How could I use 1 channel inputs for seresnext50?

Just wondering is there a way to change the input shape channels from model structure? It gives me the error if I used one channel inputs.

Shapes (7, 7, 1, 64) and (64, 3, 7, 7) are incompatible

Couldn't download ResNet50 for imagenet

Hi, I have a trouble. I'm in great need of your assistance.
I couldn't download the pretrained model for ResNet50 from "https://github.com/qubvel/classification_models/releases/download/0.0.1/resnet50_imagenet_1000_no_top.h5".
It will terminate when I download it to more than 30MB.
In linux, It prompts such an error “Connection reset by peer”
In windows, I downloaded it in the browser and it prompts "Failed - has been banned".
I have tried many times and failed. How can I download the file-resnet50_imagenet_1000_no_top.h5?

Thanks a lot!

Pretrained Resnet18 trainable false doesnot freeze base layers

resnet18, preprocess_input = Classifiers.get('resnet18')
base_model = resnet18(input_shape=(224,224,3), weights='imagenet', include_top=False,trainable=False)
x = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(10, activation='softmax')(x)
model = keras.models.Model(inputs=base_model.input, outputs=output)
model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

The output of the above snippet taken from pretrained finetuning in ReadMe give the following summary of trainable and non-trainable params. The numbers seem off even if trainable is set to False
Total params: 11,192,019
Trainable params: 11,184,077
Non-trainable params: 7,942

I test the model performance but cannot get what I want

import keras
from classification_models.tfkeras import Classifiers

ResNet18, preprocess_input = Classifiers.get('resnet18')
base_model = ResNet18(input_shape=(224,224,3))
base_model.load_weights('resnet18_imagenet_1000.h5')
base_model.compile(
optimizer="sgd",
loss=["categorical_crossentropy"],
metrics=["categorical_accuracy"],
)
def preprocess(data):
img = lqz.preprocess_input(data["image"]*255)
print(img)
label = tf.one_hot(data["label"], 1000)
return img, label
dataset = (
tfds.load("imagenet2012:5.0.0", split=tfds.Split.VALIDATION)
.map(preprocess, num_parallel_calls=tf.data.experimental.AUTOTUNE)
.batch(128)
.prefetch(1)
)

base_model.evaluate(dataset)

Would you like to tell me where I am doing wrong?

About the SE blocks in SE-Net

May I ask the reason why you opted to use conv layers instead of dense layers like in the paper(or at least that's what I think the paper used)? Actually, I have tried both and using conv layers yields better results altho not by much so I'm wandering if it's just fluke like they're actually about the same or there are some good reasons behind it.

Edit: After thinking about it again, I suppose there's really no difference between using 1x1 convolutions and fully connected layers.

How could I get a softmax output of these models?

In this case i don't want the prediction class so that I add include_top = False inside the model. I want a softmax layer at the end of the network, and get the output of specified size. Any code to help please?

Is it really BGR?

Hello qubvel,
thanks for the great repo. Are you sure about the preprocessing for resnets (i.e. using BGR)? I get better results when I use RGB:

import numpy as np
from skimage.io import imread
from keras.applications.imagenet_utils import decode_predictions

from classification_models.classification_models import ResNet18
from classification_models.classification_models.resnet import preprocess_input

# read and prepare image
img_rgb = imread('imgs/tests/Lion.jpg')

for order in ['rgb', 'bgr']:
  print("Order: ", order.upper())
  x = preprocess_input(img_rgb, size=(224, 224), BGRTranspose=order == 'bgr')
  x = np.expand_dims(x, 0)

  # load model
  model = ResNet18(input_shape=(224, 224, 3), weights='imagenet', classes=1000)

  # processing image
  y = model.predict(x)

  # result
  print(decode_predictions(y))

Output:

Order:  RGB
[[('n02129165', 'lion', 0.9629949), ('n02115913', 'dhole', 0.01578529), ('n02112137', 'chow', 0.0087916795), ('n02114855', 'coyote', 0.0038442488), ('n02114712', 'red_wolf', 0.0014433597)]]
Order:  BGR
[[('n02129165', 'lion', 0.3915644), ('n02112137', 'chow', 0.10712016), ('n02114855', 'coyote', 0.09258213), ('n02114367', 'timber_wolf', 0.08412136), ('n02091467', 'Norwegian_elkhound', 0.027228368)]]

lion

use with fit_generator in keras ?

Hi,
Thanks for your code. I actually can't manage to make it work. Is it possible to use it with fit_generator in keras ? I got this error:
valueError: Cannot feed value of shape (32, 45) for Tensor 'relu1_target:0', which has shape '(?, ?, ?, ?)'
(Batch 32, 45 classes)
It may comes from the fact that i didn't use the preprocess function. Do you know how to handle it with fit_generator ? (if it's easy for you)

Expected Range for Inputs (also for segmentation models)

From the tutorial it seems like all preprocessing functions for pretrained models expect 8 bit RGB images in the range 0-255. From what I understand, this is also true for all pretrained models in segmentation_models. Can you please confirm that this is the case for all models in both libraries?

#37 confirms this for ResNets

Thank you very much!

resnext.preprocessing.py

Hello, is pre-trained weights of resnext trained by RGB images? or BGR images? Because I didn't see the option 'BGRTranspose' in resnext.preprocessing.py

AttributeError: module 'tensorflow' has no attribute 'placeholder'

Following code is failing for me and have shared TF version summary also. Is this module not compatible with tensorflow-gpu 2.0.0b1 ?

from classification_models.keras import Classifiers
ResNet18, preprocess_input = Classifiers.get('resnet18')

!pip list | grep tensorflow

n_classes = 100

build model

base_model = ResNet18(input_shape=(32,32,3), weights='imagenet', include_top=False)
x = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation='softmax')(x)
model = keras.models.Model(inputs=[base_model.input], outputs=[output])

train

model.compile(optimizer='SGD', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()

model.fit(X, y)

############ Error Details #############
mesh-tensorflow 0.0.5
tensorflow 1.14.0
tensorflow-estimator 1.14.0
tensorflow-gpu 2.0.0b1
tensorflow-hub 0.6.0
tensorflow-metadata 0.14.0
tensorflow-probability 0.7.0

AttributeError Traceback (most recent call last)
in ()
6 n_classes = 100
7 # build model
----> 8 base_model = ResNet18(input_shape=(32,32,3), weights='imagenet', include_top=False)
9 x = keras.layers.GlobalAveragePooling2D()(base_model.output)
10 output = keras.layers.Dense(n_classes, activation='softmax')(x)

6 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in placeholder(shape, ndim, dtype, sparse, name)
539 x = tf.sparse_placeholder(dtype, shape=shape, name=name)
540 else:
--> 541 x = tf.placeholder(dtype, shape=shape, name=name)
542 x._keras_shape = shape
543 x._uses_learning_phase = False

AttributeError: module 'tensorflow' has no attribute 'placeholder'

can not use the ResNet18 models

Hello, there is a error in model.fit_generator when I load the pretrained model to fine-tune the whole the model.

InternalError: cuDNN launch failure : input shape ([32,224,224,3])
[[Node: resnet18/bn_data/FusedBatchNorm = FusedBatchNorm[T=DT_FLOAT, data_format="NHWC", epsilon=2e-05, is_training=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_resnet18_input_0_3/_1271, resnet18/bn_data/Const, bn_data/beta/read, resnet18/stage4_unit2_bn1/Const, resnet18/stage4_unit2_bn1/Const)]]
[[Node: metrics/acc/Mean/_1359 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_6505_metrics/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Caused by op 'resnet18/bn_data/FusedBatchNorm', defined at:
File "/root/anaconda3/lib/python3.6/site-packages/spyder/utils/ipython/start_kernel.py", line 269, in
main()
File "/root/anaconda3/lib/python3.6/site-packages/spyder/utils/ipython/start_kernel.py", line 265, in main
kernel.start()
File "/root/anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 486, in start
self.io_loop.start()
File "/root/anaconda3/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 127, in start
self.asyncio_loop.run_forever()
File "/root/anaconda3/lib/python3.6/asyncio/base_events.py", line 427, in run_forever
self._run_once()
File "/root/anaconda3/lib/python3.6/asyncio/base_events.py", line 1440, in _run_once
handle._run()
File "/root/anaconda3/lib/python3.6/asyncio/events.py", line 145, in _run
self._callback(*self._args)
File "/root/anaconda3/lib/python3.6/site-packages/tornado/platform/asyncio.py", line 117, in _handle_events
handler_func(fileobj, events)
File "/root/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 450, in _handle_events
self._handle_recv()
File "/root/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 480, in _handle_recv
self._run_callback(callback, msg)
File "/root/anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 432, in _run_callback
callback(*args, **kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/tornado/stack_context.py", line 276, in null_wrapper
return fn(*args, **kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
return self.dispatch_shell(stream, msg)
File "/root/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
handler(stream, idents, msg)
File "/root/anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
user_expressions, allow_stdin)
File "/root/anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 208, in do_execute
res = shell.run_cell(code, store_history=store_history, silent=silent)
File "/root/anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 537, in run_cell
return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2662, in run_cell
raw_cell, store_history, silent, shell_futures)
File "/root/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2785, in _run_cell
interactivity=interactivity, compiler=compiler, result=result)
File "/root/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2909, in run_ast_nodes
if self.run_code(code, result):
File "/root/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
runfile('/cptjack/totem/huajia/0614/resnet18_me.py', wdir='/cptjack/totem/huajia/0614')
File "/root/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "/root/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/cptjack/totem/huajia/0614/resnet18_me.py", line 54, in
top_model.add(base)
File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/sequential.py", line 166, in add
layer(x)
File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/base_layer.py", line 460, in call
output = self.call(inputs, **kwargs)
File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 573, in call
output_tensors, _, _ = self.run_internal_graph(inputs, masks)
File "/root/anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 730, in run_internal_graph
output_tensors = to_list(layer.call(computed_tensor, **kwargs))
File "/root/anaconda3/lib/python3.6/site-packages/keras/layers/normalization.py", line 183, in call
epsilon=self.epsilon)
File "/root/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 1841, in normalize_batch_in_training
epsilon=epsilon)
File "/root/anaconda3/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 1816, in _fused_normalize_batch_in_training
data_format=tf_data_format)
File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/nn_impl.py", line 904, in fused_batch_norm
name=name)
File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 3416, in _fused_batch_norm
is_training=is_training, name=name)
File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3290, in create_op
op_def=op_def)
File "/root/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1654, in init
self._traceback = self._graph._extract_stack() # pylint: disable=protected-access

InternalError (see above for traceback): cuDNN launch failure : input shape ([32,224,224,3])
[[Node: resnet18/bn_data/FusedBatchNorm = FusedBatchNorm[T=DT_FLOAT, data_format="NHWC", epsilon=2e-05, is_training=true, _device="/job:localhost/replica:0/task:0/device:GPU:0"](_arg_resnet18_input_0_3/_1271, resnet18/bn_data/Const, bn_data/beta/read, resnet18/stage4_unit2_bn1/Const, resnet18/stage4_unit2_bn1/Const)]]
[[Node: metrics/acc/Mean/_1359 = _Recvclient_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:CPU:0", send_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device_incarnation=1, tensor_name="edge_6505_metrics/acc/Mean", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

input images for ResNet

Hi qubvel, thanks for your great repo.

I would like to ask a question, just to clarify the ideas I have in mind. If I understood correctly your code and the previous issues, the ResNet models you define in models.py and their corresponding weights are trained using data in [0, 255] range and coded as BGR, correct?

Therefore, if I import an image in RGB using img = imread(...), I don't need to rescale it into [0,1] range but I need to "transpose" it using your function img = preprocess_input(img, BGRTranspose = True) and then use the ResNet model, am I right?

resnet.preprocessing.py

hi
I saw the code of preprocessing.py that
x = resize(x, size) * 255
when read a image use skiamge, the range of pixel is in [0, 255], so you multiply 255 means pretrained model are trained in [0, 65025] ?

AttributeError: 'Activation' object has no attribute 'outbound_nodes'

TF version: 1.13.1
Keras version== 2.2.4
image-classifiers==0.2.0

from tensorflow.python.keras.layers import Dense, Dropout, Flatten, Activation, Input, BatchNormalization, Add, GlobalAveragePooling2D,AveragePooling2D,GlobalMaxPooling2D,concatenate
from tensorflow.python.keras.models import Model,load_model
def resnext_model(input_shape,batch_size = 512):
  base_model = SEResNeXt50(input_shape=input_shape,weights='imagenet',include_top = False)
  x = base_model.output
  out1 = GlobalMaxPooling2D()(x)
  out2 = GlobalAveragePooling2D()(x)
  out3 = Flatten()(x)
  out = concatenate([out1,out2,out3])
  out = BatchNormalization(epsilon = 1e-5)(out)
  out = Dropout(0.4)(out)
  fc = Dense(512,activation = 'relu')(out)
  fc = BatchNormalization(epsilon = 1e-5)(fc)
  fc = Dropout(0.3)(fc)
  fc = Dense(256,activation = 'relu')(fc)
  fc = BatchNormalization(epsilon = 1e-5)(fc)
  fc = Dropout(0.3)(fc)
  X = Dense(1, activation='sigmoid', kernel_initializer='glorot_uniform', bias_initializer='zeros')(fc)
  model =  Model(inputs=base_model.input, outputs=X)
  #model.compile(optimizer=tf.keras.optimizers.Adam(lr = 0.0001), loss=tf.keras.losses.binary_crossentropy, metrics=['acc'])
  return model

Works fine when importing layers directly from keras.
But I want to train my model on Cloud TPU, so importing layers and model from tensorflow.
Getting the following error.

AttributeError                            Traceback (most recent call last)
<ipython-input-6-8f1104681c92> in <module>()
----> 1 res_model = resnext_model((96,96,3))
      2 print(res_model.summary())

<ipython-input-5-230de8cde50c> in resnext_model(input_shape, batch_size)
      3   x = base_model.output
      4 
----> 5   out1 = GlobalMaxPooling2D()(x)
      6   out2 = GlobalAveragePooling2D()(x)
      7   out3 = Flatten()(x)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    582           if base_layer_utils.have_all_keras_metadata(inputs):
    583             inputs, outputs = self._set_connectivity_metadata_(
--> 584                 inputs, outputs, args, kwargs)
    585           if hasattr(self, '_set_inputs') and not self.inputs:
    586             # Subclassed network: explicitly set metadata normally set by

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
   1414     kwargs.pop('mask', None)  # `mask` should not be serialized.
   1415     self._add_inbound_node(
-> 1416         input_tensors=inputs, output_tensors=outputs, arguments=kwargs)
   1417     return inputs, outputs
   1418 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _add_inbound_node(self, input_tensors, output_tensors, arguments)
   1522         input_tensors=input_tensors,
   1523         output_tensors=output_tensors,
-> 1524         arguments=arguments)
   1525 
   1526     # Update tensor history metadata.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __init__(self, outbound_layer, inbound_layers, node_indices, tensor_indices, input_tensors, output_tensors, arguments)
   1740         # For compatibility with external Keras, we use the deprecated
   1741         # accessor here.
-> 1742         layer.outbound_nodes.append(self)
   1743     # For compatibility with external Keras, we use the deprecated
   1744     # accessor here.

AttributeError: 'Activation' object has no attribute 'outbound_nodes'

Got the same error with SEResNet, ResNeXt as well.

About preprocessing

Hi,

This repo is fantastic!

I have questions about the preprocessing part. I only saw 'return x' in the preprocess_input in resnet.py. Is that true for all the resnet pretrained models?

Also, for the input tensor, does it expect 'RGB' or 'BGR'?

Thanks!
Joey

Typo

The example in readme

'' from skimage.transfrom import resize ''
transfrom should be transform

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.