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
Twins: Revisiting the Design of Spatial Attention in Vision Transformers

Twins: Revisiting the Design of Spatial Attention in Vision Transformers Very recently, a variety of vision transformer architectures for dense predic

482 Dec 18, 2022
Created as part of CS50 AI's coursework. This AI makes use of knowledge entailment to calculate the best probabilities to win Minesweeper.

Minesweeper-AI Created as part of CS50 AI's coursework. This AI makes use of knowledge entailment to calculate the best probabilities to win Minesweep

Beckham 0 Jul 20, 2022
Modified prey-predator system - Modified prey–predator model describes the rate of change for each species by adding coupling terms.

Modified prey-predator system We aim to study the behaviors of the modified prey–predator model and establish the effects of several parameters that p

Seoyoung Oh 1 Jan 02, 2022
This repo is the code release of EMNLP 2021 conference paper "Connect-the-Dots: Bridging Semantics between Words and Definitions via Aligning Word Sense Inventories".

Connect-the-Dots: Bridging Semantics between Words and Definitions via Aligning Word Sense Inventories This repo is the code release of EMNLP 2021 con

12 Nov 22, 2022
a spacial-temporal pattern detection system for home automation

Argos a spacial-temporal pattern detection system for home automation. Based on OpenCV and Tensorflow, can run on raspberry pi and notify HomeAssistan

Angad Singh 133 Jan 05, 2023
[CVPR 2021] VirTex: Learning Visual Representations from Textual Annotations

VirTex: Learning Visual Representations from Textual Annotations Karan Desai and Justin Johnson University of Michigan CVPR 2021 arxiv.org/abs/2006.06

Karan Desai 533 Dec 24, 2022
QRec: A Python Framework for quick implementation of recommender systems (TensorFlow Based)

Introduction QRec is a Python framework for recommender systems (Supported by Python 3.7.4 and Tensorflow 1.14+) in which a number of influential and

Yu 1.4k Dec 30, 2022
Introduction to AI assignment 1 HCM University of Technology, term 211

Sokoban Bot Introduction to AI assignment 1 HCM University of Technology, term 211 Abstract This is basically a solver for Sokoban game using Breadth-

Quang Minh 4 Dec 12, 2022
An implementation of the AdaOPS (Adaptive Online Packing-based Search), which is an online POMDP Solver used to solve problems defined with the POMDPs.jl generative interface.

AdaOPS An implementation of the AdaOPS (Adaptive Online Packing-guided Search), which is an online POMDP Solver used to solve problems defined with th

9 Oct 05, 2022
Film review classification

Film review classification Решение задачи классификации отзывов на фильмы на положительные и отрицательные с помощью рекуррентных нейронных сетей 1. З

Nikita Dukin 3 Jan 21, 2022
Deep Learning with PyTorch made easy 🚀 !

Deep Learning with PyTorch made easy 🚀 ! Carefree? carefree-learn aims to provide CAREFREE usages for both users and developers. It also provides a c

381 Dec 22, 2022
Implementation and replication of ProGen, Language Modeling for Protein Generation, in Jax

ProGen - (wip) Implementation and replication of ProGen, Language Modeling for Protein Generation, in Pytorch and Jax (the weights will be made easily

Phil Wang 71 Dec 01, 2022
Multi-Stage Spatial-Temporal Convolutional Neural Network (MS-GCN)

Multi-Stage Spatial-Temporal Convolutional Neural Network (MS-GCN) This code implements the skeleton-based action segmentation MS-GCN model from Autom

Benjamin Filtjens 8 Nov 29, 2022
Karate Club: An API Oriented Open-source Python Framework for Unsupervised Learning on Graphs (CIKM 2020)

Karate Club is an unsupervised machine learning extension library for NetworkX. Please look at the Documentation, relevant Paper, Promo Video, and Ext

Benedek Rozemberczki 1.8k Jan 07, 2023
Learning from Guided Play: A Scheduled Hierarchical Approach for Improving Exploration in Adversarial Imitation Learning Source Code

Learning from Guided Play: A Scheduled Hierarchical Approach for Improving Exploration in Adversarial Imitation Learning Source Code

STARS Laboratory 8 Sep 14, 2022
PyTorch Code for NeurIPS 2021 paper Anti-Backdoor Learning: Training Clean Models on Poisoned Data.

Anti-Backdoor Learning PyTorch Code for NeurIPS 2021 paper Anti-Backdoor Learning: Training Clean Models on Poisoned Data. Check the unlearning effect

Yige-Li 51 Dec 07, 2022
Library for fast text representation and classification.

fastText fastText is a library for efficient learning of word representations and sentence classification. Table of contents Resources Models Suppleme

Facebook Research 24.1k Jan 01, 2023
Detectron2 for Document Layout Analysis

Detectron2 trained on PubLayNet dataset This repo contains the training configurations, code and trained models trained on PubLayNet dataset using Det

Himanshu 163 Nov 21, 2022
Hard cater examples from Hopper ICLR paper

CATER-h Honglu Zhou*, Asim Kadav, Farley Lai, Alexandru Niculescu-Mizil, Martin Renqiang Min, Mubbasir Kapadia, Hans Peter Graf (*Contact: honglu.zhou

NECLA ML Group 6 May 11, 2021
A PyTorch implementation for Unsupervised Domain Adaptation by Backpropagation(DANN), support Office-31 and Office-Home dataset

DANN A PyTorch implementation for Unsupervised Domain Adaptation by Backpropagation Prerequisites Linux or OSX NVIDIA GPU + CUDA (may CuDNN) and corre

8 Apr 16, 2022