コード例 #1
0
def main(args):
    
    model_class = get_model(args[1])
    dataset = dataset_by_name(args[2])

    params = get_params(args[1], dataset.name)
    
    tf.logging.debug('Creating attack ops')
    attack = CWAttackOp(model_class,
                        params,
                        dataset.shape,
                        dataset.num_classes,
                        kappa=cfg.kappa,
                        rand_start_std=cfg.rand_start_std)
    
    ckpt_dir = get_dir(cfg.ckpt_dir, dataset.name, attack.model.name)
    saver = tf.train.Saver(var_list=tf.global_variables(scope=attack.model.scope))
    
    tf.logging.debug('Starting session')
    with tf.Session() as sess:
        try:
            save_path = tf.train.latest_checkpoint(ckpt_dir)
            saver.restore(sess, save_path)
            create_adv(sess, dataset, attack)
        except KeyboardInterrupt:
            print("Manual interrupt occured")
コード例 #2
0
def main(args):

    model_class = get_model(args[1])
    dataset = dataset_by_name(args[2])

    params = get_params(args[1], dataset.name)

    tf.logging.debug('Building model')
    model = model_class(trainable=False,
                        num_classes=dataset.num_classes,
                        shape=dataset.shape,
                        **params)

    tf.logging.debug('Creating attack ops')
    fgsm = FGSM(model, fgsm_eps)

    ckpt_dir = get_dir(cfg.ckpt_dir, dataset.name, model.name)
    saver = tf.train.Saver(var_list=tf.global_variables(scope=model.scope))

    tf.logging.debug('Starting session')
    with tf.Session() as sess:
        try:
            save_path = tf.train.latest_checkpoint(ckpt_dir)
            saver.restore(sess, save_path)
            create_adv(sess, dataset, fgsm)
        except KeyboardInterrupt:
            print("Manual interrupt occurred")
コード例 #3
0
def main(args):

    model_name = args[1]
    model_class = get_model(model_name)
    source_name = args[2]
    dataset = dataset_by_name(args[3])

    if len(args) < 5:
        attacks = ['carlini_wagner', 'boundary_attack', 'deepfool', 'universal_perturbation']
    else:
        attacks = args[4].split(',')

    params = get_params(model_name, dataset.name)

    tf.logging.debug('Creating model graph')
    model = model_class(trainable=False, num_classes=dataset.num_classes, shape=dataset.shape, **params)

    ckpt_dir = get_dir(cfg.ckpt_dir, dataset.name, model.name)
    saver = tf.train.Saver(var_list=tf.global_variables(scope=model.scope))

    if cfg.stop_before_session:
        exit()

    tf.logging.debug('Starting session')
    with tf.Session() as sess:
        try:
            save_path = tf.train.latest_checkpoint(ckpt_dir)
            saver.restore(sess, save_path)
            for attack in attacks:
                measure_attack(attack, dataset.name, model, sess, source_name)

        except KeyboardInterrupt:
            print("Manual interrupt occurred")
コード例 #4
0
def main():
    np.random.seed(239048123)

    imgs = {}
    AdvImg = namedtuple('AdvImg', ('orig', 'pert', 'adv'))

    for dataset_name in model_names:
        dataset = dataset_by_name(dataset_name)
        for attack_name in attack_names:

            orig = get_attack_original(attack_name, dataset)[0]
            orig = orig[:num_adv[attack_name]]

            for model_type in model_names[dataset_name]:
                model_name = model_names[dataset_name][model_type]

                adv = get_adv(model_name, attack_name, dataset_name)
                if 'universal' in attack_name:
                    pert = adv
                    orig = orig[:len(pert)]
                    adv = np.clip(orig + pert, 0, 1)
                else:
                    adv = adv[:num_adv[attack_name]]
                    pert = orig - adv

                imgs['_'.join([dataset_name, attack_name, model_type])] = \
                    AdvImg(orig, pert, adv)

    # Filter out stuff
    for dataset_name in model_names:
        for attack_name in attack_names:
            names = [
                '_'.join([dataset_name, attack_name, model_type])
                for model_type in model_names[dataset_name]
            ]
            idx = np.logical_and(
                *[filter_idx(imgs[name].pert) for name in names])

            for name in names:
                v = imgs[name]
                imgs[name] = AdvImg(orig=v.orig[idx].squeeze(),
                                    pert=v.pert[idx].squeeze(),
                                    adv=v.adv[idx].squeeze())

    for name, img in imgs.items():
        for n in range(NUM_IMGS):
            imsave(name, 'orig', n, img.orig[n])
            imsave(name, 'adv', n, img.adv[n])

            imsave(name, 'pert', n, scale_pert(img.pert[n]))
            imsave(name, 'pertvisible', n, scale_pert_visible(img.pert[n]))
コード例 #5
0
def main(args):

    model_class = get_model(args[1])
    dataset = dataset_by_name(args[2])

    params = get_params(args[1], dataset.name)
    load_config(dataset.name, optional=True)

    with tf.variable_scope('data'):
        tf.logging.debug('Load data')
        train_data = to_tf_dataset(dataset, is_train=True, batch_size=cfg.batch_size,
                                   aug_strength=cfg.data_aug, 
                                   aug_prob=cfg.aug_prob,
                                   aug_flip=cfg.aug_flip)
        test_data = to_tf_dataset(dataset, is_train=False, batch_size=cfg.batch_size)

        iterator = tf.data.Iterator.from_structure(train_data.output_types,
                                                   train_data.output_shapes)
        img, label = iterator.get_next()

        tf.logging.debug('Creating iterator initializer')
        train_init = iterator.make_initializer(train_data)
        test_init = iterator.make_initializer(test_data)

    tf.train.create_global_step()
    create_epoch()

    tf.logging.debug('Creating model graph')
    model = model_class(img=img, label=label, num_classes=dataset.num_classes, **params)

    ckpt_dir = get_dir(cfg.ckpt_dir, dataset.name, model.name)
    log_dir = get_dir(cfg.log_dir, dataset.name, model.name)

    if cfg.stop_before_session:
        exit()

    tf.logging.debug('Starting session')
    with tf.Session() as sess:
        try:
            train_with_test(sess, model, train_init, test_init, ckpt_dir, log_dir)
        except KeyboardInterrupt:
            print("Manual interrupt occured")
コード例 #6
0
def main():
    np.random.seed(239048123)

    for dataset_name in model_names:
        dataset = dataset_by_name(dataset_name)

        orig = np.concatenate([
            get_attack_original(attack, dataset)[0] for attack in attack_names
        ])

        rand_pert = np.random.normal(size=orig.shape)
        save_sing(rand_pert, dataset_name, 'random')

        for model_type in model_names[dataset_name]:
            model_name = model_names[dataset_name][model_type]

            adv = np.concatenate([
                get_adv(model_name, attack, dataset_name)
                for attack in attack_names
            ])
            save_sing(orig - adv, dataset_name, model_type)
コード例 #7
0
def main(args):

    model_class = get_model(args[1])
    dataset = dataset_by_name(args[2])

    params = get_params(args[1], dataset.name)

    tf.logging.debug('Creating attack ops')
    deepfool = DeepfoolOp(model_class, dataset=dataset, params=params)

    ckpt_dir = get_dir(cfg.ckpt_dir, dataset.name, deepfool.model.name)
    saver = tf.train.Saver(var_list=tf.global_variables(
        scope=deepfool.model.scope))

    tf.logging.debug('Starting session')
    with tf.Session() as sess:
        try:
            sess.graph.finalize()
            save_path = tf.train.latest_checkpoint(ckpt_dir)
            saver.restore(sess, save_path)
            create_adv(sess, dataset, deepfool)
        except KeyboardInterrupt:
            print("Manual interrupt occured")