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
TensorFlowOnSpark brings TensorFlow programs to Apache Spark clusters.

TensorFlowOnSpark TensorFlowOnSpark brings scalable deep learning to Apache Hadoop and Apache Spark clusters. By combining salient features from the T

Yahoo 3.8k Jan 04, 2023
Solution to the Weather4cast 2021 challenge

This code was used for the entry by the team "antfugue" for the Weather4cast 2021 Challenge. Below, you can find the instructions for generating predi

Jussi Leinonen 13 Jan 03, 2023
Source code of the paper Meta-learning with an Adaptive Task Scheduler.

ATS About Source code of the paper Meta-learning with an Adaptive Task Scheduler. If you find this repository useful in your research, please cite the

Huaxiu Yao 16 Dec 26, 2022
Self-supervised Multi-modal Hybrid Fusion Network for Brain Tumor Segmentation

JBHI-Pytorch This repository contains a reference implementation of the algorithms described in our paper "Self-supervised Multi-modal Hybrid Fusion N

FeiyiFANG 5 Dec 13, 2021
🔥3D-RecGAN in Tensorflow (ICCV Workshops 2017)

3D Object Reconstruction from a Single Depth View with Adversarial Learning Bo Yang, Hongkai Wen, Sen Wang, Ronald Clark, Andrew Markham, Niki Trigoni

Bo Yang 125 Nov 26, 2022
Pynomial - a lightweight python library for implementing the many confidence intervals for the risk parameter of a binomial model

Pynomial - a lightweight python library for implementing the many confidence intervals for the risk parameter of a binomial model

Demetri Pananos 9 Oct 04, 2022
Implementation of ToeplitzLDA for spatiotemporal stationary time series data.

Code for the ToeplitzLDA classifier proposed in here. The classifier conforms sklearn and can be used as a drop-in replacement for other LDA classifiers. For in-depth usage refer to the learning from

Jan Sosulski 5 Nov 07, 2022
converts nominal survey data into a numerical value based on a dictionary lookup.

SWAP RATE Converts nominal survey data into a numerical values based on a dictionary lookup. It allows the user to switch nominal scale data from text

Jake Rhodes 1 Jan 18, 2022
Deep universal probabilistic programming with Python and PyTorch

Getting Started | Documentation | Community | Contributing Pyro is a flexible, scalable deep probabilistic programming library built on PyTorch. Notab

7.7k Dec 30, 2022
List of content farm sites like g.penzai.com.

内容农场网站清单 Google 中文搜索结果包含了相当一部分的内容农场式条目,比如「小 X 知识网」「小 X 百科网」。此种链接常会 302 重定向其主站,页面内容为自动生成,大量堆叠关键字,揉杂一些爬取到的内容,完全不具可读性和参考价值。 尤为过分的是,该类网站可能有成千上万个分身域名被 Goog

WDMPA 541 Jan 03, 2023
Continuous Augmented Positional Embeddings (CAPE) implementation for PyTorch

PyTorch implementation of Continuous Augmented Positional Embeddings (CAPE), by Likhomanenko et al. Enhance your Transformer positional embeddings with easy-to-use augmentations!

Guillermo Cámbara 26 Dec 13, 2022
Numenta Platform for Intelligent Computing is an implementation of Hierarchical Temporal Memory (HTM), a theory of intelligence based strictly on the neuroscience of the neocortex.

NuPIC Numenta Platform for Intelligent Computing The Numenta Platform for Intelligent Computing (NuPIC) is a machine intelligence platform that implem

Numenta 6.3k Dec 30, 2022
Udacity Suse Cloud Native Foundations Scholarship Course Walkthrough

SUSE Cloud Native Foundations Scholarship Udacity is collaborating with SUSE, a global leader in true open source solutions, to empower developers and

Shivansh Srivastava 34 Oct 18, 2022
FedScale: Benchmarking Model and System Performance of Federated Learning

FedScale: Benchmarking Model and System Performance of Federated Learning (Paper) This repository contains scripts and instructions of building FedSca

268 Jan 01, 2023
Open source code for the paper of Neural Sparse Voxel Fields.

Neural Sparse Voxel Fields (NSVF) Project Page | Video | Paper | Data Photo-realistic free-viewpoint rendering of real-world scenes using classical co

Meta Research 647 Dec 27, 2022
PixelPyramids: Exact Inference Models from Lossless Image Pyramids (ICCV 2021)

PixelPyramids: Exact Inference Models from Lossless Image Pyramids This repository contains the PyTorch implementation of the paper PixelPyramids: Exa

Visual Inference Lab @TU Darmstadt 8 Dec 11, 2022
[ICSE2020] MemLock: Memory Usage Guided Fuzzing

MemLock: Memory Usage Guided Fuzzing This repository provides the tool and the evaluation subjects for the paper "MemLock: Memory Usage Guided Fuzzing

Cheng Wen 54 Jan 07, 2023
Code for the paper "Spatio-temporal Self-Supervised Representation Learning for 3D Point Clouds" (ICCV 2021)

Spatio-temporal Self-Supervised Representation Learning for 3D Point Clouds This is the official code implementation for the paper "Spatio-temporal Se

Hesper 63 Jan 05, 2023
NAS Benchmark in "Prioritized Architecture Sampling with Monto-Carlo Tree Search", CVPR2021

NAS-Bench-Macro This repository includes the benchmark and code for NAS-Bench-Macro in paper "Prioritized Architecture Sampling with Monto-Carlo Tree

35 Jan 03, 2023
BoxInst: High-Performance Instance Segmentation with Box Annotations

Introduction This repository is the code that needs to be submitted for OpenMMLab Algorithm Ecological Challenge, the paper is BoxInst: High-Performan

88 Dec 21, 2022