-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinetune.py
More file actions
284 lines (225 loc) · 10.2 KB
/
Copy pathfinetune.py
File metadata and controls
284 lines (225 loc) · 10.2 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""
finetune.py — Calibration curve: how accuracy scales with number of calibration epochs.
Simulates a realistic BCI deployment scenario:
- A pre-trained model (EEGNet) is adapted on small amounts of subject-specific data.
- We measure how quickly accuracy improves as more calibration epochs are added.
- This tells us: "How many trials does the user need to sit through to reach X% accuracy?"
Also generates a reliability (calibration) plot showing whether the model's
confidence scores are well-calibrated (i.e., 70% confidence -> 70% accuracy).
Usage:
python finetune.py
Outputs saved to data/:
- calibration_curve.png — accuracy vs. number of calibration epochs
- reliability_curve.png — confidence vs. actual accuracy (reliability diagram)
- finetuned_eegnet_S{id}.pt — fine-tuned checkpoints per test subject
"""
import sys, os
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import Adam
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import accuracy_score
from sklearn.calibration import calibration_curve
from utils.preprocess import load_subject
from utils.dataset import EEGDataset
from models.eegnet import EEGNet
# -- Constants -----------------------------------------------------------------
SEED = 42
TEST_SUBS = list(range(8, 11)) # S008–S010
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
FINETUNE_LR = 5e-4
FINETUNE_EP = 20 # fine-tuning epochs per calibration batch
CLASS_NAMES = ['Left Hand', 'Right Hand']
# Number of calibration epochs to try (simulate increasing amounts of labeled data)
CALIB_SIZES = [5, 10, 15, 20, 30, 45]
np.random.seed(SEED)
torch.manual_seed(SEED)
def load_pretrained_eegnet(n_channels: int, n_times: int) -> EEGNet:
"""Load the best EEGNet checkpoint from training."""
ckpt_path = os.path.join(DATA_DIR, 'eegnet_best.pt')
if not os.path.exists(ckpt_path):
raise FileNotFoundError(
f"EEGNet checkpoint not found at {ckpt_path}.\n"
"Run python training/train.py first."
)
model = EEGNet(n_classes=2, n_channels=n_channels, n_times=n_times)
ckpt = torch.load(ckpt_path, map_location=DEVICE)
model.load_state_dict(ckpt['model_state_dict'])
return model
def get_predictions_and_probs(model, epochs: np.ndarray, model_type='eegnet'):
"""Return (predicted_labels, max_confidence) arrays for a set of epochs."""
ds = EEGDataset(epochs, np.zeros(len(epochs), dtype=np.int64), model_type=model_type)
loader = torch.utils.data.DataLoader(ds, batch_size=64, shuffle=False)
all_preds, all_probs = [], []
model.eval()
with torch.no_grad():
for x, _ in loader:
x = x.to(DEVICE)
logits = model(x)
probs = F.softmax(logits, dim=1).cpu().numpy()
preds = probs.argmax(axis=1)
all_preds.append(preds)
all_probs.append(probs)
return np.concatenate(all_preds), np.concatenate(all_probs, axis=0)
def finetune_model(base_model: EEGNet, calib_epochs: np.ndarray,
calib_labels: np.ndarray) -> EEGNet:
"""
Fine-tune EEGNet on a small set of subject-specific calibration epochs.
We freeze Block 1 (low-level features are generic) and only update
Block 2 + classifier (high-level, subject-specific features).
"""
import copy
model = copy.deepcopy(base_model).to(DEVICE)
# Freeze Block 1 — temporal and depthwise conv layers are generic
for param in model.block1.parameters():
param.requires_grad = False
# Only fine-tune Block 2 and classifier
optimizer = Adam(
filter(lambda p: p.requires_grad, model.parameters()),
lr=FINETUNE_LR,
weight_decay=1e-4
)
criterion = nn.CrossEntropyLoss()
ds = EEGDataset(calib_epochs, calib_labels, model_type='eegnet')
loader = torch.utils.data.DataLoader(ds, batch_size=min(16, len(ds)), shuffle=True)
model.train()
for _ in range(FINETUNE_EP):
for x, y in loader:
x, y = x.to(DEVICE), y.to(DEVICE)
optimizer.zero_grad()
loss = criterion(model(x), y)
loss.backward()
optimizer.step()
return model
def plot_calibration_curve(results: dict):
"""
Plot accuracy vs. number of calibration epochs for each test subject + mean.
"""
sns.set_style('whitegrid')
fig, ax = plt.subplots(figsize=(8, 5))
colors = ['steelblue', 'coral', 'seagreen']
for i, (subj, accs) in enumerate(results['per_subject'].items()):
ax.plot(CALIB_SIZES, accs, marker='o', label=subj,
color=colors[i % len(colors)], linewidth=1.5)
# Mean across subjects
mean_accs = np.mean(list(results['per_subject'].values()), axis=0)
ax.plot(CALIB_SIZES, mean_accs, marker='D', label='Mean',
color='black', linewidth=2.5, linestyle='--')
# Baseline (no fine-tuning)
ax.axhline(results['baseline_acc'], color='gray', linestyle=':',
linewidth=1.5, label=f'Zero-shot baseline ({results["baseline_acc"]:.2%})')
ax.set_xlabel('Number of calibration epochs per subject')
ax.set_ylabel('Test accuracy')
ax.set_title('EEGNet Fine-tuning: Calibration Curve\n(Accuracy vs. Subject-Specific Training Data)')
ax.set_ylim(0.4, 1.0)
ax.legend(loc='lower right')
plt.tight_layout()
out = os.path.join(DATA_DIR, 'calibration_curve.png')
plt.savefig(out, dpi=100, bbox_inches='tight')
plt.close()
print(f" Saved calibration curve -> {out}")
def plot_reliability_diagram(all_confidences: np.ndarray, all_correct: np.ndarray):
"""
Reliability diagram: plots model confidence vs. observed accuracy.
A perfectly calibrated model follows the diagonal.
"""
sns.set_style('whitegrid')
fig, ax = plt.subplots(figsize=(5, 5))
frac_pos, mean_pred = calibration_curve(all_correct, all_confidences,
n_bins=10, strategy='uniform')
ax.plot([0, 1], [0, 1], 'k--', label='Perfect calibration', linewidth=1.5)
ax.plot(mean_pred, frac_pos, marker='o', color='steelblue',
linewidth=2, label='EEGNet')
ax.set_xlabel('Mean predicted confidence')
ax.set_ylabel('Fraction of correct predictions')
ax.set_title('Reliability Diagram (Confidence Calibration)')
ax.legend()
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)
plt.tight_layout()
out = os.path.join(DATA_DIR, 'reliability_curve.png')
plt.savefig(out, dpi=100, bbox_inches='tight')
plt.close()
print(f" Saved reliability diagram -> {out}")
def main():
print("\n" + "="*55)
print(" Fine-tuning / Calibration Curve Generator")
print("="*55)
# -- Load base model + find data dimensions --------------------------------
test_epochs_all = np.load(os.path.join(DATA_DIR, 'test_epochs.npy'))
test_labels_all = np.load(os.path.join(DATA_DIR, 'test_labels.npy'))
n_channels = test_epochs_all.shape[1]
n_times = test_epochs_all.shape[2]
base_model = load_pretrained_eegnet(n_channels, n_times)
base_model.to(DEVICE)
# -- Zero-shot baseline (no fine-tuning) -----------------------------------
print("\nComputing zero-shot baseline (pre-trained, no fine-tuning)...")
preds_zero, probs_zero = get_predictions_and_probs(base_model, test_epochs_all)
baseline_acc = accuracy_score(test_labels_all, preds_zero)
print(f" Zero-shot accuracy: {baseline_acc:.4f}")
# Collect confidence and correctness for reliability diagram
all_confidences = probs_zero.max(axis=1)
all_correct = (preds_zero == test_labels_all).astype(float)
plot_reliability_diagram(all_confidences, all_correct)
# -- Per-subject fine-tuning loop ------------------------------------------
per_subject_accs = {}
for sid in TEST_SUBS:
print(f"\n Subject S{sid:03d}:")
try:
ep, lb, _, _ = load_subject(sid, verbose=False)
except Exception as e:
print(f" [ERROR] {e}")
continue
subject_accs = []
np.random.seed(SEED)
shuffled_idx = np.random.permutation(len(ep))
for n_calib in CALIB_SIZES:
if n_calib > len(ep):
# Not enough epochs — repeat last valid accuracy
subject_accs.append(subject_accs[-1] if subject_accs else baseline_acc)
continue
# Use first n_calib epochs as calibration data, rest as test
calib_idx = shuffled_idx[:n_calib]
test_idx = shuffled_idx[n_calib:]
if len(test_idx) == 0:
subject_accs.append(subject_accs[-1] if subject_accs else baseline_acc)
continue
calib_ep = ep[calib_idx]
calib_lb = lb[calib_idx]
test_ep = ep[test_idx]
test_lb = lb[test_idx]
# Fine-tune on calibration epochs
ft_model = finetune_model(base_model, calib_ep, calib_lb)
preds, _ = get_predictions_and_probs(ft_model, test_ep)
acc = accuracy_score(test_lb, preds)
subject_accs.append(acc)
print(f" n_calib={n_calib:3d} -> acc={acc:.4f}")
# Save the best fine-tuned model (most data = last calib size)
if n_calib == CALIB_SIZES[-1]:
ft_path = os.path.join(DATA_DIR, f'finetuned_eegnet_S{sid:03d}.pt')
torch.save(ft_model.state_dict(), ft_path)
per_subject_accs[f'S{sid:03d}'] = subject_accs
# -- Save results + plots --------------------------------------------------
results = {
'per_subject': per_subject_accs,
'baseline_acc': baseline_acc,
'calib_sizes': CALIB_SIZES
}
np.save(os.path.join(DATA_DIR, 'finetune_results.npy'), results)
plot_calibration_curve(results)
print("\n" + "="*55)
print(" Fine-tuning complete!")
print(f" Zero-shot baseline : {baseline_acc:.4f}")
if per_subject_accs:
max_calib_accs = [v[-1] for v in per_subject_accs.values()]
print(f" After {CALIB_SIZES[-1]} calib epochs : {np.mean(max_calib_accs):.4f} (mean)")
print("="*55)
if __name__ == "__main__":
main()