Exemple #1
0
def main(config):
    # Create test dataloader
    test_ds_cfg = config['test_data_loader']['args']
    test_dataset = MNISTDataset(test_ds_cfg['root'],
                                test_ds_cfg['csv_file'],
                                False,
                                transforms=transforms)
    test_loader = DataLoader(test_dataset,
                             test_ds_cfg['batch_size'],
                             test_ds_cfg['shuffle'],
                             num_workers=test_ds_cfg['num_workers'])

    # Create classification model
    model = CNNModel()

    # Create criterion (loss function)
    criterion = getattr(loss_md, config['loss'])

    # Create metrics for evaluation
    metrics = [getattr(loss_md, x) for x in config['metrics']]

    # Create optimizer
    optimizer = getattr(otpm, config['optimizer']['name'])(
        model.parameters(), **config['optimizer']['args'])

    # Create learning rate scheduler
    lr_scheduler = getattr(otpm.lr_scheduler, config['lr_scheduler']['name'])\
                    (optimizer, **config['lr_scheduler']['args'])

    # Create train procedure: classification trainer
    csf_trainer = ClassificationTrainer(config, model, criterion, metrics,
                                        optimizer, lr_scheduler)

    csf_trainer.test(test_loader)

####################### Dataloading ################################################
train_dataset = Fashion_MNIST_Data("./data/train.csv")
trainloader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)
test_dataset = Fashion_MNIST_Data("./data/test.csv")
testloader = DataLoader(test_dataset, batch_size=batch_size,shuffle=False, num_workers=4)
classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot')
####################################################################################


####################### Network ####################################################
net = CNNModel()
net.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(net.parameters(),lr=learning_rate)
####################################################################################


####################### Training ###################################################
for epoch in range(num_epochs):
    running_loss = 0.0
    for i, (images, labels) in enumerate(trainloader):
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad()
        outputs = net(images)
        loss = criterion(outputs, torch.max(labels,1)[1])
        loss.backward()
        optimizer.step()

        running_loss += loss.item()
Exemple #3
0
                loss_name = loss.__class__.__name__
                print(f"Loss: {loss_name}\n")
                for noise_value in noise_values:
                    # RUN Experiments

                    name = f'CNN_{loss_name}_{tp_noise}_{noise_value}'

                    print(f"Training {name} with noise of type {tp_noise} and probability {noise_value}...")

                    # data preparation
                    dataset = FashionMnistHandler(data_dir, False)
                    dataset.load()
                    train_loader, val_loader, test_loader = dataset.get_noisy_loaders(p_noise=noise_value,
                                                                                      type_noise=tp_noise,
                                                                                      val_size=1 / 6,
                                                                                      train_batch_size=batch_size,
                                                                                      val_batch_size=128,
                                                                                      test_batch_size=128)

                    # model, optimizer, summary
                    model = CNNModel()
                    optimizer = torch.optim.Adam(model.parameters(), lr=lr)
                    summ = Summary(name, type_noise=tp_noise, noise_rate=noise_value)

                    solver = Solver(name, PROJECT_DIR, batch_model_dir, batch_summaries_dir, model,
                                    optimizer, loss, summ, train_loader, val_loader, test_loader)
                    solver.pretrain()
                    solver.train(loss)

                    print(f"Completed training...")