Ejemplo n.º 1
0
def main():

    # Define variable
    datatype = 'gtsrb_binary'
    modelpath = '../binary/checkpoints/gtsrb_binary_mlp_100.pkl'

    # Define which data sample to be processed
    data_idx = 4
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # Predict
    pred_y = model.predict(x_test)

    print('pred_y: ', pred_y[0], pred_y[1], pred_y[2], pred_y[3], pred_y[4], pred_y[5], pred_y[6])
    print('y_test: ', y_test[0], y_test[1], y_test[2], y_test[3], y_test[4], y_test[5], y_test[6])
    print('\npred_y: ', pred_y[-1], pred_y[-2], pred_y[-3], pred_y[-4], pred_y[-5], pred_y[-6])
    print('y_test: ', y_test[-1], y_test[-2], y_test[-3], y_test[-4], y_test[-5], y_test[-6])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))


    # Create a model wrapper
    predictWrapper = modelWrapper(model)
    hopskipjump.attack(predictWrapper, x_train, x_test, y_train, y_test, input_shape, x_test[data_idx])
Ejemplo n.º 2
0
def main():

    # Define variable
    datatype = 'gtsrb_binary'
    modelpath = '../binary/checkpoints/gtsrb_binary_simplenet_100.pkl'

    # Define which data sample to be processed
    data_idx = 0

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # Predict
    pred_y = model.predict(x_test)
    print('pred_y: ', pred_y[:10])
    print('y_test: ', y_test[:10])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model, datatype)

    hopskipjump.attack(predictWrapper, x_train, x_test, y_train, y_test, input_shape, x_test[data_idx])
def main():

    # Define variable
    datatype = 'cifar10'
    model = BNN([
        '../binary/checkpoints/cifar10_mlpbnn_approx_%d.h5' % (i)
        for i in range(100)
    ])

    print('------------- model -------------\n', 'cifar10_mlpbnn_approx')

    # Define which data sample to be processed
    data_idx = 1500
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Predict
    vote = {}
    print('\n\nVote id: {}\n')
    pred_y = model.predict(x_test, best_index=vote).astype(int)

    print('pred_y[0]: ', pred_y[data_idx])
    print('y_test[0]: ', y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model, vote)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                  y_test, input_shape, x_test[data_idx])

    print('adv_data predict: ', model.predict(adv_data, best_index=vote))

    np.save('bnn_adv_data_1500/bnn_adv_data_{}', adv_data)
Ejemplo n.º 4
0
def main():

    # Define variable
    datatype = 'cifar10'
    model = BNN([
        '../binary/checkpoints/cifar10_mlpbnn_approx_%d.h5' % (i)
        for i in range(100)
    ])

    print('------------- model -------------\n', 'cifar10_mlpbnn_approx')

    # Define which data sample to be processed
    data_idx = 0
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # m is the number of feature
    m = 3 * 32 * 32
    model.nfeatures = math.sqrt(m) / m
    print('nfeatures: ', model.nfeatures)

    # Predict
    pred_y = model.predict(x_test).astype(int)
    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                  y_test, input_shape, x_test[data_idx])

    print('adv_data predict: ', model.predict(adv_data))
Ejemplo n.º 5
0
def main():

    # Define variable
    datatype = 'cifar10'
    modelpath = '../binary/checkpoints/cifar10_mlp_100.pkl'

    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 800
    print('---------------data point---------------\n', data_idx)


    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # Predict
    pred_y = model.predict(x_test)

    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))


    # Create a model wrapper
    predictWrapper = modelWrapper(model)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train, y_test, input_shape, x_test[data_idx])

    print('adv_data predict: ', model.predict(adv_data))
Ejemplo n.º 6
0
def main():

    # Define variable
    datatype = 'gtsrb_binary'
    modelpath = '../binary/checkpoints/gtsrb_binary_scd01mlpbnn_100_br02_h20_nr075_ni10000_i1_0.pkl'

    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 0
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # Predict
    pred_y = model.predict(x_test, cuda=False)
    print('pred_y: ', pred_y[0], pred_y[1], pred_y[2], pred_y[3], pred_y[4], pred_y[5], pred_y[6])
    print('y_test: ', y_test[0], y_test[1], y_test[2], y_test[3], y_test[4], y_test[5], y_test[6])
    print('\npred_y: ', pred_y[-1], pred_y[-2], pred_y[-3], pred_y[-4], pred_y[-5], pred_y[-6])
    print('y_test: ', y_test[-1], y_test[-2], y_test[-3], y_test[-4], y_test[-5], y_test[-6])
    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))


    # Create a model wrapper
    predictWrapper = modelWrapper(model)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train, y_test, input_shape, x_test[data_idx])

    print('adv_data predict: ', model.predict(adv_data))
Ejemplo n.º 7
0
def main():

    # Define variable
    datatype = 'cifar10'
    modelpath = '../binary/checkpoints/cifar10_scdcemlpbnn_100_br02_h20_nr075_ni10000_i1_0.pkl'

    print('\n------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 1500
    print('\n---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # model.round=1
    # print('number of vote: ', model.round)

    adv_lst = []

    # Predict
    for vote in range(100):
        print('\n\nVote id: {}\n'.format(vote))
        pred_y = model.predict(x_test,
                               cuda=False,
                               kind='best',
                               best_index=vote).astype(int)

        print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
        print('true_y[{}]: '.format(data_idx), y_test[data_idx])
        print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

        # Create a model wrapper
        predictWrapper = modelWrapper(model, vote)
        adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                      y_test, input_shape, x_test[data_idx])

        adv_lst.append(adv_data)

        print(
            'adv_data predict: ',
            model.predict(adv_data, cuda=False, kind='best', best_index=vote))

    adv = np.array(adv_lst)
    print('shape', adv.shape)
    adv = np.squeeze(adv, axis=1)
    print('shape', adv.shape)
    np.save('scdcemlp_bnn_adv_data_1500', adv)
Ejemplo n.º 8
0
def main():

    # Define variable
    datatype = 'cifar10'
    group = 9
    modelpath = '../binary/checkpoints/cifar10_resnet50_10_{}.pkl'.format(
        group)

    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 1500
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    adv_lst = []

    # Predict
    for vote in range(10):
        print('\n\nVote id: {}\n'.format(vote))
        pred_y = model.predict(x_test, best_index=vote).astype(int)

        print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
        print('y_test[{}]: '.format(data_idx), y_test[data_idx])
        print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

        # Create a model wrapper
        predictWrapper = modelWrapper(model, datatype, vote)
        adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                      y_test, input_shape, x_test[data_idx])

        adv_lst.append(adv_data)

        if datatype == 'gtsrb_binary':
            adv_data = adv_data.reshape(-1, 3, 48, 48)
        elif datatype == 'cifar10':
            adv_data = adv_data.reshape(-1, 3, 32, 32)
        print('adv_data predict: ', model.predict(adv_data, best_index=vote))

    adv = np.array(adv_lst)
    print('shape', adv.shape)
    adv = np.squeeze(adv, axis=1)
    print('shape', adv.shape)
    np.save('resnet_adv_data_1500/resnet_adv_data_{}'.format(group), adv)
Ejemplo n.º 9
0
def main():

    # Define variable
    datatype = 'celeba'
    modelpath = '../binary/checkpoints/celeba_resnet50_10.pkl'

    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 800
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # Predict
    pred_y = model.predict(x_test).astype(int)
    print('pred_y[0:7]: ', pred_y[0], pred_y[1], pred_y[2], pred_y[3],
          pred_y[4], pred_y[5], pred_y[6])
    print('y_test[0:7]: ', y_test[0], y_test[1], y_test[2], y_test[3],
          y_test[4], y_test[5], y_test[6])
    print('\npred_y[-1:-7]: ', pred_y[-1], pred_y[-2], pred_y[-3], pred_y[-4],
          pred_y[-5], pred_y[-6])
    print('y_test[-1:-7]: ', y_test[-1], y_test[-2], y_test[-3], y_test[-4],
          y_test[-5], y_test[-6])
    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model, datatype)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                  y_test, input_shape, x_test[data_idx])

    if datatype == 'gtsrb_binary':
        adv_data = adv_data.reshape(-1, 3, 48, 48)
    elif datatype == 'cifar10':
        adv_data = adv_data.reshape(-1, 3, 32, 32)
    elif datatype == 'celeba':
        adv_data = adv_data.reshape(-1, 3, 96, 96)

    print('adv_data predict: ', model.predict(adv_data))
def main():

    # Define variable
    datatype = 'cifar10'
    modelpath = '../binary/checkpoints/cifar10_mlp_100.pkl'

    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 1500
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    adv_lst = []

    # Predict
    for vote in range(100):
        print('\n\nVote id: {}\n'.format(vote))
        pred_y = model.estimators_[vote].predict(x_test)

        print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
        print('y_test[{}]: '.format(data_idx), y_test[data_idx])
        print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

        # Create a model wrapper
        predictWrapper = modelWrapper(model, vote)
        adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                      y_test, input_shape, x_test[data_idx])

        adv_lst.append(adv_data)

        print('adv_data predict: ', model.estimators_[vote].predict(adv_data))

    adv = np.array(adv_lst)
    print('shape', adv.shape)
    adv = np.squeeze(adv, axis=1)
    print('shape', adv.shape)
    np.save('mlp_adv_data_1500', adv)
Ejemplo n.º 11
0
def main():

    # Define variable
    datatype = 'cifar10'
    model = BNN([
        '../binary/checkpoints/cifar10_mlpbnn_approx_%d.h5' % (i)
        for i in range(100)
    ])

    print('------------- model -------------\n', 'cifar10_mlpbnn_approx')

    # Define which data sample to be processed
    data_idx = 1991
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    adv_lst = []

    # Predict
    for vote in range(100):
        print('\n\nVote id: {}\n'.format(vote))
        pred_y = model.predict(x_test, best_index=vote).astype(int)

        print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
        print('y_test[{}]: '.format(data_idx), y_test[data_idx])
        print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

        # Create a model wrapper
        predictWrapper = modelWrapper(model, vote)
        adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                      y_test, input_shape, x_test[data_idx])

        adv_lst.append(adv_data)

        print('adv_data predict: ', model.predict(adv_data, best_index=vote))

    adv = np.array(adv_lst)
    print('shape', adv.shape)
    adv = np.squeeze(adv, axis=1)
    print('shape', adv.shape)
    np.save('bnn_adv_data', adv)
Ejemplo n.º 12
0
def main():

    # Define variable
    datatype = 'cifar10'
    modelpath = '../binary/checkpoints/cifar10_scdcemlpbnn_100_br02_h20_nr075_ni10000_i1_0.pkl'

    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 0
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # model.round=1
    # print('number of vote: ', model.round)

    # m is the number of feature
    m = 3 * 32 * 32
    model.nfeatures = math.sqrt(m) / m
    print('nfeatures: ', model.nfeatures)

    # Predict
    pred_y = model.predict(x_test, cuda=False).astype(int)
    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                  y_test, input_shape, x_test[data_idx])

    print('adv_data predict: ', model.predict(adv_data, cuda=False))
def main():

    # Define variable
    datatype = 'cifar10'
    # Resnet 10 votes
    modelpath = '../binary/checkpoints/cifar10_resnet50_10.pkl'
    print('------------- model -------------\n', modelpath)

    # Define which data sample to be processed
    data_idx = 800
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # # Load model
    with open(modelpath, 'rb') as f:
        model = pickle.load(f)

    # # 100 votes
    # model = CNNVote('../binary/checkpoints/cifar10_resnet50_10', 10)
    # print('------------- model -------------\n', 'cifar10_resnet50_10_100 votes')

    # Predict
    pred_y = model.predict(x_test).astype(int)
    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model, datatype)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                  y_test, input_shape, x_test[data_idx])

    if datatype == 'gtsrb_binary':
        adv_data = adv_data.reshape(-1, 3, 48, 48)
    elif datatype == 'cifar10':
        adv_data = adv_data.reshape(-1, 3, 32, 32)
    print('adv_data predict: ', model.predict(adv_data))
Ejemplo n.º 14
0
def main():

    # Define variable
    datatype = 'celeba'
    model = BNN([
        '../binary/checkpoints/celeba_mlpbnn_approx_%d.h5' % (i)
        for i in range(100)
    ])

    print('------------- model -------------\n', 'celeba_mlpbnn_approx')

    # Define which data sample to be processed
    data_idx = 1800
    print('---------------data point---------------\n', data_idx)

    # Load data
    x_train, x_test, y_train, y_test, input_shape = loadData(datatype)

    # Predict
    pred_y = model.predict(x_test).astype(int)
    print('pred_y: ', pred_y[0], pred_y[1], pred_y[2], pred_y[3], pred_y[4],
          pred_y[5], pred_y[6])
    print('y_test: ', y_test[0], y_test[1], y_test[2], y_test[3], y_test[4],
          y_test[5], y_test[6])
    print('\npred_y: ', pred_y[-1], pred_y[-2], pred_y[-3], pred_y[-4],
          pred_y[-5], pred_y[-6])
    print('y_test: ', y_test[-1], y_test[-2], y_test[-3], y_test[-4],
          y_test[-5], y_test[-6])
    print('pred_y[{}]: '.format(data_idx), pred_y[data_idx])
    print('y_test[{}]: '.format(data_idx), y_test[data_idx])
    print('Accuracy: ', accuracy_score(y_true=y_test, y_pred=pred_y))

    # Create a model wrapper
    predictWrapper = modelWrapper(model)
    adv_data = hopskipjump.attack(predictWrapper, x_train, x_test, y_train,
                                  y_test, input_shape, x_test[data_idx])

    print('adv_data predict: ', model.predict(adv_data))