Beispiel #1
0
def model():
    data_normal = load_data('Augmentation Data', ['Normal'])
    data_symptoms = load_data('Augmentation Data', ['Symptoms'])
    train_data = np.concatenate(
        (np.array(data_normal), np.array(data_symptoms)))
    labels = np.concatenate((np.zeros(6000), np.ones(3600)))

    model = VGGFace(weights='vggface',
                    include_top=False,
                    input_shape=(200, 200, 3))

    train_data, features, features_flatten = create_features(train_data, model)

    X_train, X_valid, y_train, y_valid = train_test_split(features,
                                                          labels,
                                                          test_size=0.2,
                                                          random_state=42)
    X_train, X_test, y_train, y_test = train_test_split(X_train,
                                                        y_train,
                                                        test_size=0.15,
                                                        random_state=42)

    checkpointer = ModelCheckpoint(filepath='model/cnn_best.hdf5',
                                   verbose=1,
                                   save_best_only=True)

    es = EarlyStopping(monitor='val_loss', mode='min', verbose=2, patience=10)
    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(6, 6, 512)))
    model.add(MaxPooling2D((2, 2)))
    model.add(Conv2D(128, (3, 3), activation='relu', padding="Same"))
    model.add(MaxPooling2D((2, 2)))

    model.add(Flatten())
    model.add(Dense(64, activation='relu'))
    model.add(Dropout(0.3))
    model.add(Dense(32, activation='relu'))
    model.add(Dropout(0.3))
    model.add(Dense(2, activation='softmax'))

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    model_history = model.fit(X_train,
                              y_train,
                              epochs=100,
                              callbacks=[es, checkpointer],
                              verbose=2,
                              validation_data=(X_valid, y_valid))

    return model
x_train.shape, y_train.shape, x_test.shape, y_test.shape

model = VGGFace(include_top=False, input_shape=(224, 224, 3), model='resnet50')

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

last_layer = model.get_layer('avg_pool').output
x_ = Flatten(name='flatten')(last_layer)
x_ = Dense(512, activation='relu', name='fc6')(x_)
x_ = Dense(512, activation='relu', name='fc7')(x_)
out = Dense(num_class, activation='sigmoid', name='fc8')(x_)
model = Model(model.input, out)

adam = optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(optimizer=adam, loss='binary_crossentropy', metrics=['accuracy'])
# model.summary()

from keras.callbacks import ModelCheckpoint, EarlyStopping

save_best = ModelCheckpoint('saved_model.hdf',
                            save_best_only=True,
                            monitor='val_loss',
                            mode='min')
earlyStopping = EarlyStopping(monitor='val_loss',
                              patience=2,
                              verbose=0,
                              mode='min')

result = model.fit(x_train,
                   y_train,
def main():
    """------ Argparse parameters ------
    """
    # Instantiating the ArgumentParser  object as parser
    parser = argparse.ArgumentParser(
        description=
        "[INFO] Classify emotions and print out performance accuracy report")

    # Adding optional (with defaults) and required arguments
    parser.add_argument("-trd",
                        "--train_data",
                        required=True,
                        help="Directory of training data")
    parser.add_argument("-vald",
                        "--val_data",
                        required=True,
                        help="Directory of validation data")
    parser.add_argument(
        "-optim",
        "--optimizer",
        required=True,
        help=
        "Method to update the weight parameters to minimize the loss function. Choose between SGD and Adam."
    )
    parser.add_argument(
        "-lr",
        "--learning_rate",
        default=0.001,
        type=float,
        help=
        "The amount that the weights are updated during training. Default = 0.001"
    )
    parser.add_argument(
        "-ep",
        "--epochs",
        default=50,
        help=
        "Defines how many times the learning algorithm will work through the entire training dataset. Default = 50"
    )

    # Parsing the arguments
    args = vars(parser.parse_args())

    # Saving parameters as variables
    trd = args["train_data"]  # training data dir
    vald = args["val_data"]  # validation data dir
    optim = args["optimizer"]  # optimizer
    lr = args["learning_rate"]  # learning rate
    ep = int(args["epochs"])  # epochs
    """------ Loading data and preprocessing ------
    """

    # getting training and validation data
    print("[INFO] loading and preprocessing training and validation data ...")
    train = get_data(os.path.join(trd))
    val = get_data(os.path.join(vald))

    #Create ouput folder, if it doesn´t exist already, for saving the classification report, performance graph and model´s architecture
    if not os.path.exists("../output"):
        os.makedirs("../output")
    """------ Preparing training and validations sets ------
    """

    # empty lists for training and validation images and labels
    x_train = []
    y_train = []
    x_val = []
    y_val = []

    # appending features (images as numpy arrays) and labels to the empty lists for further processing
    for feature, label in train:
        x_train.append(feature)
        y_train.append(label)

    for feature, label in val:
        x_val.append(feature)
        y_val.append(label)

    # normalizing the data (rescaling RGB channel values from 0-255 to 0-1)
    x_train = np.array(x_train) / 255
    x_val = np.array(x_val) / 255

    # integers to one-hot vectors
    lb = LabelBinarizer()
    y_train = lb.fit_transform(y_train)
    y_val = lb.fit_transform(y_val)
    """------ Loading and modifying VGG-Face CNN model ------
    """
    # Loading VGG-Face model
    vggface = VGGFace()

    # Convolution Features (loading only feature extraction layers and adjusting the input shape)
    vgg_face = VGGFace(include_top=False,
                       input_shape=(48, 48, 3),
                       pooling='avg',
                       weights='vggface')  # pooling: None, avg or max

    # Disabling the convolutional layers before training.
    # marking loaded layers as not trainable
    for layer in vgg_face.layers:
        layer.trainable = False

    # Adding new classifier layers
    flat1 = Flatten()(vgg_face.layers[-1].output)
    class1 = Dense(256, activation='relu')(flat1)

    #dropout = Dropout(0.5)(class1)

    output = Dense(7, activation='softmax')(class1)

    # Defining new model
    vgg_face = Model(inputs=vgg_face.inputs, outputs=output)

    # ploting and saving model´s architecture
    plot_model(vgg_face,
               to_file='../output/VGG-Face_CNN´s_architecture.png',
               show_shapes=True,
               show_dtype=True,
               show_layer_names=True)

    # Printing that model´s architecture graph has been saved
    print(f"\n[INFO] Model´s architecture graph has been saved")

    if optim == "Adam":
        opt = Adam(lr=lr)

        # Compile model
        vgg_face.compile(optimizer=opt,
                         loss='categorical_crossentropy',
                         metrics=['accuracy'])

        # train the model
        print("[INFO] training VGG-Face CNN model ...")
        H = vgg_face.fit(x_train,
                         y_train,
                         validation_data=(x_val, y_val),
                         batch_size=128,
                         epochs=ep,
                         verbose=1)

    elif optim == "SGD":

        opt = SGD(lr=lr)

        # Compile model
        vgg_face.compile(optimizer=opt,
                         loss='categorical_crossentropy',
                         metrics=['accuracy'])

        # train the model
        print("[INFO] training VGG-Face CNN model ...")
        H = vgg_face.fit(x_train,
                         y_train,
                         validation_data=(x_val, y_val),
                         batch_size=128,
                         epochs=ep,
                         verbose=1)

    else:
        print("Not a valid optimizer. Choose between 'SGD' and 'Adam'.")
    """------ VGG-Face CNN model´s output ------
    """

    # ploting and saving model´s performance graph
    plot_history(H, ep)

    # Printing that performance graph has been saved
    print(f"\n[INFO] Model´s performance graph has been saved")

    # Extracting the labels
    labels = os.listdir(os.path.join(trd))
    # Classification report
    predictions = vgg_face.predict(x_val, batch_size=32)
    print(
        classification_report(y_val.argmax(axis=1),
                              predictions.argmax(axis=1),
                              target_names=labels))

    # defining full filepath to save .csv file
    outfile = os.path.join("../", "output", "Emotions_classifier_report.csv")

    # turning report into dataframe and saving as .csv
    report = pd.DataFrame(
        classification_report(y_val.argmax(axis=1),
                              predictions.argmax(axis=1),
                              target_names=labels,
                              output_dict=True)).transpose()
    report.to_csv(outfile)
    print(f"\n[INFO] Classification report has been saved")

    print(
        f"\n[INFO] VGG-Face CNN model has been trained and evaluated successfully"
    )