示例#1
0
def main():
    args = parse_args()
    model = load_checkpoint(args.checkpoint)
    cat_to_name = load_cat_name = load_cat_names(args.category_names)
    img_path = args.filepath
    probs, classes = predict(img_path, model, int(args.top_k),gpu)
    lables = [cat_to_name[str(index)] for index in classes]
    probability = probs
    print ('File selected: ' + img_path)
    print (labels)
    print (probability)
    i=0
    while i < len(labels):
       print ("{} with a probability of {}".format(labels[i],probability[i]))
       i = i + 1
示例#2
0
def main():
    args = parse_args()
    gpu = args.gpu
    model = load_checkpoint(args.checkpoint)
    cat_to_name = load_cat_names(args.category_names)

    img_path = args.filepath
    probs, classes = predict(img_path, model, int(args.top_k), gpu)
    labels = [cat_to_name[str(index)] for index in classes]
    probability = probs
    print('File selected: ' + img_path)

    print(labels)
    print(probability)

    i = 0  # this prints out top k classes and probs as according to user
    while i < len(labels):
        print("{} with a probability of {}".format(labels[i], probability[i]))
        i += 1  # cycle through
示例#3
0
def main():
    args = parse_args()
    gpu = args.gpu
    model = load_checkpoint(args.checkpoint)
    cat_to_name = load_cat_names(args.category_names)
    if args.filepath == None:
        img_num = random.randint(1, 102)
        image = random.choice(
            os.listdir('./flowers/test/' + str(img_num) + '/'))
        image_path = './flowers/test/' + str(img_num) + '/' + image
        prob, classes = predict(image_path, model, int(args.top_k), gpu)
        print('Image selected: ' + str(cat_to_name[str(img_num)]))
    else:
        image_path = args.filepath
        prob, classes = predict(image_path, model, int(args.top_k), gpu)
        print('File selected: ' + image_path)
    print(prob)
    print(classes)
    print([cat_to_name[x] for x in classes])
示例#4
0
def main():
    args = parse_args()
    gpu = args.gpu
    model = load_checkpoint(args.checkpoint)
    cat_to_name = load_cat_names(args.category_names)

    if args.filepath:
        img_path = args.filepath
    else:
        print('Cannot run prediction ..')
        img_path = input("Please provide path to image: ")

    probs, classes = predict(img_path, model, int(args.top_k), gpu)

    print('\n======')
    print('The filepath of the selected image is: ' + img_path, '\n')
    print('The top K CLASSES for the selected image are: \n', classes, '\n')
    print('The top K PROBABILITIES for the selected image are: \n ', probs,
          '\n')
    print('The top K CATEGORY NAMES for the selected image are: \n',
          [cat_to_name[x].title() for x in classes])
    print('======\n')
示例#5
0
def main():
    in_arg = get_input_args()
    gpu = in_arg.gpu
    model = load_checkpoint(in_arg.checkpoint)
    
    cat_to_name = load_cat_names(in_arg.category_names)
    
    if in_arg.filepath == None:
        image_num = random.randint(1, 102)
        image = random.choice(os.listdir('./flowers/test/' + str(image_num) + '/'))
        img_path = './flowers/test/' + str(image_num) + '/' + image
        prob, classes = predict(img_path, model, in_arg.top_k, gpu)
        print("Selected Image is: " + str(cat_to_name[str(image_num)]))
    else:
        # Show random predicted image of the original image (displayed above) from a particular subfolder
        #image = random.choice(os.listdir('./flowers/test/' + str(image_num) + '/'))
        img_path = in_arg.filepath
        prob, classes = predict(img_path, model, in_arg.top_k, gpu)
        print("Selected Image is: " + img_path)

    print("\nProbabilities are: \n", prob)
    print("\nClasses are: \n", classes)
    print("\nFlowers names are: \n", [cat_to_name[i] for i in classes])
示例#6
0
    img_torch = img_torch.float()
    if gpu == 'gpu':
        with torch.no_grad():
            output = model.forward(img_torch.cuda())
    else:
        with torch.no grad():
            output = model.forward(img_torch)
    probability = F.softmax(output.data,dim=1)
    probos = np.array(probability.topk(topk)[0][0])
    index-to_class = {val: key for key, val in model.class_to_idx()}
    top_classes = [np.int(index_to_class[each] for np.array(probability.topk(topk)[1][0])]
    return probs, top_classes
def main():
    args = parse_arg()
    model = load.checkpoint(args.checkpoint)
    cat_to_name = load_cat_name = load_cat_names(args.category_names)
    img_path = args.filepath
    probs, classes = predict(img_path, model, int(args.top_k),gpu)
    lables = [cat_to_name[str(index)] for index in classes]
    probability = probs
    print ('File selected: ' + img_path)
    print (labels)
    print (probability)
    i=0
    while i < len(labels):
       print ("{} with a probability of {}.format(labels[i], probability[i]]))
       i = i + 1
if __name__ == "__main__":
    main()