def test(epoch, patch, patch_shape): netClassifier.eval() cor = 0 total = 0 smoothed_classifier = Smooth(netClassifier, 10, opt.sigma) for batch_idx, (data, labels) in enumerate(test_loader): if labels.item() == target: continue if torch.cuda.is_available: data = data.cuda() labels = labels.cuda() data, labels = Variable(data), Variable(labels) data = data[:, [2, 1, 0], :, :] # rgb to bgr prediction = netClassifier(data) total += 1 # transform path data_shape = data.data.cpu().numpy().shape if patch_type == 'circle': patch, mask, patch_shape = circle_transform( patch, data_shape, patch_shape, image_size) elif patch_type == 'square': patch, mask = square_transform(patch, data_shape, patch_shape, image_size) patch, mask = torch.FloatTensor(patch), torch.FloatTensor(mask) if torch.cuda.is_available: patch, mask = patch.cuda(), mask.cuda() patch, mask = Variable(patch), Variable(mask) adv_x = torch.mul((1 - mask), data) + torch.mul(mask, patch) adv_x = torch.clamp(adv_x, min_out, max_out) #adv_label = netClassifier(adv_x).data.max(1)[1][0] ori_label = labels.data[0] if epoch == opt.epochs: prediction = smoothed_classifier.predict(adv_x, opt.N, opt.alpha, opt.batch) cor += int(prediction == int(labels)) # log the prediction and whether it was correct #print("{}\t{}\t{}\t{}\t{}".format(labels, prediction, cor, time_elapsed), file=f, flush=True) masked_patch = torch.mul(mask, patch) patch = masked_patch.data.cpu().numpy() new_patch = np.zeros(patch_shape) for i in range(new_patch.shape[0]): for j in range(new_patch.shape[1]): new_patch[i][j] = submatrix(patch[i][j]) patch = new_patch if epoch == opt.epochs: print("final accuracy is ", cor / total) else: print("continue to run")
def test(epoch, patch, patch_shape): netClassifier.eval() cor = 0 total = 0 smoothed_classifier = Smooth(netClassifier, 16, opt.sigma) for batch_idx, (data, labels) in enumerate(test_loader): if labels.item() == target: continue if torch.cuda.is_available: data = data.cuda() labels = labels.cuda() data, labels = Variable(data), Variable(labels) prediction = netClassifier(data) # only computer adversarial examples on examples that are originally classified correctly # if prediction.data.max(1)[1][0] != labels.data[0]: # continue total += 1 # transform path data_shape = data.data.cpu().numpy().shape if patch_type == 'circle': patch, mask, patch_shape = circle_transform( patch, data_shape, patch_shape, image_size) elif patch_type == 'square': patch, mask = square_transform(patch, data_shape, patch_shape, image_size) patch, mask = torch.FloatTensor(patch), torch.FloatTensor(mask) if torch.cuda.is_available: patch, mask = patch.cuda(), mask.cuda() patch, mask = Variable(patch), Variable(mask) adv_x = torch.mul((1 - mask), data) + torch.mul(mask, patch) adv_x = torch.clamp(adv_x, min_out, max_out) ori_label = labels.data[0] if epoch == opt.epochs: prediction = smoothed_classifier.predict(adv_x, opt.N, opt.alpha, opt.batch) cor += int(prediction == int(labels)) #print(total) if total % 100 == 0: print(cor / total * 100) masked_patch = torch.mul(mask, patch) patch = masked_patch.data.cpu().numpy() new_patch = np.zeros(patch_shape) for i in range(new_patch.shape[0]): for j in range(new_patch.shape[1]): new_patch[i][j] = submatrix(patch[i][j]) patch = new_patch print("The final accuracy is ", cor / total * 100)
# x = x + delta # make the prediction count = 0 correct = 0 for m in range(5): change = pgd_glass(base_classifier, x, labels, glass, epsilon=1, alpha=0.07843, num_iter=i, randomize=True) x = x + change prediction = smoothed_classifier.predict( x, args.N, args.alpha, args.batch) #print("label is ", label, "prediction is ", prediction) after_time = time() correct += int(prediction == int(label)) time_elapsed = str( datetime.timedelta(seconds=(after_time - before_time))) if correct == 5: cor = cor + 1 # log the prediction and whether it was correct print("{}\t{}\t{}\t{}\t{}".format(i, label, prediction, correct, time_elapsed), file=f, flush=True) # if correct==1 : # cor +=1