def main(args): config = { "csv_path": args.path, "ckpt_path": args.ckpt_path, "epoch": args.epoch, "lr": args.lr, "momentum": args.momentum, "weight_decay": args.weight_decay, "optimizer": args.optimizer, "criterion": nn.MSELoss(), "eval_step": args.eval_step, "fc_bias": bias, } fix_seed(args.seed) bias = True if args.bias else False model = ResNet34(num_classes=args.num_classes, fc_bias=bias) trainer = Trainer(model=model, config=config) t = time.time() global_step, best_val_loss = trainer.train() train_time = time.time() - t m, s = divmod(train_time, 60) h, m = divmod(m, 60) print() print("Training Finished.") print("** Total Time: {}-hour {}-minute".format(int(h), int(m))) print("** Total Step: {}".format(global_step)) print("** Best Validation Loss: {:.3f}".format(best_val_loss))
def main(args): bias = True if args.bias else False config = { "csv_path": args.path, "ckpt_path": args.ckpt_path, "batch_size": args.batch_size, "epoch": args.epoch, "lr": args.lr, "momentum": args.momentum, "weight_decay": args.weight_decay, "optimizer": args.optimizer, "criterion": softXEnt, "eval_step": args.eval_step, "fc_bias": bias, "label_type": args.label_type, "iid": args.iid, "transform": args.transform, } fix_seed(args.seed) model = ResNet34(num_classes=args.num_classes, fc_bias=bias) trainer = Trainer(model=model, config=config) t = time.time() # global_step, best_val_loss = trainer.train() trainer.train() train_time = time.time() - t m, s = divmod(train_time, 60) h, m = divmod(m, 60) print() print("Training Finished.") print("** Total Time: {}-hour {}-minute".format(int(h), int(m)))
def main(): global args args = parser.parse_args() # load dataset if args.dataset == 'sign_mnist': loader = dataset_loader(True) if args.model == 'LeNet5': train_loader, test_loader = loader.load_sign_mnist( 28, isGrayScale=args.gray_scale) elif args.model == 'ResNet34': train_loader, test_loader = loader.load_sign_mnist( 224, isGrayScale=args.gray_scale) else: raise RuntimeError('unrecognized model name ' + repr(args.model)) elif args.dataset == 'kinect_leap': loader = dataset_loader(False) if args.model == 'LeNet5': train_loader, test_loader = loader.load_kinect_leap( img_size=28, isGrayScale=args.gray_scale) elif args.model == 'ResNet34': train_loader, test_loader = loader.load_kinect_leap( img_size=224, isGrayScale=args.gray_scale) else: raise RuntimeError('unrecognized model name ' + repr(args.model)) else: raise RuntimeError('unrecogniazed dataset name' + repr(args.dataset)) # load model if args.model == 'LeNet5': model = LeNet5(class_num=loader.class_num, is_gray_scale=args.gray_scale).cuda() elif args.model == 'ResNet34': model = ResNet34(class_num=loader.class_num, is_gray_scale=args.gray_scale).cuda() else: raise RuntimeError('unrecognized model name ' + repr(args.model)) print(model) criterion = nn.CrossEntropyLoss().cuda() optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9) # time counting start_time = time.time() for epoch in range(1, args.epoch + 1): train(model, train_loader, criterion, optimizer, epoch) test(model, test_loader, epoch) end_time = time.time() print('training process using ', end_time - start_time) # save model if args.save_model: saving_path = './trained_model/' + args.model + '.pth' torch.save(model, saving_path) return
def train(): global GLOBAL_STEP, reduction_arc, cell_arc # Dataset dataset = cifarDataset( batchSize=args.batch_size, dataPath=args.data_path, numOfWorkers=args.data_nums_workers, noise_rate=args.nr, is_cifar100=args.train_cifar100, filename=args.fn, ) dataLoader = dataset.getDataLoader() if args.train_cifar100: num_classes = 100 fixed_cnn = ResNet34(num_classes=num_classes) else: num_classes = 10 fixed_cnn = SCEModel() if args.loss == 'SCE': if args.train_cifar100: criterion = SCELoss(alpha=6.0, beta=0.1, num_classes=num_classes) else: criterion = SCELoss(alpha=0.1, beta=1.0, num_classes=num_classes) elif args.loss == 'CE': criterion = torch.nn.CrossEntropyLoss() else: print("Unknown loss") print(criterion.__class__.__name__) print("Number of Trainable Parameters %.4f" % count_parameters_in_MB(fixed_cnn)) fixed_cnn = torch.nn.DataParallel(fixed_cnn) fixed_cnn.to(device) fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(), lr=args.lr, momentum=0.9, nesterov=True, weight_decay=args.l2_reg) if args.train_cifar100: milestone = [80, 120] else: milestone = [40, 80] fixed_cnn_scheduler = MultiStepLR(fixed_cnn_optmizer, milestone, gamma=0.1) utilHelper = TrainUtil(checkpoint_path=args.checkpoint_path, version=args.version) starting_epoch = 0 train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion, fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper)
def train(): global GLOBAL_STEP, reduction_arc, cell_arc # Dataset dataset = DatasetGenerator(batchSize=args.batch_size, dataPath=args.data_path, numOfWorkers=args.data_nums_workers, noise_rate=args.nr, asym=args.asym, seed=args.seed, dataset_type=args.dataset_type) dataLoader = dataset.getDataLoader() if args.dataset_type == 'cifar100': num_classes = 100 args.epoch = 150 fixed_cnn = ResNet34(num_classes=num_classes) elif args.dataset_type == 'cifar10': num_classes = 10 args.epoch = 120 fixed_cnn = SCEModel() else: raise ('Unimplemented') if args.loss == 'SCE': criterion = SCELoss(alpha=args.alpha, beta=args.beta, num_classes=num_classes) elif args.loss == 'CE': criterion = torch.nn.CrossEntropyLoss() else: logger.info("Unknown loss") logger.info(criterion.__class__.__name__) logger.info("Number of Trainable Parameters %.4f" % count_parameters_in_MB(fixed_cnn)) fixed_cnn = torch.nn.DataParallel(fixed_cnn) fixed_cnn.to(device) fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(), lr=args.lr, momentum=0.9, nesterov=True, weight_decay=args.l2_reg) fixed_cnn_scheduler = torch.optim.lr_scheduler.StepLR(fixed_cnn_optmizer, 1, gamma=0.97) utilHelper = TrainUtil(checkpoint_path=args.checkpoint_path, version=args.version) starting_epoch = 0 train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion, fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper)
def initialize_model(use_pu=True, num_classes=10): model = None if (use_pu): model = ResNet34_PU(num_classes=num_classes) model.load_state_dict(torch.load(opt.teacher_model_dir), strict=False) num_ftrs = 512 + 256 + 128 + 64 model.fc = nn.Linear(num_ftrs, 1) else: model = ResNet34(num_classes=num_classes) model.load_state_dict(torch.load(opt.teacher_model_dir)) return model
def get_model(): if args.model == 'ResNet18': return ResNet18(p_dropout=args.dropout) elif args.model == 'ResNet34': return ResNet34(p_dropout=args.dropout) elif args.model == 'ResNet50': return ResNet50(p_dropout=args.dropout) elif args.model == 'ResNet101': return ResNet101(p_dropout=args.dropout) elif args.model == 'ResNet152': return ResNet152(p_dropout=args.dropout) elif args.model == 'VGG11': return VGG('VGG11', p_dropout=args.dropout) elif args.model == 'VGG13': return VGG('VGG13', p_dropout=args.dropout) elif args.model == 'VGG16': return VGG('VGG16', p_dropout=args.dropout) elif args.model == 'VGG19': return VGG('VGG19', p_dropout=args.dropout) else: raise 'Model Not found'
def main(args): bias = True if args.bias else False config = { "csv_path": args.path, "ckpt_path": args.ckpt_path, "epoch": args.epoch, "lr": args.lr, "momentum": args.momentum, "weight_decay": args.weight_decay, "optimizer": args.optimizer, "criterion": nn.MSELoss(), "eval_step": args.eval_step, "fc_bias": bias, } fix_seed(args.seed) model = ResNet34(num_classes=args.num_classes, fc_bias=bias) checkpoint = (torch.load("checkpoints/best_model_bias_true.ckpt") if bias else torch.load("checkpoints/best_model_bias_false.ckpt")) new_state_dict = OrderedDict() for k, v in checkpoint.items(): name = k[7:] # remove 'module.' of dataparallel new_state_dict[name] = v model.load_state_dict(new_state_dict) trainer = Trainer(model=model, config=config) if args.mode == "test": mse, mae, mape = trainer.test() print() print("Test Finished.") print("** Test Loss (MSE): {:.3f}".format(mse)) print("** Test Loss (MAE): {:.3f}".format(mae)) print("** Test Loss (MAPE): {:.3f}".format(mape)) return pred, true = trainer.test_values() return pred, true
download=True, transform=transform) id_testloader = torch.utils.data.DataLoader(id_testset, batch_size=args.test_bs, shuffle=False, num_workers=args.num_workers) """ out-of-distribtuion data looader(SVHN) """ # ood_testloader will be used for test the given ood detection algorithm ood_testset = torchvision.datasets.SVHN(root='./data', split='test', download=True, transform=transform) ood_testloader = torch.utils.data.DataLoader(ood_testset, batch_size=args.test_bs, shuffle=False, num_workers=args.num_workers) # load model trained on CIFAR-10 model = ResNet34() #model.load_state_dict(torch.load('./data/resnet34-31.pth')) model.load_state_dict(torch.load('./data/resnet_cifar10.pth')) # ood dectection test if args.task == 'ood_detection': if args.alg == 'baseline': print('result of baseline alg') ood_test_baseline(model, id_trainloader, id_testloader, ood_testloader, args) elif args.alg == 'mahalanobis': print('result of mahalanobis alg') ood_test_mahalanobis(model, id_trainloader, id_testloader, ood_testloader, args) else: print('--alg should be baseline or mahalanobis') # classification test
output = model(input) acc1, acc5 = accuracy(output, target, topk=(1, 5)) top1.update(acc1[0].item(), input.size(0)) top5.update(acc5[0].item(), input.size(0)) g = open('/root/volume/MidProject/Saliencymix_project/' + 'test' + '.txt', 'a') save_acc = str(epoch) + '\t' + str(top1) + '\t' + str(top5) + '\n' g.write(save_acc) g.close() print('==> Test Accuracy: Acc@1 {top1.avg:.3f} || Acc@5 {top5.avg:.3f}'. format(top1=top1, top5=top5)) return top1.avg model = ResNet34(num_classes=num_classes).cuda() optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9, nesterov=True, weight_decay=5e-4) scheduler = MultiStepLR(optimizer, milestones=[60, 90, 120], gamma=0.2) criterion = torch.nn.CrossEntropyLoss().cuda() f = open('/root/volume/MidProject/Saliencymix_project/acc.txt', 'w') ########################################################### best_acc = 0 for epoch in range(epochs): xentropy_loss_avg = 0.
def train(): # Dataset if args.dataset_type == 'clothing1m': dataset = Clothing1MDatasetLoader(batchSize=args.batch_size, dataPath=args.data_path, numOfWorkers=args.data_nums_workers) elif args.dataset_type == 'imagenet': dataset = ImageNetDatasetLoader(batchSize=args.batch_size, dataPath=args.data_path, seed=args.seed, target_class_num=200, nosiy_rate=0.4, numOfWorkers=args.data_nums_workers) else: dataset = DatasetGenerator(batchSize=args.batch_size, dataPath=args.data_path, numOfWorkers=args.data_nums_workers, noise_rate=args.nr, asym=args.asym, seed=args.seed, dataset_type=args.dataset_type) dataLoader = dataset.getDataLoader() eta_min = 0 ln_neg = 1 if args.dataset_type == 'clothing1m': # Train Clothing1M args.epoch = 20 args.l2_reg = 1e-3 num_classes = 14 fixed_cnn = torchvision.models.resnet50(num_classes=14) # fixed_cnn.fc = torch.nn.Linear(2048, 14) elif args.dataset_type == 'cifar100': # Train CIFAR100 args.lr = 0.1 args.epoch = 200 num_classes = 100 fixed_cnn = ResNet34(num_classes=num_classes) # NLNL if args.loss == 'NLNL': args.epoch = 2000 ln_neg = 110 elif args.dataset_type == 'cifar10': # Train CIFAR10 args.epoch = 120 num_classes = 10 fixed_cnn = SCEModel(type='cifar10') # NLNL if args.loss == 'NLNL': args.epoch = 1000 elif args.dataset_type == 'mnist': # Train mnist args.epoch = 50 num_classes = 10 fixed_cnn = SCEModel(type='mnist') eta_min = 0.001 args.l2_reg = 1e-3 # NLNL if args.loss == 'NLNL': args.epoch = 720 elif args.dataset_type == 'imagenet': args.epoch = 100 args.l2_reg = 3e-5 num_classes = 200 fixed_cnn = torchvision.models.resnet50(num_classes=num_classes) logger.info("num_classes: %s" % (num_classes)) loss_options = { 'SCE': SCELoss(alpha=args.alpha, beta=args.beta, num_classes=num_classes), 'CE': torch.nn.CrossEntropyLoss(), 'NCE': NormalizedCrossEntropy(scale=args.alpha, num_classes=num_classes), 'MAE': MeanAbsoluteError(scale=args.alpha, num_classes=num_classes), 'NMAE': NormalizedMeanAbsoluteError(scale=args.alpha, num_classes=num_classes), 'GCE': GeneralizedCrossEntropy(num_classes=num_classes, q=args.q), 'RCE': ReverseCrossEntropy(scale=args.alpha, num_classes=num_classes), 'NRCE': NormalizedReverseCrossEntropy(scale=args.alpha, num_classes=num_classes), 'NGCE': NormalizedGeneralizedCrossEntropy(scale=args.alpha, num_classes=num_classes, q=args.q), 'NCEandRCE': NCEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes), 'NCEandMAE': NCEandMAE(alpha=args.alpha, beta=args.beta, num_classes=num_classes), 'GCEandMAE': GCEandMAE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q), 'GCEandRCE': GCEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q), 'GCEandNCE': GCEandNCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q), 'MAEandRCE': MAEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes), 'NGCEandNCE': NGCEandNCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q), 'NGCEandMAE': NGCEandMAE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q), 'NGCEandRCE': NGCEandRCE(alpha=args.alpha, beta=args.beta, num_classes=num_classes, q=args.q), 'FocalLoss': FocalLoss(gamma=args.gamma), 'NFL': NormalizedFocalLoss(scale=args.alpha, gamma=args.gamma, num_classes=num_classes), 'NLNL': NLNL(num_classes=num_classes, train_loader=dataLoader['train_dataset'], ln_neg=ln_neg), 'NFLandNCE': NFLandNCE(alpha=args.alpha, beta=args.beta, gamma=args.gamma, num_classes=num_classes), 'NFLandMAE': NFLandMAE(alpha=args.alpha, beta=args.beta, gamma=args.gamma, num_classes=num_classes), 'NFLandRCE': NFLandRCE(alpha=args.alpha, beta=args.beta, gamma=args.gamma, num_classes=num_classes), 'DMI': DMILoss(num_classes=num_classes) } if args.loss in loss_options: criterion = loss_options[args.loss] else: raise("Unknown loss") logger.info(criterion.__class__.__name__) logger.info("Number of Trainable Parameters %.4f" % count_parameters_in_MB(fixed_cnn)) fixed_cnn.to(device) if args.loss == 'DMI': criterion = loss_options['CE'] fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(), lr=args.lr, momentum=0.9, weight_decay=args.l2_reg) fixed_cnn_scheduler = CosineAnnealingLR(fixed_cnn_optmizer, float(args.epoch), eta_min=eta_min) if args.dataset_type == 'clothing1m': fixed_cnn_scheduler = MultiStepLR(fixed_cnn_optmizer, milestones=[5, 10], gamma=0.1) elif args.dataset_type == 'imagenet': fixed_cnn_scheduler = MultiStepLR(fixed_cnn_optmizer, milestones=[30, 60, 80], gamma=0.1) utilHelper = TrainUtil(checkpoint_path=args.checkpoint_path, version=args.version) starting_epoch = 0 for arg in vars(args): logger.info("%s: %s" % (arg, getattr(args, arg))) train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion, fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper) if args.loss == 'DMI': criterion = loss_options['DMI'] fixed_cnn_optmizer = torch.optim.SGD(params=fixed_cnn.parameters(), lr=1e-6, momentum=0.9, weight_decay=args.l2_reg) starting_epoch = 0 fixed_cnn_scheduler = None train_fixed(starting_epoch, dataLoader, fixed_cnn, criterion, fixed_cnn_optmizer, fixed_cnn_scheduler, utilHelper)
def main(): parser = argparse.ArgumentParser(description='PyTorch MNIST Example') parser.add_argument('--result_dir', type=str, help='dir to save result txt files', default='results/') parser.add_argument('--noise_rate', type=float, help='corruption rate, should be less than 1', default=0.5) parser.add_argument('--forget_rate', type=float, help='forget rate', default=None) parser.add_argument('--noise_type', type=str, help='[pairflip, symmetric]', default='symmetric') parser.add_argument( '--num_gradual', type=int, default=10, help= 'how many epochs for linear drop rate, can be 5, 10, 15. This parameter is equal to Tk for R(T) in Co-teaching paper.' ) parser.add_argument( '--exponent', type=float, default=1, help= 'exponent of the forget rate, can be 0.5, 1, 2. This parameter is equal to c in Tc for R(T) in Co-teaching paper.' ) parser.add_argument('--top_bn', action='store_true') parser.add_argument('--dataset', type=str, help='mnist, cifar10, or cifar100', default='mnist') parser.add_argument('--n_epoch', type=int, default=10) parser.add_argument('--seed', type=int, default=1) parser.add_argument('--print_freq', type=int, default=50) parser.add_argument('--num_workers', type=int, default=4, help='how many subprocesses to use for data loading') parser.add_argument('--num_iter_per_epoch', type=int, default=400) parser.add_argument('--epoch_decay_start', type=int, default=80) parser.add_argument('--eps', type=float, default=9.9) parser.add_argument('--batch-size', type=int, default=1000, metavar='N', help='input batch size for training (default: 256)') parser.add_argument('--test-batch-size', type=int, default=4000, metavar='N', help='input batch size for testing (default: 1000)') parser.add_argument('--lr', type=float, default=0.005, metavar='LR', help='learning rate (default: 0.01)') parser.add_argument('--momentum', type=float, default=0.9, metavar='M', help='SGD momentum (default: 0.5)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument( '--log-interval', type=int, default=100, metavar='N', help='how many batches to wait before logging training status') parser.add_argument('--save-model', action='store_true', default=False, help='For Saving the current Model') args = parser.parse_args() torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) batch_size = args.batch_size if args.dataset == 'mnist': input_channel = 1 num_classes = 10 args.top_bn = False args.epoch_decay_start = 80 args.n_epoch = 200 train_dataset = MNIST(root='./data/', download=True, train=True, transform=transforms.ToTensor(), noise_type=args.noise_type, noise_rate=args.noise_rate) test_dataset = MNIST(root='./data/', download=True, train=False, transform=transforms.ToTensor(), noise_type=args.noise_type, noise_rate=args.noise_rate) if args.dataset == 'cifar10': input_channel = 3 num_classes = 10 args.top_bn = False args.epoch_decay_start = 80 args.n_epoch = 200 train_dataset = CIFAR10(root='./data/', download=True, train=True, transform=transforms.ToTensor(), noise_type=args.noise_type, noise_rate=args.noise_rate) test_dataset = CIFAR10(root='./data/', download=True, train=False, transform=transforms.ToTensor(), noise_type=args.noise_type, noise_rate=args.noise_rate) if args.dataset == 'cifar100': input_channel = 3 num_classes = 100 args.top_bn = False args.epoch_decay_start = 100 args.n_epoch = 200 train_dataset = CIFAR100(root='./data/', download=True, train=True, transform=transforms.ToTensor(), noise_type=args.noise_type, noise_rate=args.noise_rate) test_dataset = CIFAR100(root='./data/', download=True, train=False, transform=transforms.ToTensor(), noise_type=args.noise_type, noise_rate=args.noise_rate) if args.forget_rate is None: forget_rate = args.noise_rate else: forget_rate = args.forget_rate noise_or_not = train_dataset.noise_or_not # Data Loader (Input Pipeline) print('loading dataset...') train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=args.batch_size, num_workers=args.num_workers, drop_last=True, shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, num_workers=args.num_workers, drop_last=True, shuffle=False) use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") cnn1 = ResNet34().to(device) optimizer = torch.optim.SGD(cnn1.parameters(), lr=args.lr) acc = [] loss = [] loss_pure = [] loss_corrupt = [] out = [] for epoch in range(1, args.n_epoch + 1): l1 = train(args, cnn1, device, train_loader, optimizer, epoch, eps=args.eps, nums=num_classes) loss.append(l1) acc.append(test(args, cnn1, device, test_loader, num_classes)) print(l1) name = str(args.dataset) + " " + str(args.noise_type) + " " + str( args.noise_rate) + " " + str(args.eps) + " " + str(args.seed) np.save("el_" + name + " acc.npy", acc) np.save("el_" + name + " loss.npy", loss)
lr = 1e-2 batch_size = 32 epochs = 60 print_interval = 20 save_model_dir = os.path.join(root_dir, 'model/') pretrained_params = 'resnet34_params.pt' print('%s Start Preprocessing' % (datetime.now().ctime())) train_dataset = RSClsDataset(train_csv, train_image_dir, 'train') train_dataloader = data.DataLoader( train_dataset, batch_size=batch_size, shuffle=True, num_workers=2) valid_dataset = RSClsDataset(valid_csv, valid_image_dir, 'valid') valid_dataloader = data.DataLoader( valid_dataset, batch_size=batch_size, shuffle=False, num_workers=2) net = ResNet34().cuda(device) net.load_state_dict( torch.load(os.path.join(save_model_dir, pretrained_params)), strict=False) criterion = SoftLabelCrossEntropyLoss().cuda(device) optimizer = optim.SGD( net.parameters(), lr=lr, momentum=0.9, weight_decay=0.0005) scheduler = optim.lr_scheduler.MultiStepLR( optimizer, milestones=[10, 30, 50], gamma=0.1) print('%s Finish Preprocessing' % (datetime.now().ctime())) print('%s Start Training' % (datetime.now().ctime())) print( 'Dtype[%s] Base lr[%f] Epochs[%d] Dataset: train[%d] valid[%d] Iter: train[%d] valid[%d] Batch Size[%d]' % (str(torch.get_default_dtype()), optimizer.param_groups[0]['lr'], epochs,
def _init_net(self): model = ResNet34() model = model.to("cpu") return model
def main(args): save_folder = '%s_%s' % (args.dataset, args.affix) log_folder = os.path.join(args.log_root, save_folder) model_folder = os.path.join(args.model_root, save_folder) makedirs(log_folder) makedirs(model_folder) setattr(args, 'log_folder', log_folder) setattr(args, 'model_folder', model_folder) logger = create_logger(log_folder, args.todo, 'info') print_args(args, logger) #return # model = WideResNet(depth=34, num_classes=10, widen_factor=10, dropRate=0.0) print('create model') model = ResNet34() print('done...') # file_name = os.path.join(args.model_folder, 'checkpoint.pth') # torch.save(model.state_dict(), file_name) # load_model(model, file_name, args) print('create attack') attack = FastGradientSignUntargeted(model, args.epsilon, args.alpha, min_val=0, max_val=1, max_iters=args.k, _type=args.perturbation_type) print('done...') print('create trainer') trainer = Trainer(args, logger, attack) print('done...') if args.todo == 'train': print('start train') if torch.cuda.is_available(): model.cuda() transform_train = tv.transforms.Compose([ tv.transforms.ToTensor(), tv.transforms.Lambda(lambda x: F.pad(x.unsqueeze(0), (4, 4, 4, 4), mode='constant', value=0).squeeze()), tv.transforms.ToPILImage(), tv.transforms.RandomCrop(32), tv.transforms.RandomHorizontalFlip(), tv.transforms.ToTensor(), ]) print('get dataset') print('dataset_root: ', args.data_root) tr_dataset = tv.datasets.CIFAR10(args.data_root, train=True, transform=transform_train, download=True) tr_loader = DataLoader(tr_dataset, batch_size=args.batch_size, shuffle=True, num_workers=4) # evaluation during training te_dataset = tv.datasets.CIFAR10(args.data_root, train=False, transform=tv.transforms.ToTensor(), download=True) te_loader = DataLoader(te_dataset, batch_size=args.batch_size, shuffle=False, num_workers=4) trainer.train(model, tr_loader, te_loader, args.adv_train) elif args.todo == 'valid': load_model(model, args.load_checkpoint, args) if torch.cuda.is_available(): model.cuda() te_dataset = tv.datasets.CIFAR10(args.data_root, train=False, transform=tv.transforms.ToTensor(), download=True) te_loader = DataLoader(te_dataset, batch_size=args.batch_size, shuffle=False, num_workers=4) test_acc, adv_acc = trainer.test(model, te_loader, adv_test=False) print('Test accuracy is %.3f' % test_acc) else: raise NotImplementedError