Keras code and weights files for popular deep learning models.

Overview

Trained image classification models for Keras

THIS REPOSITORY IS DEPRECATED. USE THE MODULE keras.applications INSTEAD.

Pull requests will not be reviewed nor merged. Direct any PRs to keras.applications. Issues are not monitored either.


This repository contains code for the following Keras models:

  • VGG16
  • VGG19
  • ResNet50
  • Inception v3
  • CRNN for music tagging

All architectures are compatible with both TensorFlow and Theano, and upon instantiation the models will be built according to the image dimension ordering set in your Keras configuration file at ~/.keras/keras.json. For instance, if you have set image_dim_ordering=tf, then any model loaded from this repository will get built according to the TensorFlow dimension ordering convention, "Width-Height-Depth".

Pre-trained weights can be automatically loaded upon instantiation (weights='imagenet' argument in model constructor for all image models, weights='msd' for the music tagging model). Weights are automatically downloaded if necessary, and cached locally in ~/.keras/models/.

Examples

Classify images

from resnet50 import ResNet50
from keras.preprocessing import image
from imagenet_utils import preprocess_input, decode_predictions

model = ResNet50(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print('Predicted:', decode_predictions(preds))
# print: [[u'n02504458', u'African_elephant']]

Extract features from images

from vgg16 import VGG16
from keras.preprocessing import image
from imagenet_utils import preprocess_input

model = VGG16(weights='imagenet', include_top=False)

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)

Extract features from an arbitrary intermediate layer

from vgg19 import VGG19
from keras.preprocessing import image
from imagenet_utils import preprocess_input
from keras.models import Model

base_model = VGG19(weights='imagenet')
model = Model(input=base_model.input, output=base_model.get_layer('block4_pool').output)

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

block4_pool_features = model.predict(x)

References

Additionally, don't forget to cite Keras if you use these models.

License

Comments
  • Transfer learning with Resnet50 fail with Exception

    Transfer learning with Resnet50 fail with Exception

    Hi, I am using Resnet50 to do transfer learning. The backend is tensorflow. I tried to stack three more layers on top of the Resnet but fail with following error:

    Exception: The shape of the input to "Flatten" is not fully defined (got (None, None, 2048). 
    Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.
    

    The code for stacking two models are as following:

        model = ResNet50(include_top=False, weights='imagenet')
    
        top_model = Sequential()
        top_model.add(Flatten(input_shape=model.output_shape[1:]))
        top_model.add(Dense(256, activation='relu'))
        top_model.add(Dropout(0.5))
        top_model.add(Dense(1, activation='sigmoid'))
        top_model.load_weights(top_model_weights_path)
    
        model = Model(input=model.input, output=top_model(model.output))
    
    opened by MrXu 5
  • [WIP] autocolorize model

    [WIP] autocolorize model

    opened by kashif 5
  • AttributeError: 'module' object has no attribute 'image_data_format'

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

    >>> from resnet50 import ResNet50
    Using TensorFlow backend.
    I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcublas.so.8.0 locally
    I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcudnn.so.5 locally
    I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcufft.so.8.0 locally
    I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcuda.so.1 locally
    I tensorflow/stream_executor/dso_loader.cc:135] successfully opened CUDA library libcurand.so.8.0 locally
    >>> model = ResNet50(weights='imagenet')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "resnet50.py", line 192, in ResNet50
        data_format=K.image_data_format(),
    AttributeError: 'module' object has no attribute 'image_data_format'
    >>> from keras.preprocessing import image
    >>> from imagenet_utils import preprocess_input, decode_predictions
    >>> model = ResNet50(weights='imagenet')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "resnet50.py", line 192, in ResNet50
        data_format=K.image_data_format(),
    AttributeError: 'module' object has no attribute 'image_data_format'
    

    My System

    • Tensorflow 1.0.0
    • Keras 1.2.2
    opened by MartinThoma 4
  • Inception not working as feature extractor

    Inception not working as feature extractor

    when calling predict:

    Traceback (most recent call last):
      File "/home/omar/Pycharm_ubuntu_v2/Spatial_v2_Aug-2016/features_from_keras_tool_RGB_final.py", line 72, in <module>
        model = InceptionV3(weights='imagenet', include_top=False)
      File "/home/omar/Pycharm_ubuntu_v2/Spatial_v2_Aug-2016/inception_v3.py", line 272, in InceptionV3
        model.load_weights(weights_path)
      File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2446, in load_weights
        self.load_weights_from_hdf5_group(f)
      File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2518, in load_weights_from_hdf5_group
        ' elements.')
    Exception: Layer #162 (named "batchnormalization_79" in the current model) was found to correspond to layer convolution2d_77 in the save file. However the new layer batchnormalization_79 expects 4 weights, but the saved weights have 2 elements.
    
    Process finished with exit code 1
    
    opened by omarcr 4
  • Inception-v3 fine-tuning

    Inception-v3 fine-tuning

    opened by nournia 4
  • KeyError: “Can’t open attribute (Can’t locate attribute: ‘layer_names’)

    KeyError: “Can’t open attribute (Can’t locate attribute: ‘layer_names’)

    I tried to run this code

    from vgg16 import VGG16
    from keras.preprocessing import image
    from imagenet_utils import preprocess_input
    
    model = VGG16(weights='imagenet', include_top=False)
    
    img_path = 'elephant.jpg'
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    
    features = model.predict(x)
    

    but i got KeyError: “Can’t open attribute (Can’t locate attribute: ‘layer_names’) what should i do?

    opened by lightwolfz 3
  • vgg_face model,only compatible with Theano

    vgg_face model,only compatible with Theano

    I've already converted the caffe vgg_face model to keras,but it's only compatible with Theano. I've also tried many times to use the convert_kernel function in keras.utils.np_utils to make it compatible with Tensorflow,but I can't get the right result.

    opened by EncodeTS 3
  • ResNet50 Batch Normalization Mode

    ResNet50 Batch Normalization Mode

    Would it be reasonable to add an optional batch normalization mode argument to ResNet50? Allowing for mode = 2 would enable ResNet50 to be used in a shared fashion. I think the same BN initializations could be used in mode = 2. Happy to do a PR if folks think it's worthwhile.

    opened by jmhessel 3
  • Mean image for VGG-16 net

    Mean image for VGG-16 net

    Are the weight files here as same as the original VGG-16 net? There is a mean image file with VGG-16's Caffe Model. Should I still apply it for the best result?

    opened by duguyue100 2
  • inception model fails to load pretrained weights

    inception model fails to load pretrained weights

    I have used the resnet and vgg models successfully but cannot use the freshly released inception weights.

    Keras is on the latest master commit from github and i'm using anaconda python 3.5. -- Edit it was not on the 'latest' commit. It was on a commit from several days ago when I first cloned this repo; didn't realize it needed to be updated again.

    Thoughts?

    from inception_v3 import InceptionV3
    from keras.preprocessing import image
    from imagenet_utils import preprocess_input
    
    model = InceptionV3(weights='imagenet', include_top=False)
    
    Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_th_dim_ordering_th_kernels_notop.h5
    86679552/86916664 [============================>.] - ETA: 0s
    ---------------------------------------------------------------------------
    Exception                                 Traceback (most recent call last)
    <ipython-input-5-881bb296c35e> in <module>()
          3 from imagenet_utils import preprocess_input
          4 
    ----> 5 model = InceptionV3(weights='imagenet', include_top=False)
    
    /home/agonzales/git/image_classifier/src/inception_v3.py in InceptionV3(include_top, weights, input_tensor)
        279                                         cache_subdir='models',
        280                                         md5_hash='79aaa90ab4372b4593ba3df64e142f05')
    --> 281             model.load_weights(weights_path)
        282             if K.backend() == 'tensorflow':
        283                 warnings.warn('You are using the TensorFlow backend, yet you '
    
    /home/agonzales/anaconda3/envs/keras_extract/lib/python3.5/site-packages/Keras-1.0.6-py3.5.egg/keras/engine/topology.py in load_weights(self, filepath)
       2444         if 'layer_names' not in f.attrs and 'model_weights' in f:
       2445             f = f['model_weights']
    -> 2446         self.load_weights_from_hdf5_group(f)
       2447         if hasattr(f, 'close'):
       2448             f.close()
    
    /home/agonzales/anaconda3/envs/keras_extract/lib/python3.5/site-packages/Keras-1.0.6-py3.5.egg/keras/engine/topology.py in load_weights_from_hdf5_group(self, f)
       2516                                     ' weights, but the saved weights have ' +
       2517                                     str(len(weight_values)) +
    -> 2518                                     ' elements.')
       2519                 weight_value_tuples += zip(symbolic_weights, weight_values)
       2520             K.batch_set_value(weight_value_tuples)
    
    Exception: Layer #162 (named "batchnormalization_267" in the current model) was found to correspond to layer convolution2d_77 in the save file. However the new layer batchnormalization_267 expects 4 weights, but the saved weights have 2 elements.
    
    opened by binaryaaron 2
  • SignatureDoesNotMatch when downloading the releases v0.7

    SignatureDoesNotMatch when downloading the releases v0.7

    Hello,

    We cannot fetch the file from the following URL.

    https://github.com/fchollet/deep-learning-models/releases/download/v0.7/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5

    The response is as followed

    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
    
    opened by lukkiddd 1
  • ValueError: Error when checking input: expected vgg16_input to have shape (244, 244, 3) but got array with shape (224, 224, 3)

    ValueError: Error when checking input: expected vgg16_input to have shape (244, 244, 3) but got array with shape (224, 224, 3)

    Hello I have written the following code:

    validate on val set predictions = model.predict(X_val_prep) predictions = [1 if x>0.5 else 0 for x in predictions]

    accuracy = accuracy_score(y_val, predictions) print('Val Accuracy = %.2f' % accuracy)

    confusion_mtx = confusion_matrix(y_val, predictions) cm = plot_confusion_matrix(confusion_mtx, classes = list(labels.items()), normalize=False)

    ValueError: Error when checking input: expected vgg16_input to have shape (244, 244, 3) but got array with shape (224, 224, 3)

    Could you help me how I should tackle it? thank u very much.

    opened by Aisha5 0
  • NameError: name 'X_val_prep' is not defined

    NameError: name 'X_val_prep' is not defined

    Hello I have written the following code:

    validate on val set predictions = model.predict(X_val_prep) predictions = [1 if x>0.5 else 0 for x in predictions]

    accuracy = accuracy_score(y_val, predictions) print('Val Accuracy = %.2f' % accuracy)

    confusion_mtx = confusion_matrix(y_val, predictions) cm = plot_confusion_matrix(confusion_mtx, classes = list(labels.items()), normalize=False)

    NameError: name 'X_val_prep' is not defined

    Could you help me how I should tackle it? thank u very much.

    opened by Aisha5 0
  • Loading Keras Model for Multiprocess

    Loading Keras Model for Multiprocess

    Hi, I want to load a keras model in parent process and access by child process but i got many issue.what is correct way to do this.is it possible or not?

    opened by nitishcs007 0
  • keras applications

    keras applications

    Sorry to trouble you, I have a problem about training the keras model.Recently,I used the existing models from keras applications like VGG16,VGG19. The applications provide the existing models which are converted from caffe model. I reproduced the result for inference. But when I want to use the VGG16 model with weights retrain imagenet data,the acc was rised from 0,not a higher acc. First,I think the reason is that tfrecords convert the raw image to (-1.1) but caffe used the raw image which substract mean and convert RGB. Soon, I convert the data in tfrecords look like the data in caffe, but the acc is low too... Second I replace the categorical_crossentropy with sparse_categorical_crossentropy and cancell the one-hot coding. But it doen't work. I'm sorry for my English is elementary level.

    opened by chenglong19029001 0
  • No normalization in prepocess_input function

    No normalization in prepocess_input function

    In the file imagenet_utils.py, the prepocess_input function doesn't contain a normalization procedure, so if I am about to use pretrained VGG19, is it necessary to add this normalization procedure. What's more, why should RGB be changed to BGR. In other websites, the mean value of an image is [123.68, 116.779, 103.939] for RGB, but in this file, it is reversed. which mean value is suitable for the VGG19 in the data format RGB? Do I need to change the image format from RGB to BGR if I want to transfer VGG19 to other tasks? `def preprocess_input(x, dim_ordering='default'): if dim_ordering == 'default': dim_ordering = K.image_dim_ordering() assert dim_ordering in {'tf', 'th'}

    if dim_ordering == 'th':
        x[:, 0, :, :] -= 103.939
        x[:, 1, :, :] -= 116.779
        x[:, 2, :, :] -= 123.68
        # 'RGB'->'BGR'
        x = x[:, ::-1, :, :]
    else:
        x[:, :, :, 0] -= 103.939
        x[:, :, :, 1] -= 116.779
        x[:, :, :, 2] -= 123.68
        # 'RGB'->'BGR'
        x = x[:, :, :, ::-1]
    return x`
    
    opened by Schizophreni 1
Releases(v0.8)
Owner
François Chollet
François Chollet
The original weights of some Caffe models, ported to PyTorch.

pytorch-caffe-models This repo contains the original weights of some Caffe models, ported to PyTorch. Currently there are: GoogLeNet (Going Deeper wit

Katherine Crowson 9 Nov 4, 2022
Code for Piggyback: Adapting a Single Network to Multiple Tasks by Learning to Mask Weights

Piggyback: https://arxiv.org/abs/1801.06519 Pretrained masks and backbones are available here: https://uofi.box.com/s/c5kixsvtrghu9yj51yb1oe853ltdfz4q

Arun Mallya 165 Nov 22, 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
MMdnn is a set of tools to help users inter-operate among different deep learning frameworks. E.g. model conversion and visualization. Convert models between Caffe, Keras, MXNet, Tensorflow, CNTK, PyTorch Onnx and CoreML.

MMdnn MMdnn is a comprehensive and cross-framework tool to convert, visualize and diagnose deep learning (DL) models. The "MM" stands for model manage

Microsoft 5.7k Jan 9, 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
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
Dogs classification with Deep Metric Learning using some popular losses

Tsinghua Dogs classification with Deep Metric Learning 1. Introduction Tsinghua Dogs dataset Tsinghua Dogs is a fine-grained classification dataset fo

QuocThangNguyen 45 Nov 9, 2022
Vanilla and Prototypical Networks with Random Weights for image classification on Omniglot and mini-ImageNet. Made with Python3.

vanilla-rw-protonets-project Vanilla Prototypical Networks and PNs with Random Weights for image classification on Omniglot and mini-ImageNet. Made wi

Giovani Candido 8 Aug 31, 2022
PyTorch implementation of popular datasets and models in remote sensing

PyTorch Remote Sensing (torchrs) (WIP) PyTorch implementation of popular datasets and models in remote sensing tasks (Change Detection, Image Super Re

isaac 222 Dec 28, 2022
DiffQ performs differentiable quantization using pseudo quantization noise. It can automatically tune the number of bits used per weight or group of weights, in order to achieve a given trade-off between model size and accuracy.

Differentiable Model Compression via Pseudo Quantization Noise DiffQ performs differentiable quantization using pseudo quantization noise. It can auto

Facebook Research 145 Dec 30, 2022
A pytorch implementation of Detectron. Both training from scratch and inferring directly from pretrained Detectron weights are available.

Use this instead: https://github.com/facebookresearch/maskrcnn-benchmark A Pytorch Implementation of Detectron Example output of e2e_mask_rcnn-R-101-F

Roy 2.8k Dec 29, 2022
Voice of Pajlada with model and weights.

Pajlada TTS Stripped down version of ForwardTacotron (https://github.com/as-ideas/ForwardTacotron) with pretrained weights for Pajlada's (https://gith

null 6 Sep 3, 2021
This source code is implemented using keras library based on "Automatic ocular artifacts removal in EEG using deep learning"

CSP_Deep_EEG This source code is implemented using keras library based on "Automatic ocular artifacts removal in EEG using deep learning" {https://www

Seyed Mahdi Roostaiyan 2 Nov 8, 2022
PyTorch implementation of Wide Residual Networks with 1-bit weights by McDonnell (ICLR 2018)

1-bit Wide ResNet PyTorch implementation of training 1-bit Wide ResNets from this paper: Training wide residual networks for deployment using a single

Sergey Zagoruyko 122 Dec 7, 2022
🐥A PyTorch implementation of OpenAI's finetuned transformer language model with a script to import the weights pre-trained by OpenAI

PyTorch implementation of OpenAI's Finetuned Transformer Language Model This is a PyTorch implementation of the TensorFlow code provided with OpenAI's

Hugging Face 1.4k Jan 5, 2023
High level network definitions with pre-trained weights in TensorFlow

TensorNets High level network definitions with pre-trained weights in TensorFlow (tested with 2.1.0 >= TF >= 1.4.0). Guiding principles Applicability.

Taehoon Lee 1k Dec 13, 2022
A program that can analyze videos according to the weights you select

MaskMonitor A program that can analyze videos according to the weights you select 下載 訓練完的 weight檔案 執行 MaskDetection.py 內部可更改 輸入來源(鏡頭, 影片, 圖片) 以及輸出條件(人

Patrick_star 1 Nov 7, 2021
Inflated i3d network with inception backbone, weights transfered from tensorflow

I3D models transfered from Tensorflow to PyTorch This repo contains several scripts that allow to transfer the weights from the tensorflow implementat

Yana 479 Dec 8, 2022
🔪 Elimination based Lightweight Neural Net with Pretrained Weights

ELimNet ELimNet: Eliminating Layers in a Neural Network Pretrained with Large Dataset for Downstream Task Removed top layers from pretrained Efficient

snoop2head 4 Jul 12, 2022