コード例 #1
0
def main():
    # Get input arguments
    args = get_command_line_args()
    use_gpu = torch.cuda.is_available() and args.gpu
    print("Input file: {}".format(args.input))
    print("Checkpoint file: {}".format(args.checkpoint))
    if args.top_k:
        print("Returning {} most likely classes".format(args.top_k))
    if args.category_names:
        print("Category names file: {}".format(args.category_names))
    if use_gpu:
        print("Using GPU.")
    else:
        print("Using CPU.")
    
    # Load the checkpoint
    model = predict_utils.load_checkpoint(args.checkpoint)
    print("Checkpoint loaded.")
    
    # Move tensors to GPU
    if use_gpu:
        model.cuda()
    
    # Load categories file
    if args.category_names:
        with open(args.category_names, 'r') as f:
            categories = json.load(f)
            print("Category names loaded")
    
    results_to_show = args.top_k if args.top_k else 1
    
    # Predict
    print("Processing image")
    probabilities, classes = predict_utils.predict(args.input, model, use_gpu, results_to_show, args.top_k)
    
    # Show the results
    # Print results
    if results_to_show > 1:
        print("Top {} Classes for '{}':".format(len(classes), args.input))

        if args.category_names:
            print("{:<30} {}".format("Flower", "Probability"))
            print("------------------------------------------")
        else:
            print("{:<10} {}".format("Class", "Probability"))
            print("----------------------")

        for i in range(0, len(classes)):
            if args.category_names:
                print("{:<30} {:.2f}".format(categories[classes[i]], probabilities[i]))
            else:
                print("{:<10} {:.2f}".format(classes[i], probabilities[i]))
    else:
        print("The most likely class is '{}': probability: {:.2f}" \
              .format(categories[classes[0]] if args.category_names else classes[0], probabilities[0]))
コード例 #2
0
def main():
    args = predict_args.get_args()
    print(args)

    image_path = args.image_path
    model, optimizer = predict_utils.load_checkpoint(args.checkpoint,
                                                     args.arch, args.gpu)
    probs, classes = predict(image_path, model, args.top_k)
    cat_to_name = predict_utils.load_category_names(args.category_names)
    names = np.array([cat_to_name[i] for i in classes])
    for name, prob in zip(names, probs):
        print("{}: {}".format(name, str(prob)))
コード例 #3
0
def main():
    """ Main function contains core logic for predict.

        For parameters review docstring of get_input_args above.

    """

    in_args = get_input_args()
    if in_args.verbose: print(in_args)  # temp debug

    # throw errors right away for obvious errors - avoids deep traceback stack
    if not os.path.isfile(in_args.image_path):
        sys.exit(
            f"predict.py: error: argument image_path '{in_args.image_path}' file does not exist."
        )
    if not os.path.isfile(in_args.checkpoint_filepath):
        sys.exit(
            f"predict.py: error: argument checkpoint_filepath '{in_args.checkpoint_filepath}' file does not exist."
        )

    # Build model from checkpoint
    state_dict = load_checkpoint(in_args.checkpoint_filepath)
    model, criterion, optimizer = build_model(
        state_dict['arch'], hidden_sizes=state_dict['hidden_sizes'])

    if in_args.verbose: print(model.classifier)  # temp debug

    # restore state into various model components - perhaps should pass state into build_model()???
    model.load_state_dict(state_dict['state_dict'])
    model.idx_to_class = state_dict['idx_to_class']
    model.cat_to_name = state_dict['cat_to_name']

    optimizer.load_state_dict(state_dict['state_optimizer'])

    print()
    print(
        f"Utilizing architecture {state_dict['arch']} trained over {state_dict['tot_epochs']} epochs."
    )

    # perform the inference/prediction
    predict_wrapper(in_args.image_path,
                    model,
                    top_k=in_args.top_k,
                    gpu=in_args.gpu,
                    verbose=in_args.verbose)

    return None
コード例 #4
0
import argparse
import predict_utils

parser = argparse.ArgumentParser(description='This script helps in predicting the model',)

parser.add_argument('--image_path', dest='image_path', action='store', default='./flowers/test/9/image_06410.jpg')
parser.add_argument('--checkpoint_path', dest='checkpoint_path', action='store', default='checkpoint.pth')
parser.add_argument('--top_k', dest='top_k', action='store', default=5, type=int)
parser.add_argument('--gpu', dest="mode", action="store", default="gpu")

args = parser.parse_args()

checkpoint_model = predict_utils.load_checkpoint(args.checkpoint_path)

probs, classes = predict_utils.predict(args.image_path, checkpoint_model, args.top_k)

for i in range(args.top_k):
    print("Probability - {} \t Class - {}".format(probs[i], classes[i]))
コード例 #5
0
parser.add_argument('--device', action="store", dest="device", default='gpu')
parser.add_argument('--category_names',
                    action="store",
                    dest="category_names",
                    default='cat_to_name')

args = vars(parser.parse_args())

#imputs
image_path = args['image']
checkpoint = args['input']
topk = int(args['top_k'])
device = 'cuda' if args['device'] == 'gpu' else 'cpu'

# load the model
model, learning_rate, hidden_units, class_to_idx = load_checkpoint(checkpoint)

# prediction
probs, classes = predict(image_path, device, model, topk)

# print results
cat_to_name = args['category_names'] + '.json'
with open('cat_to_name.json', 'r') as f:
    cat_to_name = json.load(f)

top_labels = [cat_to_name[cat] for cat in classes]

res = "\n".join("{} {}".format(x, y) for x, y in zip(probs, top_labels))

print(res)
コード例 #6
0
def main():
    """ Main function contains core logic for train.

        For parameters review docstring of get_input_args above.

    """

    start_time = time()

    # process arguments
    in_args = get_input_args()
    if in_args.verbose: print(in_args)  # temp debug

    # initialize loaders, datasets, transforms
    dataloaders, image_datasets, data_transforms = load_data(
        data_dir=in_args.data_dir,
        batch_size=in_args.batch_size,
        verbose=in_args.verbose)

    # determine GPU vs CPU mode
    device = torch.device(
        "cuda" if torch.cuda.is_available() and in_args.gpu else "cpu")
    print(f"Running in {device} mode.")

    # initialize model as necessary in reuse vs. new mode
    if in_args.reuse_mode:  # build model from existing checkpoint
        print(
            f"train.py: info: checkpoint {in_args.checkpoint_filepath} exists - train.py will run in reuse mode."
        )

        # obtain state from checkpoint
        state_dict = load_checkpoint(in_args.checkpoint_filepath,
                                     gpu=in_args.gpu)

        arch = state_dict['arch']
        tot_epochs = state_dict['tot_epochs']
        hidden_sizes = state_dict['hidden_sizes']

        print(
            f"Utilizing architecture {arch} existing model trained over {tot_epochs} epochs."
        )

    else:  # build model in initialization mode
        print(
            f"train.py: info: checkpoint {in_args.checkpoint_filepath} does not exists - train.py will run in new mode."
        )

        arch = in_args.arch
        tot_epochs = 0
        hidden_sizes = in_args.hidden_sizes

        print(
            f"Utilizing architecture {arch} to create new model with hidden_sizes {hidden_sizes}."
        )

    # build the model
    model, criterion, optimizer = build_model(
        arch,
        learning_rate=in_args.learning_rate,
        dropout=in_args.dropout,
        hidden_sizes=hidden_sizes,
        verbose=in_args.verbose)
    if in_args.verbose: print(model.classifier)  # temp debug

    # post process the model by adding various components to it
    if in_args.reuse_mode:  # restore or initialize state as necessary in reuse vs. new mode
        # restore state into various model components
        model.load_state_dict(state_dict['state_dict'])
        model.idx_to_class = state_dict['idx_to_class']
        model.cat_to_name = state_dict['cat_to_name']

        # TODO: debug this - optimizer state can not be reloaded on a consistent basis.
        # This is causing trouble with possible optimizer.Adam bug in which restoring state from cpu mode to cuda mode throws exception.
        # Ran out of time to debug this - however not using this doesn't seem to prevent the model from training.
        #optimizer.load_state_dict(state_dict['state_optimizer'])

    else:
        # augment model with index to class (category) and category to name mappings
        model.idx_to_class = get_idx_to_class(image_datasets['train'])
        model.cat_to_name = get_cat_to_name()

    # train the model by looping for epochs times
    train_model(model,
                in_args.epochs,
                device,
                dataloaders,
                criterion,
                optimizer,
                verbose=in_args.verbose)

    tot_epochs += in_args.epochs
    print(
        f"Completed {in_args.epochs} additional epochs for a cumulative total of {tot_epochs} {'epoch' if tot_epochs==1 else 'epochs'}."
    )

    # save the state to new or reuse(ed) checkpoint
    save_checkpoint(arch,
                    model,
                    optimizer,
                    in_args.checkpoint_filepath,
                    tot_epochs=tot_epochs)

    tot_time = time(
    ) - start_time  # calculate difference between end time and start time
    print(
        "** Total Elapsed Runtime:",
        f"{str(int((tot_time/3600)))}:{str(int((tot_time%3600)/60))}:{str(round((tot_time%3600)%60))}"
    )

    return None