def main():
    torch.manual_seed(options['seed'])
    os.environ['CUDA_VISIBLE_DEVICES'] = options['gpu']
    use_gpu = torch.cuda.is_available()
    if options['use_cpu']:
        use_gpu = False
    options.update({'use_gpu': use_gpu})
    if use_gpu:
        print('Currently using GPU: {}'.format(options['gpu']))
        cudnn.benchmark = True
        torch.cuda.manual_seed_all(options['seed'])
    else:
        print('Currently using CPU')
    if ('cifar10' == options['dataset']):
        Data = CIFAR10D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'], _eval=options['eval'])
        OODData = CIFAR100D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'])
    else:
        Data = CIFAR100D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'], _eval=options['eval'])
        OODData = CIFAR10D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'])
    (trainloader, testloader, outloader) = (Data.train_loader, Data.test_loader, OODData.test_loader)
    num_classes = Data.num_classes
    print('Creating model: {}'.format(options['model']))
    if ('wide_resnet' in options['model']):
        print('wide_resnet')
        from model.wide_resnet import WideResNet
        net = WideResNet(40, num_classes, 2, 0.0)
    elif ('allconv' in options['model']):
        print('allconv')
        from model.allconv import AllConvNet
        net = AllConvNet(num_classes)
    elif ('densenet' in options['model']):
        print('densenet')
        from model.densenet import densenet
        net = densenet(num_classes=num_classes)
    elif ('resnext' in options['model']):
        print('resnext29')
        from model.resnext import resnext29
        net = resnext29(num_classes)
    else:
        print('resnet18')
        from model.resnet import ResNet18
        net = ResNet18(num_classes=num_classes)
    criterion = nn.CrossEntropyLoss().cuda()
    if use_gpu:
        net = nn.DataParallel(net, device_ids=[i for i in range(len(options['gpu'].split(',')))]).cuda()
        criterion = criterion.cuda()
    file_name = '{}_{}_{}'.format(options['model'], options['dataset'], options['aug'])
    if options['eval']:
        (net, criterion) = load_networks(net, options['outf'], file_name, criterion=criterion)
        outloaders = Data.out_loaders
        results = test(net, criterion, testloader, outloader, epoch=0, **options)
        acc = results['ACC']
        res = dict()
        res['ACC'] = dict()
        acc_res = []
        for key in Data.out_keys:
            results = test_robustness(net, criterion, outloaders[key], epoch=0, label=key, **options)
            print('{} (%): {:.3f}\t'.format(key, results['ACC']))
            res['ACC'][key] = results['ACC']
            acc_res.append(results['ACC'])
        print('Mean ACC:', np.mean(acc_res))
        print('Mean Error:', (100 - np.mean(acc_res)))
        return
    params_list = [{'params': net.parameters()}, {'params': criterion.parameters()}]
    optimizer = torch.optim.SGD(params_list, lr=options['lr'], momentum=0.9, nesterov=True, weight_decay=0.0005)
    scheduler = lr_scheduler.MultiStepLR(optimizer, gamma=0.2, milestones=[60, 120, 160, 190])
    start_time = time.time()
    best_acc = 0.0
    for epoch in range(options['max_epoch']):
        print('==> Epoch {}/{}'.format((epoch + 1), options['max_epoch']))
        train(net, criterion, optimizer, trainloader, epoch=epoch, **options)
        if (((options['eval_freq'] > 0) and (((epoch + 1) % options['eval_freq']) == 0)) or ((epoch + 1) == options['max_epoch']) or (epoch > 160)):
            print('==> Test')
            results = test(net, criterion, testloader, outloader, epoch=epoch, **options)
            if (best_acc < results['ACC']):
                best_acc = results['ACC']
                print('Best Acc (%): {:.3f}\t'.format(best_acc))
            save_networks(net, options['outf'], file_name, criterion=criterion)
        scheduler.step()
    elapsed = round((time.time() - start_time))
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print('Finished. Total elapsed time (h:m:s): {}'.format(elapsed))
def main():
    torch.manual_seed(options['seed'])
    os.environ['CUDA_VISIBLE_DEVICES'] = options['gpu']
    use_gpu = torch.cuda.is_available()
    if options['use_cpu']:
        use_gpu = False
    options.update({'use_gpu': use_gpu})
    if use_gpu:
        print('Currently using GPU: {}'.format(options['gpu']))
        cudnn.benchmark = True
        torch.cuda.manual_seed_all(options['seed'])
    else:
        print('Currently using CPU')
    if ('cifar10' == options['dataset']):
        Data = CIFAR10D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'], _eval=options['eval'])
        OODData = CIFAR100D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'])
    else:
        Data = CIFAR100D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'], _eval=options['eval'])
        OODData = CIFAR10D(dataroot=options['data'], batch_size=options['batch_size'], _transforms=options['aug'])
    (trainloader, testloader, outloader) = (Data.train_loader, Data.test_loader, OODData.test_loader)
    num_classes = Data.num_classes
    print('Creating model: {}'.format(options['model']))
    if ('wide_resnet' in options['model']):
        print('wide_resnet')
        from model.wide_resnet import WideResNet
        net = WideResNet(40, num_classes, 2, 0.0)
    elif ('allconv' in options['model']):
        print('allconv')
        from model.allconv import AllConvNet
        net = AllConvNet(num_classes)
    elif ('densenet' in options['model']):
        print('densenet')
        from model.densenet import densenet
        net = densenet(num_classes=num_classes)
    elif ('resnext' in options['model']):
        print('resnext29')
        from model.resnext import resnext29
        net = resnext29(num_classes)
    else:
        print('resnet18')
        from model.resnet import ResNet18
        net = ResNet18(num_classes=num_classes)
    criterion = nn.CrossEntropyLoss().cuda()
    if use_gpu:
        net = nn.DataParallel(net, device_ids=[i for i in range(len(options['gpu'].split(',')))]).cuda()
        criterion = criterion.cuda()
    file_name = '{}_{}_{}'.format(options['model'], options['dataset'], options['aug'])
    if options['eval']:
        (net, criterion) = load_networks(net, options['outf'], file_name, criterion=criterion)
        outloaders = Data.out_loaders
        results = test(net, criterion, testloader, outloader, epoch=0, **options)
        acc = results['ACC']
        res = dict()
        res['ACC'] = dict()
        acc_res = []
        for key in Data.out_keys:
            results = test_robustness(net, criterion, outloaders[key], epoch=0, label=key, **options)
            print('{} (%): {:.3f}\t'.format(key, results['ACC']))
            res['ACC'][key] = results['ACC']
            acc_res.append(results['ACC'])
        print('Mean ACC:', np.mean(acc_res))
        print('Mean Error:', (100 - np.mean(acc_res)))
        return
    params_list = [{'params': net.parameters()}, {'params': criterion.parameters()}]
    optimizer = torch.optim.SGD(params_list, lr=options['lr'], momentum=0.9, nesterov=True, weight_decay=0.0005)
    scheduler = lr_scheduler.MultiStepLR(optimizer, gamma=0.2, milestones=[60, 120, 160, 190])
    start_time = time.time()
    best_acc = 0.0
    for epoch in range(options['max_epoch']):
        print('==> Epoch {}/{}'.format((epoch + 1), options['max_epoch']))
        train(net, criterion, optimizer, trainloader, epoch=epoch, **options)
        if (((options['eval_freq'] > 0) and (((epoch + 1) % options['eval_freq']) == 0)) or ((epoch + 1) == options['max_epoch']) or (epoch > 160)):
            print('==> Test')
            results = test(net, criterion, testloader, outloader, epoch=epoch, **options)
            if (best_acc < results['ACC']):
                best_acc = results['ACC']
                print('Best Acc (%): {:.3f}\t'.format(best_acc))
            save_networks(net, options['outf'], file_name, criterion=criterion)
        scheduler.step()
    elapsed = round((time.time() - start_time))
    elapsed = str(datetime.timedelta(seconds=elapsed))
    print('Finished. Total elapsed time (h:m:s): {}'.format(elapsed))