Skip to content

Commit

Permalink
Merge aa02274 into 68ca285
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet authored May 25, 2023
2 parents 68ca285 + aa02274 commit 9716069
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
10 changes: 8 additions & 2 deletions packages/inference/src/mlopspython_inference/inference_pillow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from dataclasses import dataclass
from io import BytesIO
from typing import List

import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
Expand All @@ -22,16 +24,20 @@ def load_image(filename: str|BytesIO):

BASE_PATH = Path(__file__).resolve().parent

@dataclass
class InferenceOutput:
prediction: str
values: List[float]

class Inference:
def __init__(self, logging, model_path: str):
self.logger = logging.getLogger(__name__)
self.model = load_model(model_path)

def execute(self, filepath:str|BytesIO):
def execute(self, filepath:str|BytesIO) -> InferenceOutput:
img = load_image(filepath)
result = self.model.predict(img)
values = [float(result[0][0]), float(result[0][1]), float(result[0][2])]
switcher = ['Cat', 'Dog', 'Other']
prediction = np.argmax(result[0])
return {"prediction": switcher[prediction], "values": values}
return InferenceOutput(prediction=switcher[prediction], values=values)
8 changes: 5 additions & 3 deletions packages/inference/src/tests/inference_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
class TestInference(unittest.TestCase):

def test_inference(self):
# Arrange
inference = Inference(logging, str(input_directory / "model" / "final_model.h5"))
# Act
inference_result = inference.execute(str(input_directory / "images" / "cat.png"))

# Assert
expected_result = {'prediction': 'Cat', 'values': [1.0, 2.370240289845506e-30, 0.0]}
self.assertEqual(inference_result['prediction'], expected_result['prediction'])
self.assertEqual(len(inference_result['values']), len(expected_result['values']))
self.assertEqual(inference_result.prediction, expected_result['prediction'])
self.assertEqual(len(inference_result.values), len(expected_result['values']))


if __name__ == "__main__":
Expand Down
4 changes: 3 additions & 1 deletion production/api/core/model/inference_pillow.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dataclasses
import io
from pathlib import Path
from mlopspython_inference.inference_pillow import Inference as InferencePillow
Expand All @@ -11,4 +12,5 @@ def __init__(self, logging, app_settings):
self.inference = InferencePillow(logging, str(model_path))

def execute(self, file, filename, settings=None):
return self.inference.execute(io.BytesIO(file.read()))
result = self.inference.execute(io.BytesIO(file.read()))
return dataclasses.asdict(result)
4 changes: 2 additions & 2 deletions train/evaluate/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def evaluate(logging, input_model_directory: Path, input_images_directory: Path,
continue
model_result = model.execute(str(path))

prediction = model_result["prediction"]
prediction = model_result.prediction
prediction_truth = path.parent.name.lower().replace("s", "")
status = prediction_truth == prediction.lower()
statistics["ok" if status else "ko"] += 1
result = {"filename": path.name,
"ok": status,
"prediction": prediction,
"prediction_truth": prediction_truth,
"values": model_result["values"]}
"values": model_result.values}
results.append(result)
statistics["total"] = statistics["ok"] + statistics["ko"]

Expand Down

0 comments on commit 9716069

Please sign in to comment.