img_index = args.index # the target for deep leakage gt_data = tp(dst[img_index][0]).to(device) # Data range is from 0 to 1. gt_data = gt_data.view(1, *gt_data.size()) print(gt_data.shape) gt_label = torch.Tensor([dst[img_index][1]]).long().to(device) print("Ground Truth Label is %d" % gt_label.item()) gt_label = gt_label.view(1, ) gt_onehot_label = label_to_onehot(gt_label, num_classes=10) plt.imshow(tt(gt_data[0].cpu())) from models.vision import LeNet, weights_init, ResNet18, ResNet34, AlexNet if args.arch == 'LeNet5': net = LeNet(nchannel, nclass, args.act).to(device) net.apply(weights_init) elif args.arch == 'ResNet18': net = ResNet18(nclass, nchannel, args.act).to(device) net.apply(weights_init) elif args.arch == 'ResNet34': net = ResNet34(nclass, nchannel, args.act).to(device) net.apply(weights_init) elif args.arch == 'AlexNet': net = AlexNet(nclass=nclass, in_channels=nchannel, act=args.act).to(device) else: raise NotImplementedError( "Only supports LeNet5, AlexNet, ResNet18 and ResNet34.") torch.manual_seed(1234) criterion = cross_entropy_for_onehot
gt_data = tp(dst[img_index][0]).to(device) if len(args.image) > 1: gt_data = Image.open(args.image) gt_data = tp(gt_data).to(device) gt_data = gt_data.view(1, *gt_data.size()) gt_label = torch.Tensor([dst[img_index][1]]).long().to(device) gt_label = gt_label.view(1, ) gt_onehot_label = label_to_onehot(gt_label) plt.imshow(tt(gt_data[0].cpu())) from models.vision import LeNet, weights_init net = LeNet().to(device) net.apply(weights_init) criterion = cross_entropy_for_onehot # compute original gradient pred = net(gt_data) y = criterion(pred, gt_onehot_label) dy_dx = torch.autograd.grad(y, net.parameters()) original_dy_dx = list((_.detach().clone() for _ in dy_dx)) # generate dummy data and label dummy_data = torch.randn(gt_data.size()).to(device).requires_grad_(True) dummy_label = torch.randn(gt_onehot_label.size()).to(device).requires_grad_(True)
# dst = datasets.CIFAR100("~/.torch", download=True) dst = datasets.CIFAR10("~/.torch", download=True) imageDataBatchLoad = torch.utils.data.DataLoader(dst, batch_size=batch, shuffle=True, num_workers=2) tp = transforms.ToTensor() tt = transforms.ToPILImage() # img_index = args.index # gt_data = tp(dst[img_index][0]).to(device) if len(args.image) > 1: gt_data1 = Image.open(args.image) gt_data1 = tp(gt_data1).to(device) # from models.vision import LeNet, weights_init net = LeNet().to(device) ## Accuray computation!! def AreDummyAndOriginalLabelNoMatch(gt_oneshot_label, dummy_onehot_label_copy, decimalRound=3): originalLabelNo = int(torch.argmax(gt_oneshot_label[0])) dummyLabelCopy = int(torch.argmax(dummy_onehot_label_copy[0])) if(originalLabelNo == dummyLabelCopy): print("Dummy has successfully been restored! The label no. is " + str(originalLabelNo)) return True else: print("Restored from Image from Dummy is incorrect! The original label no. is " + str(originalLabelNo) +". The dummy label no. is " + str(dummyLabelCopy)) return False # Gausian Noise adder def addGausianNoise(net, standardDeviation=[1.0]):