def Face(train=False): base_model = VGGFace(input_shape=(200, 200, 3), include_top=False, model='resnet50', weights=None, pooling='avg') # load pre-trained weights if used for fine-tuning if train: base_model.load_weights(BASE_WEIGHTS_PATH) for layer in base_model.layers[:len(base_model.layers) - 50]: layer.trainable = False base_output = base_model.output # age 1~93, treat age as classifications task output_a = Dense(93, activation='softmax', name='predications_age')(base_output) # gender 0 or 1 output_g = Dense(2, activation='softmax', name='predications_gender')(base_output) # race 0~4 output_r = Dense(5, activation='softmax', name='predications_race')(base_output) new_model = Model(inputs=base_model.input, outputs=[output_a, output_g, output_r], name='network_based_vggface') return new_model
from vggface import VGGFace from scipy import misc import copy import numpy as np if __name__ == '__main__': model = VGGFace(weights=None) model.load_weights( '../temp/weight/rcmalli_vggface_tf_weights_tf_ordering.h5') print 'model loaded.' im = misc.imread('../image/ak2.jpg') im = misc.imresize(im, (224, 224)).astype(np.float32) aux = copy.copy(im) im[:, :, 0] = aux[:, :, 2] im[:, :, 2] = aux[:, :, 0] # Remove image mean im[:, :, 0] -= 93.5940 im[:, :, 1] -= 104.7624 im[:, :, 2] -= 129.1863 im = np.expand_dims(im, axis=0) res = model.predict(im) print np.argmax(res[0])