def test(nnName, dataName, CUDA_DEVICE, epsilon, temperature): net1 = torch.load("../models/{}.pth".format(nnName)) optimizer1 = optim.SGD(net1.parameters(), lr = 0, momentum = 0) net1.cuda(CUDA_DEVICE) if dataName != "Uniform" and dataName != "Gaussian": testsetout = torchvision.datasets.ImageFolder("../data/{}".format(dataName), transform=transform) testloaderOut = torch.utils.data.DataLoader(testsetout, batch_size=1, shuffle=False, num_workers=2) if nnName == "densenet10" or nnName == "wideresnet10": testset = torchvision.datasets.CIFAR10(root='../data', train=False, download=True, transform=transform) testloaderIn = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=2) if nnName == "densenet100" or nnName == "wideresnet100": testset = torchvision.datasets.CIFAR100(root='../data', train=False, download=True, transform=transform) testloaderIn = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=2) if dataName == "Gaussian": d.testGaussian(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderIn, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName) elif dataName == "Uniform": d.testUni(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderIn, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName) else: d.testData(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderOut, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName)
def test(nnName, dataName, CUDA_DEVICE, epsilon, temperature): use_cuda = torch.cuda.is_available() device = torch.device("cuda" if use_cuda else "cpu") net1 = vgg1.VGG('VGG16').to(device) # Test set: Average loss: 0.0015, Accuracy: 9337 / 10000(93 %) if use_cuda: net1 = torch.nn.DataParallel(net1) cudnn.benchmark = True checkpoint = torch.load("../../model/{}.pth".format(nnName)) net1.load_state_dict(checkpoint['net']) net1.eval() if dataName != "Uniform" and dataName != "Gaussian": testsetout = torchvision.datasets.ImageFolder( "../../data/{}".format(dataName), transform=transform) testloaderOut = torch.utils.data.DataLoader(testsetout, batch_size=1, shuffle=False, num_workers=2) # if nnName == "densenet10" or nnName == "wideresnet10": testset = torchvision.datasets.CIFAR10(root='../../data', train=False, download=True, transform=transform) testloaderIn = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=2) # if nnName == "densenet100" or nnName == "wideresnet100": # testset = torchvision.datasets.CIFAR100(root='../../data', train=False, download=True, transform=transform) # testloaderIn = torch.utils.data.DataLoader(testset, batch_size=1, # shuffle=False, num_workers=2) if dataName == "Gaussian": d.testGaussian(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderIn, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName) elif dataName == "Uniform": d.testUni(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderIn, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName) else: d.testData(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderOut, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName)
def test(nnName, dataName, CUDA_DEVICE, epsilon, temperature): net1 = torch.load("../models/{}.pth".format(nnName)) optimizer1 = optim.SGD(net1.parameters(), lr = 0, momentum = 0) net1.cuda(CUDA_DEVICE) if dataName != "Uniform" and dataName != "Gaussian": if dataName == "SVHN": testsetout = svhn.SVHN("../data/SVHN", split='test', transform=transform, download=True) elif dataName in ["HFlip","VFlip"]: testsetout = torchvision.datasets.CIFAR10('../data', train=False, download=True, transform=Flip[dataName]) elif dataName == "CelebA": testsetout = torchvision.datasets.ImageFolder( "../data/{}".format(dataName), transform=transforms.Compose([transforms.CenterCrop(178), Resize(32), transform])) else: testsetout = torchvision.datasets.ImageFolder("../data/{}".format(dataName), transform=transform) testloaderOut = torch.utils.data.DataLoader(testsetout, batch_size=1, shuffle=False, num_workers=2) if nnName == "densenet10" or nnName == "wideresnet10": testset = torchvision.datasets.CIFAR10(root='../data', train=False, download=True, transform=transform) testloaderIn = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=2) if nnName == "densenet100" or nnName == "wideresnet100": testset = torchvision.datasets.CIFAR100(root='../data', train=False, download=True, transform=transform) testloaderIn = torch.utils.data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=2) if dataName == "Gaussian": d.testGaussian(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderIn, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName) elif dataName == "Uniform": d.testUni(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderIn, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName) else: d.testData(net1, criterion, CUDA_DEVICE, testloaderIn, testloaderOut, nnName, dataName, epsilon, temperature) m.metric(nnName, dataName)