Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

Extended VGG model to get 256 features : see vgg-extended-functional-api.py

  • Loaded trained VGG model with weights given in above link
  • Added Extra layers(Dense layers of keras, also called Fully connected layer) in the VGG net by using keras functional API to extract features as per our requirement.
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)
'''
@author: Nitish Bhardwaj
Keras Testing model for getting features from the VGG net trained model
'''
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten, Input
from keras.layers import Convolution2D, MaxPooling2D,ZeroPadding2D
from keras.optimizers import SGD
import theano
import os
import cv2
import numpy as np
from sklearn.decomposition import PCA
# input image dimensions
img_rows, img_cols = 224, 224
img_channels = 3
nb_classes = 10
image_path = 'bb.jpg'
weights_path = 'weights.h5'
#Keras deepnet VGG-trained model
def load_trained_model():
trained_model = Sequential()
trained_model.add(ZeroPadding2D((1,1),input_shape=(3,img_rows,img_cols)))
trained_model.add(Convolution2D(64, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(64, 3, 3, activation='relu'))
trained_model.add(MaxPooling2D((2,2), strides=(2,2)))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(128, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(128, 3, 3, activation='relu'))
trained_model.add(MaxPooling2D((2,2), strides=(2,2)))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(256, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(256, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(256, 3, 3, activation='relu'))
trained_model.add(MaxPooling2D((2,2), strides=(2,2)))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(512, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(512, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(512, 3, 3, activation='relu'))
trained_model.add(MaxPooling2D((2,2), strides=(2,2)))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(512, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(512, 3, 3, activation='relu'))
trained_model.add(ZeroPadding2D((1,1)))
trained_model.add(Convolution2D(512, 3, 3, activation='relu'))
trained_model.add(MaxPooling2D((2,2), strides=(2,2)))
trained_model.add(Flatten())
trained_model.add(Dense(4096, activation='relu'))
trained_model.add(Dropout(0.5))
trained_model.add(Dense(4096, activation='relu'))
trained_model.add(Dropout(0.5))
trained_model.add(Dense(1000, activation='softmax'))
trained_model.load_weights(weights_path)
return trained_model
# Load pretrained model and adding keras functional api to add more layers and to extract 256 features
def cnn():
trained_model = load_trained_model()
#VGG model
main_input = Input(shape=(3,img_rows,img_cols), dtype='float32', name='main_input')
zpad1 = ZeroPadding2D((1,1), input_shape=(3,img_rows,img_cols), name='zpad1')(main_input)
conv1 = Convolution2D(64, 3, 3, name='conv1', activation='relu')(zpad1)
zpad2 = ZeroPadding2D((1,1), name='zpad2')(conv1)
conv2 = Convolution2D(64, 3, 3, name='conv2', activation='relu')(zpad2)
maxp1 = MaxPooling2D((2,2), strides=(2,2), name='maxp1')(conv2)
zpad3 = ZeroPadding2D((1,1), name='zpad3')(maxp1)
conv3 = Convolution2D(128, 3, 3, name='conv3', activation='relu')(zpad3)
zpad4 = ZeroPadding2D((1,1), name='zpad4')(conv3)
conv4 = Convolution2D(128, 3, 3, name='conv4', activation='relu')(zpad4)
maxp2 = MaxPooling2D((2,2), strides=(2,2), name='maxp2')(conv4)
zpad5 = ZeroPadding2D((1,1), name='zpad5')(maxp2)
conv5 = Convolution2D(256, 3, 3, name='conv5', activation='relu')(zpad5)
zpad6 = ZeroPadding2D((1,1), name='zpad6')(conv5)
conv6 = Convolution2D(256, 3, 3, name='conv6', activation='relu')(zpad6)
zpad7 = ZeroPadding2D((1,1), name='zpad7')(conv6)
conv7 = Convolution2D(256, 3, 3, name='conv7', activation='relu')(zpad7)
maxp3 = MaxPooling2D((2,2), strides=(2,2), name='maxp3')(conv7)
zpad8 = ZeroPadding2D((1,1), name='zpad8')(maxp3)
conv8 = Convolution2D(512, 3, 3, name='conv8', activation='relu')(zpad8)
zpad9 = ZeroPadding2D((1,1), name='zpad9')(conv8)
conv9 = Convolution2D(512, 3, 3, name='conv9', activation='relu')(zpad9)
zpad10 = ZeroPadding2D((1,1), name='zpad10')(conv9)
conv10 = Convolution2D(512, 3, 3, name='conv10', activation='relu')(zpad10)
maxp4 = MaxPooling2D((2,2), strides=(2,2), name='maxp4')(conv10)
zpad11 = ZeroPadding2D((1,1), name='zpad11')(maxp4)
conv11 = Convolution2D(512, 3, 3, name='conv11', activation='relu')(zpad11)
zpad12 = ZeroPadding2D((1,1), name='zpad12')(conv11)
conv12 = Convolution2D(512, 3, 3, name='conv12', activation='relu')(zpad12)
zpad13 = ZeroPadding2D((1,1), name='zpad13')(conv12)
conv13 = Convolution2D(512, 3, 3, name='conv13', activation='relu')(zpad13)
maxp5 = MaxPooling2D((2,2), strides=(2,2), name='maxp5')(conv13)
flatten = Flatten(name='flatten')(maxp5)
dense1 = Dense(4096, activation='relu', name='dense1')(flatten)
dropout1 = Dropout(0.5, name='dropout1')(dense1)
dense2 = Dense(4096, activation='relu', name='dense2')(dropout1)
dropout2 = Dropout(0.5, name='dropout2')(dense2)
dense3 = Dense(1000, activation='relu', name='dense3')(dropout2)
dropout3 = Dropout(0.5, name='dropout3')(dense3)
dense4 = Dense(256, activation='relu', name='dense4')(dropout3)
print "==Defining Model =="
base_model = Model(input=[main_input], output=[dense4])
for i,layer in enumerate(trained_model.layers):
base_model.layers[i+1].set_weights(layer.get_weights())
print "==Compiling Model =="
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
base_model.compile(optimizer=sgd, loss='categorical_crossentropy')
#Getting model for intermediate output
model = Model(input=base_model.input, output=base_model.get_layer('dense4').output)
return model
def get_features(model,image):
#Getting features from the given image
test_image = [cv2.resize(image,(img_cols,img_rows))]
test_image = np.transpose(test_image, [0,3,1,2]).astype('float32')[:,:,:,:]
print "==Getting Features=="
out = model.predict(test_image)
# print out.shape
return out
#Loading DeepNet model
model = cnn()
#Reading image
frame = cv2.imread(image_path)
#printing 256 features
print get_features(model, frame)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment