Skip to content

[BTY-64] SleepKD: SalientSleepNet teachers and students on Sleep-EDF-20 - #1739

Merged
solarsys merged 2 commits into
sunlabuiuc:masterfrom
irvifa:bty-64-salientsleepnet
Nov 30, 2026
Merged

solarsys merged 2 commits into
sunlabuiuc:masterfrom
irvifa:bty-64-salientsleepnet

Conversation

@irvifa

@irvifa irvifa commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

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 SleepKD itself are in #1518.

What this adds

  • SalientSleepNet (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_units sizes it
    as SleepKD does: teacher 5, teacher assistant 3, student 2.
  • Example (examples/eeg/sleep_staging/sleepkd_salientsleepnet.py): the
    same 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.
  • Docs page and tests.

How it works with existing code

SalientSleepNet subclasses BaseModel and uses no other model's code.
It provides the encode(x) method that SleepKD (#1518) requires, which
returns epoch_feature, sequence_feature and logit per epoch. Its
epoch 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. SleepKD itself is unchanged. The example script follows the
DeepSleepNet example's structure and copies its subject_fold helper.

Limitations

  • Model sizes. Neither paper gives the U-units' inner filters or the MSE
    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.
  • The two-stream (EEG + EOG) model and its multimodal attention module are
    not included, since SleepKD uses one EEG channel.
  • Results are from two subject-wise 8:1:1 splits (seeds 0 and 1), each with
    only 2 test subjects.
  • The example accepts --dataset isruc (ISRUC-III), as the DeepSleepNet example
    does, 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.

run seed 0 seed 1 mean acc / F1 / kappa paper acc / F1
teacher 87.8 / 81.8 85.9 / 77.7 86.9 / 79.8 / 81.0
student 87.9 / 82.0 85.6 / 76.7 86.7 / 79.3 / 80.7
kd 88.0 / 82.4 87.8 / 78.8 87.9 / 80.6 / 82.5 83.62 / 78.93
fitnets 87.8 / 82.0 86.4 / 77.9 87.1 / 79.9 / 81.3 85.33 / 80.21
sleepkd_noTA 86.8 / 80.0 87.4 / 79.3 87.1 / 79.7 / 81.2
takd_ta 88.1 / 83.5 87.9 / 80.0 88.0 / 81.8 / 82.7
takd 86.8 / 81.0 86.7 / 79.4 86.8 / 80.2 / 80.8 85.57 / 80.74
sleepkd_ta 88.5 / 82.6 88.0 / 78.9 88.2 / 80.8 / 82.8
sleepkd 87.2 / 80.7 87.7 / 79.1 87.5 / 79.9 / 81.8 87.05 / 81.40

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:

  • the Keras-style parameter counts of the three sizes;
  • feature sizes, and that U-units keep their input length;
  • the per-epoch pooling against adaptive_avg_pool1d (MPS lacks the
    non-divisible case);
  • class weights, the Trainer, and SleepKD distillation between two sizes;
  • input validation.

salientsleepnet.py coverage is 99.3%.

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
@github-actions

Copy link
Copy Markdown

Test Coverage

Total: 88.22% (57525 / 65209 lines)

Modified file Coverage Lines
pyhealth/models/__init__.py 100.00% 121 / 121
pyhealth/models/salientsleepnet.py 99.35% 152 / 153

Workflow run · 57e86b9

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 30, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit 57e86b9

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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_fold helper. 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.py is at 99.35% and the whole repository at 88.22%. Both meet the bar.

Optional suggestions:

  • The default --out doesn'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

@solarsys
solarsys merged commit 40fc0a3 into sunlabuiuc:master Nov 30, 2026
2 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 30, 2026
solarsys added a commit to solarsys/PyHealth that referenced this pull request Nov 30, 2026
@irvifa
irvifa deleted the bty-64-salientsleepnet branch November 30, 2026 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants