( model, device, test_loader, epsilon )
| 263 | # |
| 264 | |
| 265 | def test( model, device, test_loader, epsilon ): |
| 266 | |
| 267 | # Accuracy counter |
| 268 | correct = 0 |
| 269 | adv_examples = [] |
| 270 | |
| 271 | # Loop over all examples in test set |
| 272 | for data, target in test_loader: |
| 273 | |
| 274 | # Send the data and label to the device |
| 275 | data, target = data.to(device), target.to(device) |
| 276 | |
| 277 | # Set requires_grad attribute of tensor. Important for Attack |
| 278 | data.requires_grad = True |
| 279 | |
| 280 | # Forward pass the data through the model |
| 281 | output = model(data) |
| 282 | init_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability |
| 283 | |
| 284 | # If the initial prediction is wrong, don't bother attacking, just move on |
| 285 | if init_pred.item() != target.item(): |
| 286 | continue |
| 287 | |
| 288 | # Calculate the loss |
| 289 | loss = F.nll_loss(output, target) |
| 290 | |
| 291 | # Zero all existing gradients |
| 292 | model.zero_grad() |
| 293 | |
| 294 | # Calculate gradients of model in backward pass |
| 295 | loss.backward() |
| 296 | |
| 297 | # Collect ``datagrad`` |
| 298 | data_grad = data.grad.data |
| 299 | |
| 300 | # Restore the data to its original scale |
| 301 | data_denorm = denorm(data) |
| 302 | |
| 303 | # Call FGSM Attack |
| 304 | perturbed_data = fgsm_attack(data_denorm, epsilon, data_grad) |
| 305 | |
| 306 | # Reapply normalization |
| 307 | perturbed_data_normalized = transforms.Normalize((0.1307,), (0.3081,))(perturbed_data) |
| 308 | |
| 309 | # Re-classify the perturbed image |
| 310 | output = model(perturbed_data_normalized) |
| 311 | |
| 312 | # Check for success |
| 313 | final_pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability |
| 314 | if final_pred.item() == target.item(): |
| 315 | correct += 1 |
| 316 | # Special case for saving 0 epsilon examples |
| 317 | if epsilon == 0 and len(adv_examples) < 5: |
| 318 | adv_ex = perturbed_data.squeeze().detach().cpu().numpy() |
| 319 | adv_examples.append( (init_pred.item(), final_pred.item(), adv_ex) ) |
| 320 | else: |
| 321 | # Save some adv examples for visualization later |
| 322 | if len(adv_examples) < 5: |
no test coverage detected