Upload 3 files
Browse filesupload file model
- .gitattributes +1 -0
- Trained_after_EFF0.keras +3 -0
- main.py +78 -0
- requirements.txt +7 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
Trained_after_EFF0.keras filter=lfs diff=lfs merge=lfs -text
|
Trained_after_EFF0.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:95c1692e95d27ee9f3893619bdf784df523f35d13d57e440151d164a7a782776
|
3 |
+
size 39075502
|
main.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
4 |
+
from fastapi.encoders import jsonable_encoder
|
5 |
+
from tensorflow.keras.models import load_model
|
6 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
import tensorflow.keras.backend as K
|
10 |
+
import os
|
11 |
+
import uvicorn
|
12 |
+
import numpy as np
|
13 |
+
|
14 |
+
# Initialize FastAPI app
|
15 |
+
app = FastAPI()
|
16 |
+
origins = ['*']
|
17 |
+
|
18 |
+
app.add_middleware(
|
19 |
+
CORSMiddleware,
|
20 |
+
allow_origins=origins,
|
21 |
+
allow_credentials=True,
|
22 |
+
allow_methods=["*"],
|
23 |
+
allow_headers=["*"],
|
24 |
+
)
|
25 |
+
|
26 |
+
# Custom F1 score function
|
27 |
+
def f1_score(y_true, y_pred):
|
28 |
+
precision = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) / K.maximum(
|
29 |
+
K.sum(K.round(K.clip(y_pred, 0, 1))), K.epsilon()
|
30 |
+
)
|
31 |
+
recall = K.sum(K.round(K.clip(y_true * y_pred, 0, 1))) / K.maximum(
|
32 |
+
K.sum(K.round(K.clip(y_true, 0, 1))), K.epsilon()
|
33 |
+
)
|
34 |
+
return 2 * (precision * recall) / (precision + recall + K.epsilon())
|
35 |
+
|
36 |
+
# Load model
|
37 |
+
MODEL_PATH = "Trained_after_EFF0.keras" # Ensure the path is correct
|
38 |
+
model = load_model(MODEL_PATH, custom_objects={'f1_score': f1_score})
|
39 |
+
|
40 |
+
# Image size for the model
|
41 |
+
IMAGE_SIZE = 224
|
42 |
+
|
43 |
+
# Preprocess image
|
44 |
+
def preprocess_image(image_path, target_size):
|
45 |
+
image = load_img(image_path, target_size=(target_size, target_size))
|
46 |
+
image_array = img_to_array(image)
|
47 |
+
image_array = np.expand_dims(image_array, axis=0)
|
48 |
+
return image_array
|
49 |
+
|
50 |
+
# API to predict image
|
51 |
+
@app.post("/predict")
|
52 |
+
async def predict_image(file: UploadFile = File(...)):
|
53 |
+
try:
|
54 |
+
upload_dir = "./uploads"
|
55 |
+
os.makedirs(upload_dir, exist_ok=True)
|
56 |
+
file_path = os.path.join(upload_dir, file.filename)
|
57 |
+
with open(file_path, "wb") as buffer:
|
58 |
+
buffer.write(await file.read())
|
59 |
+
|
60 |
+
image_array = preprocess_image(file_path, target_size=IMAGE_SIZE)
|
61 |
+
|
62 |
+
prediction = model.predict(image_array)
|
63 |
+
predicted_label = int(np.argmax(prediction))
|
64 |
+
confidence = float(np.max(prediction))
|
65 |
+
|
66 |
+
os.remove(file_path)
|
67 |
+
|
68 |
+
return JSONResponse(
|
69 |
+
content=jsonable_encoder(
|
70 |
+
{"predicted_label": predicted_label, "confidence": confidence}
|
71 |
+
)
|
72 |
+
)
|
73 |
+
except Exception as e:
|
74 |
+
return JSONResponse(content=jsonable_encoder({"error": str(e)}), status_code=500)
|
75 |
+
|
76 |
+
# Run FastAPI server
|
77 |
+
if __name__ == '__main__':
|
78 |
+
uvicorn.run(app, host="0.0.0.0", port=8002)
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
pyngrok
|
4 |
+
tensorflow
|
5 |
+
keras
|
6 |
+
python-multipart
|
7 |
+
Pillow
|