Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(evaluate): type output #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Summit.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ Then you will receive 2 invitation to join the :
- Github MLOpsPython team.
- AzureML CatsDogsOthers project : https://ml.azure.com/home (you should see project name: Cats-Dogs)

## 0. Prerequisite

- Pycharm (https://www.jetbrains.com/pycharm/)
- Download and install python 3.11.x on your laptop.
https://www.python.org/downloads/

## 1. Introduction

[README.md](README.md)
Expand Down
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(switcher[prediction], 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()))
prediction = self.inference.execute(io.BytesIO(file.read()))
return dataclasses.asdict(prediction)
2 changes: 1 addition & 1 deletion step_0_setup.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Setup

## 1. Download ans install python 3.11.x on your laptop.
## 1. Download and install python 3.11.x on your laptop.
https://www.python.org/downloads/

It is a good practice to always use the lastest version with supports:
Expand Down
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