示例#1
0
def main(num_epochs=200,
         learning_rate=0.005,
         momentum=0.5,
         log_interval=500,
         *args,
         **kwargs):

    train_loader, test_loader = loaders.loader(batch_size_train=100,
                                               batch_size_test=1000)

    # Train the model
    total_step = len(train_loader)
    curr_lr1 = learning_rate

    model1 = VGG().to(device)

    # Loss and optimizer
    criterion = nn.CrossEntropyLoss()
    optimizer1 = torch.optim.Adam(model1.parameters(), lr=learning_rate)

    # Train the model
    total_step = len(train_loader)

    best_accuracy1 = 0

    for epoch in range(num_epochs):
        for i, (images, labels) in enumerate(train_loader):
            images = images.to(device)
            labels = labels.to(device)

            # Forward
            outputs = model1(images)
            loss1 = criterion(outputs, labels)

            # Backward and optimize
            optimizer1.zero_grad()
            loss1.backward()
            optimizer1.step()

            if i == 499:
                print(
                    "Ordinary Epoch [{}/{}], Step [{}/{}] Loss: {:.4f}".format(
                        epoch + 1, num_epochs, i + 1, total_step,
                        loss1.item()))

        # Test the model
        model1.eval()

        with torch.no_grad():
            correct1 = 0
            total1 = 0

            for images, labels in test_loader:
                images = images.to(device)
                labels = labels.to(device)

                outputs = model1(images)
                _, predicted = torch.max(outputs.data, 1)
                total1 += labels.size(0)
                correct1 += (predicted == labels).sum().item()

            if best_accuracy1 >= correct1 / total1:
                curr_lr1 = learning_rate * np.asscalar(
                    pow(np.random.rand(1), 3))
                update_lr(optimizer1, curr_lr1)
                print('Test Accuracy of NN: {} % Best: {} %'.format(
                    100 * correct1 / total1, 100 * best_accuracy1))
            else:
                best_accuracy1 = correct1 / total1
                net_opt1 = model1
                print('Test Accuracy of NN: {} % (improvement)'.format(
                    100 * correct1 / total1))

            model1.train()
示例#2
0
    models = [ "VGG3D", "ResNet3D", "ResNet2D", "VGG"]
    model_id = 0    # Change this to correspond to the model in the list
    if model_id == 0:
        model = VGG3D()
    elif model_id == 1:
        model = resnet34()
    elif model_id == 2:
        model = ResNet2D(3)
    elif model_id == 3:
        model = VGG(3)

    train_size  = 15
    test_size   = 15

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.train()
    model = model.to(device)

    model = torch.load("model_m.pt")
    model.eval()

    train_loader = get_data_loader(train = True, batch_size = train_size,
                                split = 'train', model = models[model_id])
    test_loader = get_data_loader(train = False, batch_size = test_size,
                                split = 'test', model = models[model_id])

    _ , train_acc = test(model, train_loader, device)
    print("Final Train Accuracy: ", train_acc)

    _ , test_acc = test(model, test_loader, device)
    print("Final Accuracy: ", test_acc)