Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
VGG-Face model for keras

VGG-Face model for Keras

This is the Keras model of VGG-Face.

It has been obtained through the following method:

  • vgg-face-keras:directly convert the vgg-face matconvnet model to keras model
  • vgg-face-keras-fc:first convert vgg-face caffe model to mxnet model,and then convert it to keras model

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

Deep Face Recognition
O. M. Parkhi, A. Vedaldi, A. Zisserman
British Machine Vision Conference, 2015

Please cite the paper if you use the models.

Contents:

model and usage demo: see vgg-face-keras.py or vgg-face-keras-fc.py

The only difference between them is the last few layers(see the code and you'll understand),but they produce the same result.

weights: 

Notice:

Please use this model in TensorFlow mode.

from keras.models import Model
from keras.layers import Input, Conv2D, Dense, Flatten, Dropout
from keras.layers import ZeroPadding2D, MaxPool2D
from PIL import Image
import argparse
import numpy as np
class ZCM(object):
def __init__(self, input_data, filters, kernel_size, layer_name):
self.input_data = input_data
self.filters = filters
self.kernel_size = kernel_size
self.layer_name = layer_name
def model_add(self):
first_padding = ZeroPadding2D(padding=(1, 1))(self.input_data)
first_conv = Conv2D(self.filters, self.kernel_size, activation='relu', name=self.layer_name)(first_padding)
second_padding = ZeroPadding2D(padding=(1,1))(first_conv)
second_conv = Conv2D(self.filters, self.kernel_size, activation='relu', name=self.layer_name + '_2')(second_padding)
pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))(second_conv)
return pool
class ZCM3(ZCM):
def __init__(self, input_data, filters, kernel_size, layer_name):
super().__init__(input_data, filters, kernel_size, layer_name)
def model_add(self):
model = super().model_add()
third_padding = ZeroPadding2D(padding=(1, 1))(model)
third_conv = Conv2D(self.filters, self.kernel_size, activation='relu', name=self.layer_name +'_3')(third_padding)
pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))(third_conv)
return pool
def ouput_layer(model):
flat = Flatten()(model)
fc6 = Dense(4096, activation='relu', name='fc6')(flat)
fc6_drop = Dropout(0.5)(fc6)
fc7 = Dense(4096, activation='relu', name='fc7')(fc6_drop)
fc7_drop = Dropout(0.5)(fc7)
out = Dense(2622, activation='softmax', name='fc8')(fc7_drop)
return out
def vgg_face(weights_path=None):
img = Input(shape=(224, 224, 3))
conv1 = ZCM(img, 64, (3,3),'first_conv').model_add()
conv2 = ZCM(conv1, 128, (3,3),'second_conv').model_add()
conv3 = ZCM3(conv2, 256, (3, 3), 'third_conv').model_add()
conv4 = ZCM3(conv3, 512, (3, 3), 'forth_conv').model_add()
conv5 = ZCM3(conv4, 512, (3, 3), 'five_conv').model_add()
output = ouput_layer(conv5)
model = Model(inputs=img, outputs=output)
if weights_path:
model.load_weights(weights_path, by_name=True)
return model
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--image', '-i', type=str)
parser.add_argument('--weights', '-w', type=str)
return parser.parse_args()
def main():
args = get_args()
img = Image.open(args.image)
img = img.resize((224,224))
img = np.array(img).astype(np.float32)
img = np.expand_dims(img, axis=0)
weights = args.weights
# Test pretrained model
model = vgg_face(weights)
out = model.predict(img)
print(out[0][0])
if __name__ == '__main__':
main()
from keras.models import Model
from keras.layers import Input, Conv2D, Dense, Flatten, Dropout
from keras.layers import ZeroPadding2D, MaxPool2D
from PIL import Image
import argparse
import numpy as np
class ZCM(object):
def __init__(self, input_data, filters, kernel_size, layer_name):
self.input_data = input_data
self.filters = filters
self.kernel_size = kernel_size
self.layer_name = layer_name
def model_add(self):
first_padding = ZeroPadding2D(padding=(1, 1))(self.input_data)
first_conv = Conv2D(self.filters, self.kernel_size, activation='relu', name=self.layer_name)(first_padding)
second_padding = ZeroPadding2D(padding=(1,1))(first_conv)
second_conv = Conv2D(self.filters, self.kernel_size, activation='relu', name=self.layer_name + '_2')(second_padding)
pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))(second_conv)
return pool
class ZCM3(ZCM):
def __init__(self, input_data, filters, kernel_size, layer_name):
super().__init__(input_data, filters, kernel_size, layer_name)
def model_add(self):
model = super().model_add()
third_padding = ZeroPadding2D(padding=(1, 1))(model)
third_conv = Conv2D(self.filters, self.kernel_size, activation='relu', name=self.layer_name +'_3')(third_padding)
pool = MaxPool2D(pool_size=(2, 2), strides=(2, 2))(third_conv)
return pool
def ouput_layer(model):
fc6 = Convolution2D(4096, 7, 7, activation='relu', name='fc6')(model)
fc6_drop = Dropout(0.5)(fc6)
fc7 = Convolution2D(4096, 1, 1, activation='relu', name='fc7')(fc6_drop)
fc7_drop = Dropout(0.5)(fc7)
fc8 = Convolution2D(2622, 1, 1, name='fc8')(fc7_drop)
flat = Flatten()(fc8)
out = Activation('softmax')(flat)
return out
def vgg_face(weights_path=None):
img = Input(shape=(224, 224, 3))
conv1 = ZCM(img, 64, (3,3),'first_conv').model_add()
conv2 = ZCM(conv1, 128, (3,3),'second_conv').model_add()
conv3 = ZCM3(conv2, 256, (3, 3), 'third_conv').model_add()
conv4 = ZCM3(conv3, 512, (3, 3), 'forth_conv').model_add()
conv5 = ZCM3(conv4, 512, (3, 3), 'five_conv').model_add()
output = ouput_layer(conv5)
model = Model(inputs=img, outputs=output)
if weights_path:
model.load_weights(weights_path, by_name=True)
return model
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--image', '-i', type=str)
parser.add_argument('--weights', '-w', type=str)
return parser.parse_args()
def main():
args = get_args()
img = Image.open(args.image)
img = img.resize((224,224))
img = np.array(img).astype(np.float32)
img = np.expand_dims(img, axis=0)
weights = args.weights
# Test pretrained model
model = vgg_face(weights)
out = model.predict(img)
print(out[0][0])
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment