Skip to content
This repository was archived by the owner on Apr 16, 2023. It is now read-only.

Commit 4d0a3dd

Browse files
author
Patrick Kuehn
committed
Remove object oriented programming from prediction
1 parent 251d239 commit 4d0a3dd

File tree

3 files changed

+49
-67
lines changed

3 files changed

+49
-67
lines changed

autofocus/predict/app/app.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
from flask import Flask, jsonify, request
44

55
from .models.File import File
6-
from .models.Predictor import Predictor
76
from .models.ZipArchive import ZipArchive
87
from .validation.predict import validate_predict_request
98
from .validation.predict_zip import validate_predict_zip_request
109
from .validation.validation import abort_with_errors
10+
from .prediction.prediction import predict, predict_multiple
1111

1212

1313
# We are going to upload the files to the server as part of the request, so set tmp folder here.
@@ -27,16 +27,8 @@ def classify_single():
2727
# Get File object
2828
file = File(request.files["file"], app.config["UPLOAD_FOLDER"])
2929

30-
# Predict probabilities
31-
app.logger.info("Classifying image %s" % (file.getPath()))
32-
t = time.time()
33-
predictor = Predictor()
34-
predictor.predict(file)
35-
dt = time.time() - t
36-
app.logger.info("Execution time: %0.2f" % (dt * 1000.0))
37-
3830
# Return ziped probabilities
39-
return jsonify(predictor.getProbabilities())
31+
return jsonify(predict(file))
4032

4133

4234
@app.route("/predict_zip", methods=["POST"])
@@ -56,8 +48,7 @@ def classify_zip():
5648
files = file.extractAll(app.config["UPLOAD_FOLDER"], file.listAllImages())
5749

5850
# Make prediction
59-
predictor = Predictor()
60-
return jsonify(predictor.predict_multiple(files))
51+
return jsonify(predict_multiple(files))
6152

6253

6354
@app.route("/hello")

autofocus/predict/app/models/Predictor.py

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from pathlib import Path
2+
3+
from fastai.vision import load_learner, open_image
4+
5+
6+
MODEL_DIR = Path(__file__).resolve().parents[2] / "models"
7+
MODEL_NAME = "multilabel_model_20190407.pkl"
8+
model = load_learner(MODEL_DIR, MODEL_NAME)
9+
CLASSES = model.data.classes
10+
11+
12+
def predict_multiple(files):
13+
"""
14+
Predict probabilities of multiple files
15+
16+
Parameters:
17+
files: Dict with File objects of image file
18+
19+
Returns:
20+
dict: Dictionary of probabilities for each file in files
21+
"""
22+
predictions = {}
23+
for key in files:
24+
predictions[key] = predict(files[key])
25+
return predictions
26+
27+
def predict(file):
28+
"""
29+
Predict probabilities of single file
30+
31+
Parameters:
32+
file: File object of image file
33+
"""
34+
image = open_image(file.getPath())
35+
# Get the predictions (output of the softmax) for this image
36+
pred_classes, preds, probs = model.predict(image)
37+
return getProbabilities([prob.item() for prob in probs])
38+
39+
def getProbabilities(probabilities):
40+
"""
41+
Return formated Probabilities
42+
43+
Returns:
44+
dict: A dictionary of classes to probabilities
45+
"""
46+
return dict(zip(CLASSES, probabilities))

0 commit comments

Comments
 (0)