Mikecode123 commited on
Commit
190f3ca
·
verified ·
1 Parent(s): bdc1135

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -31,14 +31,14 @@ class EEGRequest(BaseModel):
31
  features: list
32
 
33
  # =========================================================
34
- # CNN MODEL (MATCHES YOUR CHECKPOINT)
35
  # =========================================================
36
 
37
  class EEG_CNN(nn.Module):
38
- def __init__(self, input_dim, output_dim):
39
  super().__init__()
40
 
41
- self.conv1 = nn.Conv1d(1, 32, kernel_size=7, padding=3)
42
  self.bn1 = nn.BatchNorm1d(32)
43
 
44
  self.conv2 = nn.Conv1d(32, 64, kernel_size=5, padding=2)
@@ -52,7 +52,8 @@ class EEG_CNN(nn.Module):
52
  self.fc = nn.Linear(128, output_dim)
53
 
54
  def forward(self, x):
55
- x = x.unsqueeze(1)
 
56
 
57
  x = torch.relu(self.bn1(self.conv1(x)))
58
  x = torch.relu(self.bn2(self.conv2(x)))
@@ -66,8 +67,6 @@ class EEG_CNN(nn.Module):
66
  # CONFIG
67
  # =========================================================
68
 
69
- INPUT_DIM = 76
70
-
71
  AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
72
  PD_MODEL_PATH = "PD_eeg_cnn.pth"
73
 
@@ -77,7 +76,7 @@ PD_MODEL_PATH = "PD_eeg_cnn.pth"
77
 
78
  print("Loading AD CNN model...")
79
 
80
- ad_model = EEG_CNN(INPUT_DIM, 3).to(DEVICE)
81
  ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))
82
  ad_model.eval()
83
 
@@ -85,7 +84,7 @@ print("AD model loaded")
85
 
86
  print("Loading PD CNN model...")
87
 
88
- pd_model = EEG_CNN(INPUT_DIM, 2).to(DEVICE)
89
  pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
90
  pd_model.eval()
91
 
@@ -94,17 +93,17 @@ print("PD model loaded")
94
  print("All models ready")
95
 
96
  # =========================================================
97
- # PREDICTION ENGINE
98
  # =========================================================
99
 
100
  def predict(model, features, classes):
101
 
102
  x = torch.tensor(features, dtype=torch.float32)
103
 
104
- if x.numel() != INPUT_DIM:
105
- raise ValueError(f"Expected {INPUT_DIM} features, got {x.numel()}")
106
 
107
- x = x.unsqueeze(0).to(DEVICE)
108
 
109
  with torch.no_grad():
110
  logits = model(x)
@@ -128,7 +127,7 @@ def predict(model, features, classes):
128
  def home():
129
  return {
130
  "status": "NeuroHealth EEG CNN API running",
131
- "input_dim": INPUT_DIM
132
  }
133
 
134
  @app.get("/health")
 
31
  features: list
32
 
33
  # =========================================================
34
+ # CNN MODEL (MATCH YOUR CHECKPOINT)
35
  # =========================================================
36
 
37
  class EEG_CNN(nn.Module):
38
+ def __init__(self, output_dim):
39
  super().__init__()
40
 
41
+ self.conv1 = nn.Conv1d(19, 32, kernel_size=7, padding=3)
42
  self.bn1 = nn.BatchNorm1d(32)
43
 
44
  self.conv2 = nn.Conv1d(32, 64, kernel_size=5, padding=2)
 
52
  self.fc = nn.Linear(128, output_dim)
53
 
54
  def forward(self, x):
55
+ # EXPECTED INPUT: (batch, 19, 76)
56
+ x = x.view(x.size(0), 19, -1)
57
 
58
  x = torch.relu(self.bn1(self.conv1(x)))
59
  x = torch.relu(self.bn2(self.conv2(x)))
 
67
  # CONFIG
68
  # =========================================================
69
 
 
 
70
  AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
71
  PD_MODEL_PATH = "PD_eeg_cnn.pth"
72
 
 
76
 
77
  print("Loading AD CNN model...")
78
 
79
+ ad_model = EEG_CNN(3).to(DEVICE)
80
  ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))
81
  ad_model.eval()
82
 
 
84
 
85
  print("Loading PD CNN model...")
86
 
87
+ pd_model = EEG_CNN(2).to(DEVICE)
88
  pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
89
  pd_model.eval()
90
 
 
93
  print("All models ready")
94
 
95
  # =========================================================
96
+ # PREDICTION FUNCTION
97
  # =========================================================
98
 
99
  def predict(model, features, classes):
100
 
101
  x = torch.tensor(features, dtype=torch.float32)
102
 
103
+ if x.numel() != 19 * 76:
104
+ raise ValueError("Expected 19x76 = 1444 features")
105
 
106
+ x = x.view(1, 19, 76).to(DEVICE)
107
 
108
  with torch.no_grad():
109
  logits = model(x)
 
127
  def home():
128
  return {
129
  "status": "NeuroHealth EEG CNN API running",
130
+ "input_shape": "19 x 76"
131
  }
132
 
133
  @app.get("/health")