Example #1
0
def go_test():
    print("=" * 30)
    model = densenet.densenet201(pretrained=True)
    # Replace classification layer
    model.classifier = torch.nn.Linear(model.classifier.in_features,
                                       num_classes)
    model.to(device)
    # Load best performing model based on validation score
    model.load_state_dict(
        torch.load(f"snapshots/densenet201_best_{MODEL_NAME}.pth"))

    print("[-] Performing validation...")
    train_loader, val_loader, val_loader2 = get_train_val_split(batch_size)

    with torch.no_grad():
        val_accuracy = validate(model, val_loader, -1)
        print("[*] Validation accuracy: {}".format(val_accuracy))
        val_accuracy2 = validate(model, val_loader2, -1)
        print("[*] Validation2 accuracy: {}\n".format(val_accuracy2))

    print("[-] Performing testing...")

    test_loader = get_test(batch_size)
    with torch.no_grad():
        test(model, test_loader)
def main():
    ##################
    # Initialization #
    ##################

    model = ensemble_models.ensemble_ver1()
    model.to(device)
    print(model)
    trained_models = [
        base_models.base_classifier(parameter=PARAMETERS[i]) for i in range(5)
    ]
    for i, mod in enumerate(trained_models):
        mod.classifier = torch.nn.Linear(mod.classifier.in_features,
                                         num_classes)
        mod.to(device)
        mod.load_state_dict(torch.load(f"snapshots/{MODEL_NAMES[i]}"))

    criterion = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=base_lr)

    ############
    # Training #
    ############
    train_loader, val_loader, val_loader2 = get_train_val_split(batch_size)

    # Use model that performs best on validation for testing
    best_val_accuracy = 0

    for epoch in range(num_epochs):
        # Train
        print("=" * 30)
        train(model, trained_models, train_loader, optimizer, criterion, epoch)

        # Validate
        with torch.no_grad():
            val_accuracy = validate(model, trained_models, val_loader, epoch)
            print("[*] Validation accuracy: {}".format(val_accuracy))
            val_accuracy2 = validate(model, trained_models, val_loader2, epoch)
            print("[*] Validation2 accuracy: {}\n".format(val_accuracy2))

        # New best performing model
        if val_accuracy2 > best_val_accuracy:
            best_val_accuracy = val_accuracy2
            print("[*] New best accuracy!\n")
            torch.save(model.state_dict(),
                       f"snapshots/base_experiment_hidden.pth")

    ###########
    # Testing #
    ###########
    print("=" * 30)
    print("[-] Performing testing...")

    # Load best performing model based on validation score
    model.load_state_dict(torch.load(f"snapshots/base_experiment_hidden.pth"))

    test_loader = get_test(batch_size)
    with torch.no_grad():
        test(model, trained_models, test_loader)
def main():
    ##################
    # Initialization #
    ##################

    # Load a model pretrained on ImageNet
    ensemble_model = ensemble.ensemble_ver3()
    ensemble_model.to(device)

    trained_models = [densenet.densenet201(pretrained=True) for i in range(5)]
    # Replace classification layer
    for i, model in enumerate(trained_models):
        model.classifier = torch.nn.Linear(model.classifier.in_features, num_classes)
        model.to(device)
        # Load best performing model based on validation score
        model.load_state_dict(torch.load(f"snapshots/densenet201_best_{MODEL_NAMES[i]}.pth"))

    criterion = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=base_lr)

    ############
    # Training #
    ############
    train_loader, val_loader, val_loader2 = get_train_val_split(batch_size)

    # Use model that performs best on validation for testing
    best_val_accuracy = 0

    for epoch in range(num_epochs):
        # Train
        print("="*30)
        train(ensemble_model, trained_models, train_loader, optimizer, criterion, epoch)

        # Validate
        val_accuracy = validate(ensemble_model, trained_models, val_loader, epoch)
        print("[*] Validation accuracy: {}".format(val_accuracy))
        val_accuracy2 = validate(ensemble_model, trained_models, val_loader2, epoch)
        print("[*] Validation2 accuracy: {}\n".format(val_accuracy2))

        # New best performing model
        if val_accuracy2 > best_val_accuracy:
            best_val_accuracy = val_accuracy2
            print("[*] New best accuracy!\n")
            torch.save(ensemble_model.state_dict(), f"snapshots/densenet201_experiment_usual.pth")

    ###########
    # Testing #
    ###########
    print("="*30)
    print("[-] Performing testing...")

    # Load best performing model based on validation score
    ensemble_model.load_state_dict(torch.load(f"snapshots/densenet201_experiment_usual.pth"))

    test_loader = get_test(batch_size)
    test(ensemble_model, trained_models, test_loader)
Example #4
0
def main():
    # Initialization

    model = base_models.base_classifier(parameter=PARAMETER)
    model.to(device)
    print(model)

    criterion = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(model.parameters(), lr=base_lr)

    # Training #
    train_loader, val_loader, ensemble_train_loader, ensemble_val_loader = get_train_val_split(
        batch_size, model_num=int(sys.argv[1]))

    # Use model that performs best on validation for testing
    best_val_accuracy = 0

    for epoch in range(num_epochs):
        print("=" * 30)
        train(model, train_loader, optimizer, criterion, epoch)

        # Validate
        with torch.no_grad():
            val_accuracy = validate(model, val_loader, epoch)
            print("[*] Validation accuracy: {}".format(val_accuracy))
            val_accuracy2 = validate(model, val_loader2, epoch)
            print("[*] Validation2 accuracy: {}\n".format(val_accuracy2))

        # New best performing model
        if val_accuracy > best_val_accuracy:
            best_val_accuracy = val_accuracy
            print("[*] New best accuracy!\n")
            torch.save(model.state_dict(), f"snapshots/{MODEL_NAME}")

    ###########
    # Testing #
    ###########
    print("=" * 30)
    print("[-] Performing testing...")

    # Load best performing model based on validation score
    model.load_state_dict(torch.load(f"snapshots/{MODEL_NAME}"))

    test_loader = get_test(batch_size)
    with torch.no_grad():
        test(model, test_loader)
Example #5
0
    pathlib.Path('./snapshots').mkdir(parents=True, exist_ok=True)

    # Load a model pretrained on ImageNet
    model = densenet.densenet121(pretrained=True)
    # Replace classification layer
    model.classifier = torch.nn.Linear(model.classifier.in_features,
                                       num_classes)
    model.to(device)

    criterion = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=base_lr, momentum=0.9)

    ############
    # Training #
    ############
    train_loader, val_loader = get_train_val_split(batch_size)

    # Use model that performs best on validation for testing
    best_val_accuracy = 0

    for epoch in range(num_epochs):
        # Train
        train(model, train_loader)
        print("=" * 30)

        # Validate
        val_accuracy = validate(model, val_loader)
        print("Validation accuracy: {}".format(val_accuracy))

        # New best performing model
        if val_accuracy > best_val_accuracy: