def training(): config = Configuration() net = Network() optimizer = optim.Adam(net.parameters(), lr=0.01) train_loader = get_train_loader() test_loader = get_test_loader() for epoch in range(config.nn_training_epochs): train(net, optimizer, train_loader, epoch) test(net, test_loader) net.save_model()
def main(): config = Configuration() image_id = 1362 bnn = BNNWrapper() bnn.load_model() loss_fn = pyro.infer.Trace_ELBO( num_particles=config.bnn_adversary_samples ).differentiable_loss nn = Network() nn.load_model() test_loader = get_test_loader(1, shuffle=False) x, y = test_loader.dataset[image_id] y = torch.tensor([y]) bnn_d, bnn_imgs, bnn_pertubation_imgs = bnn_adversary.run_attack( bnn, loss_fn, x, y, config.epsilons, image_id ) nn_d, nn_imgs, nn_pertubation_imgs = nn_adversary.run_attack( nn, x, y, config.epsilons, 3 ) ids = [2, 5] utils.img_show( x, f"Image, BNN Prediction: {bnn_d.iloc[0]['y_']}, NN Prediction: {nn_d.iloc[0]['y_']}", ) for id in ids: utils.img_two_show( bnn_pertubation_imgs[id].cpu(), f"BNN Noise (epsilon: {bnn_d.iloc[id]['epsilon']})", nn_pertubation_imgs[id].cpu(), f"NN Noise (epsilon: {bnn_d.iloc[id]['epsilon']})", ) utils.img_two_show( bnn_imgs[id].cpu(), f"Noise added to image, BNN Prediction: {bnn_d.iloc[id]['y_']}", nn_imgs[id].cpu(), f"Noise added to image, NN Prediction: {nn_d.iloc[id]['y_']}", )
def main(): config = Configuration() image_id = 1362 nn = Network() nn.load_model() test_loader = get_test_loader(1, shuffle=False) x, y = test_loader.dataset[image_id] y = torch.tensor([y]) nn_d, nn_imgs, nn_pertubation_imgs = nn_adversary.run_attack( nn, x, y, config.epsilons, 3) id = 2 utils.img_show(x, f"Image, NN Prediction: {nn_d.iloc[0]['y_']}") utils.img_show(nn_pertubation_imgs[id].cpu(), f"Noise (epsilon: {nn_d.iloc[id]['epsilon']})") utils.img_show(nn_imgs[id].cpu(), f"Noise added to image, Prediction: {nn_d.iloc[id]['y_']}")
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/") result_path.mkdir(exist_ok=True, parents=False) result_df.to_csv(result_path.joinpath(f"{config.id:02}_nn_result.csv"))
def plot_images(config: Configuration): bnn = BNNWrapper() bnn.load_model() loss_fn = pyro.infer.Trace_ELBO( num_particles=config.bnn_adversary_samples ).differentiable_loss nn = Network() nn.load_model() # sample images from the MNIST test data set imgs = [3, 4, 1362, 6930] fig, axes = plt.subplots(12, 2 * len(imgs), figsize=(36, 26)) for j, img in enumerate(imgs): test_loader = get_test_loader(1, shuffle=False) x, y = test_loader.dataset[img] y = torch.tensor([y]) bnn_d, bnn_imgs, _ = bnn_adversary.run_attack( bnn, loss_fn, x, y, config.epsilons, img ) # font sizes title = 22 body = 20 index = 2 * j axes[0][index].set_title("Bayesian Neural Network", fontsize=title) for i in range(12): axes[i][index].imshow( bnn_imgs[i][0].cpu().detach(), cmap="gray", vmin=0, vmax=1 ) axes[i][index].set_xlabel( f"Label: {bnn_d['y'][i]}, Prediction: {bnn_d['y_'][i]}", fontsize=body ) axes[i][index].set_ylabel(f"Eps: {bnn_d['epsilon'][i]}", fontsize=body) axes[i][index].set_yticklabels([]) axes[i][index].set_xticklabels([]) nn_d, nn_imgs, _ = nn_adversary.run_attack(nn, x, y, config.epsilons, 3) index = 2 * j + 1 axes[0][index].set_title("Deep Neural Network", fontsize=title) for i in range(12): axes[i][index].imshow( nn_imgs[i][0].cpu().detach(), cmap="gray", vmin=0, vmax=1 ) axes[i][index].set_xlabel( f"Label: {nn_d['y'][i]}, Prediction: {nn_d['y_'][i]}", fontsize=body ) axes[i][index].set_ylabel(f"Eps: {bnn_d['epsilon'][i]}", fontsize=body) axes[i][index].set_yticklabels([]) axes[i][index].set_xticklabels([]) fig.tight_layout() fig.subplots_adjust(hspace=0.5) plot_path = Path(f"data/{config.id:02}_example_imgs.svg") plt.savefig(plot_path) plt.show() plt.close()