Esempio n. 1
0
def tp_xception_multi_period_score_fusion(dataset, config):
    result = []
    for i in range(iteration):
        pre_list = []
        y_test_list = []
        for period in ['R1', 'R3', 'R4', 'R5', 'R6']:
            model = BaseModel.Combined_Model(parallels=4, config=config)
            model.load_weights('./{}_pdm_iteration-{}-{}.hdf5'.format(
                dataset, i, period))
            x_list = np.loadtxt('{}-{}_file_list.txt'.format(dataset, period))
            img_x_list, shape_x, texture_x, vein_x, y_list = utils.data_loader_for_combined_model(
                file_list=x_list,
                dataset=dataset,
                config=config,
                isVenation=True)
            img_x = np.array(img_x_list)
            y = np.array(y_list)
            id_map = np.loadtxt(dataset + '_id.txt')
            for index, d in enumerate(y):
                for label in id_map:
                    if d == label[0]:
                        y[index] = label[1]
            y_one_hot = to_categorical(y)
            test_index = np.load(
                '{}_iteration_{}_img_{}_tp_xception_test_index.npy'.format(
                    dataset, i, period))

            img_x_test = img_x[test_index]
            shape_x_test = shape_x[test_index]
            texture_x_test = texture_x[test_index]
            vein_x_test = vein_x[test_index]
            y_test = y_one_hot[test_index]
            y_test_list.append(y_test)

            x_test_list = create_input_list(shape_x_test,
                                            texture_x_test,
                                            vein_x_test,
                                            img_x_test,
                                            config,
                                            isVenation=True)
            pre = model.predict(x_test_list)
            pre_list.append(pre)

        pre_final_arr = np.array(pre_list)
        pre_final = np.sum(pre_final_arr, axis=2)
        pre_final_label = [np.argmax(d) for d in pre_final]
        for i in range(1, len(y_test_list) + 1):
            if y_test_list[i - 1] != y_test_list[i]:
                print("The test label of different period should be the same")
                return -1
        y_test_label = [np.argmax(d) for d in y_test_list[0]]

        performance = get_performance(pre_final_label, y_test_label)
        result.append(performance)

    plot_result(result)
Esempio n. 2
0
def tp_xception_model_training_and_test(img_x_list, shape_x, texture_x, vein_x,
                                        isVenation, y_list, config):
    img_x = np.array(img_x_list)
    shape_x = np.array(shape_x)
    texture_x = np.array(texture_x)
    if isVenation:
        vein_x = np.array(vein_x)
    y = np.array(y_list)
    dataset = config['dataset']
    id_map = np.loadtxt(dataset + '_id.txt')
    for index, d in enumerate(y):
        for label in id_map:
            if d == label[0]:
                y[index] = int(label[1])
    y_one_hot = to_categorical(y)
    result = []
    lr_adjust = ReduceLROnPlateau(monitor='val_loss',
                                  factor=0.5,
                                  patience=5,
                                  min_lr=1e-6)
    dataset = config['dataset']
    if dataset == 'soybean':
        period = config['period']
    else:
        period = dataset

    for i in range(iteration):
        index = np.arange(len(y))
        # print(index)
        X_train_index, X_test_index, y_train, y_test = train_test_split(
            index,
            y_one_hot,
            test_size=0.3,
            random_state=i,
            shuffle=True,
            stratify=y_one_hot)
        print(len(X_train_index))
        print(len(X_test_index))
        np.save(
            '{}_iteration_{}_img_{}_tp_xception_train_index.npy'.format(
                dataset, i, period), X_train_index)
        np.save(
            '{}_iteration_{}_img_{}_tp_xception_test_index.npy'.format(
                dataset, i, period), X_test_index)

        shape_x_train = shape_x[X_train_index]
        texture_x_train = texture_x[X_train_index]
        img_x_train = img_x[X_train_index]
        shape_x_train_list = [
            shape_x_train[:, i, :, :] for i in range(config["shape_views"])
        ]
        texture_x_train_list = [
            texture_x_train[:, i, :, :] for i in range(config["texture_views"])
        ]

        if isVenation:
            vein_x_train = vein_x[X_train_index]
            vein_x_train[:, 0, :, 1] = (vein_x_train[:, 0, :, 1] - np.mean(
                vein_x_train[:, 0, :, 1])) / np.std(vein_x_train[:, 0, :, 1])
            vein_x_train[:, 1, :, 1] = (vein_x_train[:, 1, :, 1] - np.mean(
                vein_x_train[:, 1, :, 1])) / np.std(vein_x_train[:, 1, :, 1])
            vein_x_train_list = [
                vein_x_train[:, i, :, :] for i in range(config["vein_views"])
            ]

        x_train_list = []

        for index, d in enumerate(texture_x_train_list):
            texture_x_train_list[index] = np.reshape(
                d, [d.shape[0], d.shape[1], d.shape[2], 1])
        if isVenation:
            for index, d in enumerate(vein_x_train_list):
                vein_x_train_list[index] = np.reshape(
                    d, [d.shape[0], d.shape[1], d.shape[2], 1])

        x_train_list.extend(shape_x_train_list)
        x_train_list.extend(texture_x_train_list)
        if isVenation:
            x_train_list.extend(vein_x_train_list)
        x_train_list.append(img_x_train)

        y_train_label = [np.argmax(d) for d in y_train]
        my_class_weights = list(
            class_weight.compute_class_weight('balanced',
                                              np.unique(y_train_label),
                                              y_train_label))
        class_weight_dict = dict(
            zip([x for x in np.unique(y_train_label)], my_class_weights))

        save_best_weight = ModelCheckpoint(
            './{}_pdm_iteration-{}-{}.hdf5'.format(dataset, i, period),
            monitor='val_loss',
            verbose=1,
            save_best_only=True,
            mode='auto',
            save_weights_only=True)

        model = BaseModel.Combined_Model(parallels=1, config=config)

        lr_reduce = LearningRateScheduler(lr_reducer)
        model.fit(x_train_list,
                  y_train,
                  batch_size=16,
                  epochs=100,
                  validation_split=0.1,
                  class_weight=class_weight_dict,
                  callbacks=[save_best_weight, lr_reduce, lr_adjust])

        shape_x_test = shape_x[X_test_index]
        texture_x_test = texture_x[X_test_index]

        img_x_test = img_x[X_test_index]

        shape_x_test_list = [
            shape_x_test[:, i, :, :] for i in range(config["shape_views"])
        ]
        texture_x_test_list = [
            texture_x_test[:, i, :, :] for i in range(config["texture_views"])
        ]

        if isVenation:
            vein_x_test = vein_x[X_test_index]
            vein_x_test[:, 0, :, 1] = (vein_x_test[:, 0, :, 1] - np.mean(
                vein_x_train[:, 0, :, 1])) / np.std(vein_x_train[:, 0, :, 1])
            vein_x_test[:, 1, :, 1] = (vein_x_test[:, 1, :, 1] - np.mean(
                vein_x_train[:, 1, :, 1])) / np.std(vein_x_train[:, 1, :, 1])
            vein_x_test_list = [
                vein_x_test[:, i, :, :] for i in range(config["vein_views"])
            ]

        x_test_list = []

        for index, d in enumerate(texture_x_test_list):
            texture_x_test_list[index] = np.reshape(
                d, [d.shape[0], d.shape[1], d.shape[2], 1])

        x_test_list.extend(shape_x_test_list)
        x_test_list.extend(texture_x_test_list)

        if isVenation:
            for index, d in enumerate(vein_x_test_list):
                vein_x_test_list[index] = np.reshape(
                    d, [d.shape[0], d.shape[1], d.shape[2], 1])
            x_test_list.extend(vein_x_test_list)

        x_test_list.append(img_x_test)

        K.clear_session()

        model2 = BaseModel.Combined_Model(parallels=1, config=config)
        model2.load_weights('./{}_pdm_iteration-{}-{}.hdf5'.format(
            dataset, i, period))
        score = model2.evaluate(x_test_list, y_test, batch_size=128)
        print(score)
        y_test_label = np.array([np.argmax(d) for d in y_test])
        y_pre = model2.predict(x_test_list)
        y_pre_label = np.array([np.argmax(d) for d in y_pre])

        performance = get_performance(y_pre_label, y_test_label)

        performance['test_acc'] = score[1]
        performance['test_loss'] = score[0]

        result.append(performance)
        K.clear_session()
        print("precision_score: {}".format(performance['precision']))
        print("recall_score: {}".format(performance['recall']))
        print("f1_score: {}".format(performance['f1_score']))
        json_str = json.dumps(performance, indent=4)
        with open(
                './{}_pd-rgb-combined-iteration-{}-{}.json'.format(
                    dataset, i, period), 'w') as json_file:
            json_file.write(json_str)

        plot_result(result)