Beispiel #1
0
para_random_spike = None if args.random_spike is None else parse_rand_spike(
    args.random_spike)
_is_batch = args.random_spike_batch == 'yes'
dropout = args.dropout
gpu_idx = args.gpu_idx
rotation = args.rotation
data_format = args.data_format
is_distillation = args.is_dis == 'yes'
is_data_gen = args.is_data_gen == 'yes'
ex_data_dir = args.ex_data_dir
ex_data_name = args.ex_data_name
ex_data_size = args.ex_data_size
ex_data_sel_rand = args.ex_data_sel_rand == 'yes'
pre_idx_path = args.pre_idx

setup_visibile_gpus(str(gpu_idx))

k.tensorflow_backend.set_session(tf.Session(config=gpu_config))

if not os.path.exists(save_model_dir):
    os.makedirs(save_model_dir)

data = MNIST(data_dir,
             data_name,
             validation_size,
             model_meta=model_mnist_meta,
             input_data_format=CHANNELS_LAST,
             output_data_format=data_format,
             train_size=train_size,
             train_sel_rand=train_sel_rand)
    is_rand = args.is_data_rand == 'yes'
    target_rand = args.is_target_rand == 'yes'
    const_factor = args.cw_const_factor
    initial_const = args.cw_init_const
    is_det_joint = args.is_det_joint == 'yes'
    eval_lab = args.eval_lab.split(",")

    boxmin = args.boxmin
    boxmax = args.boxmax
    top_k = args.top_k
    att_label_num = args.att_label_num
    gpu_config.gpu_options.per_process_gpu_memory_fraction = args.gpu_mem

    fgm_eps = args.fgm_eps

    setup_visibile_gpus(",".join(map(str, gpu_idx)))
    selected_gpus = list(range(len(gpu_idx)))
    gpu_count = len(gpu_idx)

    if set_name == 'mnist':
        model_meta = model_mnist_meta
        MODEL = MNISTModel
        para_random_spike = None if para_random_spike is None else parse_rand_spike(para_random_spike)
    elif set_name == 'fashion':
        model_meta = model_mnist_meta
        MODEL = FASHIONModel
        para_random_spike = 0 if para_random_spike is None else int(para_random_spike)
    elif set_name == "cifar10":
        model_meta = model_cifar10_meta
        MODEL = CIFAR10Model
        para_random_spike = 0 if para_random_spike is None else int(para_random_spike)
Beispiel #3
0
     (img_height + margin) * math.ceil(count / col_len) - margin),
    default_color)

col_width = len(str(col_len)) + 1
col_tmpl = "%" + str(col_width) + "d"
for i in range(col_len):
    print(col_tmpl % i, end='')

print()

for i in range(col_len * col_width):
    print("-", end='')

print()

setup_visibile_gpus(None)

with tf.Session(config=gpu_config) as sess:
    with tf.device('/cpu:0'):
        # load the original training and testing data
        data = MNIST(args.data_dir,
                     '',
                     model_meta=model_meta,
                     normalize=False,
                     batch_size=batch_size)

        data_type = 'test' if is_test else 'train'
        data_idx = data_idx[start_idx // step:start_idx // step + count]
        argsort_adv_img_idx = np.argsort(data_idx)
        back_argsort_adv_img_idx = np.argsort(argsort_adv_img_idx)
        data_ref, _, _ = data.get_data_by_idx(data_idx[argsort_adv_img_idx],
parser.add_argument('--det_model_names', help='detect model names', type=str, default='')
parser.add_argument('--reformer_name', help='detect model reformers', type=str, default='')
parser.add_argument('--is_det_joint', help='whether use one threshold for all detectors', type=str, default='no')
parser.add_argument('--is_logits', help='whether tp apply bagging with logits or softmax', type=str, default='yes')

requiredNamed = parser.add_argument_group('required named arguments')
requiredNamed.add_argument('-d', '--data_dir', help='attack dir', type=str, required=True)
requiredNamed.add_argument('-n', '--data_name', help='attack name', type=str, required=True)
requiredNamed.add_argument('--model_dir', help='model save dir', type=str, required=True)
requiredNamed.add_argument('--model_name', help='model save name', type=str, required=True)
requiredNamed.add_argument('-s', '--set_name', help='attack set name', type=str, required=True)
requiredNamed.add_argument('--data_format', help='data_format', type=str, required=True)

args = parser.parse_args()

setup_visibile_gpus(str(args.gpu_idx))

set_name = args.set_name
if set_name == 'mnist':
    def parse_rand_spike(_str):
        _str = _str.split(',')
        return [float(x) for x in _str]

    model_meta = model_mnist_meta
    MODEL = MNISTModel
    para_random_spike = parse_rand_spike(args.rspike_para) if args.rspike_para is not None else []
elif set_name == 'fashion':
    model_meta = model_mnist_meta
    MODEL = FASHIONModel
    para_random_spike = args.rspike_layer
elif set_name == "cifar10":
Beispiel #5
0
ex_data_name = args.ex_data_name
ex_data_size = args.ex_data_size
ex_data_sel_rand = args.ex_data_sel_rand == 'yes'
pre_idx_path = args.pre_idx


def parse_gpus_str(in_str):
    _tmp = in_str.split(',')
    if len(_tmp) == 1:
        return list(range(int(_tmp[0]))) if _tmp[0] != '' else []
    else:
        return [int(x) for x in _tmp if x != '']


selected_gpus = parse_gpus_str(gpus_str)
setup_visibile_gpus(",".join(map(str, selected_gpus)))
selected_gpus = list(range(len(selected_gpus)))
k.tensorflow_backend.set_session(tf.Session(config=gpu_config))

if not os.path.exists(save_model_dir):
    os.makedirs(save_model_dir)

data = MNIST(data_dir, data_name, validation_size, model_meta=model_mnist_meta,
             input_data_format=CHANNELS_LAST, output_data_format=data_format,
             train_size=train_size, train_sel_rand=train_sel_rand)

if pre_idx_path is not None:
    pre_idx = utils.load_model_idx(pre_idx_path)
    data.apply_pre_idx(pre_idx)
if ex_data_dir is not None and ex_data_name is not None and ex_data_size > 0:
    data.append_train_data(ex_data_dir, ex_data_name, ex_data_size,