Skip to content

Instantly share code, notes, and snippets.

@baraldilorenzo
Last active September 13, 2025 12:17
Show Gist options
  • Select an option

  • Save baraldilorenzo/07d7802847aaad0a35d3 to your computer and use it in GitHub Desktop.

Select an option

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

Copy link
Copy Markdown

Hi,
The input size of images is 2828. However I want to train them via VGG 16 where input size of image should be 244244.
How can I do that? I tried several methods but I am not successful.
I found flow_from_directory() function from keras but it requires path of directory, how I am directly accessing data from load_data().

Could you please tell me how can I achieve this?

@yynopenguin

Copy link
Copy Markdown

F o r t h e l a z y :)

from keras import backend as K
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout
from keras.layers.convolutional import Conv2D, MaxPooling2D, ZeroPadding2D
from keras.layers.core import Activation
from keras.optimizers import SGD
import cv2, numpy as np

K.set_image_data_format('channels_first')

def VGG_16(weights_path=None):
    model = Sequential()
    
  
    model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
    model.add(Conv2D(64, (3, 3),padding='same'))
    model.add(Activation('relu'))  


    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(64, (3, 3),padding='same'))
    model.add(Activation('relu'))     
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(128, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(128, (3, 3),padding='same'))
    model.add(Activation('relu')) 
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(256, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(256, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(256, (3, 3),padding='same'))
    model.add(Activation('relu')) 
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(512, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(512, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(512, (3, 3),padding='same'))
    model.add(Activation('relu')) 
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(512, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(512, (3, 3),padding='same'))
    model.add(Activation('relu')) 

    model.add(ZeroPadding2D((1,1)))
    model.add(Conv2D(512, (3, 3),padding='same'))
    model.add(Activation('relu')) 
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2)))
    

    model.add(Flatten())
    model.add(Dense(4096))
    model.add(Activation('relu')) 
    model.add(Dropout(0.5))
    
    model.add(Dense(4096))
    model.add(Activation('relu')) 
    model.add(Dropout(0.5))

    model.add(Dense(1000))
    model.add(Activation('softmax')) 


    if weights_path:
        model.load_weights(weights_path,by_name=True)

    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)
    

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

Do you still have the weight file vgg16_weights.h5, I found the original google drive link is broken.

@gitosu67

Copy link
Copy Markdown

file is in user's trash

@jpthenasseril

jpthenasseril commented Jul 20, 2020

Copy link
Copy Markdown

@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

mikechen66 commented Sep 8, 2020

Copy link
Copy Markdown

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
Copy Markdown

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

@mikechen66

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

@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
Copy Markdown

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

@Calmett

Calmett commented Jan 13, 2022

Copy link
Copy Markdown

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

@Calmett

Calmett commented Mar 3, 2022

Copy link
Copy Markdown

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
Copy Markdown

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

@zulhaikal

Copy link
Copy Markdown

@soon-will

soon-will commented Dec 26, 2022 via email

Copy link
Copy Markdown

@agulli

agulli commented Dec 27, 2022 via email

Copy link
Copy Markdown

@firas1000

Copy link
Copy Markdown

the file for the weights doesn't exist

@soon-will

soon-will commented Sep 13, 2025 via email

Copy link
Copy Markdown

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