Skip to content

Instantly share code, notes, and snippets.

@baraldilorenzo
Last active November 21, 2023 22:41
Show Gist options
  • Save baraldilorenzo/07d7802847aaad0a35d3 to your computer and use it in GitHub Desktop.
Save baraldilorenzo/07d7802847aaad0a35d3 to your computer and use it in GitHub Desktop.
VGG-16 pre-trained model for Keras

##VGG16 model for Keras

This is the Keras model of the 16-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-16 model is denoted as configuration D. It achieves 7.5% top-5 error on ILSVRC-2012-val, 7.4% top-5 error on ILSVRC-2012-test.

Please cite the paper if you use the models.

###Contents:

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

weights: vgg16_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_16(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(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(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(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_16('vgg16_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)
@jpthenasseril
Copy link

jpthenasseril commented Jul 20, 2020

@baraldilorenzo (and @Zebreu!) thanks for this!

Is there a way I can use this to actually label images? I can get an index into the class vector (my 'cat.jpg' comes out to class 669) but I've no idea how to actually find the class labels the index refers to...

Tensorflow outputs in alphabetical order so if you have a list of all classes, sort them and find the label at index 669

@mikechen66
Copy link

mikechen66 commented Sep 8, 2020

The above-mentioned topic includes the long historical timeline discussions on the VGG16 model and need time to consume. I am thinking whether the following keras models can be used.

A Concise VGG16 Model
Release Date: Dec 3, 2019
https://github.com/1297rohit/VGG16-In-Keras

Official Keras VGG16 Model by the keras team
Release Data: Mar 29, 2019
https://github.com/keras-team/keras-applications/blob/master/keras_applications/vgg16.py

@toshniwal-aadhar12
Copy link

Can you please provide the weight files the above download link isn't working.

@mikechen66
Copy link

Thanks for your response. While using the weights provided by François Chollet, it can run. In this condition, I use Chollet's code with VGG16 model.

@dantewarriorcp
Copy link

is the SGD the optimizer? I want to write the same to the paper in code, i didnt read the SGD, somebody friends.. i dont understand , this code is the same from the paper , even the optimizer? ..

@dantewarriorcp
Copy link

@mikedewar You can download it at http://dl.caffe.berkeleyvision.org/caffe_ilsvrc12.tar.gz (there are other files too, but one of them has a thousand lines with synset IDs and names, so you can just use that as an index). Works well in my case.
Also, from the out array, you can do numpy.argmax(out) to get the class with the highest probability.

Also, there is no normalization done in the gist above. If you want accurate results, you better do those steps to any input image:

    img = cv2.resize(cv2.imread('../../Downloads/cat2.jpg'), (224, 224))

    mean_pixel = [103.939, 116.779, 123.68]
    img = img.astype(np.float32, copy=False)
    for c in range(3):
        img[:, :, c] = img[:, :, c] - mean_pixel[c]
    img = img.transpose((2,0,1))
    img = np.expand_dims(img, axis=0)

The mean pixel values are taken from the VGG authors, which are the values computed from the training dataset.

is the sgd the exactly optimizer from the paper ? , like the code .. or only the model vgg16 .. i want the exxactly code , in the paper dont mencione the OPTIMIZER

@qu3ntinprime
Copy link

does anyone have a copy of the weights or any working link for download. the link provided seems dead at this moment

@Calmett
Copy link

Calmett commented Jan 13, 2022

qu3ntinprime you can find the weights here: https://github.com/fchollet/deep-learning-models/releases.

@Calmett
Copy link

Calmett commented Mar 3, 2022

I got this error when runing the code vgg16: "return tf.random.uniform ( ResourceExhaustedError: failed to allocate memory [Op:AddV2]". Could someone help me?

@satyasaigudla
Copy link

Unable to download vgg16.h5 file,Can please solve this issue.

@zulhaikal
Copy link

@soon-will
Copy link

soon-will commented Dec 26, 2022 via email

@agulli
Copy link

agulli commented Dec 27, 2022 via email

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