Upload 3 files
Browse files- .dockerignore +24 -0
- app.py +10 -0
- cnnmodels.py +45 -0
.dockerignore
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Git
|
| 2 |
+
.git
|
| 3 |
+
.gitignore
|
| 4 |
+
|
| 5 |
+
# Python
|
| 6 |
+
__pycache__/
|
| 7 |
+
*.pyc
|
| 8 |
+
|
| 9 |
+
# Virtual env
|
| 10 |
+
venv/
|
| 11 |
+
|
| 12 |
+
BrainTumorDetectionModel/
|
| 13 |
+
.env
|
| 14 |
+
|
| 15 |
+
# ML artifacts (if regenerated)
|
| 16 |
+
logs/
|
| 17 |
+
checkpoints/
|
| 18 |
+
*.pt
|
| 19 |
+
*.pth
|
| 20 |
+
*.ckpt
|
| 21 |
+
|
| 22 |
+
# OS
|
| 23 |
+
.DS_Store
|
| 24 |
+
Thumbs.db
|
app.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from cnnmodels import classify_tumor
|
| 3 |
+
|
| 4 |
+
app = FastAPI(title="Brain Tumor CNN Service")
|
| 5 |
+
|
| 6 |
+
@app.post("/classify")
|
| 7 |
+
async def classify(file: UploadFile = File(...)):
|
| 8 |
+
image_bytes = await file.read()
|
| 9 |
+
result = classify_tumor(image_bytes)
|
| 10 |
+
return result
|
cnnmodels.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.layers import TFSMLayer
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
import io
|
| 5 |
+
|
| 6 |
+
CLASS_NAMES = ['glioma', 'meningioma', 'no tumor', 'pituitary']
|
| 7 |
+
|
| 8 |
+
# Load models ONCE
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
classification_model = TFSMLayer(
|
| 12 |
+
"BrainTumorClassificationModel/model",
|
| 13 |
+
call_endpoint="serving_default"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
def preprocess_image_bytes(image_bytes, target_size=(224, 224)):
|
| 17 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 18 |
+
image = image.resize(target_size)
|
| 19 |
+
image = np.array(image).astype("float32") / 255.0
|
| 20 |
+
image = np.expand_dims(image, axis=0)
|
| 21 |
+
return image
|
| 22 |
+
|
| 23 |
+
def classify_tumor(image_bytes):
|
| 24 |
+
# 1. Preprocess the image
|
| 25 |
+
image = preprocess_image_bytes(image_bytes)
|
| 26 |
+
|
| 27 |
+
# 2. Run Classification directly
|
| 28 |
+
# Removed the detection_model block entirely
|
| 29 |
+
class_pred_tensor = classification_model(image)["output_0"]
|
| 30 |
+
class_pred = class_pred_tensor.numpy()
|
| 31 |
+
|
| 32 |
+
# 3. Get the index of the highest confidence score
|
| 33 |
+
idx = int(np.argmax(class_pred))
|
| 34 |
+
confidence = float(np.max(class_pred) * 100)
|
| 35 |
+
tumor_type = CLASS_NAMES[idx]
|
| 36 |
+
|
| 37 |
+
# 4. Determine has_tumor status based on the classification result
|
| 38 |
+
# We assume "no tumor" is a string in your CLASS_NAMES list
|
| 39 |
+
is_tumor_detected = tumor_type.lower() != "no tumor"
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
"has_tumor": is_tumor_detected,
|
| 43 |
+
"tumor_type": tumor_type,
|
| 44 |
+
"confidence": confidence
|
| 45 |
+
}
|