def predict(image_path, model, top_k=1, gpu=False): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' device = torch.device("cuda" if ( gpu == True and torch.cuda.is_available()) else "cpu") model.to(device) im = utility.process_image(image_path) im_in = im.view(1, 3, 224, 224).to(device) with torch.no_grad(): model.eval() logps = model(im_in) ps = torch.exp(logps) probs, idx_list = ps.topk(top_k, dim=1) idx_to_class = {v: k for k, v in model.class_to_idx.items()} class_list = [] [ class_list.append(idx_to_class[idx]) for idx in idx_list.cpu().numpy().tolist()[0] ] return probs.cpu().numpy()[0], class_list
def predict(image_path, model, topk): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' # TODO: Implement the code to predict the class from an image file model.cpu() img = utility_functions.process_image(image_path) img = torch.from_numpy(img).type(torch.FloatTensor) img = img.unsqueeze_(0) with torch.no_grad(): logps = model.forward(img) ps = torch.exp(logps) probs, indices = torch.topk(ps, topk) probs = probs.tolist()[0] indices = indices.tolist()[0] ind_cls = [] for item in range(len(model.class_to_idx.items())): ind_cls.append(list(model.class_to_idx.items())[item][0]) top_classes = [] for i in range(topk): top_classes.append(ind_cls[indices[i]]) return probs, top_classes
def predict(image_path, model, topk=5, with_gpu=True): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' # TODO: Implement the code to predict the class from an image file #load the image image = utilfunc.process_image(image_path) #unsqueeze to get a 1 dimensionnal tensor image.unsqueeze_(0) model.eval() if with_gpu == True: model.to("cuda") image = image.to("cuda") #use the network with torch.no_grad(): output = torch.exp(model(image)) #predict the top5 most likely labels top_p, top_class = output.topk(topk) #convert the indices to classes idx_to_class = {model.class_to_idx[k]: k for k in model.class_to_idx} probs = top_p.cpu().numpy()[0] classes = list() for tclass in top_class.cpu().numpy()[0]: classes.append(idx_to_class[tclass]) return probs, classes
def predict(model, image_path, topk, labels, gpu): ''' Predict the class (or classes) of an image using a trained deep learning model. Inputs: topk - number of top values we want to retrieve. Outputs: top_probs - labels_list - ''' device = torch.device( 'cuda' if torch.cuda.is_available() and gpu else 'cpu') model.to(device) model.eval() with torch.no_grad(): image = utility_functions.process_image(image_path) image = torch.from_numpy(np.array([image])).float() image.to(device) log_ps = model(image) ps = torch.exp(log_ps) probs, classes = ps.topk(topk, dim=1) top_probs = probs.tolist()[0] top_classes = classes.tolist()[0] idx_to_class = { value: key for key, value in model.class_to_idx.items() } labels_list = [] for c in top_classes: labels_list.append(labels[idx_to_class[c]]) utility_functions.imshow(utility_functions.process_image(image_path)) predictions, classes = top_probs, labels_list fig, ax = plt.subplots() y = np.arange(len(classes)) plt.barh(y, predictions, align='center') plt.yticks(np.arange(len(classes)), classes) plt.show() return top_probs, labels_list
def sanity_checking(model, image_path, index, labels): utility_functions.imshow( utility_functions.process_image(image_path)).set_title( labels[str(index)]) predictions, classes = predict(image_path, model) fig, ax = plt.subplots() y = np.arange(len(classes)) plt.barh(y, predictions, align='center') plt.yticks(np.arange(len(classes)), classes) plt.show()
def main(): # 1. Getting Input Arguments args = get_input_arg() # 2. Loading Checkpoint (Trained Model) model, model_spec = load_checkpoint(args.checkpoint) # 3. Image Preprocessing np_img = process_image(args.input) # 4. further processing to fit numpy image to the trained model img = process_image_to_tensor(np_img) # 5. Decide using CPU or GPU for Prediction device = torch.device("cuda:0" if ( torch.cuda.is_available() and args.gpu) else "cpu") print(f"Device used for prediction: {device}") # 6. Class Prediciton df_topk_predict, predicted_class = class_predict(img, model, device, args.top_k) # 7. Parse Class inx back to label df = parse_idx_to_label(df_topk_predict, args.category_names, model_spec) print(f"Top {args. top_k} Prediciton: \n {df}")
def predict(image_path, model, topk, device): ''' Predicts the class (or classes) of an image using a trained deep learning model. ''' image = h.process_image(image_path) # add another dimension as PyTorch expects to see the batch size image = torch.from_numpy(np.expand_dims(image, axis=0)) # prepare model and data image = image.to(device) model.to(device) model.eval() # make prediction and output probs and classes prediction = model.forward(image) topk = prediction.topk(topk) log_probs, classes = topk[0], topk[1] # take exponent of log probs to give probs probs = torch.exp(log_probs) return (probs, classes)
def predict(image_path, checkpoint_path="checkpoint.pth", top_k=1, category_names=None, device=None): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' if device: device = 'cuda' else: device = 'cpu' with open(category_names, 'r') as f: cat_to_name = json.load(f) model = flower_model() model.load(checkpoint_path) model.model.to(device) # Processing image and setting to GPU if active img = process_image(image_path) img = img.to(device) #img = img.float() # Running a forward step with the model to classify the image model.model.eval() with torch.no_grad(): logps = model.model.forward(img) ps = torch.exp(logps) # .topk finds the highest probabilities classes of the model outputs top_p, top_class = ps.topk(top_k) top_class = np.array(top_class.reshape(-1)) top_p = np.array(top_p.reshape(-1)) class_name = [] for k in top_class: for i in model.model.class_to_idx.keys(): if model.model.class_to_idx[str(i)] == k: class_name.append(cat_to_name[i]) return top_p, class_name
help='class_to_name json file') parser.add_argument('--device', type=str, default='cuda', help='GPU or CPU') arguments = parser.parse_args() # Load in a mapping from category label to category name class_to_name_map = utility_functions.load_json(arguments.json) # Load pretrained network model = model_functions.get_checkpoint(arguments.checkpoint) print(model) checkpoint = torch.load(arguments.checkpoint) # Scales, crops, and normalizes a PIL image for the PyTorch model; returns a Numpy array image = utility_functions.process_image(arguments.image_path) # Display image processing_functions.imshow(image) # Highest k probabilities and the indices of those probabilities corresponding to the classes (converted to the actual class labels) probabilities, classes = model_functions.predict(arguments.image_path, model, arguments.topk, arguments.device) print(probabilities) print(classes) # Display the image along with the top 5 classes processing_functions.display_image(arguments.image_path, class_to_name_map, classes)
# open the json file with the flower classes with open(cat_file, 'r') as f: cat_to_name = json.load(f) # get the specified model model = getattr(models, arch)(pretrained=True) # load our checkpoint print('Loading checkpoint...\n') model = load_checkpoint(model, device, checkpoint_path) print('Checkpoint loaded!\n') # process the image print('Processing image...\n') proc_image = process_image(input_path) # run the prediction print('Running prediction...\n') probs, classes = predict(proc_image, model, top_k, device) # get the flower names flowers = [cat_to_name[i] for i in classes] # print the top_k number of flowers and probabilities print('\nThe top ' + str(top_k) + ' classes and probabilities are: ') for i in range(top_k): idx = i + 1 print( str(idx) + '. ' + str(flowers[i]) + ' || ' + str(probs[i] * 100) + '%')
results = parser.parse_args() img_path = results.img_path checkpoint_path = results.checkpoint_path top_k = results.top_k category_names = results.category_names gpu = results.switch if gpu == True: using_gpu = torch.cuda.is_available() device = 'gpu' print('gpu On') else: print('gpu Off') device = 'cpu' model = utility_functions.load_checkpoint(checkpoint_path) processed_image = utility_functions.process_image(img_path) probs, classes = utility_functions.predict(processed_image, model, top_k, device) # Label mapping cat_to_name = utility_functions.labeling(category_names) labels = [] for class_index in classes: labels.append(cat_to_name[str(class_index)]) # Converting from tensor to numpy-array print('Name of class: ', labels) print('Probability: ', probs)
parser.add_argument('image', action='store') parser.add_argument('checkpoint', action='store') parser.add_argument('--cat_names', action='store', default='cat_to_name.json') parser.add_argument('--gpu', action='store', type=bool, default=True) parser.add_argument('--top_k', action='store', type=int, default=5) args = parser.parse_args() ##### PREDICTION ### Load model checkpoint and dictionary #train_dataset, trainloader = load_train('flowers/train') model, epochs, learn, optim_state, optimizer, criterion, classes_d = load_checkpoint( args.checkpoint) label_dict = get_labels(args.cat_names) #model.class_to_idx = train_dataset.class_to_idx ### Configure device to use - gpu or cpu device = choose_device(args.gpu) ### Load and process image, run forward propagation to obtain probabilities and classes for top_k predictions image = process_image(args.image) probabilities, classes = predict(image, model, device, classes_d, k=args.top_k) ### Print result print('The top {} predictions of the model are the following:'.format( args.top_k)) for p, c, r in zip(probabilities, classes, range(1, args.top_k + 1)): print('{}. {}, p = {}'.format(r, label_dict[str(c)], round(p, 3)))
def predict_one_image(image_filepath, model, topk, cat_to_name): ''' Predict flower species. To be honest, I struggled with this part and got stuck so badly, that I had to take a peek at waht other students had submitted The code inn this cell is adapted from: ttps://github.com/S-Tabor/udacity-image-classifier-project/blob/master/Image%20Classifier%20Project.ipynb by S-Tabor I didn't copy paste the code, but I got the general idea for implementation. I made sure to avoid plagiarism https://udacity.zendesk.com/hc/en-us/articles/360001451091-What-is-plagiarism- Not Plagiarism: Looking at someone else’s code to get a general idea of implementation, then putting it away and starting to write your own code from scratch. ''' # process the image img = process_image(image_filepath) # Converting to torch tensor from Numpy array #https://discuss.pytorch.org/t/how-to-convert-array-to-tensor/28809 #https://discuss.pytorch.org/t/difference-between-tensor-and-torch-floattensor/24448/2 img_tensor = torch.from_numpy(img).type(torch.FloatTensor) # Adding a dimension to image #https://discuss.pytorch.org/t/what-is-the-difference-between-view-and-unsqueeze/1155 img_add_dim = img_tensor.unsqueeze_(0) # Setting model to evaluation mode and turning off gradients to speed up the following step model.eval() with torch.no_grad(): # Running image through network try: output = model(img_add_dim) except: output = model(img_add_dim.cuda()) # probabilities probabilities = torch.exp(output) # print(probabilities) top_probabilities = probabilities.topk(topk)[0] top_indexes = probabilities.topk(topk)[1] # Converting probabilities and outputs to numpy arrays probs_top_list = np.array(top_probabilities)[0] # print(type(probs_top_list)) index_top_list = np.array(top_indexes[0]) # Loading index and class mapping class_to_idx = model.class_to_idx # Inverting index-class dictionary #https://stackoverflow.com/questions/483666/reverse-invert-a-dictionary-mapping indx_to_class = {A: B for B, A in class_to_idx.items()} # Converting index list to class list classes_top_list = [] for index in index_top_list: classes_top_list += [indx_to_class[index]] ############################# class_names = [] for i in classes_top_list: class_names += [cat_to_name[i]] df = pd.DataFrame( {'classes_top_list': classes_top_list, 'top_class_names': class_names, 'probs_top_list': probs_top_list }) print() print(image_filepath) # extract folder number from jpeg filepath and then get actual class name from it # https://stackoverflow.com/questions/5041008/how-to-find-elements-by-class # https://stackoverflow.com/questions/25353652/regular-expressions-extract-text-between-two-markers # https://stackoverflow.com/questions/7167279/regex-select-all-text-between-tags pattern = '\/test\/(.*?)\/image_' folder_number = re.findall(pattern, image_filepath) folder_number = folder_number[0] actual_class = cat_to_name[folder_number] print() print('Actual class') print(actual_class) # print('top probs', probs_top_list) # print('top_classes',classes_top_list) print() print('Prediction') print (df.to_string(index=False)) return probs_top_list, classes_top_list
image = results.image_path top_k = results.topk gpu_mode = results.gpu cat_names = results.cat_name_dir with open(cat_names, 'r') as f: cat_to_name = json.load(f) # Establish model template pre_tr_model = results.pretrained_model model = getattr(models, pre_tr_model)(pretrained=True) # Load model loaded_model = load_checkpoint(model, save_dir, gpu_mode) # Preprocess image - assumes jpeg format processed_image = process_image(image) if gpu_mode == True: processed_image = processed_image.to('cuda') else: pass # to perform prediction probs, classes = predict(processed_image, loaded_model, top_k, gpu_mode) # to print probabilities and predicted classes print(probs) print(classes) names = [] for i in classes:
def main(): # def predict( class_dict, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. Arguments: - image_path (str - path to image to process) - model (trained neural network model) - class_dict (class_to_idx dictionary from checkpoint) - topk (int - number of top classes/probabilities to calculate) Output: - probs (list - top 5 predicted probabilities) - classes (list - top 5 predicted classes) ''' topk = 5 # Topk probabilities/classes # Get command line arguments in_arg = get_input_args() # Get image path for testing image_path = in_arg.img_path checkpoint = in_arg.chk_pt # Switch to "cpu" mode device = torch.device("cpu") # Load the checkpoint model = model_functions.load_checkpoint(checkpoint) model.to(device) model.eval() # Run image through the model with torch.no_grad(): # Process image image = utility_functions.process_image(image_path) image = image.unsqueeze(0) image.to(device) # Run through model log_pbs = model.forward(image) ps = torch.exp(log_pbs) top_p, top_c = ps.topk(topk, dim=1, largest=True, sorted=True) probs = top_p.tolist()[0] classes = top_c.tolist()[0] # Print out the top 5 classes and corresponding flower names # Invert the class dictionary class_dict_invert = dict([[v, k] for k, v in model.class_to_idx.items()]) # Load the category to name json file cat_to_name = utility_functions.import_cat_to_names(in_arg.cat_names) flower_names = [] class_num = [] for c in classes: class_num.append(class_dict_invert[c]) flower_names.append(cat_to_name[class_dict_invert[c]]) # Print functions ------------ print("/n") print("Image: {}".format(image_path)) print("Top Flower Name: {}".format(flower_names[0].title())) print("Top Probability: {:.3f}".format(probs[0])) print("---------------------") print("Top 5 Class:Probabilities") print("{}: {:.3f}".format(flower_names[0].title(), probs[0])) print("{}: {:.3f}".format(flower_names[1].title(), probs[1])) print("{}: {:.3f}".format(flower_names[2].title(), probs[2])) print("{}: {:.3f}".format(flower_names[3].title(), probs[3])) print("{}: {:.3f}".format(flower_names[4].title(), probs[4])) print("/n")
parser.add_argument("image_path") parser.add_argument("checkpoint_path") parser.add_argument("--top_k", action="store", default=2, type=int) parser.add_argument("--category_names", action="store", default='cat_to_name.json', type=str) parser.add_argument("--gpu", action="store_true") args = parser.parse_args() top_p, class_name = predict(args.image_path, checkpoint_path=args.checkpoint_path, top_k=args.top_k, category_names=args.category_names, device=args.gpu) print('Classes : {}'.format(class_name)) print('Probabilities: {}'.format(top_p)) print('Figure saved to Prediction.png') im = process_image(args.image_path) fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1) imshow(im, ax=ax1, title=class_name[0]) ax2.barh([i + 1 for i in range(len(class_name))][::-1], width=top_p, tick_label=class_name) plt.tight_layout() ax2.set_aspect(0.06) fig.set_size_inches(6, 6) fig.savefig('Prediction.png') plt.show()