Spaces:
Sleeping
Sleeping
Update src/inference/lr_model.py
Browse files- src/inference/lr_model.py +63 -63
src/inference/lr_model.py
CHANGED
|
@@ -1,63 +1,63 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import joblib
|
| 3 |
-
import numpy as np
|
| 4 |
-
from PIL import Image
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
class LRModel:
|
| 8 |
-
"""
|
| 9 |
-
Inference pipeline for Logistic Regression model
|
| 10 |
-
trained on 64x64 grayscale flattened images.
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
def __init__(self, model_path: str, labels_path: str, image_size: int = 64):
|
| 14 |
-
self.model = joblib.load(model_path)
|
| 15 |
-
self.labels = self._load_labels(labels_path)
|
| 16 |
-
self.image_size = image_size
|
| 17 |
-
|
| 18 |
-
def _load_labels(self, labels_path):
|
| 19 |
-
with open(labels_path, "r") as f:
|
| 20 |
-
label_dict = json.load(f)
|
| 21 |
-
|
| 22 |
-
# Ensure keys are integer indices, not strings
|
| 23 |
-
label_dict = {int(k): v for k, v in label_dict.items()}
|
| 24 |
-
return label_dict
|
| 25 |
-
|
| 26 |
-
def preprocess(self, image: Image.Image) -> np.ndarray:
|
| 27 |
-
"""
|
| 28 |
-
Preprocessing matching training:
|
| 29 |
-
- Resize to 64x64
|
| 30 |
-
- Grayscale
|
| 31 |
-
- Normalize to [0,1]
|
| 32 |
-
- Flatten to (1, D)
|
| 33 |
-
"""
|
| 34 |
-
img = image.resize((self.image_size, self.image_size))
|
| 35 |
-
img = img.convert("L") # grayscale
|
| 36 |
-
arr = np.array(img, dtype=np.float32) / 255.0
|
| 37 |
-
arr = arr.reshape(1, -1) # shape: (1, D)
|
| 38 |
-
return arr
|
| 39 |
-
|
| 40 |
-
def predict(self, image: Image.Image):
|
| 41 |
-
"""
|
| 42 |
-
Returns:
|
| 43 |
-
{
|
| 44 |
-
"class_id": int,
|
| 45 |
-
"class_name": str,
|
| 46 |
-
"probabilities": {class_name: prob, ...}
|
| 47 |
-
}
|
| 48 |
-
"""
|
| 49 |
-
x = self.preprocess(image)
|
| 50 |
-
probs = self.model.predict_proba(x)[0]
|
| 51 |
-
class_id = int(np.argmax(probs))
|
| 52 |
-
class_name = self.labels[class_id]
|
| 53 |
-
|
| 54 |
-
# Build probability dict (optional)
|
| 55 |
-
prob_dict = {
|
| 56 |
-
self.labels[i]: float(probs[i]) for i in range(len(probs))
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
-
return {
|
| 60 |
-
"class_id": class_id,
|
| 61 |
-
"class_name": class_name,
|
| 62 |
-
"probabilities": prob_dict
|
| 63 |
-
}
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class LRModel:
|
| 8 |
+
"""
|
| 9 |
+
Inference pipeline for Logistic Regression model
|
| 10 |
+
trained on 64x64 grayscale flattened images.
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
def __init__(self, model_path: str, labels_path: str, image_size: int = 64):
|
| 14 |
+
self.model = joblib.load(model_path)
|
| 15 |
+
self.labels = self._load_labels(labels_path)
|
| 16 |
+
self.image_size = image_size
|
| 17 |
+
|
| 18 |
+
def _load_labels(self, labels_path):
|
| 19 |
+
with open(labels_path, "r") as f:
|
| 20 |
+
label_dict = json.load(f)
|
| 21 |
+
|
| 22 |
+
# Ensure keys are integer indices, not strings
|
| 23 |
+
label_dict = {int(k): v for k, v in label_dict.items()}
|
| 24 |
+
return label_dict
|
| 25 |
+
|
| 26 |
+
def preprocess(self, image: Image.Image) -> np.ndarray:
|
| 27 |
+
"""
|
| 28 |
+
Preprocessing matching training:
|
| 29 |
+
- Resize to 64x64
|
| 30 |
+
- Grayscale
|
| 31 |
+
- Normalize to [0,1]
|
| 32 |
+
- Flatten to (1, D)
|
| 33 |
+
"""
|
| 34 |
+
img = image.resize((self.image_size, self.image_size))
|
| 35 |
+
img = img.convert("L") # grayscale
|
| 36 |
+
arr = np.array(img, dtype=np.float32) / 255.0
|
| 37 |
+
arr = arr.reshape(1, -1) # shape: (1, D)
|
| 38 |
+
return arr
|
| 39 |
+
|
| 40 |
+
def predict(self, image: Image.Image, top_k: int = 5):
|
| 41 |
+
"""
|
| 42 |
+
Returns:
|
| 43 |
+
{
|
| 44 |
+
"class_id": int,
|
| 45 |
+
"class_name": str,
|
| 46 |
+
"probabilities": {class_name: prob, ...}
|
| 47 |
+
}
|
| 48 |
+
"""
|
| 49 |
+
x = self.preprocess(image)
|
| 50 |
+
probs = self.model.predict_proba(x)[0]
|
| 51 |
+
class_id = int(np.argmax(probs))
|
| 52 |
+
class_name = self.labels[class_id]
|
| 53 |
+
|
| 54 |
+
# Build probability dict (optional)
|
| 55 |
+
prob_dict = {
|
| 56 |
+
self.labels[i]: float(probs[i]) for i in range(len(probs))
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
return {
|
| 60 |
+
"class_id": class_id,
|
| 61 |
+
"class_name": class_name,
|
| 62 |
+
"probabilities": prob_dict
|
| 63 |
+
}
|