Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Tumo505/SSL-for-ECG-classification/llms.txt
Use this file to discover all available pages before exploring further.
ResNet1D is a fully-supervised baseline model for ECG classification that adapts the standard ResNet architecture to one-dimensional cardiac signals. It provides a strong non-SSL baseline against which SimCLR and BYOL pretraining can be compared. Both ResNet1D and its building block ResidualBlock1D live in ssrl_ecg.models.resnet1d.
ResidualBlock1D
ResidualBlock1D is the core residual unit. It applies two sequential 1D convolutions (each followed by batch normalization) and adds a skip connection from the input. When the spatial resolution or channel count changes (stride > 1 or in_channels != out_channels), a learnable downsample projection is applied to the identity branch.
Constructor Parameters
Number of input channels.
Number of output channels.
Width of both 1D convolutional kernels within the block. Padding is set to
kernel_size // 2 so temporal length is preserved (given stride=1).Stride applied to the first convolution. A stride of 2 halves temporal resolution.
Optional projection applied to the identity (skip) branch when channel count
or temporal resolution changes. Typically a
Conv1d(1×1) + BN created by
ResNet1D._make_layer. Pass None when dimensions match.Forward
Input feature map of shape
[batch, in_channels, time].Output feature map of shape
[batch, out_channels, ceil(time/stride)].ResNet1D
ResNet1D assembles four residual layers — each containing two or three ResidualBlock1D units — preceded by a stem convolution and max-pooling layer. A global average pool and a linear classification head map the final feature map to class logits.
Architecture
The network follows the channel schedule[width, width×2, width×4, width×8] across the four stages. At the default width=64, the feature dimensions progress as:
Depth vs. Blocks per Layer
Thedepth parameter mirrors the ResNet naming convention from the original paper. All residual layers in the current implementation use 2 blocks regardless of depth setting; the depth argument is reserved for future extension to deeper variants.
| Depth | Blocks per layer | Total residual layers |
|---|---|---|
| 18 | 2 | 4 (8 blocks total) |
| 34 | 3 | 4 (12 blocks total) |
In the current source,
_make_layer is always called with blocks=2. The depth
parameter is stored as an attribute but does not yet change block counts. A future
release will wire depth 34 to 3 blocks per layer.Constructor Parameters
Number of input ECG leads.
Number of output classes. For the PTB-XL five-class rhythm task, use the default
5.Base channel width. The four residual stages use
[width, width×2, width×4, width×8]
channels respectively. The final linear layer has width×8 input features (512 by default).ResNet depth variant. Accepted values:
18 or 34. See the depth table above.Attributes
| Attribute | Type | Description |
|---|---|---|
conv1 | nn.Conv1d | Stem convolution (kernel 15, stride 1). |
bn1 | nn.BatchNorm1d | Batch norm after stem conv. |
maxpool | nn.MaxPool1d | Max pool (kernel 3, stride 2). |
layer1–4 | nn.Sequential | Four residual stages. |
adaptive_pool | nn.AdaptiveAvgPool1d | Global average pool to length 1. |
dropout | nn.Dropout | 20 % dropout before classifier. |
fc | nn.Linear | Classification head. |
Forward
ECG tensor of shape
[batch, in_channels, time].Un-activated class scores of shape
[batch, num_classes]. Apply torch.sigmoid
for multi-label probabilities or torch.softmax for single-label classification.Example Usage
Using ResNet1D as an Encoder Backbone
ResNet1D is designed as a supervised classifier, but its intermediate feature maps can be extracted for use as a backbone: