コード例 #1
0
def get_prediction(classifier, img_path, debug=False):
    img_width, img_height = 224, 224  # Default input size for VGG16
    conv_base = VGGFace(include_top=False, input_shape=(img_width, img_height, 3),model='resnet50',pooling=None)
#    print(conv_base.summary())
    #conv_base.layers.pop() # Get rid of the classification layer
    #conv_base.layers.pop() # Get rid of the dropout layer
    #conv_base.outputs = [conv_base.layers[-1].output]
    #conv_base.layers[-1].outbound_nodes = []
    #    conv_base.layers.add(MaxPooling2D(pool_size=(2, 2)))
    conv_base.layers.pop()
    conv_base.outputs = [conv_base.layers[-1].output]
    conv_base.layers[-1].outbound_nodes =[]
    #print(conv_base.summary())
    # Get picture
    if not img_path:
        print("no image passed in, using default image from 26-30")
        truth = '26-30'
        base_dir = '../'
        img_path = path.join(base_dir, 'maleTest/26-30', '28_0_3_20170105175516710.jpg.chip.jpg')
    else:
        truth = img_path.split('/')[-2]
    if debug:
        print("image is:", img_path)
        print("truth for this image is:", truth)
    img = image.load_img(img_path, target_size=(img_width, img_height))
    img_tensor = image.img_to_array(img)  # Image data encoded as integers in the 0–255 range
    img_tensor /= 255.  # Normalize to [0,1] for plt.imshow application

    # Extract features
    features = conv_base.predict(img_tensor.reshape(1,img_width, img_height, 3))
    print(features.shape)
    
    # Make prediction
    try:
        prediction = classifier.predict(features)
    except:
        prediction = classifier.predict(features.reshape(1, 7*7*2048))

    # Show picture
    #plt.imshow(img_tensor)
    #plt.show()

    # Write prediction
    [prediction] = prediction
    labels = ['1-2', '14-16', '17-20', '21-25', '26-30', '3-5', '31-35', '36-40', '41-45', '46-50', '51-55', '56-60', '6-8', '61-70', '9-13']
    oneOff= {
        '1-2': ['1-2','3-5'],
        '3-5': ['1-2','3-5','6-8'],
        '6-8': ['3-5','6-8','9-13'],
        '9-13':['6-8','9-13','14-16'],
        '14-16':['9-13','14-16','17-20'],
        '17-20':['14-16','17-20','21-25'],
        '21-25':['17-20','21-25','26-30'],
        '26-30':['21-25','26-30','31-35'],
        '31-35':['26-30','31-35','36-40'],
        '36-40':['31-35','36-40','41-45'],
        '41-45':['36-40','41-45','46-50'],
        '46-50':['41-45','46-50','51-55'],
        '51-55':['46-50','51-55','56-60'],
        '56-60':['51-55','56-60','61-70'],
        '61-70':['56-60','61-70']
    }
    predictionList=[]
    predictionText = labels[int(prediction)]
    
    if predictionText != truth:
        if debug:
            print("PREDICTION WAS WRONG!!!!")
        predictionList.append(0)
    else:
        predictionList.append(1)


    if predictionText in oneOff[truth]:
        predictionList.append(1)
    else:
        predictionList.append(0)
    return predictionList
コード例 #2
0
    frame = frame[x:x + w, y:y + h]
    frame = np.array(frame.resize((224, 224))).reshape((1, 224, 224, 3))
    frame = np.expand_dims(frame, axis=0)
    frame = preprocess_input(frame)
    return vgg_features.predict(frame[0]), frame[0]


##############################
# Preparing Model
##############################

from keras_vggface.vggface import VGGFace
vgg_features = VGGFace(include_top=True, input_shape=(224, 224, 3))
vgg_features.layers.pop()
vgg_features.layers.pop()
vgg_features.outputs = [vgg_features.layers[-1].output]
vgg_features.layers[-1].outbound_nodes = []

########################################
# Preprocessing data to Extract Features
#########################################

data_points = os.listdir("zippedFaces/unzippedFaces"
                         )  ## GETTING DATA POINTS THAT IS NAME OF CELEBRTIES.

no_of_record_per_image = 3  ## NUMBER OF TIMES EVERY CELEBRITIES (DIFFERENT) IMAGES IS TO BE STORED
face_feature = np.zeros((len(data_points) * no_of_record_per_image,
                         2048))  ### ARRAY THAT WILL STORE THE FACE FEATURE
if not ("Face_Feature" in os.listdir()):  ### MAKING DIR
    os.mkdir("Face_Feature")
    os.mkdir("Face_Feature/Faces")
コード例 #3
0
def build_model(neurons=[32, 8],
                droprate=0.5,
                activation=LeakyReLU(),
                optimizer=Adam(lr=0.0001),
                feature_model_type='resnet50',
                n_hidden_layers=2,
                n_nodes_output_cat=3,
                bmomentum=0.99,
                bepsilon=0.001,
                reg_val=0.001):

    # ------------------------------------
    # The first part of the model consists
    # of the pre-trained VGG face model
    # ------------------------------------

    # Grab relavent part of network
    if feature_model_type == 'vgg16':
        vgg_model = VGGFace(model='vgg16')
        n_lyrs = 22
        for l in range(4):
            vgg_model.layers.pop()
            vgg_model.outputs = [vgg_model.layers[-1].output]
            vgg_model.layers[-1].outbound_nodes = []

    elif feature_model_type == 'resnet50':
        vgg_model = VGGFace(model='resnet50')
        out = vgg_model.get_layer('flatten_1').output
        n_lyrs = 175
        vgg_model.layers.pop()
        vgg_model.outputs = [vgg_model.layers[-1].output]
        vgg_model.layers[-1].outbound_nodes = []

    # Add input layer for age and gender
    age_gender_input = Input(shape=(2, ), name='age_gender_input')

    vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.outputs)

    # Merge auxilary input to end of pretrained model
    x = concatenate([vgg_model.output, age_gender_input])

    # Add Batch Normalization. Auxillary inputs are on a different scale as VGG outputs
    x = BatchNormalization(momentum=bmomentum, epsilon=bepsilon)(x)

    # First new layer
    if reg_val is None:
        x_cat = Dense(neurons[0])(x)
        x_cont = Dense(neurons[0])(x_cont)
    else:
        x_cat = Dense(neurons[0], kernel_regularizer=l2(reg_val))(x)
        x_cont = Dense(neurons[0], kernel_regularizer=l2(reg_val))(x)

    x_cat = activation(x_cat)
    x_cat = Dropout(droprate)(x_cat)
    x_cont = activation(x_cont)
    x_cont = Dropout(droprate)(x_cont)

    # Create split layers for classification and for regression
    for n in range(1, n_hidden_layers):
        if reg_val is None:
            x_cat = Dense(neurons[n])(x_cat)
            x_cont = Dense(neurons[n])(x_cont)
        else:
            x_cat = Dense(neurons[n], kernel_regularizer=l2(reg_val))(x_cat)
            x_cont = Dense(neurons[n], kernel_regularizer=l2(reg_val))(x_cont)

        x_cat = activation(x_cat)
        x_cat = Dropout(droprate)(x_cat)
        x_cont = activation(x_cont)
        x_cont = Dropout(droprate)(x_cont)

    # Add output layers for classification and regression tasks
    x_cat = Dense(n_nodes_output_cat, activation='softmax',
                  name='cat_out')(x_cat)
    x_cont = Dense(1, activation='linear', name='cont_out')(x_cont)

    model = Model(inputs=[vgg_model.input, age_gender_input],
                  outputs=[x_cat, x_cont])

    # Freeze layers associated with VGGFace model
    for layer in model.layers[:n_lyrs]:
        layer.trainable = False

    model.compile(loss={
        'cat_out': 'categorical_crossentropy',
        'cont_out': 'mean_squared_error'
    },
                  optimizer=optimizer,
                  metrics={
                      'cat_out': ['acc'],
                      'cont_out': ['mse']
                  })

    return model