Mikecode123 commited on
Commit
3ecb854
·
verified ·
1 Parent(s): 7e25021

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
app.py CHANGED
@@ -11,7 +11,7 @@ import numpy as np
11
 
12
  app = FastAPI(
13
  title="NeuroHealth EEG API",
14
- version="9.0"
15
  )
16
 
17
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -31,7 +31,7 @@ class EEGRequest(BaseModel):
31
  features: list
32
 
33
  # =========================================================
34
- # AD MODEL (CNN - EXACTLY FROM CHECKPOINT STRUCTURE)
35
  # =========================================================
36
 
37
  class EEG_CNN_AD(nn.Module):
@@ -66,36 +66,45 @@ class EEG_CNN_AD(nn.Module):
66
  return self.fc(x)
67
 
68
  # =========================================================
69
- # PD MODEL (STRICT MATCH TO YOUR CHECKPOINT KEYS)
70
  # =========================================================
71
 
72
- class EEG_PD(nn.Module):
73
  def __init__(self):
74
  super().__init__()
75
 
76
- self.net = nn.Sequential(
77
- nn.Linear(76, 256), # net.0
78
- nn.ReLU(),
79
-
80
- nn.BatchNorm1d(256), # net.2
81
 
82
- nn.Linear(256, 128), # net.4
83
- nn.ReLU(),
84
 
85
- nn.BatchNorm1d(128), # net.6
 
86
 
87
- nn.Linear(128, 2) # net.7
88
- )
89
 
90
  def forward(self, x):
91
- return self.net(x)
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  # =========================================================
94
  # PATHS
95
  # =========================================================
96
 
97
  AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
98
- PD_MODEL_PATH = "PD_eeg_cnn.pth"
99
 
100
  # =========================================================
101
  # LOAD MODELS
@@ -108,7 +117,7 @@ ad_model.eval()
108
  print("AD model loaded")
109
 
110
  print("Loading PD model...")
111
- pd_model = EEG_PD().to(DEVICE)
112
  pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
113
  pd_model.eval()
114
  print("PD model loaded")
@@ -120,13 +129,14 @@ print("System ready")
120
  # =========================================================
121
 
122
  def prepare(features, model_type):
 
123
  x = torch.tensor(features, dtype=torch.float32)
124
 
125
  if model_type == "ad":
126
  return x.view(1, 19, 76)
127
 
128
  if model_type == "pd":
129
- return x.view(1, 76)
130
 
131
  raise ValueError("Invalid model type")
132
 
@@ -161,8 +171,8 @@ def home():
161
  return {
162
  "status": "running",
163
  "ad_input": "19x76 CNN",
164
- "pd_input": "76 features MLP",
165
- "version": "9.0"
166
  }
167
 
168
  @app.get("/health")
 
11
 
12
  app = FastAPI(
13
  title="NeuroHealth EEG API",
14
+ version="10.0"
15
  )
16
 
17
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
31
  features: list
32
 
33
  # =========================================================
34
+ # AD MODEL (UNCHANGED - 19 x 76 CNN)
35
  # =========================================================
36
 
37
  class EEG_CNN_AD(nn.Module):
 
66
  return self.fc(x)
67
 
68
  # =========================================================
69
+ # PD MODEL (UPDATED CNN - MATCHES YOUR TRAINED MODEL)
70
  # =========================================================
71
 
72
+ class EEG_CNN_PD(nn.Module):
73
  def __init__(self):
74
  super().__init__()
75
 
76
+ self.conv1 = nn.Conv1d(64, 32, 7, padding=3)
77
+ self.bn1 = nn.BatchNorm1d(32)
 
 
 
78
 
79
+ self.conv2 = nn.Conv1d(32, 64, 5, padding=2)
80
+ self.bn2 = nn.BatchNorm1d(64)
81
 
82
+ self.conv3 = nn.Conv1d(64, 128, 3, padding=1)
83
+ self.bn3 = nn.BatchNorm1d(128)
84
 
85
+ self.pool = nn.AdaptiveAvgPool1d(1)
86
+ self.fc = nn.Linear(128, 2)
87
 
88
  def forward(self, x):
89
+ # x: (batch, 64, 256)
90
+ x = torch.relu(self.conv1(x))
91
+ x = self.bn1(x)
92
+
93
+ x = torch.relu(self.conv2(x))
94
+ x = self.bn2(x)
95
+
96
+ x = torch.relu(self.conv3(x))
97
+ x = self.bn3(x)
98
+
99
+ x = self.pool(x).squeeze(-1)
100
+ return self.fc(x)
101
 
102
  # =========================================================
103
  # PATHS
104
  # =========================================================
105
 
106
  AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
107
+ PD_MODEL_PATH = "PD_CNN_FINAL.pth"
108
 
109
  # =========================================================
110
  # LOAD MODELS
 
117
  print("AD model loaded")
118
 
119
  print("Loading PD model...")
120
+ pd_model = EEG_CNN_PD().to(DEVICE)
121
  pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
122
  pd_model.eval()
123
  print("PD model loaded")
 
129
  # =========================================================
130
 
131
  def prepare(features, model_type):
132
+
133
  x = torch.tensor(features, dtype=torch.float32)
134
 
135
  if model_type == "ad":
136
  return x.view(1, 19, 76)
137
 
138
  if model_type == "pd":
139
+ return x.view(1, 64, 256)
140
 
141
  raise ValueError("Invalid model type")
142
 
 
171
  return {
172
  "status": "running",
173
  "ad_input": "19x76 CNN",
174
+ "pd_input": "64x256 CNN",
175
+ "version": "10.0"
176
  }
177
 
178
  @app.get("/health")