Spaces:
Sleeping
Sleeping
Application Code Added
Browse files- Dockerfile +11 -0
- alzheimer_weight.pth +3 -0
- app.py +24 -0
- requirements.txt +9 -0
- utils.py +24 -0
Dockerfile
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . /code
|
10 |
+
|
11 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
alzheimer_weight.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:854d74d38ba0ba6d4adb8085063cea702f96ab4a76171a759137d4e467cfc0c4
|
3 |
+
size 16351531
|
app.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from PIL import Image
|
3 |
+
import cv2
|
4 |
+
from fastapi import FastAPI, UploadFile, File
|
5 |
+
from utils import classify_img,get_alzheimer_model
|
6 |
+
import json
|
7 |
+
from io import BytesIO
|
8 |
+
import torch
|
9 |
+
import torchvision
|
10 |
+
|
11 |
+
app=FastAPI(title="Alzheimer Detection API")
|
12 |
+
|
13 |
+
@app.get("/")
|
14 |
+
def message():
|
15 |
+
return "Welcome"
|
16 |
+
|
17 |
+
@app.post("/predict")
|
18 |
+
def predict(file: UploadFile):
|
19 |
+
img=Image.open(file.file).convert("RGB")
|
20 |
+
img=img.resize(480,480)
|
21 |
+
img=np.array(img)
|
22 |
+
model= get_alzheimer_model()
|
23 |
+
label,probability=classify_img(model,img)
|
24 |
+
return {"label":label.item(),"probability":probability.item()}
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy==1.25.2
|
2 |
+
torch==2.0.1
|
3 |
+
fastapi==0.104.1
|
4 |
+
uvicorn==0.24.0.post1
|
5 |
+
python-multipart==0.0.6
|
6 |
+
torchvision==0.15.2
|
7 |
+
opencv-python-headless==4.5.4.60
|
8 |
+
numpy==1.25.2
|
9 |
+
streamlit==1.26.0
|
utils.py
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
from torchvision.models import efficientnet_b0
|
5 |
+
import torchvision.transforms.functional as tf
|
6 |
+
|
7 |
+
def classify_img(model,img):
|
8 |
+
img=tf.to_tensor(img)
|
9 |
+
img=img.unsqueeze(0)
|
10 |
+
with torch.no_grad():
|
11 |
+
predict=model(img)
|
12 |
+
predict=nn.functional.softmax(predict,1)
|
13 |
+
label=torch.argmax(predict)
|
14 |
+
probability=torch.max(predict)
|
15 |
+
return label,probability
|
16 |
+
|
17 |
+
def get_alzheimer_model():
|
18 |
+
model=efficientnet_b0(weights=None)
|
19 |
+
in_features=model.classifier[1].in_features
|
20 |
+
model.classifier[1]=nn.Linear(in_features=in_features,out_features=4)
|
21 |
+
weights=torch.load("alzheimer_weight.pth",map_location="cpu")
|
22 |
+
model.load_state_dict(weights)
|
23 |
+
model.eval()
|
24 |
+
return model
|