Пример #1
0
def train_model(model,
                save_path=save_path,
                learning_rate=0.01,
                momentum=0.9,
                loss="binary_crossentropy",
                path_train=path_train):
    train_dataset = dataset.CatDataset(path_train, im_width, im_height)
    # train_dataset.augment()
    X_train, y_train = train_dataset.X, train_dataset.Y
    model.compile(optimizer=SGD(learning_rate=learning_rate,
                                momentum=momentum),
                  loss=loss,
                  metrics=["accuracy"])
    callbacks = [
        EarlyStopping(patience=10, verbose=1),
        ReduceLROnPlateau(factor=0.1, patience=3, min_lr=0.00001, verbose=1),
        ModelCheckpoint(save_path,
                        monitor='accuracy',
                        mode='max',
                        verbose=1,
                        save_best_only=True,
                        save_weights_only=True)
    ]
    results = model.fit(X_train,
                        y_train,
                        batch_size=32,
                        epochs=100,
                        callbacks=callbacks)
    return results
Пример #2
0
def test_model(weight_path, threshold, path_test=path_test):
    if not os.path.exists("./Output"):
        os.mkdir("./Output")
    if not os.path.exists("./Output/x"):
        os.mkdir("./Output/x")
    if not os.path.exists("./Output/y"):
        os.mkdir("./Output/y")
    if not os.path.exists("./Output/pred"):
        os.mkdir("./Output/pred")
    test_dataset = dataset.CatDataset(path_test, im_width, im_height)
    X_test, y_test = test_dataset.X, test_dataset.Y
    model.load_weights(weight_path)
    pred_test = model.predict(X_test, verbose=1)
    # dice_coe_result = dice_coe(pred_test, y_test, loss_type="sorensen")
    # print('Sorensen Dice Coefficient result is {}'.format(dice_coe_result))
    for i in range(pred_test.shape[0]):
        test_pred = pred_test[i]
        test_y = y_test[i] * 255
        test_x = X_test[i] * 255
        test_pred[test_pred > threshold] = 255
        test_pred[test_pred <= threshold] = 0
        save_img("./Output/x/x_{}.jpg".format(i), test_x)
        save_img("./Output/y/y_{}.jpg".format(i), test_y)
        save_img("./Output/pred/pred_{}.jpg".format(i), test_pred)

    model.compile(optimizer=SGD(),
                  loss="binary_crossentropy",
                  metrics=["accuracy"])
    model.evaluate(X_test, y_test)
Пример #3
0
def predict(weight_path, pred_path, threshold=0.2, out_path="./Output"):
    pred_dataset = dataset.CatDataset(pred_path, im_width, im_height)
    X_pred, y_pred = pred_dataset.X, pred_dataset.Y

    model.load_weights(weight_path)
    preds = model.predict(X_pred, verbose=1)
    for i in range(preds.shape[0]):
        pred = preds[i]
        print(np.where(pred > threshold))
        pred[pred > threshold] = 255.0
        pred[pred < threshold] = 0
        save_img(f'{out_path}/{i}.jpg', pred)