예제 #1
0
def compare(cfg):
    data_loader = make_dataloader(cfg, True)
    train_data, test_data, train_label, test_label = data_loader.load_data()
    cfg_cp = cfg.clone()
    for m in cfg.MODEL.ARCHITECTURES:
        cfg_cp.MODEL.ARCHITECTURE = m
        model = build_classification_model(cfg_cp)
        model.load_model()
        pre_train_data, pre_train_label, pre_test_data, pre_test_label = model.data_preprocess(
            train_data, train_label, test_data, test_label)
        acc = model.get_accuracy(pre_test_data, pre_test_label)
        prediction = model.predict_prob(pre_test_data)
        fpr, tpr, thresholds = roc_curve(pre_test_label, prediction)
        roc_auc = auc(fpr, tpr)
        plt.plot(fpr,
                 tpr,
                 lw=1,
                 label=model.model_name + '(area = {0:.2f})'.format(roc_auc))
        print(model.model_name + "测试集正确率为:" + "{:.2f}".format(acc * 100) + "%")
    plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')  # 画对角线
    plt.xlim([-0.05, 1.05])  # 设置x、y轴的上下限,设置宽一点,以免和边缘重合,可以更好的观察图像的整体
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')  # 可以使用中文,但需要导入一些库即字体
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    plt.show()
예제 #2
0
def train(cfg):
    model = build_classification_model(cfg)
    data_loader = make_dataloader(cfg, True)
    train_data, val_data, train_label, val_label = data_loader.load_data()
    train_data, train_label, val_data, val_label = model.data_preprocess(train_data, train_label, val_data, val_label)
    model.train(train_data, train_label, val_data, val_label)
    acc = model.get_accuracy(val_data,val_label)
    prediction = model.predict_prob(val_data)
    fpr, tpr, thresholds = roc_curve(val_label,prediction)
    roc_auc = auc(fpr, tpr)
    plt.plot(fpr, tpr, lw=1, label='ROC fold {0:.2f} (area = {1:.2f})'.format(1, roc_auc))
    plt.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Luck')  # 画对角线
    plt.xlim([-0.05, 1.05])  # 设置x、y轴的上下限,设置宽一点,以免和边缘重合,可以更好的观察图像的整体
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')  # 可以使用中文,但需要导入一些库即字体
    plt.title('Receiver operating characteristic example')
    plt.legend(loc="lower right")
    print("测试集正确率为:" + "{:.2f}".format(acc * 100) + "%")
    plt.show()
    model.save_model()
예제 #3
0
def cross_val(cfg):
    KF = KFold(n_splits=5, shuffle=True)
    tprs = []
    aucs = []
    accs = []
    mean_fpr = np.linspace(0, 1, 100)

    model = build_classification_model(cfg)
    data_loader = make_dataloader(cfg, True)
    data, label_value, label_classify = data_loader.split_data_lable()
    for i, (train_idx, val_idx) in enumerate(KF.split(data)):
        train_data, val_data = data[train_idx], data[val_idx]
        train_label, val_label = label_classify[train_idx], label_classify[
            val_idx]
        train_data, train_label, val_data, val_label = model.data_preprocess(
            train_data, train_label, val_data, val_label)
        model.train(train_data, train_label, val_data, val_label)
        acc = model.get_accuracy(val_data, val_label)
        accs.append(acc)
        prediction = model.predict_prob(val_data)
        fpr, tpr, thresholds = roc_curve(val_label, prediction)
        roc_auc = auc(fpr, tpr)
        tprs.append(interp(mean_fpr, fpr, tpr))
        tprs[-1][0] = 0.0
        aucs.append(roc_auc)
        print(model.model_name + "测试集正确率为:" + "{0:.3f}".format(acc * 100) +
              "%")
        plt.plot(mean_fpr,
                 tprs[-1],
                 lw=1,
                 alpha=0.3,
                 label='ROC fold %d(area=%0.2f)' % (i, roc_auc))

    mean_acc = np.mean(accs)
    print(model.model_name + "测试集平均正确率为:" + "{0:.3f}".format(mean_acc * 100) +
          "%")
    # 画对角线
    plt.plot([0, 1], [0, 1],
             linestyle='--',
             lw=2,
             color='r',
             label='Luck',
             alpha=.8)
    mean_tpr = np.mean(tprs, axis=0)
    mean_tpr[-1] = 1.0
    mean_auc = auc(mean_fpr, mean_tpr)  # 计算平均AUC值
    plt.plot(mean_fpr,
             mean_tpr,
             color='b',
             label=r'Mean ROC (area=%0.3f)' % mean_auc,
             lw=2,
             alpha=.8)
    std_tpr = np.std(tprs, axis=0)
    tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
    tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
    plt.fill_between(mean_tpr, tprs_lower, tprs_upper, color='gray', alpha=.2)
    plt.xlim([-0.05, 1.05])
    plt.ylim([-0.05, 1.05])
    plt.xlabel('False Positive Rate')
    plt.ylabel('True Positive Rate')
    plt.title(model.model_name + ':ROC')
    plt.legend(loc='lower right')
    plt.show()
    np.savez(cfg.MODEL.CROSS_VAL_RESULTS_PATH + model.model_name + '-' +
             cfg.DATA_LOADER.DATA_PATH.split('/')[-1].split('.')[0] + '.npz',
             mean_fpr=mean_fpr,
             tprs=tprs,
             aucs=aucs,
             accs=accs)
예제 #4
0
def optimize(cfg):
    model =build_classification_model(cfg)
    dataloader = make_dataloader(cfg)
    optimizer = build_optimizer(cfg,model)
    data, label_value, label_classify = dataloader.split_data_lable()
    optimizer.select_param(data=data, label=label_classify)