Beispiel #1
0
# Commented out IPython magic to ensure Python compatibility.
# instantiate loaders and optimizer and start tensorboard
train_loader = torch.utils.data.DataLoader(train_dataset,
                                           batch_size=4,
                                           shuffle=True)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=25)
optimizer = torch.optim.Adam(model.parameters(), lr=1.e-3)
# %tensorboard --logdir runs

# we have moved all the boilerplate for the full training procedure to utils now
if not os.path.exists("checkpoints/best_checkpoint_SimpleCNN.tar"):
    n_epochs = 10
    utils.run_cifar_training(model,
                             optimizer,
                             train_loader,
                             val_loader,
                             device=device,
                             name='SimpleCNN',
                             n_epochs=n_epochs)

# evaluate the model on test data
test_dataset = utils.make_cifar_test_dataset(cifar_dir)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=25)

model = utils.load_checkpoint("best_checkpoint_SimpleCNN.tar", model,
                              optimizer)[0]

predictions, labels = utils.validate(model,
                                     test_loader,
                                     torch.nn.NLLLoss(),
                                     device,
    val_images, val_labels, transform=utils.get_default_cifar_transform())

train_loader = DataLoader(
    train_dataset,
    batch_size=4,
    shuffle=True,
)
val_loader = DataLoader(val_dataset, batch_size=25)
optimizer = Adam(model.parameters(), lr=1.e-3)
# %tensorboard --logdir runs

n_epochs = 5
utils.run_cifar_training(model,
                         optimizer,
                         train_loader,
                         val_loader,
                         device=device,
                         name='resnet18_augmented',
                         n_epochs=n_epochs)

# evaluate the model on test data
test_dataset = utils.make_cifar_test_dataset(cifar_dir)
test_loader = DataLoader(test_dataset, batch_size=25)
predictions, labels = utils.validate(model,
                                     test_loader,
                                     nn.NLLLoss(),
                                     device,
                                     step=0,
                                     tb_logger=None)

print("Test accuracy:")
# Commented out IPython magic to ensure Python compatibility.
# instantiate loaders and optimizer and start tensorboard
train_loader = torch.utils.data.DataLoader(train_dataset,
                                           batch_size=4,
                                           shuffle=True)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=25)
optimizer = torch.optim.Adam(model.parameters(), lr=1.e-3)
# %tensorboard --logdir runs

# we have moved all the boilerplate for the full training procedure to utils now
n_epochs = 10
if not os.path.exists("checkpoints/best_checkpoint_SimpleCNN_augmented.tar"):
    utils.run_cifar_training(model,
                             optimizer,
                             train_loader,
                             val_loader,
                             device=device,
                             name='SimpleCNN_augmented',
                             n_epochs=n_epochs)

# evaluate the model on test data
test_dataset = utils.make_cifar_test_dataset(cifar_dir)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=25)

model = utils.load_checkpoint("best_checkpoint_SimpleCNN_augmented.tar", model,
                              optimizer)[0]
predictions, labels = utils.validate(model,
                                     test_loader,
                                     torch.nn.NLLLoss(),
                                     device,
                                     step=0,