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
MDETR: Modulated Detection for End-to-End Multi-Modal Understanding

MDETR: Modulated Detection for End-to-End Multi-Modal Understanding Website • Colab • Paper This repository contains code and links to pre-trained mod

Aishwarya Kamath 770 Dec 28, 2022
A map update dataset and benchmark

MUNO21 MUNO21 is a dataset and benchmark for machine learning methods that automatically update and maintain digital street map datasets. Previous dat

16 Nov 30, 2022
Paper list of log-based anomaly detection

Paper list of log-based anomaly detection

Weibin Meng 411 Dec 05, 2022
gtfs2vec - Learning GTFS Embeddings for comparing PublicTransport Offer in Microregions

gtfs2vec This is a companion repository for a gtfs2vec - Learning GTFS Embeddings for comparing PublicTransport Offer in Microregions publication. Vis

Politechnika Wrocławska - repozytorium dla informatyków 5 Oct 10, 2022
Code release for ICCV 2021 paper "Anticipative Video Transformer"

Anticipative Video Transformer Ranked first in the Action Anticipation task of the CVPR 2021 EPIC-Kitchens Challenge! (entry: AVT-FB-UT) [project page

Facebook Research 123 Dec 13, 2022
PyTorch Implementation of Small Lesion Segmentation in Brain MRIs with Subpixel Embedding (ORAL, MICCAIW 2021)

Small Lesion Segmentation in Brain MRIs with Subpixel Embedding PyTorch implementation of Small Lesion Segmentation in Brain MRIs with Subpixel Embedd

22 Oct 21, 2022
The AWS Certified SysOps Administrator

The AWS Certified SysOps Administrator – Associate (SOA-C02) exam is intended for system administrators in a cloud operations role who have at least 1 year of hands-on experience with deployment, man

Aiden Pearce 32 Dec 11, 2022
Trading and Backtesting environment for training reinforcement learning agent or simple rule base algo.

TradingGym TradingGym is a toolkit for training and backtesting the reinforcement learning algorithms. This was inspired by OpenAI Gym and imitated th

Yvictor 1.1k Jan 02, 2023
Leveraging Instance-, Image- and Dataset-Level Information for Weakly Supervised Instance Segmentation

Leveraging Instance-, Image- and Dataset-Level Information for Weakly Supervised Instance Segmentation This paper has been accepted and early accessed

Yun Liu 39 Sep 20, 2022
StarGAN2 for practice

StarGAN2 for practice This version of StarGAN2 (coined as 'Post-modern Style Transfer') is intended mostly for fellow artists, who rarely look at scie

vadim epstein 87 Sep 24, 2022
Saliency - Framework-agnostic implementation for state-of-the-art saliency methods (XRAI, BlurIG, SmoothGrad, and more).

Saliency Methods 🔴 Now framework-agnostic! (Example core notebook) 🔴 🔗 For further explanation of the methods and more examples of the resulting ma

PAIR code 849 Dec 27, 2022
PyTorchVideo is a deeplearning library with a focus on video understanding work

PyTorchVideo is a deeplearning library with a focus on video understanding work. PytorchVideo provides resusable, modular and efficient components needed to accelerate the video understanding researc

Facebook Research 2.7k Jan 07, 2023
Deep-learning-roadmap - All You Need to Know About Deep Learning - A kick-starter

Deep Learning - All You Need to Know Sponsorship To support maintaining and upgrading this project, please kindly consider Sponsoring the project deve

Instill AI 4.4k Dec 26, 2022
Datasets and source code for our paper Webly Supervised Fine-Grained Recognition: Benchmark Datasets and An Approach

Introduction Datasets and source code for our paper Webly Supervised Fine-Grained Recognition: Benchmark Datasets and An Approach Datasets: WebFG-496

21 Sep 30, 2022
Pre-trained NFNets with 99% of the accuracy of the official paper

NFNet Pytorch Implementation This repo contains pretrained NFNet models F0-F6 with high ImageNet accuracy from the paper High-Performance Large-Scale

Benjamin Schmidt 133 Dec 09, 2022
Medical Image Segmentation using Squeeze-and-Expansion Transformers

Medical Image Segmentation using Squeeze-and-Expansion Transformers Introduction This repository contains the code of the IJCAI'2021 paper 'Medical Im

askerlee 172 Dec 20, 2022
discovering subdomains, hidden paths, extracting unique links

python-website-crawler discovering subdomains, hidden paths, extracting unique links pip install -r requirements.txt discover subdomain: You can give

merve 4 Sep 05, 2022
Equipped customers with insights about their EVs Hourly energy consumption and helped predict future charging behavior using LSTM model

Equipped customers with insights about their EVs Hourly energy consumption and helped predict future charging behavior using LSTM model. Designed sample dashboard with insights and recommendation for

Yash 2 Apr 07, 2022
TuckER: Tensor Factorization for Knowledge Graph Completion

TuckER: Tensor Factorization for Knowledge Graph Completion This codebase contains PyTorch implementation of the paper: TuckER: Tensor Factorization f

Ivana Balazevic 296 Dec 06, 2022
Monitor your ML jobs on mobile devices📱, especially for Google Colab / Kaggle

TF Watcher TF Watcher is a simple to use Python package and web app which allows you to monitor 👀 your Machine Learning training or testing process o

Rishit Dagli 54 Nov 01, 2022