Code Monkey home page Code Monkey logo

deep-learning-models's Introduction

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

deep-learning-models's People

Contributors

fchollet avatar joeyhng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deep-learning-models's Issues

IOError: Unable to open file (Unable to find a valid file signature)

Seems some problem with weights format?

model.load_weights(weights_path)
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 2532, in load_weights
 f = h5py.File(filepath, mode='r')
File "/usr/local/lib/python2.7/dist-packages/h5py/_hl/files.py", line 260, in __init__
 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "/usr/local/lib/python2.7/dist-packages/h5py/_hl/files.py", line 89, in make_fid
 fid = h5f.open(name, flags, fapl=fapl)
File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip-build-eziK5R/h5py/h5py/_objects.c:2574)
File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip-build-eziK5R/h5py/h5py/_objects.c:2533)
File "h5py/h5f.pyx", line 76, in h5py.h5f.open (/tmp/pip-build-eziK5R/h5py/h5py/h5f.c:1811)
IOError: Unable to open file (Unable to find a valid file signature)

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))

Inception preprocessing issue

I have a problem with using Inception for fine-tuning. Actually, there is either an issue with initalized Inception itself according to its predictions or the preprocessing script is not suitable for both ResNet and Inception models.

Running

import numpy as np

from keras.preprocessing import image

from imagenet_utils import preprocess_input, decode_predictions
from resnet50 import ResNet50

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))

gives me African_elephant with p=0.97174376, whilst running

import numpy as np

from keras.preprocessing import image

from imagenet_utils import preprocess_input, decode_predictions
from inception_v3 import InceptionV3

model = InceptionV3(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(299, 299))
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))

states that my elephant is a suspension_bridge with p=0.99999976.

I've already tried deleting keras cache and I use tf backend.

UPD. It occurs that the latter is true — preprocess_input from imagenet_utils is for ResNet only (at least not for Inception), and the Inception header should look like this

from imagenet_utils import decode_predictions
from inception_v3 import InceptionV3, preprocess_input

that leads to reasonable outputs in the end.

Anyway taking out some preprocessing function into imagenet_utils script while keeping another imagenet-targeted one in model file is not expected behavior. Main keras repository replicates that. Mind me doing PR draft for better distinction of different preprocesing functions?

identification verification code use keras

I am use keras to do some Deep Learning Project.
I Want to write a program can identification verification code(4 chars only numbers and uppercase letters),so I think this model should have one input and four outputs,but I don't know how to write this model,I am a newcomer in Deep Learning. Thank you for read and help.

model.load_weight(path, by_name=True) throws error for Xception model

I was trying to use Xception model for fine-tuning and came across this error.

How to reproduce this error?

  1. In xception.py file, just change model.load_weights(weights_path) to model.load_weights(weights_path, by_name=True) and run. You will get the following traceback.
Traceback (most recent call last):
  File "xception.py", line 227, in <module>
    model = Xception(include_top=True, weights='imagenet')
  File "xception.py", line 212, in Xception
    model.load_weights(weights_path, by_name=True)
  File "/home/anish/env/local/lib/python2.7/site-packages/Keras-1.1.1-py2.7.egg/keras/engine/topology.py", line 2498, in load_weights
    self.load_weights_from_hdf5_group_by_name(f)
  File "/home/anish/env/local/lib/python2.7/site-packages/Keras-1.1.1-py2.7.egg/keras/engine/topology.py", line 2633, in load_weights_from_hdf5_group_by_name
    K.batch_set_value(weight_value_tuples)
  File "/home/anish/env/local/lib/python2.7/site-packages/Keras-1.1.1-py2.7.egg/keras/backend/tensorflow_backend.py", line 990, in batch_set_value
    assign_op = x.assign(assign_placeholder)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 555, in assign
    return state_ops.assign(self._variable, value, use_locking=use_locking)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_state_ops.py", line 47, in assign
    use_locking=use_locking, name=name)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2261, in create_op
    set_shapes_for_outputs(ret)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1636, in set_shapes_for_outputs
    shapes = shape_func(op)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1570, in call_with_requiring
    return call_cpp_shape_fn(op, require_shape_fn=True)
  File "/home/anish/env/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 643, in call_cpp_shape_fn
    raise ValueError(err.message)
ValueError: Dimension 0 in both shapes must be equal, but are 1 and 3 for 'Assign' (op: 'Assign') with input shapes: [1,1,64,128], [3,3,3,32].

Workaround:

base_model = Xception(include_top=True, weights='imagenet')
model = Model(input=base_model.input, output=<fine-tune model>)

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?

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

Labels picked up from imagenet_class_index.json

I have imagenet 2014 val data with me and the index predicted by the model does not match the ground truth. I can see that the label (text ) suits the image well but the index does not. How is this indexing done in json file ?

Any pointers would be appreciated .

preprocess_input() zero-centering channels are backwards

In
ksimonyan/VGG_ILSVRC_16_layers_deploy.prototxt, it states:

The input images should be zero-centered by mean pixel (rather than mean image) subtraction. Namely, the following BGR values should be subtracted: [103.939, 116.779, 123.68].

preprocess_input() implements like so:

x[:, :, :, 0] -= 103.939
x[:, :, :, 1] -= 116.779
x[:, :, :, 2] -= 123.68
# 'RGB'->'BGR'
x = x[:, :, :, ::-1]

Since the values are subtracted before the channels are swapped, the (R) mean is subtracted from the (B) channel and vice versa.

Of course, it may be that this version is correct and ksimonyan/VGG_ILSVRC_16_layers_deploy.prototxt has the order backwards

Finetune ResNet meets error, please help me~

488452559
841787571
1657741171

I finetuned vgg-16 on my datasets, it works correctly, but when I turns to ResNet50, just changed the model and the weights, but it reports error like above, it says there's dimension mismatched in the median part of model, I am disturbed by this problem.
At present my computer can't connect to Internet, so I have to take photos, sorry for that.
Keras version is 1.1.1, theano version is 0.8.2 and Ubuntu 14.04

Example on README.md don't work

numpy is not imported on the example

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))

import numpy as np should solve the problem.

Memory issue for Inception V3 pretrained model

I tried using a EC2 g2.2xlarge instance to run inception v3 (one of the pretrained models in Keras), and it didm’t work (tried theano first, then Tensorflow). When I do model.fit() it would freeze after the first 200 samples. Is this a memory issue? It did not give an error it would just freeze up my browser. I did a similar code for VGG16 pretrained model following Chollet's tutorial Building powerful image classification models using very little data and it ran smoothly.

It was this ami (https://aws.amazon.com/marketplace/pp/B01EYKBEQ0?qid=1487097763767&sr=0-1&ref_=srh_res_product_title), which has Cuda 7.5 Toolkit, cuDNN 5.1, TensorFlow 0.12. And I was training 22000 images (each has size 139X139, minimum requirement for inception), and I followed this tutorial
https://github.com/yinniyu/kaggle_solutions/blob/master/dogs_vs_cats/03_train_head_on_bottlenecks.ipynb. The tutorial was done with ResNet, but I wanted to try InceptionV3 since it can take smaller image size than ResNet.

Did anyone else run into similar issue? I'd appreciate any feedback on solving this problem. Thank you!

_obtain_input_shape

For some reason, I am unable to import _obtain_input_shape

My line:
from keras.applications.imagenet_utils import decode_predictions, preprocess_input, _obtain_input_shape

Thank you for all the outstanding work! Happy Holidays!

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?

weights path

Hello, I am just wondering where are the weights path for resnet 50, it seems that it is deleted.

Create a ResNet50 model fails

Hi,

I have a problem when I try to create a ResNet50 model. I am getting the next error:

Note: My keras version is keras.version='1.1.0'

Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels_notop.h5

Exception Traceback (most recent call last)
in ()
23 # CNN CONFIGURATION
24 frame = Input(shape=frameDims)
---> 25 resNet50 = ResNet50(include_top=False, weights='imagenet')
26 resNet50.trainable = False
27 flat = FlattenK()

/home/fhdiaze/.local/lib/python2.7/site-packages/keras/applications/resnet50.pyc in ResNet50(include_top, weights, input_tensor)
207 TH_WEIGHTS_PATH_NO_TOP,
208 cache_subdir='models',
--> 209 md5_hash='f64f049c92468c9affcd44b0976cdafe')
210 model.load_weights(weights_path)
211 if K.backend() == 'tensorflow':

/home/fhdiaze/.local/lib/python2.7/site-packages/keras/utils/data_utils.pyc in get_file(fname, origin, untar, md5_hash, cache_subdir)
82 urlretrieve(origin, fpath, dl_progress)
83 except URLError as e:
---> 84 raise Exception(error_msg.format(origin, e.errno, e.reason))
85 except HTTPError as e:
86 raise Exception(error_msg.format(origin, e.code, e.msg))

Exception: URL fetch failure on https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels_notop.h5: None -- [Errno 111] Connection refused

Thanks.

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.

VGG-16 fine-tuning is hard to converge

I have tried to fine-tune VGG-16 on Stanford Cars dataset, but it's hard to converge. The loss is around 5.27 and doesn't decrease any more. What's the matter?

'''
Train on a Titan X GPU.
'''

from __future__ import print_function
import numpy as np
np.random.seed(1337)  # for reproducibility

from keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input, Dense, RepeatVector, Permute, merge
from keras.layers import Dense, Dropout, Activation, Flatten, Reshape
from keras.layers import Convolution2D, MaxPooling2D, Convolution1D
from keras.models import Model
from keras.optimizers import SGD
from keras.utils import np_utils
from keras import backend as K
from keras.utils.visualize_util import plot
from deep_learning_models.vgg16 import VGG16
import stanford_cars

batch_size = 64
nb_epoch = 12

# number of classes 
nb_classes = 196
# input image dimensions
img_rows, img_cols = 224, 224

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = stanford_cars.load_data(target_size=(img_rows, img_cols))
if K.image_dim_ordering() == 'th':
    X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
    X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
    input_shape = (3, img_rows, img_cols)
else:
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3)
    X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3)
    input_shape = (img_rows, img_cols, 3)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(X_train[0][1])
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
print(y_train[-10:])
# model define
inputs = Input(shape=input_shape)
model = VGG16(input_tensor=inputs, include_top=True, 
              weights='imagenet', nb_classes=nb_classes)
plot(model, show_shapes=True, to_file='final_gru_model.png')

# data augment this will do preprocessing and realtime data augmentation
datagen = ImageDataGenerator(
    featurewise_center=False,  # set input mean to 0 over the dataset
    samplewise_center=False,  # set each sample mean to 0
    featurewise_std_normalization=False,  # divide inputs by std of the dataset
    samplewise_std_normalization=False,  # divide each input by its std
    zca_whitening=False,  # apply ZCA whitening
    rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)
    width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)
    height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)
    horizontal_flip=True,  # randomly flip images
    vertical_flip=False)  # randomly flip images

datagen.fit(X_train)

# train
model.compile(loss='categorical_crossentropy',
              optimizer='adam', metrics=['accuracy'])
model.fit_generator(datagen.flow(X_train, Y_train,
                    batch_size=batch_size),
                    samples_per_epoch=X_train.shape[0],
                    nb_epoch=nb_epoch,
                    validation_data=(X_test, Y_test),
                    verbose=1)

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd, metrics=['accuracy'])
model.fit_generator(datagen.flow(X_train, Y_train,
                    batch_size=batch_size),
                    samples_per_epoch=X_train.shape[0],
                    nb_epoch=nb_epoch,
                    validation_data=(X_test, Y_test),
                    verbose=1)


# test
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])

Not converging when using cached ResNet50 features

I'm trying to use ResNet50 from keras.applications as a feature extractor for my model. I successfully accomplished that with this:

inputs = Input(shape=(224, 224, 3))
base_model = ResNet50(include_top=False, weights='imagenet', input_tensor=inputs)

for layer in base_model.layers:
    layer.trainable = False

conv_feature = Flatten()(base_model.output)

x = Dense(512, activation='relu', W_regularizer=l2(l=0.01))(conv_feature)
x = Dropout(p=0.5)(x)
cls = Dense(20, activation='softmax', name='cls')(x)

self.model = Model(input=base_model.input, output=cls)

However, to speed up the experiment, I'd like to cache the extracted features:

inputs = Input(shape=(224, 224, 3))
model = ResNet50(include_top=False, weights='imagenet', input_tensor=inputs)
generator = gen.pascal_datagen_singleobj(64, include_label=False, random=False) # my custom generator

features = model.predict_generator(generator, gen.pascal.train_set.size)

np.save('cnn_features_train.npy', features)

It should basically be the same with the first code above up to conv_feature. I then use the cached features for my FC layers:

X_train, y_train = load_train_data() # np.load('cnn_features_train.npy')

inputs = Input(shape=(X_train.shape[1:]))
conv_features = Flatten()(inputs)

x = Dense(512, activation='relu', W_regularizer=l2(l=0.01))(conv_features)
x = Dropout(p=0.5)(x)
cls = Dense(20, activation='softmax', name='cls')(x)

But then, the optimization result for those two (should be) equivalent model is different. The extracted features perform way worse to the point it won't even converge.

# Using ResNet as feature extractor in online manner
Epoch 1/20
4800/4800 [==============================] - 62s - loss: 6.5075 - acc: 0.7392       
Epoch 2/20
4800/4800 [==============================] - 58s - loss: 2.8369 - acc: 0.8435     
Epoch 3/20
4800/4800 [==============================] - 61s - loss: 1.6589 - acc: 0.8608   

# Using offline extracted features
Epoch 1/50
4956/4956 [==============================] - 0s - loss: 10.0733 - acc: 0.1354      
Epoch 2/50
4956/4956 [==============================] - 0s - loss: 8.0192 - acc: 0.1336     
Epoch 3/50
4956/4956 [==============================] - 0s - loss: 6.5425 - acc: 0.1499
.
.
.
Epoch 48/50
4956/4956 [==============================] - 0s - loss: 2.6887 - acc: 0.1461     
Epoch 49/50
4956/4956 [==============================] - 0s - loss: 2.6886 - acc: 0.1461     
Epoch 50/50
4956/4956 [==============================] - 0s - loss: 2.6887 - acc: 0.1461

On a side note, I've tried VGG16 features, and it works.

Any thoughts?

Question: Converting from caffe/torch models.

Did you use any utilities to convert from caffe/torch models? If yes, could you paste links to those projects? It would probably help others (who are converting models) if you mentioned them in README

How can I solve the socket.error: [Errno 104] Connection reset by peer?

when I run the: python vgg19.py, the got the error:
Traceback (most recent call last):
File "vgg19.py", line 158, in
model = VGG19(include_top=True, weights='imagenet')
File "vgg19.py", line 146, in VGG19
cache_subdir='models')
File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 113, in get_file
functools.partial(dl_progress, progbar=progbar))
File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 52, in urlretrieve
for chunk in chunk_read(response, reporthook=reporthook):
File "/usr/local/lib/python2.7/dist-packages/keras/utils/data_utils.py", line 41, in chunk_read
chunk = response.read(chunk_size)
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/python2.7/httplib.py", line 573, in read
s = self.fp.read(amt)
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/python2.7/ssl.py", line 341, in recv
return self.read(buflen)
File "/usr/lib/python2.7/ssl.py", line 260, in read
return self._sslobj.read(len)
socket.error: [Errno 104] Connection reset by peer

ValueError: CorrMM images and kernel must have the same stack size while running the example script

Hi,

I have been trying to follow the example script of

model = ResNet50(weights='imagenet')
img_path = '/data/dsp_emerging/ugwz/elephants.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))

However, I got the following error message of ValueError: CorrMM images and kernel must have the same stack size. I did not change anything in the code. Keras is of version 2.0.3 and Theano is of version 0.9.0

The complete traceback is

Traceback (most recent call last):
  File "example.py", line 15, in <module>
    preds = model.predict(x)
  File "/tmp/lib/python3.5/site-packages/keras/engine/training.py", line 1573, in predict
    batch_size=batch_size, verbose=verbose)
  File "/tmp/lib/python3.5/site-packages/keras/engine/training.py", line 1203, in _predict_loop
    batch_outs = f(ins_batch)
  File "/tmp/lib/python3.5/site-packages/keras/backend/theano_backend.py", line 1122, in __call__
    return self.function(*inputs)
  File "/tmp/lib/python3.5/site-packages/theano/compile/function_module.py", line 898, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File "/tmp/lib/python3.5/site-packages/theano/gof/link.py", line 325, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "/tmp/lib/python3.5/site-packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/tmp/lib/python3.5/site-packages/theano/compile/function_module.py", line 884, in __call__
    self.fn() if output_subset is None else\
ValueError: CorrMM images and kernel must have the same stack size

Apply node that caused the error: CorrMM{valid, (2, 2), (1, 1)}(InplaceDimShuffle{0,3,1,2}.0, Subtensor{::, ::, ::int64, ::int64}.0)
Toposort index: 444
Inputs types: [TensorType(float32, 4D), TensorType(float32, 4D)]
Inputs shapes: [(1, 3, 230, 230), (7, 7, 64, 3)]
Inputs strides: [(12, 4, 2760, 12), (4, 28, -588, -196)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[InplaceDimShuffle{0,2,3,1}(CorrMM{valid, (2, 2), (1, 1)}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "example.py", line 6, in <module>
    model = ResNet50(weights='imagenet')
  File "/home/DL-Phase2/keras-DLmodel/resnet50.py", line 208, in ResNet50
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
  File "/tmp/lib/python3.5/site-packages/keras/engine/topology.py", line 578, in __call__
    output = self.call(inputs, **kwargs)
  File "/tmp/lib/python3.5/site-packages/keras/layers/convolutional.py", line 164, in call
    dilation_rate=self.dilation_rate)
  File "/tmp/lib/python3.5/site-packages/keras/backend/theano_backend.py", line 1769, in conv2d
    filter_dilation=dilation_rate)


No possible to extract features from resnet

When using resnet50 as feature extractor, by putting include_top=false I receive the following error with both tf or th image orderings.

Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5
94609408/94653016 [============================>.] - ETA: 0sTraceback (most recent call last):
  File "/home/omar/Pycharm_ubuntu_v2/Spatial_v2_Aug-2016/features_from_keras_tool_RGB_final.py", line 79, in <module>
    features = model.predict(train_data)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1180, in predict
    batch_size=batch_size, verbose=verbose)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 879, in _predict_loop
    batch_outs = f(ins_batch)
  File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 657, in __call__
    return self.function(*inputs)
  File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 871, in __call__
    storage_map=getattr(self.fn, 'storage_map', None))
  File "/usr/local/lib/python2.7/dist-packages/theano/gof/link.py", line 314, in raise_with_op
    reraise(exc_type, exc_value, exc_trace)
  File "/usr/local/lib/python2.7/dist-packages/theano/compile/function_module.py", line 859, in __call__
    outputs = self.fn()
ValueError: GpuDnnConv images and kernel must have the same stack size

Apply node that caused the error: GpuDnnConv{algo='small', inplace=True}(GpuContiguous.0, GpuContiguous.0, GpuAllocEmpty.0, GpuDnnConvDesc{border_mode='valid', subsample=(1, 1), conv_mode='conv', precision='float32'}.0, Constant{1.0}, Constant{0.0})
Toposort index: 1606
Inputs types: [CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, 4D), <theano.gof.type.CDataType object at 0x7f91eeca4e10>, Scalar(float32), Scalar(float32)]
Inputs shapes: [(32, 128, 28, 28), (512, 256, 1, 1), (32, 512, 28, 28), 'No shapes', (), ()]
Inputs strides: [(100352, 784, 28, 1), (256, 1, 0, 0), (401408, 784, 28, 1), 'No strides', (), ()]
Inputs values: ['not shown', 'not shown', 'not shown', <PyCObject object at 0x7f91a9f447d8>, 1.0, 0.0]
Inputs name: ('image', 'kernel', 'output', 'descriptor', 'alpha', 'beta')

Outputs clients: [[GpuDimShuffle{0,2,3,1}(GpuDnnConv{algo='small', inplace=True}.0)]]

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.

How to interpret the output of the feature extractor?

Hi,

I followed the instruction on the README to extract feature from an image by using VGG16 model. When I did

features = model.predict(x)

I found the dimension of the features is of (1, 7, 7, 512). I thought for feature extraction, each image should be represented as a single vector? I'm wondering how to interpret this output and the dimension properly?

Thanks.

Train inception

Hi everyone,
I'm trying to train the inception model from custom images.
But I got one problems..
Here is the code I use
`import os
import numpy as np
import pandas as pd
from scipy.misc import imread
from scipy.misc import imresize
from inception_v3 import InceptionV3
import keras

root_dir = os.path.abspath('.')
data_dir = os.path.join(root_dir, 'data')
os.path.exists(root_dir)
os.path.exists(data_dir)

train = pd.read_csv(os.path.join(data_dir, 'images', 'test.csv'))
train.head()
temp = []
for img_name in train.filename:
image_path = os.path.join(data_dir, 'images', img_name)
img_tmp = imread(image_path)
img = imresize(img_tmp, (299, 299))
img = img.astype('float32')
temp.append(img)

train_x = np.stack(temp)

split_size = int(train_x.shape[0]*0.7)

train_x = train_x#, train_x[split_size:]
train_y = train.label#,train.label.ix[split_size:]
inception_model = InceptionV3(weights=None)
inception_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
trained_model = inception_model.fit(train_x, train_y, validation_data=0.33, nb_epoch=1, batch_size=8)
trained_model.save('car_model.h5')
print ('Fin de l'entrainement')
`
But I got this error :
Exception: Error when checking model target: expected predictions to have shape (None, 1000) but got array with shape (9, 1)

Do you know why ?
I think I did all good..

PS : For the shape(9,1), I guess the 9 refers to my 9 classes than I want to learn from

Xception: Is there a need for batch_norm to be deactivated during evaluation?

In the inception models (for TF-slim implementations), it seems that batch norm is deactivated once the model is sent for evaluation. Does Keras have this functionality as well, and is this option available for the Xception model?

Is there a need for deactivating the batch_norm during evaluation?

Thank you! :D

Error on trying to use inception_v3.py

I'm trying to use your inception_v3.py by following the example:

from inception_v3 import InceptionV3
from keras.preprocessing import image
from imagenet_utils import preprocess_input, decode_predictions

model = InceptionV3(weights='imagenet')

img_path = 'data/cat.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))

And as a result I'm getting this error

Using Theano backend.
Traceback (most recent call last):
  File "iv3.py", line 5, in <module>
    model = InceptionV3(weights='imagenet')
  File "inception_v3.py", line 281, in InceptionV3
    model.load_weights(weights_path)
  File "lib/python2.7/site-packages/keras/engine/topology.py", line 2446, in load_weights
    self.load_weights_from_hdf5_group(f)
  File "lib/python2.7/site-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.

ResNet50 layer names

Can I send a trivial pull request that names all the unnamed ResNet50 layers? Names like act3a_branch2a. merge_3b, act_3b max_pool etc.

conv_utils.convert_kernel [bug]

When converting weights (InceptionV3 for instance), if the backend is switched, loading crashes with an error pointing to h5py. It looks like since the conv2d layers are usually included last in any h5 file they are the only ones who have their indexes checked but h5py expects the index to be > 0 as it is not the first layer in the file.

How to reproduce:

  1. Change your backend to 'theano' in the keras.json config file
  2. Run this script:
from keras.applications.inception_v3 import *
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions
from keras import backend as K
import numpy as np

model = InceptionV3(weights='imagenet')

img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(299, 299))
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))

With the Tensorflow backend this works fine and outputs the following:

[[(u'n02504458', u'African_elephant', 0.90738213)]]

However, if the backend is switched to Theano you get the following error (full traceback):

Using Theano backend.
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    model = InceptionV3(weights='imagenet')
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/keras/applications/inception_v3.py", line 383, in InceptionV3
    model.load_weights(weights_path)
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2491, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2893, in load_weights_from_hdf5_group
    original_backend)
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2833, in preprocess_weights_for_loading
    weights[0] = conv_utils.convert_kernel(weights[0])
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/keras/utils/conv_utils.py", line 86, in convert_kernel
    return np.copy(kernel[slices])
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (/tmp/pip-nCYoKW-build/h5py/_objects.c:2840)
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper (/tmp/pip-nCYoKW-build/h5py/_objects.c:2798)
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/h5py/_hl/dataset.py", line 474, in __getitem__
    selection = sel.select(self.shape, args, dsid=self.id)
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/h5py/_hl/selections.py", line 90, in select
    sel[args]
  File "/home/kent/.virtualenvs/keras2/local/lib/python2.7/site-packages/h5py/_hl/selections.py", line 363, in __getitem__
    raise TypeError("Indexing elements must be in increasing order")
TypeError: Indexing elements must be in increasing order

This is definitely unexpected behavior, however, I'm unable to see an obvious issue anywhere in the Keras-2 changes which is why I'm posting it here.

How to get pool_3 layer from inception using Keras

I used the following code following example in Readme.md


    base_model = InceptionV3(include_top=False, weights='imagenet')
    model = Model(input=base_model.input, output=base_model.get_layer('pool_3').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)

    pool_3_features = model.predict(x)


I get following error. What am I doing wrong.

Traceback (most recent call last):

  File "inception_v3.py", line 420, in <module>
    model = Model(input=base_model.input, output=base_model.get_layer('pool_3').output)
AttributeError: 'NoneType' object has no attribute 'output'

Inveption V3 weights loading problem

this code:
model=InceptionV3(include_top=False, weights='imagenet', input_tensor=None, input_shape=None, pooling=None)
always return this error

Exception: URL fetch failure on https://github.com/fchollet/deep-learning-models/releases/download/v0.5/inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5: None -- [Errno 10060]

So I want to manully download the weight file and use the model.load_weights method. However for keras 2 there are only TF weight file . I am using Theano 0.9.0 as backend.
Is there any way to load inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5 while using Theano?

Also , How to fix this URL fetch failure

"IndexError: index 64 is out of bounds for axis 1 with size 64"

I am getting the following error trace while using resnet50 given here-

IndexError: index 64 is out of bounds for axis 1 with size 64
Apply node that caused the error: AbstractConv2d{border_mode='valid', subsample=(1, 1), filter_flip=True, imshp=(None, None, None, None), kshp=(64, 64, 1, 1)}(DimShuffle{0,3,1,2}.0, DimShuffle{3,2,0,1}.0)
Toposort index: 1894
Inputs types: [TensorType(float32, 4D), TensorType(float32, 4D)]
Inputs shapes: [(1L, 112L, 31L, 55L), (64L, 64L, 1L, 1L)]
Inputs strides: [(763840L, 6820L, 220L, 4L), (4L, 256L, 4L, 4L)]
Inputs values: ['not shown', 'not shown']
Outputs clients: [[DimShuffle{0,2,3,1}(AbstractConv2d{border_mode='valid', subsample=(1, 1), filter_flip=True, imshp=(None, None, None, None), kshp=(64, 64, 1, 1)}.0)]]

Backtrace when the node is created(use Theano flag traceback.limit=N to make it longer):
  File "C:/Users/Widow Maker/Virgillos/check.py", line 6, in <module>
    model = ResNet50(weights='imagenet')
  File "resnet50.py", line 170, in ResNet50
    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
  File "resnet50.py", line 90, in conv_block
    name=conv_name_base + '2a')(input_tensor)
  File "C:\Users\Widow Maker\Anaconda2\lib\site-packages\keras\engine\topology.py", line 514, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
  File "C:\Users\Widow Maker\Anaconda2\lib\site-packages\keras\engine\topology.py", line 572, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "C:\Users\Widow Maker\Anaconda2\lib\site-packages\keras\engine\topology.py", line 149, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "C:\Users\Widow Maker\Anaconda2\lib\site-packages\keras\layers\convolutional.py", line 466, in call
    filter_shape=self.W_shape)
  File "C:\Users\Widow Maker\Anaconda2\lib\site-packages\keras\backend\theano_backend.py", line 1307, in conv2d
    filter_shape=filter_shape)

I am using the keras version 1.1.1 , and running the demo examples provided

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

model = ResNet50(weights='imagenet')

img_path = 'ele.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))

I tried using the 1.1.0 version which did not gave the above error , but it was always predicting the same value for any given image

I have a separate keras 1.1.0 installation on a different pc which runs fine .

cannot download vgg16_weights

Hi fchollet,
Now, I'm using vgg16 under Keras to classify oxford 102 flower dataset and I want to download the trained weight file: vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5. But I cannot download it...
Wourld you please email this file to me? thank u very much.
my email: [email protected]

Are you sure about your ResNet architecture?

Are you sure about your ResNet architecture?
In the document https://arxiv.org/pdf/1512.03385.pdf
At the beginning of the network we have

    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

But in the resnet50.py

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

Please explain why so

ValueError: The model expects 0 input arrays, but only received one array.

Hi,i am in trouble when i use the pre-train model ''InceptionV3" in keras.
here's my code:

width = image_size[0]
height = image_size[1]
input_tensor = Input((height, width, 3))
x = input_tensor

base_model = InceptionV3(input_tensor=x, weights='imagenet', include_top=False)
model = Model(base_model.input, GlobalAveragePooling2D()(base_model.output))

gen = ImageDataGenerator()
train_generator = gen.flow_from_directory("train2", image_size, shuffle=False,batch_size=16)
test_generator = gen.flow_from_directory("test2", image_size, shuffle=False, batch_size=16, class_mode=None)
                                             
train_step = train_generator.samples / (float)(train_generator.batch_size )
test_step = test_generator.samples / (float)(test_generator.batch_size )
  
train = model.predict_generator(train_generator, train_step)
test = model.predict_generator(test_generator, test_step)

but when this code runs,it happens a ValueError. here is the traceback:

    train = model.predict_generator(train_generator, train_step)
  File "/usr/local/lib/python2.7/dist-packages/keras/legacy/interfaces.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 2094, in predict_generator
    outs = self.predict_on_batch(x)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1677, in predict_on_batch
    self._feed_input_shapes)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 100, in _standardize_input_data
    'Found: array with shape ' + str(data.shape))
ValueError: The model expects 0 input arrays, but only received one array. Found: array with shape (16, 299, 299, 3)

what's wrong with my code? it is OK when i use the older version of InceptionV3, and now it is still OK when i use ResNet50 and Xception model. could you help me? , thanks :(

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

Same prediction for all inputs with inception model

Hi everyone!
I trained an inception model with my custom images (dataset of 8.000 images), then saved it.
And when I try to make predictions with it, whatever is the input images to predict, it's the same output!
I checked everything, and all was seeming good to me...

# Here is the code to train the model
`import os
import numpy as np
import pandas as pd
from scipy.misc import imread
from scipy.misc import imresize
from inception_v3 import InceptionV3
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
import keras
from keras.callbacks import ModelCheckpoint
root_dir = os.path.abspath('.')
data_dir = os.path.join(root_dir, 'data')
os.path.exists(root_dir)
os.path.exists(data_dir)
nb_epoch = int(raw_input("Nb epoch ? : "))
print ('..Debut parsing et resize..')
train = pd.read_csv(os.path.join(data_dir, 'images', 'test.csv'))
train.head()
temp = []
for img_name in train.filename:
image_path = os.path.join(data_dir, 'images', img_name)
img_tmp = imread(image_path, mode='RGB')
img = imresize(img_tmp, (299, 299))
img = img.astype('float32')
temp.append(img)
print ('..Fin parsing et resize..')
train_x = np.stack(temp)
train_x = train_x#, train_x[split_size:]
train_y = keras.utils.np_utils.to_categorical(train.label.values)
base_model = InceptionV3(weights=None, include_top=False)

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(196, activation='softmax')(x)

model = Model(input=base_model.input, output=predictions)

print ('..Debut compilation du model..')
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
print ('..Fin compilation du model..')
filepath="weights-improvement-{epoch:02d}.hdf5"
checkpoint = ModelCheckpoint(filepath, verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
print ('Debut entrainement')
trained_model = model.fit(train_x, train_y, nb_epoch=nb_epoch, callbacks=callbacks_list, batch_size=8)
model.save('car_model.h5')
print ('Fin de l'entrainement')
`

## And here is the code to make predictions

`import os
import numpy as np
import pandas as pd
from scipy.misc import imread
from scipy.misc import imresize
from inception_v3 import InceptionV3
from inception_v3 import preprocess_input
from keras.preprocessing import image
from keras.models import Model
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
import keras
from keras.callbacks import ModelCheckpoint
from imagenet_utils import decode_predictions
root_dir = os.path.abspath('.')
data_dir = os.path.join(root_dir, 'data')
os.path.exists(root_dir)
os.path.exists(data_dir)

base_model = InceptionV3(weights=None, include_top=False)

x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(197, activation='softmax')(x)

model = Model(input=base_model.input, output=predictions)

print ('..Debut compilation du model..')
model.load_weights('car_model.h5')
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
print ('..Fin compilation du model..')

#while True:
#img_path = raw_input('Img path : ')
img_path = '/home/images/truck1.jpg' # Pas propre. A changer
print (img_path)
img_tmp = imread(img_path, mode='RGB')
img = imresize(img_tmp, (299, 299))
img = img.astype('float32')
x_ = np.expand_dims(img, axis=0)
preds = model.predict(x_)
decode_predictions(preds)
print ('Fin prediction')
`

I hope you'll find the problem, because I don't see at all..

(I trained the model over 50 epochs, so the problem don't come for here)

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.