Discover this year's submissions to the Dataiku Frontrunner Awards and give kudos to your favorite use cases and success stories!READ MORE

Using VGGface to transfer learning, check my deep learning code please? :-)

Level 3
Using VGGface to transfer learning, check my deep learning code please? :-)

Hey there. 

 I want to train a model that given an image can predict the gender and eye color (Green/Blue) of the subject. So i have database full of 224x224 face photographs. They were extracted using MTCNN from online photographs. These photos are categorized to Blue-eyed Males, Blue-eyed Females, Green-eyed Males and Green-eyed Females. I have split the images 70/30 for Train/Test. Since i don't want to train the entire network from zero, i want to use VGGface

Here's my preprocessing code: 

from keras.preprocessing.image import img_to_array, load_img
def preprocess_image(image_file):
    img = load_img(image_file,target_size=(224, 224, 3))
    array = img_to_array(img)
    array /= 255
    return array


Here's my architecture code: 

from keras.layers import Input, Dense, Flatten
from keras.models import Model
import os
import dataiku
from keras_vggface.vggface import VGGFace
from keras_vggface import utils

def build_model(input_shapes, n_classes=None):
    image_shape = (224, 224, 3)
    image_input_name = "path_preprocessed"
image_input = Input(shape=image_shape, name=image_input_name)
    base_model = VGGFace(model='resnet50', include_top=False, weights=None, input_tensor=image_input)
    folder = dataiku.Folder("VGG_model")
    weights_path = "rcmalli_vggface_tf_notop_resnet50.h5"
    base_model.load_weights(os.path.join(folder.get_path(), weights_path), by_name=True)
    x = base_model.layers[-1].output
    x = Flatten()(x)
    predictions = Dense(n_classes, activation="softmax")(x)
    model = Model(input=base_model.input, output=predictions)
    return model

def compile_model(model):
    return model



So 2 questions here.

1) Should i add this snippet to architecture? Why/Why not?


for layer in base_model.layers:
layer.trainable = False


2) Does this look OK?

base_model = VGGFace(model='resnet50', include_top=False, weights=None, input_tensor=image_input)

Thanks in advance and any input is welcome! laugh

0 Kudos
2 Replies

Just to clarify, do you currently have an issue ? 🙂
0 Kudos
Level 3
Oh no, code runs OK. I was just asking for input to improve the model. Thanks Clement 🙂
0 Kudos


Labels (2)
A banner prompting to get Dataiku