model_helper.py
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
 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 .
def predict(image_path, model, gpu_check,topk=5):
    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 ( image_path , model , gpu_check , topk = 5 ) : 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
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.
    '''
image_path
model
gpu_check
topk