Skip to content

Instantly share code, notes, and snippets.

@baraldilorenzo
Created January 16, 2016 12:57
Show Gist options
  • Save baraldilorenzo/8d096f48a1be4a2d660d to your computer and use it in GitHub Desktop.
Save baraldilorenzo/8d096f48a1be4a2d660d to your computer and use it in GitHub Desktop.
VGG-19 pre-trained model for Keras

##VGG19 model for Keras

This is the Keras model of the 19-layer network used by the VGG team in the ILSVRC-2014 competition.

It has been obtained by directly converting the Caffe model provived by the authors.

Details about the network architecture can be found in the following arXiv paper:

Very Deep Convolutional Networks for Large-Scale Image Recognition
K. Simonyan, A. Zisserman
arXiv:1409.1556

In the paper, the VGG-19 model is denoted as configuration E. It achieves 7.5% top-5 error on ILSVRC-2012-val, 7.3% top-5 error on ILSVRC-2012-test.

Please cite the paper if you use the models.

###Contents:

model and usage demo: see vgg-19_keras.py

weights: vgg19_weights.h5

from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import cv2, numpy as np
def VGG_19(weights_path=None):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
if weights_path:
model.load_weights(weights_path)
return model
if __name__ == "__main__":
im = cv2.resize(cv2.imread('cat.jpg'), (224, 224)).astype(np.float32)
im[:,:,0] -= 103.939
im[:,:,1] -= 116.779
im[:,:,2] -= 123.68
im = im.transpose((2,0,1))
im = np.expand_dims(im, axis=0)
# Test pretrained model
model = VGG_19('vgg19_weights.h5')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy')
out = model.predict(im)
print np.argmax(out)
@lucasdavid
Copy link

This only seems to work for TH backend. You'll need to transpose the matrices if you're using TF.
Also, just for the reference, the VGG16 and VGG19 models pre-trained over imagenet are now available through the applications module of Keras:

from keras.layers import Input
from keras.applications import VGG19

model = VGG19(weights='imagenet')
model.predict(...)

@v7t-codes
Copy link

Hey, I get this error!

/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py:1203:
UserWarning: DEPRECATION: the 'ds' parameter is not going to exist anymore as it is going to be replaced by the parameter 'ws'.
mode='max')
/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py:1203: UserWarning: DEPRECATION: the 'st' parameter is not going to exist anymore as it is going to be replaced by the parameter 'stride'.
mode='max')
/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py:1203: UserWarning: DEPRECATION: the 'padding' parameter is not going to exist anymore as it is going to be replaced by the parameter 'pad'.
mode='max')
Traceback (most recent call last):
File "VGG19.py", line 73, in
model = VGG_19('vgg19_weights.h5')
File "VGG19.py", line 53, in VGG_19
model.add(Dense(4096, activation='relu'))
File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 307, in add
output_tensor = layer(self.outputs[0])
File "/usr/local/lib/python2.7/dist-packages/keras/engine/topology.py", line 484, in call
self.build(input_shapes[0])
File "/usr/local/lib/python2.7/dist-packages/keras/layers/core.py", line 604, in build
name='{}_W'.format(self.name))
File "/usr/local/lib/python2.7/dist-packages/keras/initializations.py", line 59, in glorot_uniform
return uniform(shape, s, name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/initializations.py", line 32, in uniform
return K.random_uniform_variable(shape, -scale, scale, name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 111, in random_uniform_variable
dtype=dtype, name=name)
File "/usr/local/lib/python2.7/dist-packages/keras/backend/theano_backend.py", line 39, in variable
value = np.asarray(value, dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)

MemoryError

Thanks in advance!

@DanlanChen
Copy link

How can I used it for grey scaled image?

@AdityaSoni19031997
Copy link

AdityaSoni19031997 commented Aug 30, 2017

For gray scale we need to change the channel to 1 ...
For colored its 3...
We need to play around with the input parameter of the very first layer...(3->1)

@manimtechrs
Copy link

I have 4 classes to predict . can i use VGG-16 model to predict classes .

@flyingduck92
Copy link

My tensorflow version is 1.8.0

I cannot load the vgg-19 weight file.
This is the error i got:
You are trying to load a weight file containing 0 layers into a model with 19 layers.

@NEUUCAS
Copy link

NEUUCAS commented Mar 17, 2019

My tensorflow version is 1.8.0

I cannot load the vgg-19 weight file.
This is the error i got:
You are trying to load a weight file containing 0 layers into a model with 19 layers.

I have the same error. How to fix it?

@NimmiGhetia
Copy link

NimmiGhetia commented Apr 15, 2019

I am getting this error, can you help me resolve the error. I haven't change the code

Using TensorFlow backend.
vgg-19_keras.py:11: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(64, (3, 3), activation="relu")
model.add(Convolution2D(64, 3, 3, activation='relu'))
WARNING:tensorflow:From /opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
vgg-19_keras.py:13: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(64, (3, 3), activation="relu")
model.add(Convolution2D(64, 3, 3, activation='relu'))
vgg-19_keras.py:17: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(128, (3, 3), activation="relu")
model.add(Convolution2D(128, 3, 3, activation='relu'))
vgg-19_keras.py:19: UserWarning: Update your Conv2D call to the Keras 2 API: Conv2D(128, (3, 3), activation="relu")
model.add(Convolution2D(128, 3, 3, activation='relu'))
Traceback (most recent call last):
File "vgg-19_keras.py", line 73, in
model = VGG_19('vgg19_weights.h5')
File "vgg-19_keras.py", line 20, in VGG_19
model.add(MaxPooling2D((2,2), strides=(2,2)))
File "/opt/conda/envs/py2/lib/python2.7/site-packages/keras/engine/sequential.py", line 181, in add
output_tensor = layer(self.outputs[0])
File "/opt/conda/envs/py2/lib/python2.7/site-packages/keras/engine/base_layer.py", line 457, in call
output = self.call(inputs, **kwargs)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/keras/layers/pooling.py", line 205, in call
data_format=self.data_format)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/keras/layers/pooling.py", line 268, in _pooling_function
pool_mode='max')
File "/opt/conda/envs/py2/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 3978, in pool2d
data_format=tf_data_format)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/ops/nn_ops.py", line 2748, in max_pool
name=name)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 5137, in max_pool
data_format=data_format, name=name)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 788, in _apply_op_helper
op_def=op_def)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3300, in create_op
op_def=op_def)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1823, in init
control_input_ops)
File "/opt/conda/envs/py2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1662, in _create_c_op
raise ValueError(str(e))
ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_2/MaxPool' (op: 'MaxPool') with input shapes: [?,1,112,128].

@b-hakim
Copy link

b-hakim commented Apr 20, 2019

I got the same issue:

ValueError: You are trying to load a weight file containing 0 layers into a model with 19 layers.

@b-hakim
Copy link

b-hakim commented Apr 20, 2019

@NimmiGhetia your problem is in the keras,
edit the
~/.keras/keras.json and change the "image_data_format" to "image_data_format": "channels_first"

@Rouxkein
Copy link

***this my model run complete , i using vgg19 to train data cifar 10 ,you can refer it

from keras.datasets import cifar10
import cv2
import random
import numpy as np
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.optimizers import SGD
import cv2, numpy as np
import tensorflow as tf
#load data
(x_train,y_train),(x_test,y_test)=cifar10.load_data()

train data load 0 to 2000

ind_train = random.sample(list(range(x_train.shape[0])), 2000)
x_train = x_train[ind_train]
y_train = y_train[ind_train]

test data load 0 to 2000

ind_test = random.sample(list(range(x_test.shape[0])), 2000)
x_test = x_test[ind_test]
y_test = y_test[ind_test]
#resize image
def resize_data(data):
data_upscale=np.zeros((data.shape[0],224,224,3))
for i,img in enumerate(data):
l_img=cv2.resize(img,dsize=(224,224),interpolation=cv2.INTER_CUBIC)
data_upscale[i]=l_img
return data_upscale

resize train and test data

x_train_r = resize_data(x_train)
x_test_r = resize_data(x_test)

make explained variable hot-encoded

train_y= to_categorical(y_train)
test_y = to_categorical(y_test)

def VGG_19(weights=None, include_top=True, classes=10,input_shape=(224,224,3)):
model = Sequential()
model.add(ZeroPadding2D((1,1),input_shape=(224,224,3)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(64, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(128, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(256, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(ZeroPadding2D((1,1)))
model.add(Convolution2D(512, 3, 3, activation='relu'))
model.add(MaxPooling2D((2,2), strides=(2,2)))

model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
return model

model = VGG_19(input_shape = (224, 224, 3), classes = 10)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history=model.fit(x_train_r , train_y, epochs=5, batch_size=20, validation_data=(x_test_r , test_y ))
model.summary()

@mounirlazid
Copy link

I am looking for a simple example of a vgg16 model to classify 3D medical images. thank you for your help

@zhouhongyuthu
Copy link

HI,the h5 link is no longer valid. Could you fix it?

@ThatIsMyUsername
Copy link

HI,the h5 link is no longer valid. Could you fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment