def handle_movie_review(movieReview): path = 'sgd/static/trained/natural-language-processor' learn = learner.load_learner(path) predictions = dict() predictions['raw'] = learn.predict(movieReview) predictions['originalReview'] = movieReview return predictions
def _classification(image_rgb, weights): """Helper function. Classifies the input image according to `weights`. Parameters ---------- image_rgb : 3D array RGB image of the lepidopteran (ruler and tags cropped out). weights : str or pathlib.Path Path of the file containing weights. Returns ------- prediction : int Prediction obtained with the given weights. Notes ----- If a string is given in `weights`, it will be converted into a pathlib.Path object. """ if isinstance(weights, str): weights = Path(weights) connection.download_weights(weights) # parameters here were defined when training the networks. learner = load_learner(fname=weights) _, prediction, _ = learner.predict(image_rgb) return int(prediction)
def handle_uploaded_file(image): imageLocation = save(image) image = open_image(imageLocation) path = 'sgd/static/trained/comp-vision' learn = learner.load_learner(path) predictions = dict() predictions['raw'] = learn.predict(image) predictions['friendly'] = getFriendlyProbabilities(predictions['raw']) return predictions
def predict(url): response = requests.get(url) img = Image.open(BytesIO(response.content)).resize((512, 384), Image.ANTIALIAS) filename = "./ml-model/data/img.jpg" img.save(filename) learn = load_learner("./ml-model/") category = str(learn.predict(open_image(filename))[0]) tensor_probs = learn.predict(open_image(filename))[2] pred_probs = { "cardboard": round(float(tensor_probs[0]), 2), "glass": round(float(tensor_probs[1]), 2), "metal": round(float(tensor_probs[2]), 2), "paper": round(float(tensor_probs[3]), 2), "plastic": round(float(tensor_probs[4]), 2), "trash": round(float(tensor_probs[5]), 2) } ans = {"url": url, "category": category, "pred_probs": pred_probs} return ans
def predicting_classes(image_rgb, weights=WEIGHTS_CLASSES): """Predicts position and gender of the lepidopteran in `image_rgb`, according to `weights`. Parameters ---------- image_rgb : 3D array RGB image of the lepidopteran (ruler and tags cropped out). weights : str or pathlib.Path Path of the file containing weights. Returns ------- prediction : string Prediction obtained with the given weights, between the classes `female`, `male`, or `upside_down`. probabilities : 1D array Probabilities for the prediction returned by the network for each class. Notes ----- If a string is given in `weights`, it will be converted into a pathlib.Path object. """ if isinstance(weights, str): weights = Path(weights) connection.download_weights(weights) # parameters here were defined when training the networks. learner = load_learner(fname=weights) prediction, _, probabilities = learner.predict(image_rgb) return prediction, probabilities
def binarization(image_rgb, weights=WEIGHTS_BIN): """Extract the shape of the elements in an input image using the U-net deep learning architecture. Parameters ---------- image_rgb : (M, N, 3) ndarray Input RGB image of a lepidopteran, with ruler and tags. weights : str or pathlib.Path Path of the file containing weights for segmentation. Returns ------- tags_bin : (M, N) ndarray Binary image containing tags in the input image. ruler_bin : (M, N) ndarray Binary image containing the ruler in the input image. lepidop_bin : (M, N) ndarray Binary image containing the lepidopteran in the input image. """ if isinstance(weights, str): weights = Path(weights) connection.download_weights(weights) learner = load_learner(fname=weights) print('Processing U-net...') _, _, classes = learner.predict(image_rgb) _, tags_bin, ruler_bin, lepidop_bin = classes[:4] # rescale the predicted images back up and binarize them. tags_bin = img_as_bool(_rescale_image(image_rgb, tags_bin)) ruler_bin = img_as_bool(_rescale_image(image_rgb, ruler_bin)) lepidop_bin = img_as_bool(_rescale_image(image_rgb, lepidop_bin)) return tags_bin, ruler_bin, lepidop_bin
from fastai.vision import open_image from fastai.vision.learner import load_learner import sys #defaults.device = torch.device('cpu') if (len(sys.argv) != 2): print("USAGE: classify.py <path to image>") sys.exit(1) img = open_image(sys.argv[1]) learn = load_learner(".", fname="its_raining_men.pkl") pred_class,pred_idx,outputs = learn.predict(img) print("p(ramen) = %1.2f, p(soba) = %1.2f, p(udon) = %1.2f. I think this is %s." % (outputs[0],outputs[1],outputs[2],pred_class))
# In[ ]: learn.save("/content/drive/My Drive/data/models/model3") # In[ ]: learn.export("/content/drive/My Drive/data/models/model3.pkl") # In[ ]: learn = load_learner("/content/drive/My Drive/data/models/") # In[ ]: learn.load("/content/drive/My Drive/data/models/model3") # In[49]: learn.predict(open_image("stack-of-paper.jpg")) # In[ ]: