def main(): start_time = time() # Handle Arguments in_arg = get_input_args_predict() print(in_arg) # Load checkpoint and rebuild network model = model_utils.load_checkpoint(in_arg.input, in_arg.gpu) # Process image image = data_image_utils.process_image(in_arg.image_path) # Label mapping cat_to_name = data_image_utils.get_label_mapping(in_arg.category_names) # Predict probs, classes = model_utils.predict(Variable(image).unsqueeze(0), model, in_arg.top_k) model_utils.print_prediction(classes, probs, model.class_to_idx, cat_to_name) tot_time = time()- start_time print("\n** Total Elapsed Runtime:", str(int((tot_time/3600)))+":"+str(int((tot_time%3600)/60))+":" +str(int((tot_time%3600)%60)) )
def main(): # command line arguments c_img_path = None c_chk_path = None c_top_k = None c_mapping_path = None c_use_gpu = False # Get command line arguments in_args = get_input_args_predict() c_img_path = in_args.img_path c_chk_path = in_args.checkpoint c_top_k = in_args.top_k c_mapping_path = in_args.category_names c_use_gpu = in_args.gpu print("Running predict.py with the following arguments: ") print("Image Path: {}\nCheckpoint: {}\nTop K: {}\nMapping: {}\nGPU: {}". format(c_img_path, c_chk_path, c_top_k, c_mapping_path, c_use_gpu)) # load the checkpoint model = checkpoint_manager.load_checkpoint(c_chk_path) # set the device (gpu or cpu) device = torch.device( "cuda" if torch.cuda.is_available() and c_use_gpu else "cpu") # call the predict function and get the topK (c_top_k) classes and probabilities probs, classes = predict(c_img_path, model, device, c_top_k) # check to see if we want to map the classes to the category names (one of the command # line arguments) if c_mapping_path is not None: cat_to_name = hlp.open_label_mapping_file(c_mapping_path) classes = map_cat_to_real_names(classes, cat_to_name) # print the results print_results(probs, classes)
def main(): # data_dir = 'flowers' # train_dir = data_dir + '/train' # valid_dir = data_dir + '/valid' # test_dir = data_dir + '/test' # load arguments in_arg = get_input_args_predict() # load the model and optimizer we already used model = load_checkpoint(in_arg.checkpoint) # print(model) # initialize device as CPU device = torch.device("cpu") # set up cuda or cpu since we will need some gpu time if in_arg.gpu: if torch.cuda.is_available(): device = torch.device("cuda:0") else: device = torch.device("cpu") print("GPU selected, but there is no GPU available.") print("Model running on: ", device) image_path = in_arg.input topk = in_arg.top_k probs, classes = predict(image_path, model, topk, device) print("Probabilities: ", probs) print("Classes: ", classes) if (in_arg.category_names != None): # we have a file path category_path = in_arg.category_names flowers = category_mapper(probs, classes, category_path)
def main(): # Get the arguments passed to the python script in_arg = get_input_args_predict() # Load check point from pass model = load_checkpoint(in_arg.checkpoint) # Send model to gpu model_gpu, device = hw_control(model, in_arg.gpu) # Perform the prediction probs, classes = predict(in_arg.path, model, in_arg.top_k, in_arg.gpu) with open('cat_to_name.json', 'r') as f: cat_to_name = json.load(f) Class_names = [cat_to_name[str(class_name)] for class_name in classes] print("Most probable cat.:\t", Class_names) print("Probalbilities \t", probs) return probs, classes
#PROGRAMMER: Joshua Cayanan # DATE CREATED: June 29, 2020 # REVISED DATE: # PURPOSE: Feed an image into classifier neural network and return prediction with associated probability # Example call: # python predict.py --image 07573.jpg --model checkpoint --mapping cat_to_name.json #Import functions created for this program from get_input_args import get_input_args_predict from prediction_model import predict #Funnel command line arguments into program in_arg = get_input_args_predict() #Assign input arguments to variables test_image = in_arg.test_image checkpoint = in_arg.checkpoint top_k = in_arg.top_k category_names = in_arg.category_names gpu = in_arg.gpu if gpu: gpu = 'cuda' else: gpu = 'cpu' #Call the prediction model function flower_names, probs = predict(test_image, checkpoint, top_k, category_names, gpu) print(flower_names) print(probs)
import torch from torchvision import datasets, transforms, models from PIL import Image import numpy as np import matplotlib.pyplot as plt import json import get_input_args as args from torch import nn from collections import OrderedDict #import train #Load predict command line args arg = args.get_input_args_predict() image_path = arg.image_path checkpoint = arg.checkpoint #Name checkpoint file, e.g. checkpoint.pth topk = arg.topk cat_to_name = arg.cat_to_name gpu = arg.gpu arch = arg.arch #Gives a dictionary, mapping the integer encoded categories to the actual names of the flowers. Used in predict() with open(cat_to_name, 'r') as f: cat_to_name = json.load(f) cat_to_name def build_network(input_size, hidden_units, model, output_size=102, drpout=0.2,
def main(): # read initial inputs in_args = get_input_args_predict() # train neural network predict(in_args)