def main():
    start_time = time()

    in_args = get_input_args()

    # Check for GPU
    use_gpu = torch.cuda.is_available() and in_args.gpu

    if in_args.verbose:
        print("Predicting on {} using {}".format("GPU" if use_gpu else "CPU",
                                                 in_args.checkpoint))

    # Loads a pretrained model
    model = model_helper.load_checkpoint(in_args.checkpoint, in_args.verbose)

    # Move tensors to GPU if available
    if use_gpu:
        model.cuda()

    # Load category mapping dictionary
    use_mapping_file = False

    if in_args.category_names:
        with open(in_args.category_names, 'r') as f:
            cat_to_name = json.load(f)
            use_mapping_file = True

    # Get prediction
    number_of_results = in_args.top_k if in_args.top_k else 1

    probs, classes = model_helper.predict(in_args.input, model, use_gpu,
                                          number_of_results)

    # Print results
    if number_of_results > 1:
        print("\nTop {} Classes predicted for '{}':".format(
            len(classes), in_args.input))

        if use_mapping_file:
            print("\n{:<30} {}".format("Flower", "Probability"))
            print("{:<30} {}".format("------", "-----------"))
        else:
            print("\n{:<10} {}".format("Class", "Probability"))
            print("{:<10} {}".format("------", "-----------"))

        for i in range(0, len(classes)):
            if use_mapping_file:
                print("{:<30} {:.2f}".format(
                    get_title(classes[i], cat_to_name), probs[i]))
            else:
                print("{:<10} {:.2f}".format(classes[i], probs[i]))
    else:
        print("\nMost likely image class is '{}' with probability of {:.2f}".
              format(
                  get_title(classes[0], cat_to_name)
                  if use_mapping_file else classes[0], probs[0]))

    # Computes overall runtime in seconds & prints it in hh:mm:ss format
    end_time = time()
    utility.print_elapsed_time(end_time - start_time)
Ejemplo n.º 2
0
def main():
    # get the input arguments
    in_arg = get_predict_input_args()
    #print_input_arguments(in_arg)

    # Load the model we saved during training
    model, optimizer = load_checkpoint(in_arg.checkpoint)
    print(model, optimizer, end='\n' * 5)

    process_image(in_arg.image_path)
    if in_arg.gpu:
        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    else:
        device = 'cpu'
    print(f"Device used: {device}", end='\n' * 5)

    probabilities, labels = predict(in_arg.image_path,
                                    model,
                                    device,
                                    top_k=in_arg.top_k,
                                    cat_to_name=in_arg.category_names)

    print(f'Prediction Results for top_k = {in_arg.top_k}:\n')
    identifier = 'name' if in_arg.category_names else 'label'
    for probability, flower in zip(probabilities, labels):
        print('Predicted flower {}: {:20} Probability: {}'.format(
            identifier, flower, probability))
def main():
    start_time = time()

    in_args = get_input_args()
    use_gpu = torch.cuda.is_available() and in_args.gpu

    print("Predicting on {} using {}".format("GPU" if use_gpu else "CPU",
                                             in_args.checkpoint))

    model = model_helper.load_checkpoint(in_args.checkpoint)

    if use_gpu:
        model.cuda()

    use_mapping_file = False

    if in_args.category_names:
        with open(in_args.category_names, 'r') as f:
            cat_to_name = json.load(f)
            use_mapping_file = True

    probs, classes = model_helper.predict(in_args.input, model, use_gpu,
                                          in_args.top_k)

    print("\nTop {} Classes predicted for '{}':".format(
        len(classes), in_args.input))

    if use_mapping_file:
        print("\n{:<30} {}".format("Flower", "Probability"))
        print("{:<30} {}".format("------", "-----------"))
    else:
        print("\n{:<10} {}".format("Class", "Probability"))
        print("{:<10} {}".format("------", "-----------"))

    for i in range(0, len(classes)):
        if use_mapping_file:
            print("{:<30} {:.2f}".format(get_title(classes[i], cat_to_name),
                                         probs[i]))
        else:
            print("{:<10} {:.2f}".format(classes[i], probs[i]))

    end_time = time()
    utility.print_elapsed_time(end_time - start_time)
Ejemplo n.º 4
0
def predict_test(tester, arch, enable_gpu):
    checkpoint_dir = testing_dir + '/gpu' if enable_gpu else '/cpu'
    checkpoint = checkpoint_dir + '/' + arch + '_checkpoint.pth'

    model = model_helper.load_checkpoint(checkpoint)

    if enable_gpu:
        model.cuda()

    probs, classes = model_helper.predict(test_image, model, enable_gpu, top_k)

    tester.assertEqual(len(classes), top_k, 'Incorrect number of results')
    tester.assertEqual(classes[0], correct_prediction_class,
                       'Incorrect prediction')

    with open(category_names, 'r') as f:
        cat_to_name = json.load(f)

    tester.assertEqual(cat_to_name[classes[0]], correct_prediction_category,
                       'Incorrect prediction')
Ejemplo n.º 5
0
def main():
    Input_aruguments = argument_parser()
    print("Loading checkpoints in-progress.")
    model = model_helper.load_checkpoint(Input_aruguments.checkpoint)
    print("Loading checkpoints completed. Checking for GPU, please wait.")
    gpu_check = torch.cuda.is_available() and Input_aruguments.gpu
    if gpu_check:
        model.cuda()
        print("GPU Device available.")
    else:
        warnings.warn(
            'No GPU found. Please use a GPU to train your neural network.')
    use_mapping_file = False
    if Input_aruguments.category_names:
        with open(Input_aruguments.category_names, 'r') as f:
            cat_to_name = json.load(f)
            use_mapping_file = True
    print("Prediction in-progress. Please wait.")
    probs, classes = model_helper.predict(Input_aruguments.input, model,
                                          gpu_check, Input_aruguments.top_k)

    print("\nTop {} Classes predicted for '{}':".format(
        len(classes), Input_aruguments.input))
    if use_mapping_file:
        print("\n{:<30} {}".format("Flower", "Probability"))
        print("{:<30} {}".format("------", "-----------"))
    else:
        print("\n{:<10} {}".format("Class", "Probability"))
        print("{:<10} {}".format("------", "-----------"))

    for i in range(0, len(classes)):
        if use_mapping_file:
            print("{:<30} {:.2f}".format(get_title(classes[i], cat_to_name),
                                         probs[i]))
        else:
            print("{:<10} {:.2f}".format(classes[i], probs[i]))
Ejemplo n.º 6
0
        "use a mapping of categories to real names: argument takes path to JSON file containing a dict mapping"
    )
    parser.add_argument("-g",
                        "--gpu",
                        help="flag to toggle GPU usage for model training",
                        action="store_true",
                        default=False)

    sysargs = parser.parse_args()
    print(sysargs)

    # Load model checkpoint
    checkpoint = torch.load(sysargs.checkpoint)

    # Use checkpoint to load model
    num_epochs, model, optimizer = model_helper.load_checkpoint(checkpoint)

    # Enable CUDA if available
    device = torch.device("cuda:0" if (
        sysargs.gpu and torch.cuda.is_available()) else "cpu")

    # Make predictions
    probs, classes = model_helper.predict(sysargs.image_path, model, device,
                                          sysargs.top_k)

    # Load category names if provided, print top K predictions
    print("Top {} predictions:".format(sysargs.top_k))
    print("----------")

    if sysargs.category_names:
        with open(sysargs.category_names, 'r') as f:
def test_cnn(MODEL_NAME, MODEL_NAME_TARGET, BATCH_SIZE, N_LABELS,
             PATH_TO_IMAGES, DEBUG_MODE, CHECKPOINT_PATH,
             CHECKPOINT_PATH_TARGET):
    """
    Train torchvision model to NIH data given high level hyperparameters.

    Args:
        MODEL_NAME: model name
        MODEL_NAME_TARGET: the other model name
        BATCH_SIZE: number of batch data per training
        N_LABELS: number of class labels
        PATH_TO_IMAGES: path to NIH images
        DEBUG_MODE: if true then no log will be created
        CHECKPOINT_PATH: load checkpoint path
        CHECKPOINT_PATH_TARGET: load the other checkpoint path
    Returns:
        # preds: torchvision model predictions on test fold with ground truth for comparison
        # aucs: AUCs for each train,test tuple
    """

    # use imagenet mean,std for normalization
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    normalize = transforms.Normalize(mean=mean, std=std)

    # define torchvision transforms
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.Resize(256),
            transforms.TenCrop(224),
            transforms.Lambda(lambda crops: torch.stack(
                [normalize(transforms.ToTensor()(crop)) for crop in crops]))
        ]),
        'val':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean, std)
        ]),
        'test':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean, std)
        ])
    }

    # create train/val dataloaders
    transformed_datasets = {
        x: datasets.ImageFolder(os.path.join(PATH_TO_IMAGES, x),
                                data_transforms[x])
        for x in ['test']
    }

    dataloaders = {
        x: torch.utils.data.DataLoader(transformed_datasets[x],
                                       batch_size=BATCH_SIZE,
                                       shuffle=True,
                                       num_workers=0)
        for x in ['test']
    }

    # please do not attempt to train without GPU as will take excessively long
    if not use_gpu:
        raise ValueError("Error, requires GPU")

    model = init_model(MODEL_NAME, N_LABELS)
    model = load_checkpoint(model, CHECKPOINT_PATH)
    model = model.cuda()

    if (CHECKPOINT_PATH_TARGET):
        model_target = init_model(MODEL_NAME_TARGET, N_LABELS)
        model_target = load_checkpoint(model_target, CHECKPOINT_PATH_TARGET)
        model_target = model_target.cuda()

    loading_bar = ''
    dataloaders_length = len(dataloaders['test'])
    for i in range(dataloaders_length):
        loading_bar += '-'

    model_labels = []
    model_pred = []
    model_target_pred = []

    model_pred_bin = []
    model_target_pred_bin = []

    for phase in ['test']:
        for data in dataloaders[phase]:
            loading_bar = f'={loading_bar}'
            loading_bar = loading_bar[:dataloaders_length]
            print(f'Testing: {loading_bar}', end='\r')

            inputs, labels = data
            if phase == 'test':

                inputs = inputs.cuda()
                labels = labels.cpu().data.numpy()
                model_labels.extend(labels)

                outputs = model(inputs)
                outputs_pred = torch.max(outputs, dim=1)[1].cpu().data.numpy()
                model_pred.extend(outputs_pred)
                if (CHECKPOINT_PATH_TARGET):
                    outputs_target = model_target(inputs)
                    outputs_target_pred = torch.max(
                        outputs_target, dim=1)[1].cpu().data.numpy()
                    model_target_pred.extend(outputs_target_pred)

    print('')

    for i, _ in enumerate(model_labels):
        model_pred_bin.append(1 if model_labels[i] == model_pred[i] else 0)
        if (CHECKPOINT_PATH_TARGET):
            model_target_pred_bin.append(1 if model_labels[i] ==
                                         model_target_pred[i] else 0)

    print(accuracy_score(model_labels, model_pred))
    print(f1_score(model_labels, model_pred, average='micro'))

    if (CHECKPOINT_PATH_TARGET):
        print(accuracy_score(model_labels, model_target_pred))
        print(f1_score(model_labels, model_target_pred, average='micro'))
        tp = 0
        fp = 0
        tn = 0
        fn = 0
        for i, _ in enumerate(model_pred_bin):
            if model_pred_bin[i] == model_target_pred_bin[i]:
                if model_pred_bin[i] == 1:
                    tp += 1
                else:
                    tn += 1
            else:
                if model_pred_bin[i] == 0:
                    fp += 1
                else:
                    fn += 1

        print(f"True positive = {tp}")
        print(f"False positive = {fp}")
        print(f"False negative = {fn}")
        print(f"True negative = {tn}")

        print("Finish testing")
Ejemplo n.º 8
0
                    type=int,
                    help='Specify top k flower probabilities.')
parser.add_argument('--category_names',
                    action="store",
                    default=None,
                    help='Use a mapping of categories to real names.')
parser.add_argument('--gpu',
                    action="store_true",
                    default=False,
                    help='Use GPU for inference.')

arguments = parser.parse_args()
print('Running predict.py with the following arguments {}'.format(arguments))

# Load checkpoint and rebuild model
model = model_helper.load_checkpoint(arguments.checkpoint)

# Preprocess image into image tensor
image_tensor = model_helper.process_image(arguments.input)

# Return top K most likely classes and their probabilities
probabilities, topk_classes = model_helper.predict(image_tensor,
                                                   model,
                                                   arguments.gpu,
                                                   arguments.top_k,
                                                   arguments.category_names,
                                                   debug=True)

position = 1
for p, c in zip(probabilities, topk_classes):
    if arguments.category_names is not None:
def train_cnn(MODEL_NAME,
              PRETRAINED,
              FREEZE,
              EPOCHS,
              BATCH_SIZE,
              N_LABELS,
              OPTIMIZERS,
              PATH_TO_IMAGES,
              LR,
              WEIGHT_DECAY,
              LR_DECAY_STEPS,
              DEBUG_MODE,
              CHECKPOINT_PATH='',
              DISTILLATE_WITH=''):
    """
    Train torchvision model to NIH data given high level hyperparameters.

    Args:
        MODEL_NAME: model name
        PRETRAINED: if model pretrained
        FREEZE: model layer frozen or not
        EPOCHS: epochs iteration
        BATCH_SIZE: number of batch data per training
        N_LABELS: number of class labels
        OPTIMIZERS: optimizers used
        PATH_TO_IMAGES: path to NIH images
        LR: learning rate
        WEIGHT_DECAY: weight decay parameter for SGD
        LR_DECAY_STEPS: how many steps before LR decayed and dropped
        DEBUG_MODE: if true then no log will be created
        CHECKPOINT_PATH: load checkpoint path
        DISTILLATE_WITH: distillate the model with
    Returns:
        # preds: torchvision model predictions on test fold with ground truth for comparison
        # aucs: AUCs for each train,test tuple

    """

    if not os.path.exists('results'):
        os.makedirs('results')

    # use imagenet mean,std for normalization
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    normalize = transforms.Normalize(mean=mean, std=std)

    # define torchvision transforms
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.Resize(256),
            transforms.TenCrop(224),
            transforms.Lambda(lambda crops: torch.stack(
                [normalize(transforms.ToTensor()(crop)) for crop in crops]))
        ]),
        'val':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean, std)
        ]),
        'test':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize(mean, std)
        ])
    }

    # create train/val dataloaders
    transformed_datasets = {
        x: datasets.ImageFolder(os.path.join(PATH_TO_IMAGES, x),
                                data_transforms[x])
        for x in ['train', 'val', 'test']
    }

    dataloaders = {
        x: torch.utils.data.DataLoader(transformed_datasets[x],
                                       batch_size=BATCH_SIZE,
                                       shuffle=True,
                                       num_workers=0)
        for x in ['train', 'val']
    }

    # please do not attempt to train without GPU as will take excessively long
    if not use_gpu:
        raise ValueError("Error, requires GPU")

    # Check model used
    import modified_densenet
    import modified_alexnet

    model = (
        models.densenet121(
            pretrained=PRETRAINED) if MODEL_NAME == 'densenet' else
        modified_densenet.densenet121(type=MODEL_NAME, pretrained=PRETRAINED)
        if MODEL_NAME == 'va-densenet' or MODEL_NAME == 'reva-densenet'
        or MODEL_NAME == 'fp-densenet' or MODEL_NAME == 'start-densenet'
        or MODEL_NAME == 'every-densenet' or MODEL_NAME == 'sedensenet'
        or MODEL_NAME == 'triplelossdensenet' else models.alexnet(
            pretrained=PRETRAINED) if MODEL_NAME == 'alexnet' else
        modified_alexnet.alexnet(type=MODEL_NAME, pretrained=PRETRAINED)
        if MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'reva-alexnet'
        or MODEL_NAME == 'fp-alexnet' or MODEL_NAME == 'start-alexnet' else
        models.resnet152(pretrained=PRETRAINED) if MODEL_NAME ==
        'resnet' else models.vgg16(
            pretrained=PRETRAINED) if MODEL_NAME == 'VGG' else models.vgg16_bn(
                pretrained=PRETRAINED) if MODEL_NAME == 'VGG_Bn' else '')

    # get num_ftrs based on model name
    num_ftrs = (model.classifier.in_features
                if MODEL_NAME == 'densenet' or MODEL_NAME == 'va-densenet'
                or MODEL_NAME == 'reva-densenet' or MODEL_NAME == 'fp-densenet'
                or MODEL_NAME == 'start-densenet'
                or MODEL_NAME == 'every-densenet' or MODEL_NAME == 'sedensenet'
                or MODEL_NAME == 'triplelossdensenet' else
                model.classifier[6].in_features if MODEL_NAME == 'alexnet'
                or MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'reva-alexnet'
                or MODEL_NAME == 'fp-alexnet' or MODEL_NAME == 'start-alexnet'
                or MODEL_NAME == 'VGG' or MODEL_NAME == 'VGG_Bn' else
                model.fc.in_features if MODEL_NAME == 'resnet' else
                model.fc3.in_features if MODEL_NAME == 'small_va' else '')

    # change classifier class to N_LABELS
    if (MODEL_NAME == 'densenet' or MODEL_NAME == 'va-densenet'
            or MODEL_NAME == 'reva-densenet' or MODEL_NAME == 'fp-densenet'
            or MODEL_NAME == 'start-densenet' or MODEL_NAME == 'every-densenet'
            or MODEL_NAME == 'sedensenet'
            or MODEL_NAME == 'triplelossdensenet'):
        model.classifier = nn.Linear(num_ftrs, N_LABELS)
    elif (MODEL_NAME == 'alexnet' or MODEL_NAME == 'va-alexnet'
          or MODEL_NAME == 'va-alexnet' or MODEL_NAME == 'reva-alexnet'
          or MODEL_NAME == 'fp-alexnet' or MODEL_NAME == 'start-alexnet'
          or MODEL_NAME == 'VGG' or MODEL_NAME == 'VGG_Bn'):
        model.classifier[6] = nn.Linear(num_ftrs, N_LABELS)
    elif (MODEL_NAME == 'resnet'):
        model.fc = nn.Linear(num_ftrs, N_LABELS)
    else:
        raise ValueError("Error model name")

    if CHECKPOINT_PATH != '':
        model = load_checkpoint(model, CHECKPOINT_PATH)

    # show params to learn
    if FREEZE:
        for name, param in model.named_parameters():
            attention_pattern = re.compile(
                r'^(conv2d1x1|valinear|transconv|start|every|se_).+$')
            classifier_pattern = re.compile(
                r'^(classifier(?!\.\d)|classifier\.6|fc).+$')
            if attention_pattern.match(name):
                param.requires_grad = True
            elif classifier_pattern.match(name) and CHECKPOINT_PATH == '':
                param.requires_grad = True
            else:
                param.requires_grad = False

    if FREEZE:
        print('Params to learn:')
        for name, param in model.named_parameters():
            if param.requires_grad == True:
                print(name)
        print('==================================')

    # Distillate
    distillate_time = ''
    if DISTILLATE_WITH != '':
        print(f'Distillate with {DISTILLATE_WITH}')
        distillate_time = datetime.datetime.now().isoformat()
        model_distillate = models.densenet121(pretrained=PRETRAINED)
        num_ftrs_distillate = model_distillate.classifier.in_features
        model_distillate.classifier = nn.Linear(num_ftrs_distillate, N_LABELS)
        model_distillate = load_checkpoint(model_distillate, DISTILLATE_WITH)
        print('Loaded checkpoint for distillation')
        model_distillate = model_distillate.cuda()

        loading_bar = ''
        dataloaders_length = len(dataloaders['train']) + len(
            dataloaders['val'])
        for i in range(dataloaders_length):
            loading_bar += '-'

        for phase in ['train', 'val']:
            for data in dataloaders[phase]:
                loading_bar = f'={loading_bar}'
                loading_bar = loading_bar[:dataloaders_length]
                print(f'Distillating: {loading_bar}', end='\r')

                inputs, labels = data
                if phase == 'train':
                    for i in range(10):
                        inp = inputs.clone()[:, i]
                        inp = inp.cuda()
                        labels = labels.cuda()
                        outputs = model_distillate(inp).cpu().data.numpy()
                        if len(outputs) != BATCH_SIZE:
                            outputs_padding = np.zeros((BATCH_SIZE, N_LABELS))
                            outputs_padding[:outputs.shape[0], :outputs.
                                            shape[1]] = outputs
                            outputs = outputs_padding
                        if Path(f'results_distillation/d-{distillate_time}.npy'
                                ).exists():
                            loaded_np = np.load(
                                f'results_distillation/d-{distillate_time}.npy'
                            )
                            outputs = np.append(loaded_np, [outputs], axis=0)
                        else:
                            outputs = [outputs]
                        np.save(
                            f'results_distillation/d-{distillate_time}.npy',
                            outputs)
                else:
                    inputs = inputs.cuda()
                    labels = labels.cuda()
                    outputs = model_distillate(inputs).cpu().data.numpy()
                    if len(outputs) != BATCH_SIZE:
                        outputs_padding = np.zeros((BATCH_SIZE, N_LABELS))
                        outputs_padding[:outputs.shape[0], :outputs.
                                        shape[1]] = outputs
                        outputs = outputs_padding
                    loaded_np = np.load(
                        f'results_distillation/d-{distillate_time}.npy')
                    outputs = np.append(loaded_np, [outputs], axis=0)
                    np.save(f'results_distillation/d-{distillate_time}.npy',
                            outputs)
        print('')

    # put model on GPU
    model = model.cuda()
    model.name = MODEL_NAME

    # define criterion, optimizer for training
    if distillate_time != '':
        criterion = nn.MSELoss()
    else:
        # class_weight_value = [
        # 1 - (1 / 77),
        # 1 - (3 / 77),
        # 1 - (12 / 77),
        # 1 - (19 / 77),
        # 1 - (3 / 77),
        # 1 - (33 / 77),
        # 1 - (6 / 77)
        # ]
        # class_weight = torch.FloatTensor(class_weight_value).cuda()
        # criterion = nn.CrossEntropyLoss(weight=class_weight)
        criterion = nn.CrossEntropyLoss()

    # Check if SGD or Adam
    optimizer = (optim.SGD(model.parameters(), lr=LR, momentum=0.9) if
                 OPTIMIZERS == 'SGD' else optim.Adam(model.parameters(), lr=LR)
                 if OPTIMIZERS == 'Adam' else '')

    scheduler = lr_scheduler.StepLR(optimizer,
                                    step_size=LR_DECAY_STEPS,
                                    gamma=0.1)

    dataset_sizes = {x: len(transformed_datasets[x]) for x in ['train', 'val']}

    # train model
    model, best_epoch = train_model(model,
                                    criterion,
                                    optimizer,
                                    optim_name=OPTIMIZERS,
                                    LR=LR,
                                    num_epochs=EPOCHS,
                                    dataloaders=dataloaders,
                                    dataset_sizes=dataset_sizes,
                                    weight_decay=WEIGHT_DECAY,
                                    scheduler=scheduler,
                                    debug_mode=DEBUG_MODE,
                                    pretrained=PRETRAINED,
                                    freeze=FREEZE,
                                    checkpoint=CHECKPOINT_PATH,
                                    distillate_time=distillate_time)

    print("Finished Training")