def predict(image_path, model, use_gpu, topk=5): model.eval() image = Image.open(image_path) np_array = utility.process_image(image) tensor = torch.from_numpy(np_array) if use_gpu: var_inputs = Variable(tensor.float().cuda(), volatile=True) else: var_inputs = Variable(tensor, volatile=True) var_inputs = var_inputs.unsqueeze(0) output = model.forward(var_inputs) ps = torch.exp(output).data.topk(topk) probs = ps[0].cpu() if use_gpu else ps[0] classes = ps[1].cpu() if use_gpu else ps[1] inverted_class_to_idx = { model.class_to_idx[k]: k for k in model.class_to_idx } mapped_classes = list() for label in classes.numpy()[0]: mapped_classes.append(inverted_class_to_idx[label]) return probs.numpy()[0], mapped_classes
def telemetry(sid, data): if data: # The current steering angle of the car steering_angle = data["steering_angle"] # The current throttle of the car throttle = data["throttle"] # The current speed of the car speed = data["speed"] # The current image from the center camera of the car imgString = data["image"] image = Image.open(BytesIO(base64.b64decode(imgString))) image_array = np.asarray(image) image_array = process_image(image_array, isRGB=True) steering_angle = float( model.predict(image_array[None, :, :, :], batch_size=1)) throttle = controller.update(float(speed)) print(steering_angle) send_control(steering_angle, throttle) # save frame if args.image_folder != '': timestamp = datetime.utcnow().strftime('%Y_%m_%d_%H_%M_%S_%f')[:-3] image_filename = os.path.join(args.image_folder, timestamp) image.save('{}.jpg'.format(image_filename)) else: # NOTE: DON'T EDIT THIS. sio.emit('manual', data={}, skip_sid=True)
def predict( image, checkpoint = "", top_k = 1, gpu = False,\ category_names = 'cat_to_name.json' ): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' # Implement the code to predict the class from an image file if checkpoint == "": print ("Check_point save path is not selected.") return else: try: predict_model = load_checkpoint( checkpoint ) except: print ("Error occurs when loading model!") return device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") if gpu: if device.type != 'cuda': print ( "Please enable GPU mode." ) return predict_model.to( device ) predict_model.eval() flower_image = Image.open( image ) image_processed = process_image( flower_image ) image_processed = torch.from_numpy(image_processed) image_processed.resize_((1, 3, 224, 224)) image_processed = image_processed.to(device).float() cat_to_name = load_cat_to_name( category_names ) if cat_to_name is None: print ("Cannot find category_names file!") return with torch.no_grad(): outputs = predict_model.forward(image_processed) probs, indexes = torch.topk(outputs.data, top_k) probs = np.exp(probs).numpy().ravel() idx_to_class = { value:key for key,value in predict_model.class_to_idx.items() } classes = [idx_to_class[int(idx)] for idx in list(indexes.cpu().numpy().ravel())] flowers = [ cat_to_name[class_idx] for class_idx in classes ] for index in range(0, top_k) : print ( "flower name:{} probability:{}".format( flowers[index], probs[index] )) return probs, flowers
def predict(image_path, model, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' print("Inside Predict : START") model.eval() img = utility.process_image(image_path) img = torch.from_numpy(img) img = img.unsqueeze_(0).float() with torch.no_grad(): output = model.forward(img) ps = torch.exp(output) probs, indices = torch.topk(ps, topk) print("Inside Predict : END") return probs, indices, img
def predict_helper(image_path, model, topk=5): dev = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.cuda() model.eval() image = u.process_image(image_path) image = torch.from_numpy(np.array([image])).float() image = image.to(dev) logps = model.forward(image) ps = torch.exp(logps) top_p, top_class = ps.topk(topk, dim=1) p = top_p[:topk, :].tolist()[0] c = top_class[:topk, :].tolist()[0] return p, c
def predict(image_path, model, cat_to_name, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' model.eval() #process images image = process_image(image_path) log_ps = model(image.view(1, 3, 224, 224)) ps = torch.exp(log_ps) probs, idx = ps.topk(topk, dim=1) probs = probs.detach().numpy().flatten() idx = idx.detach().numpy().flatten() classes = [dict(map(reversed, model.class_to_idx.items()))[i] for i in idx] class_name = [cat_to_name[str(x)] for x in classes] return probs, class_name
def predict(image_path, model, category_names, device, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' # Process given image processed_image_np = process_image(image_path) # Convert numpy array into a Torch tensor if device == torch.device('cuda'): image_tensor_type = torch.cuda.FloatTensor else: image_tensor_type = torch.FloatTensor processed_image_tensor = torch.from_numpy(processed_image_np).type( image_tensor_type) # Indicate a batch size of 1, shape of tensor is now (batch_size, rgb, width, height) processed_image_tensor.unsqueeze_(0) # Get ouput from model model.to(device) output = model(processed_image_tensor) # Get top k probabilities and indices, convert PyTorch autograd.Variable to numpy array top_k_probs, top_k_indices = torch.topk(input=(F.softmax(output, dim=1)), k=topk, sorted=True) top_k_probs = [probability.item() for probability in top_k_probs[0].data] # Invert class_to_idx dictionary idx_to_class = { idx: class_name for class_name, idx in model.class_to_idx.items() } # Get class_to_label dictionary with open(category_names, 'r') as f: class_to_label = json.load(f) # Get top k labels top_k_classes = [idx_to_class[index.item()] for index in top_k_indices[0]] top_k_labels = [class_to_label[index] for index in top_k_classes] return top_k_probs, top_k_labels
def predict(image_path, model, topk=5, gpu='gpu'): ''' 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 if gpu == 'gpu' and torch.cuda.is_available(): model = model.to('cuda') else: model = model.to('cpu') image = process_image(image_path) image = image.unsqueeze_(0) with torch.no_grad(): if gpu == 'gpu' and torch.cuda.is_available(): image = image.to('cuda') else: image = image.to('cpu') #from mentor's advice model.eval() log_ps = model.forward(image) ps = torch.exp(log_ps) probabilities, indices = ps.topk(topk) if gpu == 'gpu' and torch.cuda.is_available(): probabilities, indices = probabilities.to('cuda'), indices.to( 'cuda') else: probabilities, indices = probabilities.to('cpu'), indices.to('cpu') idx_to_class = { value: key for key, value in model.class_to_idx.items() } classes = [] indices = indices.cpu().numpy() probabilities = probabilities.cpu().numpy() for index in indices[0]: classes.append(idx_to_class[index]) return probabilities[0], classes
def predict(image_path, model, use_gpu, topk=5): ''' Predict the class (or classes) of an image using a previously trained deep learning model. ''' # Put model in inference mode model.eval() image = Image.open(image_path) np_array = utility.process_image(image) tensor = torch.from_numpy(np_array) # Use GPU if available if use_gpu: var_inputs = Variable(tensor.float().cuda(), volatile=True) else: var_inputs = Variable(tensor, volatile=True).float() # Model is expecting 4d tensor, add another dimension var_inputs = var_inputs.unsqueeze(0) # Run image through model output = model.forward(var_inputs) # Model's output is log-softmax, # take exponential to get the probabilities ps = torch.exp(output).data.topk(topk) # Move results to CPU if needed probs = ps[0].cpu() if use_gpu else ps[0] classes = ps[1].cpu() if use_gpu else ps[1] # Map classes to indices inverted_class_to_idx = { model.class_to_idx[k]: k for k in model.class_to_idx } mapped_classes = list() for label in classes.numpy()[0]: mapped_classes.append(inverted_class_to_idx[label]) # Return results return probs.numpy()[0], mapped_classes
def predict(self, image_path, topk=5, plot_predictions=False): """ Predict the class (or classes) of an image using a trained deep learning model. param: image_path: path to the image to predict param: topk(int): number of top classes to predict """ # Preprocess the image before passing it to the model preproccessed_img = process_image(image_path) preproccessed_img = preproccessed_img.unsqueeze(0) # move imag to GPU if CUDA is available preproccessed_img = preproccessed_img.to(self.device) # Use VGG16 to predict the class of the image self.model.eval() pred = F.softmax(self.model.forward(preproccessed_img), dim=1) prob, idx = pred.topk(topk) prob = np.squeeze(prob.cpu().detach().numpy()) idx = np.squeeze(idx.cpu().detach().numpy()) # Dictionary to map the indices to their class numbers idx_to_class = {self.class_to_idx[i]: i for i in self.class_to_idx} # Getting the class names for the top k predictions classes = [idx_to_class[i] for i in idx] if not self.cat_to_name is None: classes = [self.cat_to_name[c].title() for c in classes] if not plot_predictions: for p, c in zip(prob, classes): print(f'Class: {c:<25} Probability: {p:.2%}') elif not plot_predictions: for p, c in zip(prob, classes): print(f'Class: {c:<5} Probability: {p:.2%}') if plot_predictions: self.plot_predicted_classes(image_path, prob, classes) return prob, classes
def main(): print('Predict') in_arg = get_input_args() print("Command Line Arguments:\n input =", in_arg.input, "\n checkpoint =", in_arg.checkpoint, "\n top_k =", in_arg.top_k, "\n category_names =", in_arg.category_names, "\n gpu =", in_arg.gpu) # Load checkpoint model, checkpoint = load_checkpoint(in_arg.checkpoint) # Load catagory mapping dictionary cat_to_name = category_mapping(in_arg.category_names) # Process the image to return a transposed_image transposed_image = process_image(in_arg.input) # Get the prediction for an image file. top_classes = predict(transposed_image, model, in_arg.top_k, cat_to_name, in_arg.gpu) # Print the chart with the top classes and probabilities. print(top_classes)
def predict(image_path, model, gpu_check, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' model.eval() image = Image.open(image_path) np_array = utility.process_image(image) tensor = torch.from_numpy(np_array) if gpu_check: var_inputs = Variable(tensor.float().cuda(), volatile=True) else: var_inputs = Variable(tensor, volatile=True) output = model.forward(var_inputs.unsqueeze(0)) ps = torch.exp(output).data.topk(topk) probs = ps[0].cpu() if gpu_check else ps[0] classes = ps[1].cpu() if gpu_check else ps[1] mapped_classes = list() for label in classes.numpy()[0]: mapped_classes.append(model.class_to_idx[label]) return probs.numpy()[0], mapped_classes
def predict(): args = utility.get_predict_inputs() image = utility.process_image(args.imagepath) model = utility.load_checkpoint(args.checkpoint) model.to(args.gpu) image.unsqueeze_(0) image = image.to(args.gpu) output = model.forward(image) ps = torch.exp(output) props, index = ps.topk(args.top_k) with torch.no_grad(): props, index = props.to('cpu')[0].detach().numpy(), index.to( 'cpu')[0].detach().numpy() idx_to_class = { idx: class_name for class_name, idx in model.class_to_idx.items() } cats = [] for i in index: cats.append(idx_to_class[i]) if args.category_names: cat_to_name = utility.label_mapping(args.category_names) names = [] for cat in cats: names.append(cat_to_name[cat]) print(*props) print(*names) else: print(*props) print(*cats)
def predict(image_path, model, topk=5): ''' Predict the class (or classes) of an image using a trained deep learning model. ''' img = utility.process_image(image_path) im_torch = torch.from_numpy(img) im_torch.unsqueeze_(0) im_torch.requires_grad_(False) if torch.cuda.is_available(): im_torch.cuda() else: im_torch.cpu() model.eval() with torch.no_grad(): output = model(im_torch) results = torch.exp(output).topk(topk) probs = results[0][0] classes = results[1][0] return probs, classes
args = parser.parse_args() img_path = args.input topk = args.topk power = args.gpu filepath = args.checkpoint trainloader, validloader, testloader, train_data = utility.load_data() model, class_to_idx = ai_model.load_checkpoint(filepath) # Label mapping with open('cat_to_name.json', 'r') as f: cat_to_name = json.load(f) pil_image = utility.process_image(img_path) result = ai_model.predict(pil_image, model, topk, power) probs = F.softmax(result[0].data, dim=1).cpu().numpy()[0] classes = result[1].data.cpu().numpy()[0] idx_to_class = {v: k for k, v in class_to_idx.items()} classes = [idx_to_class[x] for x in classes] print(probs) print(classes) labels = [cat_to_name[str(i)] for i in classes] i = 0 while i < topk: print("It's {} percent likely that the flower is {}.".format( probs[i], labels[i]))
parser.add_argument('--category_names', action='store', help='file which maps classes to names') parser.add_argument('--gpu', action='store_true', help='use gpu to infer classes') args = parser.parse_args() if args.gpu: device = torch.device("cuda" if torch.cuda.is_available() else "cpu") else: device = "cpu" model = utility.load_model(args.checkpoint, args.gpu).eval() #process image to pytensor using device img = utility.process_image(args.input).to(device) #see how the model classifies outputs = model(img) #take exponent to undo log_softmax and get prediction of model prob = torch.exp(outputs) #and get top k probabilities result = torch.topk(prob, args.top_k) #get topk from pytroch tensor to numpy top_probs = result[0][0].cpu().detach().numpy() #get index of top5 probabilities classes = result[1][0].cpu().numpy() if (args.category_names != None): classes = utility.get_class(classes, args.checkpoint, args.category_names) else: classes = utility.get_class(classes, args.checkpoint, None)
image = results.image_path top_k = results.topk gpu = results.gpu cat_names = results.cat_name_dir with open(cat_names, 'r') as f: cat_to_name = json.load(f) model = getattr(models,pt_model)(pretrained = True) # Load model loaded_model = load_model(model, save_dir, gpu) # Preprocess image processed_image = process_image(image) # Define top K likely classes with probabilities probs, classes = predict(processed_image, loaded_model, top_k, gpu) # Define names for Classes names = [cat_to_name[i] for i in classes] # Print out top K classes and probabilities print(f"Top {top_k} classes are: {classes}, with assocatied probabilities: {probs}") # Print out most likely output print(f"The most likely outcome is a: '{names[0]} ({round(probs[0]*100, 2)}%)'")
help='how many most probable classes to print out') parser.add_argument('--category_names', action='store', help='file which maps classes to names') parser.add_argument('--gpu', action='store_true', help='use gpu to infer classes') args = parser.parse_args() if args.gpu: device = torch.device("cuda" if torch.cuda.is_available() else "cpu") else: device = "cpu" model = utility.load_model(args.checkpoint, args.gpu).eval() img = utility.process_image(args.input).to( device) #process image to pytensor using device outputs = model(img) #see how our network classifies prob = torch.exp( outputs ) #get the exponents back #get prediction of model, take exponent to undo log_softmax result = torch.topk(prob, args.top_k) #top 5 probabilities top_probs = result[0][0].cpu().detach().numpy( ) #get top5 from pytroch tensor to numpy classes = result[1][0].cpu().numpy() #index of top5 probabilities if (args.category_names != None): classes = utility.get_class(classes, args.checkpoint, args.category_names) else: classes = utility.get_class(classes, args.checkpoint, None)
def predict(image_path, model, topk=5, cat_to_name = None, device = 'cpu', image_show = False, probs_show = False, barh_show = False): ''' 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 image = torch.tensor(process_image(Image.open(image_path))) image_input = image.resize(1, image.size()[0], image.size()[1], image.size()[2]).float() # TODO: Calculate the class probabilities (log_softmax) for img model.eval() model.to(device) image_input.to(device) class_to_idx = model.class_to_idx with torch.no_grad(): output = model.forward(image_input.to(device)) _, predicted_idx = torch.topk(output, topk, dim = 1) predicted_idx = predicted_idx.cpu().numpy()[0] ps = torch.exp(output) probs = ps.cpu().numpy()[0, predicted_idx] # Map the probility-ranked index to the class name classes, class_names = [], [] i = 0 for idx in predicted_idx: for class_id, class_idx in model.class_to_idx.items(): if idx == class_idx: i = i + 1 if cat_to_name is not None: class_name = cat_to_name.get(str(class_id)) class_names.append(class_name) else: class_names.append(class_id) classes.append(class_id) classes, class_names = np.array(classes), np.array(class_names) # Print the classnames, probilities of the top k classes if probs_show: for i in np.arange(len(class_names)): print('Rank {}: Class Name: {}, Probability: {} %'.format(i+1, class_names[i], f'{probs[i]* 100:.2f}')) # Plot the image and probabilities title = class_names[0] + ': ' + f'{probs[0]*100:.1f}' + '%' if (image_show is True) & (probs_show is False): ax = imshow(image.numpy()) ax.set_title(title) elif (image_show is True) & (barh_show is True): fig = plt.figure() ax1 = fig.add_subplot(121) ax1 = imshow(image.numpy(), ax = ax1) ax1.set_title(title) ax2 = fig.add_subplot(122) x = np.arange(topk) ax2.barh(x, probs, tick_label = class_names) ax2.yaxis.tick_right() ax2.figure.set_size_inches(10, 5) ax2.set_yticklabels(class_names, fontsize=12) # return a list of top k class names and their probabilities return probs, classes, class_names