utility_functions.py
def predict(image_path, model, gpu = 'off', topk=5):
    
    ''' 
    Predict the class (or classes) of an image using a trained deep learning model.
    Function returns the top k probabilities.
        
    Parameters:
     image_path        - full path to the image which should be classified by the model
     model             - a pre-trained deep learning model, hast to include image 
                         input size
     gpu               - GPU-mode "on" or "off"
     topk              - to classes to be returned by function  
      
    Returns:
     probability_top_k - variable with the top k probabilities infered by the model 
                         from the image  
    '''
    
    # Check if GPU mode is available or not
    device = check_gpu(gpu)         
        
    # Put model in evaluation mode instead of training mode (= default)
    model = model.eval()
    
    # Send model to available device (either 'gpu' or 'cuda')
    model.to(device)
    
    # Open the image from image path
    img = Image.open(image_path)
    
    # Pre-process the image (includes cropping to proper input size saved in model 
    # checkpoint data)
    img_torch = process_images(img, model.input_size)
    
    # Batch dimension: Add dim 0 to preprocessed image so it can be fed to the model
    img_torch = img_torch.unsqueeze(0)
    
    # Send image data to available device (see check above)      
    img_torch  = img_torch.to(device)
    
    # Turn off gradients to speed up the feed forward step
    with torch.no_grad():
        
        # Forward feed, returns log probabilities (last leyer of classifier = log Softmax)
        logps = model.forward(img_torch)
    
    # Get probabilities from log probabilities
    ps = torch.exp(logps)
    
    # Get the top k probabilities and their class indices
    top_p, top_class = ps.topk(topk)
    
    # Return top probabilities and indices
    return top_p, top_class
 
    Predict the class (or classes) of an image using a trained deep learning model.
    Function returns the top k probabilities.
        
    Parameters:
     image_path        - full path to the image which should be classified by the model
     model             - a pre-trained deep learning model, hast to include image 
                         input size
     gpu               - GPU-mode "on" or "off"
     topk              - to classes to be returned by function  
      
    Returns:
     probability_top_k - variable with the top k probabilities infered by the model 
                         from the image  
    
Predict the class (or classes) of an image using a trained deep learning model.
Function returns the top k probabilities.



variable with the top k probabilities infered by the model
from the image
Predict the class ( or classes ) of an image using a trained deep learning model . Function returns the top k probabilities . variable with the top k probabilities infered by the model from the image
def predict(image_path, model, gpu = 'off', topk=5):
    device = check_gpu(gpu)         
    model = model.eval()
    model.to(device)
    img = Image.open(image_path)
    img_torch = process_images(img, model.input_size)
    img_torch = img_torch.unsqueeze(0)
    img_torch  = img_torch.to(device)
    with torch.no_grad():
        logps = model.forward(img_torch)
    ps = torch.exp(logps)
    top_p, top_class = ps.topk(topk)
    return top_p, top_class
def predict ( image_path , model , gpu = 'off' , topk = 5 ) : device = check_gpu ( gpu ) model = model . eval ( ) model . to ( device ) img = Image . open ( image_path ) img_torch = process_images ( img , model . input_size ) img_torch = img_torch . unsqueeze ( 0 ) img_torch = img_torch . to ( device ) with torch . no_grad ( ) : logps = model . forward ( img_torch ) ps = torch . exp ( logps ) top_p , top_class = ps . topk ( topk ) return top_p , top_class
Predict the class (or classes) of an image using a trained deep learning model.
Predict the class ( or classes ) of an image using a trained deep learning model .
''' 
    Predict the class (or classes) of an image using a trained deep learning model.
    Function returns the top k probabilities.
        
    Parameters:
     image_path        - full path to the image which should be classified by the model
     model             - a pre-trained deep learning model, hast to include image 
                         input size
     gpu               - GPU-mode "on" or "off"
     topk              - to classes to be returned by function  
      
    Returns:
     probability_top_k - variable with the top k probabilities infered by the model 
                         from the image  
    ''' # Check if GPU mode is available or not # Put model in evaluation mode instead of training mode (= default) # Send model to available device (either 'gpu' or 'cuda') # Open the image from image path # Pre-process the image (includes cropping to proper input size saved in model  # checkpoint data) # Batch dimension: Add dim 0 to preprocessed image so it can be fed to the model # Send image data to available device (see check above)       # Turn off gradients to speed up the feed forward step # Forward feed, returns log probabilities (last leyer of classifier = log Softmax) # Get probabilities from log probabilities # Get the top k probabilities and their class indices # Return top probabilities and indices
image_path
model
gpu
topk