apps/predict.py
def predict(image_path, model_path, topk=None, device=None):
    '''return the predictions from the model'''

    if topk is None:
        topk = 5
    if device is None:
        device = torch.device('cpu')

    with torch.no_grad():
        # get image
        img = preprocess(image_path)
        img.unsqueeze_(0)
        img.float()
        img = img.to(device)

        # get model
        _, model = load_model(model_path)
        model.to(device)

        outputs = model(img)
        probs, classes = torch.exp(outputs).topk(topk)
        probs, classes = probs[0].tolist(), classes[0].add(1).tolist()

        return zip(probs, classes)
return the predictions from the model
return the predictions from the model
return the predictions from the model
def predict(image_path, model_path, topk=None, device=None):
    if topk is None:
        topk = 5
    if device is None:
        device = torch.device('cpu')
    with torch.no_grad():
        img = preprocess(image_path)
        img.unsqueeze_(0)
        img.float()
        img = img.to(device)
        _, model = load_model(model_path)
        model.to(device)
        outputs = model(img)
        probs, classes = torch.exp(outputs).topk(topk)
        probs, classes = probs[0].tolist(), classes[0].add(1).tolist()
        return zip(probs, classes)
def predict ( image_path , model_path , topk = None , device = None ) : if topk is None : topk = 5 if device is None : device = torch . device ( 'cpu' ) with torch . no_grad ( ) : img = preprocess ( image_path ) img . unsqueeze_ ( 0 ) img . float ( ) img = img . to ( device ) _ , model = load_model ( model_path ) model . to ( device ) outputs = model ( img ) probs , classes = torch . exp ( outputs ) . topk ( topk ) probs , classes = probs [ 0 ] . tolist ( ) , classes [ 0 ] . add ( 1 ) . tolist ( ) return zip ( probs , classes )
return the predictions from the model
return the predictions from the model
'''return the predictions from the model''' # get image # get model
image_path
model_path
topk
device