Repository navigation
[BTY-64] SleepKD: SalientSleepNet teachers and students on Sleep-EDF-20 - #1739
Conversation
Add the single-stream SalientSleepNet model (U-units, MSE modules, decoder), sized by n_units as SleepKD's teacher (5), assistant (3) and student (2), with the encode() contract SleepKD needs. Add the example reproducing SleepKD's SalientSleepNet runs on two subject splits, docs and tests. Signed-off-by: Irvi Aini <irvi.fa@gmail.com>
…epnet Signed-off-by: Irvi Aini <irvi.fa@gmail.com> # Conflicts: # pyhealth/models/__init__.py
Test CoverageTotal: 88.22% (57525 / 65209 lines)
|
|
/review |
| parser.add_argument("--root", required=True) | ||
| parser.add_argument("--run", required=True, choices=RUNS) | ||
| parser.add_argument("--dataset", default="sleepedf", choices=["sleepedf", "isruc"]) | ||
| parser.add_argument("--out", default="sleepkd_salient_runs") |
There was a problem hiding this comment.
[low] Default output directory is shared across seeds and folds
The default --out is the same for every --seed and --fold, so a run on seed 1 overwrites seed 0's teacher.pt and JSON results, and a distillation run silently loads whichever teacher is there. The docstring warns to give each split its own --out, but the default makes the mistake easy. Consider deriving the default from the seed and fold, e.g. sleepkd_salient_seed{seed}[_fold{fold}], after parsing.
| dataset = ISRUCDataset(root=args.root) | ||
| samples = dataset.set_task(SleepStagingSequenceISRUC(seq_len=20)) | ||
| else: | ||
| dataset = SleepEDFDataset(root=args.root, subset="cassette") |
There was a problem hiding this comment.
[low] Sleep-EDF-20 subset is not enforced
SleepEDFDataset(root=..., subset="cassette") loads every night in sleep-cassette/. The docstring says only the 39 nights of subjects 0-19 (SC40xx-SC41xx) should be present, but nothing checks this. Pointing the example at the full Sleep-EDF cassette download would silently run on more subjects and change the results. Filter the subject IDs in the example, or check the count and fail with a clear message.
| return self.bn(F.relu(self.down2(F.relu(self.down1(h))))) | ||
|
|
||
|
|
||
| class SalientSleepNet(BaseModel): |
There was a problem hiding this comment.
[low] Class docstring is much longer than it needs to be
The docstring runs about 80 lines and repeats implementation details (Keras-style parameter counts, the BatchNorm and ReLU ordering, the SleepKD size comparison). The example docstring repeats the results table as well. Keep the architecture summary, the constructor arguments and the feature definitions, and move the parameter-count and paper-comparison notes to the PR description or the docs page.
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: approve
Thank you for this contribution. It adds a SalientSleepNet model (single EEG stream) sized for SleepKD's teacher, assistant and student. It also adds a reproduction example on Sleep-EDF-20, a docs page and 15 tests. It looks ready to merge.
- Self-contained: the model subclasses BaseModel and uses no other model's code. The only shared-code edit is the export line in models/init.py, which is registration.
- Links named: the description explains the link to SleepKD (#1518) and the copied
subject_foldhelper. The example's use of existing datasets and tasks follows the DeepSleepNet example, as the description says. - Docs and limitations: the docs and the limitations (model sizes, 2 splits, ISRUC not run) match the code and the results.
- Coverage:
salientsleepnet.pyis at 99.35% and the whole repository at 88.22%. Both meet the bar.
Optional suggestions:
- The default
--outdoesn't depend on seed or fold, so runs on different splits overwrite each other's checkpoints. You could add the seed or fold to the default path. - Nothing checks that the Sleep-EDF root holds only the 39 Sleep-EDF-20 nights. A short warning would help.
- The class docstring is long. You could move some of the architecture detail to the docs page.
Reviewed commit 57e86b9
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BdtuVKBLhWpYbhTQbaZaTt
Add SalientSleepNet (Jia et al., IJCAI 2021, https://arxiv.org/abs/2105.13864)
and reproduce the SalientSleepNet experiments of SleepKD (Liang et al., IJCAI
2023, Table 2) on Sleep-EDF-20. This completes the CNN half of BTY-64; the
DeepSleepNet half and
SleepKDitself are in #1518.What this adds
pyhealth/models/salientsleepnet.py): the single-stream(one EEG channel) model, rebuilt in PyTorch from the paper and the layout of
the authors' Keras code (https://github.com/ziyujia/SalientSleepNet). It has
U-units, multi-scale extraction (MSE) modules and a decoder over a
20-epoch sequence, and it predicts one stage per epoch.
n_unitssizes itas SleepKD does: teacher 5, teacher assistant 3, student 2.
examples/eeg/sleep_staging/sleepkd_salientsleepnet.py): thesame nine runs as the DeepSleepNet example (teacher, student, KD, FitNets,
TAKD, SleepKD with and without the assistant), with SleepKD's
SalientSleepNet settings: Adam lr 1e-3, batch 8, 60 epochs, and weights
0.3 / 0.2 / 0.4 / 0.1.
How it works with existing code
SalientSleepNetsubclassesBaseModeland uses no other model's code.It provides the
encode(x)method thatSleepKD(#1518) requires, whichreturns
epoch_feature,sequence_featureandlogitper epoch. Itsepoch features are the encoder U-unit outputs and its sequence features are
the MSE outputs, which is how the SleepKD paper (Section 5.3) assigns the two
levels.
SleepKDitself is unchanged. The example script follows theDeepSleepNet example's structure and copies its
subject_foldhelper.Limitations
filters, and the official repository leaves out its hyperparameter file.
With 8 and 16, the teacher has 484,157 parameters counted as Keras does
(SleepKD Table 4: 474,662). The TA and student keep the first 3 and 2
levels, which gives 108,925 and 55,165 parameters, against the paper's
120,181-parameter student. The paper does not say how it shrank the
student.
not included, since SleepKD uses one EEG channel.
only 2 test subjects.
--dataset isruc(ISRUC-III), as the DeepSleepNet exampledoes, but I have only run SalientSleepNet on Sleep-EDF-20.
Results (Sleep-EDF-20, seeds 0 and 1)
Accuracy / macro-F1 per seed; the mean also gives Cohen's kappa.
The SleepKD student reaches the paper's accuracy (87.5 vs 87.05) with a
lower macro-F1 (79.9 vs 81.40), and every baseline is above the paper's
accuracy. Distillation helps on seed 1, where every distilled student beats
the student trained alone by 0.8 to 2.2 points of accuracy, but not on seed 0,
where the student alone already matches the teacher. On the means, all
students are within 1.2 points of accuracy of each other and plain KD does as
well as SleepKD, so two splits of 2 test subjects can't rank the methods.
Tests
tests/core/test_salientsleepnet.py(15 tests, synthetic data) covers:adaptive_avg_pool1d(MPS lacks thenon-divisible case);
salientsleepnet.pycoverage is 99.3%.