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
A way to store images in YAML.

YAMLImg A way to store images in YAML. I made this after seeing Roadcrosser's JSON-G because it was too inspiring to ignore this opportunity. Installa

5 Mar 14, 2022
git《USD-Seg:Learning Universal Shape Dictionary for Realtime Instance Segmentation》(2020) GitHub: [fig2]

USD-Seg This project is an implement of paper USD-Seg:Learning Universal Shape Dictionary for Realtime Instance Segmentation, based on FCOS detector f

Ruolin Ye 80 Nov 28, 2022
Source code of "Hold me tight! Influence of discriminative features on deep network boundaries"

Hold me tight! Influence of discriminative features on deep network boundaries This is the source code to reproduce the experiments of the NeurIPS 202

EPFL LTS4 19 Dec 10, 2021
Using machine learning to predict undergrad college admissions.

College-Prediction Project- Overview: Many have tried, many have failed. Few trailblazers are ambitious enought to chase acceptance into the top 15 un

John H Klinges 1 Jan 05, 2022
PySOT - SenseTime Research platform for single object tracking, implementing algorithms like SiamRPN and SiamMask.

PySOT is a software system designed by SenseTime Video Intelligence Research team. It implements state-of-the-art single object tracking algorit

STVIR 4.1k Dec 29, 2022
This repository contains numerical implementation for the paper Intertemporal Pricing under Reference Effects: Integrating Reference Effects and Consumer Heterogeneity.

This repository contains numerical implementation for the paper Intertemporal Pricing under Reference Effects: Integrating Reference Effects and Consumer Heterogeneity.

Hansheng Jiang 6 Nov 18, 2022
Adversarial vulnerability of powerful near out-of-distribution detection

Adversarial vulnerability of powerful near out-of-distribution detection by Stanislav Fort In this repository we're collecting replications for the ke

Stanislav Fort 9 Aug 30, 2022
A PyTorch Implementation of Gated Graph Sequence Neural Networks (GGNN)

A PyTorch Implementation of GGNN This is a PyTorch implementation of the Gated Graph Sequence Neural Networks (GGNN) as described in the paper Gated G

Ching-Yao Chuang 427 Dec 13, 2022
Fair Recommendation in Two-Sided Platforms

Fair Recommendation in Two-Sided Platforms

gourabgggg 1 Nov 10, 2021
Personal project about genus-0 meshes, spherical harmonics and a cow

How to transform a cow into spherical harmonics ? Spot the cow, from Keenan Crane's blog Context In the field of Deep Learning, training on images or

3 Aug 22, 2022
This is an implementation of PIFuhd based on Pytorch

Open-PIFuhd This is a unofficial implementation of PIFuhd PIFuHD: Multi-Level Pixel-Aligned Implicit Function forHigh-Resolution 3D Human Digitization

Lingteng Qiu 235 Dec 19, 2022
Lexical Substitution Framework

LexSubGen Lexical Substitution Framework This repository contains the code to reproduce the results from the paper: Arefyev Nikolay, Sheludko Boris, P

Samsung 37 Sep 15, 2022
Codes for the AAAI'22 paper "TransZero: Attribute-guided Transformer for Zero-Shot Learning"

TransZero [arXiv] This repository contains the testing code for the paper "TransZero: Attribute-guided Transformer for Zero-Shot Learning" accepted to

Shiming Chen 52 Jan 01, 2023
Emotion Recognition from Facial Images

Reconhecimento de Emoções a partir de imagens faciais Este projeto implementa um classificador simples que utiliza técncias de deep learning e transfe

Gabriel 2 Feb 09, 2022
Mask-invariant Face Recognition through Template-level Knowledge Distillation

Mask-invariant Face Recognition through Template-level Knowledge Distillation This is the official repository of "Mask-invariant Face Recognition thro

Fadi Boutros 35 Dec 06, 2022
Lama-cleaner: Image inpainting tool powered by LaMa

Lama-cleaner: Image inpainting tool powered by LaMa

Qing 5.8k Jan 05, 2023
Production First and Production Ready End-to-End Speech Recognition Toolkit

WeNet 中文版 Discussions | Docs | Papers | Runtime (x86) | Runtime (android) | Pretrained Models We share neural Net together. The main motivation of WeN

2.7k Jan 04, 2023
Least Square Calibration for Peer Reviews

Least Square Calibration for Peer Reviews Requirements gurobipy - for solving convex programs GPy - for Bayesian baseline numpy pandas To generate p

Sigma <a href=[email protected]"> 1 Nov 01, 2021
A toolkit for controlling Euro Truck Simulator 2 with python to develop self-driving algorithms.

europilot Overview Europilot is an open source project that leverages the popular Euro Truck Simulator(ETS2) to develop self-driving algorithms. A con

1.4k Jan 04, 2023
A more easy-to-use implementation of KPConv

A more easy-to-use implementation of KPConv This repo contains a more easy-to-use implementation of KPConv based on PyTorch. Introduction KPConv is a

Zheng Qin 35 Dec 14, 2022