def main():
    #Dataset
    dataset = ImageNet(DATASET_PATH)
    #Model
    model, checkpoint = model_utils.make_and_restore_model(
        arch='vgg19', dataset=dataset, resume_path=CHECKPOINT_PATH)
    #DataLoader
    train_loader, val_loader = dataset.make_loaders(batch_size=BATCH_SIZE,
                                                    workers=NUM_WORKERS)
    if 'module' in dir(model): model = model.module
    # Create a cox store for logging
    out_store = cox.store.Store(OUT_DIR)
    # Hard-coded base parameters
    train_kwargs = {
        'out_dir': "train_out",
        'adv_train': 1,
        'constraint': '2',
        'eps': 3.0,
        'attack_lr': 0.5,
        'attack_steps': 7,
        'save_ckpt_iters': 5
    }
    train_args = Parameters(train_kwargs)
    # Fill whatever parameters are missing from the defaults
    train_args = defaults.check_and_fill_args(train_args,
                                              defaults.TRAINING_ARGS, ImageNet)
    train_args = defaults.check_and_fill_args(train_args, defaults.PGD_ARGS,
                                              ImageNet)
    # Train a model
    train.train_model(train_args,
                      model, (train_loader, val_loader),
                      store=out_store,
                      checkpoint=checkpoint)
示例#2
0
from cox.utils import Parameters

from robustness.main import setup_args, setup_store_with_metadata

EPS = 1.0
eps_str = f'{EPS}'.replace('.', 'p')
exp_name = f'fast_adv_l2_{eps_str}'

train_kwargs = {
    'out_dir': "logs",
    'exp_name': exp_name,
    'dataset': 'cifar',
    'data': 'data/cifar10',
    'arch': 'resnet50',
    'adv_train': 1,
    'constraint': '2',
    'eps': EPS,
    'attack_steps': 20,
    'attack_lr': 2.5 * EPS / 20,
    'fast_attack_lr': 1.5 * EPS
}
train_args = Parameters(train_kwargs)

# Fill whatever parameters are missing from the defaults
train_args = setup_args(train_args)
print(train_args)
store = setup_store_with_metadata(train_args)

final_model = main(train_args, store=store)

示例#3
0
exp_name = f'fast_adv_l2_{eps_str}'

for e in [0, 0.25, 0.5, 1.0, 2.0]:
    e_str = f'{e}'.replace('.', 'p')
    eval_exp_name = f'fast_adv_l2_{eps_str}_{e_str}'

    eval_kwargs = {
        'eval_only': 1,
        'resume': os.path.join('logs', exp_name, 'checkpoint.pt.best'),
        'out_dir': "logs_eval",
        'exp_name': eval_exp_name,
        'dataset': 'cifar',
        'data': 'data/cifar10',
        'arch': 'resnet50',
        'adv_train': 0,
        'adv_eval': 1,
        'constraint': '2',
        'eps': e,
        'attack_steps': 20,
        'attack_lr': 2.5 * e / 20,
        'fast_attack_lr': 1.5 * e
    }
    train_args = Parameters(eval_kwargs)

    # Fill whatever parameters are missing from the defaults
    train_args = setup_args(train_args)
    print(train_args)
    store = setup_store_with_metadata(train_args)

    final_model = main(train_args, store=store)
m, _ = model_utils.make_and_restore_model(arch='resnet50', dataset=ds)
train_loader, val_loader = ds.make_loaders(batch_size=128, workers=8)
# train_loader = LambdaLoader(train_loader, label_permutate)
# val_loader = LambdaLoader(val_loader, label_permutate)
# Create a cox store for logging
out_store = cox.store.Store('coxx')

# Hard-coded base parameters
train_kwargs = {
    'out_dir': "train_out",
    'adv_train': 0,
    'lr': .01,
    'epochs': 300,
    'save-ckpt-iters': 10
}
train_args = Parameters(train_kwargs)

idx = np.arange(label_dim)
order = np.load('../data/rnd_label_c10_5.npy')[idx].T
train_crit = torch.nn.BCELoss()


def custom_train_loss(logits, targ):
    if torch.cuda.is_available():
        targets = torch.from_numpy(order[targ.cpu().numpy()]).cuda()
    else:
        targets = torch.from_numpy(order[targ.numpy()])
    outputs = torch.sigmoid(logits.float())
    return train_crit(outputs.float(), targets.float())