File size: 2,413 Bytes
99a17fc
84e88de
 
87002d0
 
 
1bf8957
6fe17c3
 
7263300
99a17fc
43e9504
ae8128b
c8e3791
50a42b9
 
 
43e9504
c8e3791
 
 
 
84e88de
0c65a85
 
df4e1ca
43e9504
84e88de
ae8128b
50a42b9
c8e3791
 
84e88de
 
 
0c65a85
43e9504
c8e3791
87002d0
43e9504
 
9d153e6
c8e3791
 
 
 
43e9504
 
c8e3791
 
 
ae8128b
43e9504
c8e3791
43e9504
ae8128b
c8e3791
0c65a85
c8e3791
ae8128b
43e9504
0c65a85
87002d0
0c65a85
84e88de
50a42b9
 
 
 
 
 
 
 
 
 
43e9504
0c65a85
 
43e9504
84e88de
 
 
 
43e9504
84e88de
 
0c65a85
43e9504
87002d0
c8e3791
87002d0
43e9504
9d153e6
87002d0
 
 
 
 
 
 
84e88de
 
c8e3791
87002d0
 
 
 
 
c8e3791
87002d0
 
 
 
 
c8e3791
 
43e9504
84e88de
87002d0
 
 
 
c8e3791
87002d0
 
 
43e9504
c8e3791
87002d0
c8e3791
87002d0
c8e3791
 
87002d0
43e9504
87002d0
c8e3791
ae8128b
c8e3791
43e9504
87002d0
84e88de
ae8128b
c8e3791
ae8128b
c8e3791
 
 
ae8128b
c8e3791
 
87002d0
 
 
 
43e9504
84e88de
 
 
43e9504
c8e3791
87002d0
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import sys

from fastapi import FastAPI, File, UploadFile, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates

import torch
from torchvision import transforms
from PIL import Image


# ---------------- PATH SETUP ----------------

script_dir = os.path.dirname(
    os.path.abspath(__file__)
)

root_dir = os.path.dirname(script_dir)

if root_dir not in sys.path:
    sys.path.append(root_dir)


from model.alzheimers_model import AlzheimerNet



# ---------------- MODEL LOAD ----------------

model_path = os.path.join(
    root_dir,
    "saved_models",
    "alzheimer_model.pth"
)


if not os.path.exists(model_path):
    raise FileNotFoundError(model_path)



model = AlzheimerNet(
    num_classes=4,
    sophisticated=False
)


model.load_state_dict(
    torch.load(
        model_path,
        map_location="cpu"
    )
)

model.eval()



# ---------------- TRANSFORM ----------------

transform = transforms.Compose([
    transforms.Resize((224,224)),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[
            0.485,
            0.456,
            0.406
        ],
        std=[
            0.229,
            0.224,
            0.225
        ]
    )
])


CLASSES = [
    "nondemented",
    "very mild",
    "mild demented",
    "moderate demented"
]



# ---------------- FASTAPI ----------------

app = FastAPI()



templates = Jinja2Templates(
    directory=os.path.join(
        os.path.dirname(__file__),
        "templates"
    )
)



@app.get(
    "/",
    response_class=HTMLResponse
)
async def home(request: Request):

    return templates.TemplateResponse(
        "index.html",
        {
            "request": request
        }
    )



@app.post("/predict")
async def predict(
    file: UploadFile = File(...)
):

    image = Image.open(
        file.file
    ).convert("RGB")


    tensor = transform(image)

    tensor = tensor.unsqueeze(0)


    with torch.no_grad():

        output = model(tensor)

        probs = torch.nn.functional.softmax(
            output,
            dim=1
        )[0]


    prediction = torch.argmax(
        probs
    ).item()



    confidence = {
        CLASSES[i]:
        round(
            float(probs[i])*100,
            2
        )

        for i in range(len(CLASSES))
    }



    return {
        "prediction": CLASSES[prediction],
        "confidence": confidence
    }