def test(net: Network, test_loader: torch.utils.data.DataLoader): net.eval() test_loss = 0 correct = 0 with torch.no_grad(): for x, y in test_loader: x = x.to(net.device) y = y.to(net.device) pred = net(x.view(-1, 28 * 28)) test_loss += F.nll_loss(pred, y, size_average=False).item() correct += torch.eq(pred.max(1).indices, y).sum().item() test_loss /= len(test_loader.dataset) print( f"\nTest set, Average loss: {test_loss}, Accuracy: {float(correct) / float(len(test_loader.dataset))}\n" )
device = "cuda" if torch.cuda.is_available() else "cpu" # Configure device print('GPU USED?',torch.cuda.is_available()) ################# MODELS##################### models =[] optimizers =[] criterions =[] j = 0 for i in range(len_class): for event in events: model = Network(output_dim=numb_class[i]) model = model.to(device) model.eval() models.append(model) weights_list[j] = weights_list[j].to(device) criterions.append(nn.CrossEntropyLoss(weight=weights_list[j])) optimizers.append(optim.Adam(model.parameters(), lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)) del model j +=1 #### MODE-SPECIFIC CLASSIFIER SCHEME # 1 : LW, 2:RA, 3:RD, 4:SA, 5:SD, 6:Stand # MODEL1: LW(1)-> LW(1), RA(2), RD(3), SA(4), SD(5), Stand(6) # MODEL2: RA(2)-> LW(1), RA(2) # MODEL3: RD(3)-> LW(1), RD(3) # MODEL4: SA(4)-> LW(1), SA(4) # MODEL5: SD(5)-> LW(1), SD(5)
tmp_dict["id"].append(batch_id) tmp_dict["epsilon"].append(epsilon) tmp_dict["y"].append(y.item()) tmp_dict["y_"].append(y_) return pandas.DataFrame.from_dict(tmp_dict), pertubed_images, pertubation if __name__ == "__main__": config = Configuration() net = Network() net.load_model() net.eval() test_loader = get_test_loader(batch_size=1, shuffle=False) result = [] for batch_id, (x, y) in enumerate(test_loader): df, _ = run_attack(net, x, y, config.epsilons, batch_id) result.append(df) if batch_id % 100 == 0: print(f"Step {batch_id}/{len(test_loader.dataset)}") result_df = pandas.concat(result) # type: pandas.DataFrame result_df.reset_index(inplace=True, drop=True) result_path = Path("data/")