Example #1
0
def main():
    parser = argparse.ArgumentParser(
        description='check if CROWN verifies given alpha',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('crown_path')
    parser.add_argument('model', help='.npy model generated by mat2npy.py')
    parser.add_argument('inputs',
                        help='input files generated by model2chk.py',
                        nargs='+')
    args = parser.parse_args()

    sys.path.append(args.crown_path)
    from get_bounds_ours import compute_worst_bound, get_weights_list
    import save_nlayer_weights as nl

    model = nl.NLayerModel(None, args.model, activation='relu')

    for inp_name in args.inputs:
        with open(inp_name, 'rb') as fin:
            rec = pickle.load(fin)

        orig_inp = rec['input']
        orig_inp = np.transpose(orig_inp, (1, 2, 0))
        pred = np.squeeze(model.model.predict(orig_inp[None]), 0)

        assert np.argmax(pred) == rec['label']
        weights, biases = get_weights_list(model)

        def get_bound(inp):
            gap_gx, g_x0, max_grad_norm = compute_worst_bound(weights,
                                                              biases,
                                                              rec['label'],
                                                              rec['label'],
                                                              inp,
                                                              pred,
                                                              None,
                                                              eps=rec['eps'],
                                                              untargeted=True)
            return gap_gx

        b0 = get_bound(orig_inp)
        b1 = get_bound(orig_inp * rec['alpha'])
        print('file={} label={} gap0={} gap1={}'.format(
            inp_name, rec['label'], b0, b1))
Example #2
0
                    args.numlayer) + "layer_relu_" + str(
                        nhidden) + "_best" + suffix
                if not os.path.isfile(modelfile):
                    modelfile = args.filename
                    if not os.path.isfile(modelfile):
                        raise (RuntimeError("cannot find model file"))

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:
        if args.model == "mnist":
            data = MNIST()
            if args.cnnmodel:
                model = nl.CNNModel(modelfile)
            elif args.filename:
                model = nl.NLayerModel(args.layers, modelfile)
            else:
                model = nl.NLayerModel([nhidden] * (args.numlayer - 1),
                                       modelfile)
        elif args.model == "cifar":
            data = CIFAR()
            if args.cnnmodel:
                model = nl.CNNModel(modelfile)
            elif args.filename:
                model = nl.NLayerModel(args.layers,
                                       modelfile,
                                       image_size=32,
                                       image_channel=3)
            else:
                model = nl.NLayerModel([nhidden] * (args.numlayer - 1),
                                       modelfile,
Example #3
0
            modelfile = "models/" + args.model + "_" + str(args.numlayer) + "layer_" + activation + "_" + suffix
            # if still not found, try models/mnist_3layer_relu_1024_best
            if not os.path.isfile(modelfile):
                modelfile = "models/" + args.model + "_" + str(args.numlayer) + "layer_" + activation + "_" + str(nhidden) + suffix + "_best"
                if not os.path.isfile(modelfile):
                    raise(RuntimeError("cannot find model file"))
    if args.LP or args.LPFULL:
        # use gurobi solver
        import gurobipy as grb

    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:   
        if args.model == "mnist":
            data = MNIST()
            model = nl.NLayerModel([nhidden] * (args.numlayer - 1), modelfile, activation=activation)
        elif args.model == "cifar":
            data = CIFAR()
            model = nl.NLayerModel([nhidden] * (args.numlayer - 1), modelfile, image_size=32, image_channel=3, activation=activation)
        else:
            raise(RuntimeError("unknown model: "+args.model))
                
        print("Evaluating", modelfile)
        sys.stdout.flush()

        random.seed(1215)
        np.random.seed(1215)
        tf.set_random_seed(1215)

        # the weights and bias are saved in lists: weights and bias
        # weights[i-1] gives the ith layer of weight and so on