AIOmarRehan commited on
Commit
63b7af2
·
verified ·
1 Parent(s): 84651ff

Upload 3 files

Browse files
model.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ import numpy as np
3
+ from PIL import Image
4
+
5
+ # Load your trained CNN model
6
+ model = tf.keras.models.load_model("saved_model/Inception_V3_Animals_Classification.h5")
7
+
8
+ # Same label order you used when training (from LabelEncoder)
9
+ CLASS_NAMES = ["Cat", "Dog", "Snake"]
10
+
11
+ def preprocess_image(img: Image.Image, target_size=(256, 256)):
12
+ """
13
+ Preprocess a PIL image to match training pipeline:
14
+ - Convert to RGB
15
+ - Resize
16
+ - Convert to float32
17
+ - Normalize to [0,1]
18
+ - Add batch dimension
19
+ """
20
+ img = img.convert("RGB") # ensure 3 channels
21
+ img = img.resize(target_size)
22
+ img = np.array(img).astype("float32") / 255.0 # normalize
23
+ img = np.expand_dims(img, axis=0) # (1, 256, 256, 3)
24
+ return img
25
+
26
+ def predict(img: Image.Image):
27
+ # Apply preprocessing
28
+ input_tensor = preprocess_image(img)
29
+
30
+ # Model prediction
31
+ preds = model.predict(input_tensor)
32
+ probs = preds[0]
33
+
34
+ class_idx = int(np.argmax(probs))
35
+ confidence = float(np.max(probs))
36
+
37
+ # Map all probabilities
38
+ prob_dict = {CLASS_NAMES[i]: float(probs[i]) for i in range(len(CLASS_NAMES))}
39
+
40
+ return CLASS_NAMES[class_idx], confidence, prob_dict
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ tensorflow
4
+ numpy
5
+ python-multipart
6
+ pillow
saved_model/Inception_V3_Animals_Classification.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36279af47b3e49f968c806e51198d42428d155a4ab8bd9966581218ff2aa2252
3
+ size 142072584