In [168]:
# !pip install aeon
In [169]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn

import torch
from torch import nn
import torch.nn.functional as F
from skorch import NeuralNetClassifier
from skorch.callbacks import EpochScoring
from skorch.dataset import ValidSplit
from sklearn.metrics import accuracy_score
from skorch.callbacks import LRScheduler, EarlyStopping, Checkpoint

DEVICE = "mps" if torch.backends.mps.is_available() else "cuda" if torch.cuda.is_available() else "cpu"

2D Convolutional Neural Networks¶

DermaMNIST is a small benchmark dataset from MedMNIST for skin-lesion image classification. It is based on HAM10000 and contains 10,015 dermatoscopic images of pigmented skin lesions, resized to MNIST-like $28 \times 28$ color images. The task is 7-class multi-class classification, with official train, validation, and test splits of 7,007, 1,003, and 2,005 images.

Labels are:

  • akiec: Actinic keratoses and intraepithelial carcinoma
  • bcc: Basal cell carcinoma
  • bkl: Benign keratosis-like lesions
  • df: Dermatofibroma
  • nv: Melanocytic nevi
  • vasc: Vascular lesions
  • mel: Melanoma
In [170]:
files = np.load("Data/dermamnist.npz")

train_images = files["train_images"]
train_labels = files["train_labels"]

validation_images = files["val_images"]
validation_labels = files["val_labels"]

test_images = files["test_images"]
test_labels = files["test_labels"]

train_images.shape, train_labels.shape, validation_images.shape, validation_labels.shape, test_images.shape, test_labels.shape
Out[170]:
((7007, 28, 28, 3),
 (7007, 1),
 (1003, 28, 28, 3),
 (1003, 1),
 (2005, 28, 28, 3),
 (2005, 1))
In [171]:
np.unique(train_labels)
Out[171]:
array([0, 1, 2, 3, 4, 5, 6], dtype=uint8)
In [172]:
i = 0
plt.imshow(train_images[i])
plt.title(train_labels[i])
Out[172]:
Text(0.5, 1.0, '[0]')
No description has been provided for this image
In [173]:
train_images = train_images.astype(np.float32).transpose(0, 3, 1, 2) / 255.0
validation_images = validation_images.astype(np.float32).transpose(0, 3, 1, 2) / 255.0
test_images = test_images.astype(np.float32).transpose(0, 3, 1, 2) / 255.0
train_labels = train_labels.ravel()
validation_labels = validation_labels.ravel()
test_labels = test_labels.ravel()

train_images.shape, train_labels.shape, validation_images.shape, validation_labels.shape, test_images.shape, test_labels.shape
Out[173]:
((7007, 3, 28, 28),
 (7007,),
 (1003, 3, 28, 28),
 (1003,),
 (2005, 3, 28, 28),
 (2005,))
In [174]:
from sklearn.dummy import DummyClassifier
dummy_clf = DummyClassifier(strategy="most_frequent")
dummy_clf.fit(train_images, train_labels)
dummy_clf.score(test_images, test_labels)
Out[174]:
0.6688279301745635
In [175]:
class Conv2DClassifier(nn.Module):
    def __init__(self, in_channels: int, num_classes: int):
        super().__init__()

        self.features = nn.Sequential(
            nn.Conv2d(
                in_channels=in_channels,
                out_channels=32,
                kernel_size=3,
                padding=1
            ),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2),

            nn.Conv2d(
                in_channels=32,
                out_channels=64,
                kernel_size=3,
                padding=1
            ),
            nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2),

            nn.Conv2d(
                in_channels=64,
                out_channels=128,
                kernel_size=3,
                padding=1
            ),
            nn.BatchNorm2d(128),
            nn.ReLU()
        )

        self.pool = nn.AdaptiveAvgPool2d(output_size=(1, 1))

        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(128, num_classes)
        )

    def forward(self, x):
        """
        x shape: (batch_size, in_channels, height, width)
        """
        x = self.features(x)
        x = self.pool(x)
        x = self.classifier(x)
        return x
In [176]:
validation_images.shape, validation_labels.shape
Out[176]:
((1003, 3, 28, 28), (1003,))
In [177]:
from skorch.helper import predefined_split
from torch.utils.data import TensorDataset

valid_ds = TensorDataset(torch.Tensor(validation_images), torch.Tensor(validation_labels))
In [ ]:
 

Vanilla CNN¶

In [178]:
net = NeuralNetClassifier(
    Conv2DClassifier,
    module__in_channels=train_images.shape[1],
    module__num_classes=np.unique(train_labels).shape[0],

    criterion=nn.CrossEntropyLoss,
    optimizer=torch.optim.Adam,
    max_epochs=10,
    batch_size=32,
    lr=1e-4,

    callbacks=[
        EpochScoring(
            scoring='accuracy',
            name='train_acc',
            on_train=True,
        ),
        Checkpoint(
            monitor="valid_acc_best", 
            load_best=True,
            f_history=None,
            f_optimizer=None,
        )
    ],
    device=DEVICE,

    train_split=predefined_split(valid_ds)
)
net
Out[178]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.Conv2DClassifier'>,
  module__in_channels=3,
  module__num_classes=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.Conv2DClassifier'>,
  module__in_channels=3,
  module__num_classes=7,
)
In [179]:
net.fit(train_images, train_labels)
  epoch    train_acc    train_loss    valid_acc    valid_loss    cp     dur
-------  -----------  ------------  -----------  ------------  ----  ------
      1       0.5807        1.2946       0.6979        0.9958     +  2.6244
      2       0.6956        0.9130       0.7149        0.8589     +  2.4661
      3       0.7089        0.8358       0.7298        0.8131     +  2.4988
      4       0.7153        0.8031       0.7358        0.7858     +  2.7542
      5       0.7211        0.7824       0.7388        0.7711     +  2.7795
      6       0.7250        0.7671       0.7438        0.7595     +  2.5161
      7       0.7280        0.7544       0.7488        0.7529     +  2.4621
      8       0.7293        0.7432       0.7488        0.7434        2.6305
      9       0.7313        0.7331       0.7478        0.7349        2.4915
     10       0.7330        0.7240       0.7507        0.7301     +  2.4509
Out[179]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=Conv2DClassifier(
    (features): Sequential(
      (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (4): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (5): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (6): ReLU()
      (7): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (8): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (9): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (10): ReLU()
    )
    (pool): AdaptiveAvgPool2d(output_size=(1, 1))
    (classifier): Sequential(
      (0): Flatten(start_dim=1, end_dim=-1)
      (1): Linear(in_features=128, out_features=7, bias=True)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=Conv2DClassifier(
    (features): Sequential(
      (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (4): Conv2d(32, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (5): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (6): ReLU()
      (7): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (8): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
      (9): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (10): ReLU()
    )
    (pool): AdaptiveAvgPool2d(output_size=(1, 1))
    (classifier): Sequential(
      (0): Flatten(start_dim=1, end_dim=-1)
      (1): Linear(in_features=128, out_features=7, bias=True)
    )
  ),
)
In [180]:
y_pred = net.predict(test_images)
accuracy_score(test_labels, y_pred)
Out[180]:
0.7276807980049875
In [181]:
history = net.history
plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
No description has been provided for this image
No description has been provided for this image

Resnet¶

In [182]:
from torchvision.models import resnet18, ResNet18_Weights

class ResNet18Classifier(nn.Module):
    def __init__(self, num_classes, pretrained=True, freeze_features=True):
        super().__init__()

        weights = ResNet18_Weights.DEFAULT if pretrained else None
        self.model = resnet18(weights=weights)

        if freeze_features:
            for name, param in self.model.named_parameters():
                if not name.startswith("fc."):
                    param.requires_grad = False

        in_features = self.model.fc.in_features
        self.model.fc = nn.Linear(in_features, num_classes)

    def forward(self, x):
        return self.model(x)
In [184]:
net = NeuralNetClassifier(
    ResNet18Classifier,
    module__num_classes=np.unique(train_labels).shape[0],

    criterion=nn.CrossEntropyLoss,
    optimizer=torch.optim.Adam,
    max_epochs=10,
    batch_size=32,
    lr=1e-4,

    callbacks=[
        EpochScoring(
            scoring='accuracy',
            name='train_acc',
            on_train=True,
        ),
        Checkpoint(
            monitor="valid_acc_best", 
            load_best=True,
            f_history=None,
            f_optimizer=None,
        )
    ],
    device=DEVICE,

    train_split=predefined_split(valid_ds)
)
net
Out[184]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.ResNet18Classifier'>,
  module__num_classes=7,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.ResNet18Classifier'>,
  module__num_classes=7,
)
In [185]:
net.fit(train_images, train_labels)
  epoch    train_acc    train_loss    valid_acc    valid_loss    cp     dur
-------  -----------  ------------  -----------  ------------  ----  ------
      1       0.5477        1.4547       0.6550        1.2086     +  3.7200
      2       0.6586        1.1212       0.6630        1.1236     +  3.9270
      3       0.6652        1.0523       0.6670        1.0686     +  3.5048
      4       0.6716        1.0047       0.6730        1.0314     +  3.6844
      5       0.6769        0.9698       0.6780        1.0052     +  3.7846
      6       0.6806        0.9429       0.6830        0.9861     +  3.5550
      7       0.6835        0.9214       0.6830        0.9719        3.5019
      8       0.6869        0.9036       0.6830        0.9610        3.5804
      9       0.6897        0.8886       0.6869        0.9526     +  3.9056
     10       0.6923        0.8757       0.6859        0.9460        3.9266
Out[185]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=ResNet18Classifier(
    (model): ResNet(
      (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
      (layer1): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
        (1): BasicBlock(
          (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer2): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (downsample): Sequential(
            (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
            (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (1): BasicBlock(
          (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer3): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (downsample): Sequential(
            (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
            (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (1): BasicBlock(
          (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer4): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (downsample): Sequential(
            (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
            (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (1): BasicBlock(
          (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
      (fc): Linear(in_features=512, out_features=7, bias=True)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=ResNet18Classifier(
    (model): ResNet(
      (conv1): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (relu): ReLU(inplace=True)
      (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
      (layer1): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
        (1): BasicBlock(
          (conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer2): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (downsample): Sequential(
            (0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
            (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (1): BasicBlock(
          (conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer3): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (downsample): Sequential(
            (0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
            (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (1): BasicBlock(
          (conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer4): Sequential(
        (0): BasicBlock(
          (conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (downsample): Sequential(
            (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
            (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (1): BasicBlock(
          (conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          (relu): ReLU(inplace=True)
          (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
          (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
      (fc): Linear(in_features=512, out_features=7, bias=True)
    )
  ),
)
In [186]:
y_pred = net.predict(test_images)
accuracy_score(test_labels, y_pred)
Out[186]:
0.686284289276808
In [187]:
history = net.history
plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
No description has been provided for this image
No description has been provided for this image

1D Convolutional Neural Networks¶

In [4]:
from aeon.datasets import load_classification

Dataset¶

Heartbeat

This dataset is derived from the PhysioNet/CinC Challenge 2016. Heart sound recordings were sourced from several contributors around the world, collected at either a clinical or nonclinical environment, from both healthy subjects and pathological patients. The heart sound recordings were collected from different locations on the body. The typical four locations are aortic area, pulmonic area, tricuspid area and mitral area, but could be one of nine different locations. The sounds were divided into two classes: normal and abnormal. The normal recordings were from healthy subjects and the abnormal ones were from patients with a confirmed cardiac diagnosis. The patients suffer from a variety of illnesses, but typically they are heart valve defects and coronary artery disease patients. Heart valve defects include mitral valve prolapse, mitral regurgitation, aortic stenosis and valvular surgery. All the recordings from the patients were generally labeled as abnormal. Both healthy subjects and pathological patients include both children and adults. Each recording was truncated to 5 seconds. A Spectrogram of each instance was then created with a window size of 0.061 seconds and an overlap of 70%. Each instance in this multivariate dataset is arranged such that each dimension is a frequency band from the spectrogram. The two classes normal and abnormal consist of 113 and 296 respectivley.

In [18]:
# Import the dataset
X_train, y_train = load_classification("Heartbeat", split="train")
X_test, y_test = load_classification("Heartbeat", split="test")
X_train = X_train.astype(np.float32)
X_test = X_test.astype(np.float32)
X_train.shape, y_train.shape, X_test.shape, y_test.shape
Out[18]:
((204, 61, 405), (204,), (205, 61, 405), (205,))
In [19]:
# Encode the labels
from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
y_train = le.fit_transform(y_train)
y_test = le.transform(y_test)
LABELS = le.classes_
le.classes_
Out[19]:
array(['abnormal', 'normal'], dtype='<U8')
In [34]:
i = 100
plt.imshow(X_train[i], aspect='auto', cmap='viridis')
plt.suptitle('Class: {}'.format(LABELS[y_train[i]]), fontsize=16)
plt.show()
No description has been provided for this image
In [36]:
from sklearn.dummy import DummyClassifier

dummy_clf = DummyClassifier(strategy="most_frequent")
dummy_clf.fit(X_train, y_train)
dummy_clf.score(X_test, y_test)
Out[36]:
0.7219512195121951
In [37]:
class Conv1DClassifier(nn.Module):
    def __init__(self, in_channels: int, num_classes: int):
        super().__init__()

        self.features = nn.Sequential(
            nn.Conv1d(
                in_channels=in_channels,
                out_channels=32,
                kernel_size=7,
                padding=3
            ),
            nn.BatchNorm1d(32),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=2),

            nn.Conv1d(
                in_channels=32,
                out_channels=64,
                kernel_size=5,
                padding=2
            ),
            nn.BatchNorm1d(64),
            nn.ReLU(),
            nn.MaxPool1d(kernel_size=2),

            nn.Conv1d(
                in_channels=64,
                out_channels=128,
                kernel_size=3,
                padding=1
            ),
            nn.BatchNorm1d(128),
            nn.ReLU()
        )

        self.pool = nn.AdaptiveAvgPool1d(output_size=1)

        self.classifier = nn.Sequential(
            nn.Flatten(),
            nn.Linear(128, num_classes)
        )

    def forward(self, x):
        """
        x shape: (batch_size, in_channels, sequence_length)
        """
        x = self.features(x)
        x = self.pool(x)
        x = self.classifier(x)
        return x
In [88]:
net = NeuralNetClassifier(
    Conv1DClassifier,
    module__in_channels=X_train.shape[1],
    module__num_classes=len(le.classes_),

    criterion=nn.CrossEntropyLoss,
    optimizer=torch.optim.Adam,
    max_epochs=300,
    batch_size=128,
    lr=1e-4,

    callbacks=[
        EpochScoring(
            scoring='accuracy',
            name='train_acc',
            on_train=True,
        ),
        Checkpoint(
            monitor="valid_acc_best", 
            load_best=True,
            f_history=None,
            f_optimizer=None,
        )
    ],
    device=DEVICE,

    train_split=ValidSplit(cv=0.2, stratified=True)
)
net
Out[88]:
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.Conv1DClassifier'>,
  module__in_channels=61,
  module__num_classes=2,
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[uninitialized](
  module=<class '__main__.Conv1DClassifier'>,
  module__in_channels=61,
  module__num_classes=2,
)
In [89]:
net.fit(X_train, y_train)
  epoch    train_acc    train_loss    valid_acc    valid_loss    cp     dur
-------  -----------  ------------  -----------  ------------  ----  ------
      1       0.3497        0.7176       0.2683        0.6971     +  0.0698
      2       0.5215        0.6985       0.2683        0.7001        0.0317
      3       0.5460        0.6841       0.2683        0.7029        0.0324
      4       0.6074        0.6725       0.2683        0.7050        0.0383
      5       0.6319        0.6629       0.3415        0.7066     +  0.0412
      6       0.6503        0.6546       0.4390        0.7079     +  0.0287
      7       0.6871        0.6474       0.4878        0.7090     +  0.0440
      8       0.7117        0.6410       0.5366        0.7099     +  0.0391
      9       0.7178        0.6351       0.5854        0.7107     +  0.0407
     10       0.7178        0.6294       0.6341        0.7115     +  0.0315
     11       0.7178        0.6239       0.6341        0.7122        0.0343
     12       0.7178        0.6184       0.6341        0.7123        0.0371
     13       0.7301        0.6130       0.6585        0.7117     +  0.0412
     14       0.7362        0.6076       0.6829        0.7103     +  0.0390
     15       0.7423        0.6024       0.6829        0.7079        0.0299
     16       0.7362        0.5973       0.7073        0.7045     +  0.0320
     17       0.7362        0.5923       0.7073        0.7001        0.0343
     18       0.7362        0.5876       0.7073        0.6950        0.0375
     19       0.7423        0.5830       0.7073        0.6895        0.0439
     20       0.7423        0.5786       0.7073        0.6837        0.0412
     21       0.7423        0.5744       0.7073        0.6779        0.0298
     22       0.7423        0.5703       0.7073        0.6719        0.0315
     23       0.7423        0.5663       0.6829        0.6657        0.0357
     24       0.7423        0.5625       0.6829        0.6594        0.0468
     25       0.7423        0.5587       0.6829        0.6530        0.0548
     26       0.7423        0.5551       0.6829        0.6467        0.0449
     27       0.7485        0.5516       0.6829        0.6405        0.0354
     28       0.7546        0.5482       0.6829        0.6342        0.0339
     29       0.7546        0.5449       0.6829        0.6279        0.0356
     30       0.7546        0.5416       0.6829        0.6215        0.0412
     31       0.7485        0.5384       0.7073        0.6149        0.0742
     32       0.7485        0.5352       0.7073        0.6083        0.0444
     33       0.7546        0.5320       0.6829        0.6019        0.0308
     34       0.7546        0.5289       0.6829        0.5959        0.0319
     35       0.7546        0.5257       0.7073        0.5903        0.0355
     36       0.7546        0.5227       0.7073        0.5852        0.0361
     37       0.7607        0.5196       0.7073        0.5805        0.0392
     38       0.7669        0.5166       0.7073        0.5766        0.0377
     39       0.7669        0.5136       0.7073        0.5732        0.0289
     40       0.7669        0.5107       0.7073        0.5702        0.0309
     41       0.7669        0.5078       0.7073        0.5674        0.0319
     42       0.7669        0.5049       0.7073        0.5649        0.0428
     43       0.7669        0.5020       0.7073        0.5625        0.0469
     44       0.7669        0.4991       0.7073        0.5598        0.0504
     45       0.7669        0.4962       0.7073        0.5567        0.0335
     46       0.7730        0.4934       0.7073        0.5530        0.0326
     47       0.7791        0.4905       0.7073        0.5491        0.0397
     48       0.7791        0.4876       0.7073        0.5452        0.0418
     49       0.7730        0.4847       0.7073        0.5412        0.0298
     50       0.7791        0.4818       0.7073        0.5372        0.0309
     51       0.7791        0.4788       0.7073        0.5332        0.0320
     52       0.7791        0.4758       0.7073        0.5295        0.0357
     53       0.7791        0.4728       0.7073        0.5261        0.0355
     54       0.7853        0.4698       0.7073        0.5230        0.0425
     55       0.7914        0.4668       0.7073        0.5197        0.0469
     56       0.7975        0.4637       0.7073        0.5160        0.0337
     57       0.7975        0.4606       0.7073        0.5117        0.0360
     58       0.7975        0.4574       0.7073        0.5069        0.0438
     59       0.7975        0.4542       0.7317        0.5020     +  0.0398
     60       0.8037        0.4510       0.7561        0.4971     +  0.0323
     61       0.8037        0.4477       0.7317        0.4924        0.0328
     62       0.8098        0.4445       0.7317        0.4879        0.0368
     63       0.8160        0.4412       0.7561        0.4833        0.0461
     64       0.8160        0.4380       0.7561        0.4786        0.0376
     65       0.8160        0.4347       0.7805        0.4740     +  0.0286
     66       0.8160        0.4314       0.7805        0.4698        0.0312
     67       0.8160        0.4281       0.7805        0.4661        0.0325
     68       0.8221        0.4248       0.7805        0.4631        0.0365
     69       0.8221        0.4216       0.7805        0.4606        0.0395
     70       0.8405        0.4184       0.7805        0.4582        0.0432
     71       0.8466        0.4151       0.7805        0.4559        0.0420
     72       0.8466        0.4119       0.7805        0.4538        0.0308
     73       0.8528        0.4087       0.7805        0.4520        0.0311
     74       0.8528        0.4055       0.7805        0.4504        0.0321
     75       0.8466        0.4024       0.7805        0.4491        0.0332
     76       0.8466        0.3992       0.7805        0.4479        0.0368
     77       0.8466        0.3962       0.8049        0.4466     +  0.0402
     78       0.8466        0.3931       0.8049        0.4453        0.0408
     79       0.8528        0.3901       0.8049        0.4441        0.0296
     80       0.8528        0.3871       0.8049        0.4430        0.0317
     81       0.8528        0.3841       0.8049        0.4421        0.0320
     82       0.8712        0.3812       0.8049        0.4411        0.0342
     83       0.8773        0.3784       0.8049        0.4402        0.0371
     84       0.8896        0.3755       0.8293        0.4392     +  0.0445
     85       0.8896        0.3727       0.8293        0.4383        0.0747
     86       0.8896        0.3700       0.8293        0.4374        0.0485
     87       0.8896        0.3672       0.8293        0.4366        0.0380
     88       0.9018        0.3645       0.8293        0.4358        0.0294
     89       0.9018        0.3618       0.8293        0.4349        0.0326
     90       0.9018        0.3591       0.8293        0.4341        0.0342
     91       0.9018        0.3564       0.8293        0.4334        0.0399
     92       0.9080        0.3537       0.8537        0.4326     +  0.0507
     93       0.9080        0.3510       0.8537        0.4318        0.0394
     94       0.9080        0.3483       0.8537        0.4313        0.0422
     95       0.9141        0.3457       0.8537        0.4309        0.0431
     96       0.9141        0.3431       0.8537        0.4302        0.0377
     97       0.9141        0.3405       0.8537        0.4291        0.0417
     98       0.9141        0.3380       0.8537        0.4283        0.0397
     99       0.9141        0.3354       0.8537        0.4275        0.0294
    100       0.9141        0.3329       0.8537        0.4269        0.0319
    101       0.9141        0.3303       0.8537        0.4262        0.0325
    102       0.9264        0.3278       0.8537        0.4256        0.0359
    103       0.9264        0.3252       0.8537        0.4251        0.0376
    104       0.9264        0.3227       0.8537        0.4245        0.0432
    105       0.9325        0.3201       0.8537        0.4239        0.0385
    106       0.9325        0.3175       0.8537        0.4235        0.0302
    107       0.9325        0.3150       0.8537        0.4231        0.0342
    108       0.9387        0.3125       0.8537        0.4227        0.0345
    109       0.9387        0.3100       0.8537        0.4225        0.0399
    110       0.9387        0.3075       0.8537        0.4219        0.0464
    111       0.9387        0.3051       0.8537        0.4216        0.0425
    112       0.9448        0.3026       0.8537        0.4214        0.0314
    113       0.9448        0.3002       0.8537        0.4208        0.0323
    114       0.9448        0.2977       0.8537        0.4202        0.0347
    115       0.9509        0.2953       0.8537        0.4199        0.0371
    116       0.9509        0.2929       0.8537        0.4194        0.0413
    117       0.9509        0.2904       0.8537        0.4186        0.0404
    118       0.9509        0.2880       0.8537        0.4186        0.0303
    119       0.9509        0.2856       0.8537        0.4181        0.0318
    120       0.9509        0.2832       0.8537        0.4176        0.0320
    121       0.9509        0.2808       0.8537        0.4176        0.0368
    122       0.9509        0.2785       0.8537        0.4169        0.0361
    123       0.9509        0.2761       0.8293        0.4167        0.0433
    124       0.9509        0.2738       0.8293        0.4165        0.0398
    125       0.9448        0.2714       0.8293        0.4160        0.0290
    126       0.9509        0.2690       0.8293        0.4163        0.0314
    127       0.9571        0.2667       0.8049        0.4158        0.0418
    128       0.9571        0.2643       0.8049        0.4156        0.0401
    129       0.9571        0.2619       0.8049        0.4157        0.0458
    130       0.9571        0.2595       0.8049        0.4149        0.0453
    131       0.9571        0.2572       0.8049        0.4155        0.0457
    132       0.9571        0.2549       0.8049        0.4146        0.0460
    133       0.9571        0.2525       0.8049        0.4148        0.0455
    134       0.9571        0.2502       0.8049        0.4149        0.0380
    135       0.9571        0.2479       0.8049        0.4143        0.0296
    136       0.9571        0.2456       0.8049        0.4149        0.0320
    137       0.9571        0.2434       0.8293        0.4139        0.0337
    138       0.9571        0.2411       0.8049        0.4151        0.0360
    139       0.9571        0.2388       0.8293        0.4138        0.0409
    140       0.9632        0.2365       0.8049        0.4155        0.0409
    141       0.9693        0.2342       0.8293        0.4135        0.0299
    142       0.9632        0.2320       0.8049        0.4162        0.0310
    143       0.9693        0.2297       0.8293        0.4127        0.0329
    144       0.9632        0.2275       0.7805        0.4173        0.0343
    145       0.9693        0.2252       0.8293        0.4105        0.0368
    146       0.9632        0.2231       0.7805        0.4221        0.0442
    147       0.9693        0.2208       0.8293        0.4064        0.0434
    148       0.9632        0.2193       0.8049        0.4363        0.0357
    149       0.9693        0.2174       0.8537        0.4035        0.0592
    150       0.9632        0.2168       0.7805        0.4490        0.0472
    151       0.9693        0.2142       0.8293        0.4058        0.0300
    152       0.9632        0.2114       0.7805        0.4220        0.0334
    153       0.9693        0.2080       0.7805        0.4271        0.0356
    154       0.9693        0.2060       0.8293        0.4073        0.0376
    155       0.9632        0.2049       0.8049        0.4361        0.0420
    156       0.9693        0.2023       0.8293        0.4130        0.0458
    157       0.9693        0.1999       0.8049        0.4188        0.0453
    158       0.9693        0.1976       0.8049        0.4291        0.0385
    159       0.9693        0.1957       0.8293        0.4113        0.0295
    160       0.9693        0.1942       0.8049        0.4310        0.0427
    161       0.9693        0.1918       0.8049        0.4144        0.0350
    162       0.9693        0.1898       0.8049        0.4208        0.0386
    163       0.9693        0.1876       0.8049        0.4227        0.0443
    164       0.9693        0.1856       0.8049        0.4147        0.0402
    165       0.9693        0.1839       0.8293        0.4270        0.0296
    166       0.9693        0.1818       0.8049        0.4144        0.0310
    167       0.9693        0.1800       0.8049        0.4256        0.0323
    168       0.9693        0.1778       0.8049        0.4173        0.0337
    169       0.9693        0.1759       0.8049        0.4222        0.0368
    170       0.9755        0.1739       0.8049        0.4212        0.0418
    171       0.9755        0.1719       0.8049        0.4202        0.0421
    172       0.9755        0.1700       0.8049        0.4249        0.0330
    173       0.9755        0.1681       0.8049        0.4192        0.0407
    174       0.9755        0.1663       0.8293        0.4287        0.0440
    175       0.9755        0.1642       0.8049        0.4180        0.0414
    176       0.9755        0.1626       0.8293        0.4346        0.0369
    177       0.9755        0.1606       0.8293        0.4155        0.0420
    178       0.9755        0.1595       0.8293        0.4481        0.0420
    179       0.9755        0.1579       0.8049        0.4152        0.0292
    180       0.9755        0.1579       0.8049        0.4718        0.0315
    181       0.9755        0.1567       0.8049        0.4193        0.0344
    182       0.9755        0.1561       0.8293        0.4592        0.0372
    183       0.9755        0.1517       0.7805        0.4221        0.0438
    184       0.9755        0.1483       0.7805        0.4217        0.0407
    185       0.9755        0.1468       0.8293        0.4499        0.0306
    186       0.9755        0.1458       0.8049        0.4204        0.0330
    187       0.9755        0.1446       0.8293        0.4400        0.0342
    188       0.9755        0.1417       0.8049        0.4298        0.0401
    189       0.9755        0.1397       0.7805        0.4244        0.0486
    190       0.9755        0.1386       0.8293        0.4430        0.0426
    191       0.9755        0.1371       0.8049        0.4251        0.0315
    192       0.9755        0.1357       0.8293        0.4363        0.0315
    193       0.9755        0.1336       0.7805        0.4322        0.0350
    194       0.9755        0.1320       0.7805        0.4299        0.0420
    195       0.9755        0.1308       0.8293        0.4401        0.0413
    196       0.9755        0.1293       0.7805        0.4306        0.0376
    197       0.9755        0.1280       0.8293        0.4406        0.0287
    198       0.9816        0.1263       0.7805        0.4347        0.0308
    199       0.9816        0.1249       0.8049        0.4377        0.0319
    200       0.9816        0.1235       0.8049        0.4406        0.0371
    201       0.9816        0.1221       0.7805        0.4369        0.0358
    202       0.9816        0.1208       0.8293        0.4447        0.0439
    203       0.9816        0.1195       0.7805        0.4379        0.0394
    204       0.9816        0.1183       0.8049        0.4457        0.0300
    205       0.9816        0.1169       0.7805        0.4402        0.0329
    206       0.9816        0.1156       0.8049        0.4458        0.0337
    207       0.9816        0.1143       0.8049        0.4431        0.0365
    208       0.9816        0.1131       0.8049        0.4460        0.0408
    209       0.9816        0.1118       0.8049        0.4461        0.0407
    210       0.9816        0.1106       0.8049        0.4468        0.0301
    211       0.9816        0.1094       0.8049        0.4484        0.0309
    212       0.9816        0.1082       0.8049        0.4473        0.0320
    213       0.9816        0.1070       0.8049        0.4505        0.0336
    214       0.9816        0.1058       0.7805        0.4480        0.0363
    215       0.9816        0.1048       0.8049        0.4532        0.0413
    216       0.9816        0.1036       0.7805        0.4491        0.0458
    217       0.9816        0.1027       0.8049        0.4582        0.0457
    218       0.9816        0.1016       0.7805        0.4505        0.0698
    219       0.9877        0.1011       0.8049        0.4707        0.0454
    220       0.9816        0.1005       0.8049        0.4571        0.0450
    221       0.9939        0.1012       0.8293        0.5002        0.0446
    222       0.9816        0.1017       0.7805        0.4727        0.0365
    223       0.9939        0.1030       0.8293        0.5142        0.0288
    224       0.9816        0.1009       0.8049        0.4615        0.0306
    225       0.9939        0.0967       0.8049        0.4610        0.0317
    226       0.9877        0.0936       0.8049        0.4823        0.0360
    227       0.9816        0.0938       0.8049        0.4617        0.0376
    228       0.9939        0.0940       0.8049        0.4786        0.0440
    229       0.9816        0.0914       0.8049        0.4655        0.0454
    230       0.9877        0.0898       0.7805        0.4613        0.0302
    231       0.9939        0.0897       0.8049        0.4818        0.0312
    232       0.9816        0.0890       0.7805        0.4637        0.0332
    233       0.9939        0.0876       0.8049        0.4671        0.0363
    234       0.9877        0.0864       0.8049        0.4738        0.0402
    235       0.9877        0.0858       0.7805        0.4669        0.0397
    236       0.9939        0.0853       0.8049        0.4748        0.0290
    237       0.9877        0.0841       0.8049        0.4704        0.0304
    238       0.9877        0.0832       0.7805        0.4699        0.0321
    239       0.9939        0.0825       0.8049        0.4764        0.0332
    240       0.9877        0.0818       0.7805        0.4709        0.0370
    241       0.9939        0.0810       0.8049        0.4748        0.0432
    242       0.9877        0.0801       0.8049        0.4749        0.0458
    243       0.9939        0.0793       0.7805        0.4741        0.0451
    244       0.9939        0.0786       0.8049        0.4783        0.0460
    245       0.9877        0.0779       0.7805        0.4757        0.0455
    246       0.9939        0.0772       0.8049        0.4779        0.0443
    247       0.9939        0.0764       0.8049        0.4783        0.0390
    248       0.9939        0.0757       0.8049        0.4780        0.0292
    249       0.9939        0.0750       0.8049        0.4808        0.0311
    250       0.9939        0.0743       0.8049        0.4792        0.0331
    251       0.9939        0.0737       0.8049        0.4818        0.0353
    252       0.9939        0.0729       0.8049        0.4815        0.0399
    253       0.9939        0.0723       0.8049        0.4827        0.0455
    254       1.0000        0.0716       0.8049        0.4842        0.0450
    255       1.0000        0.0710       0.8049        0.4846        0.0444
    256       1.0000        0.0703       0.8049        0.4867        0.0384
    257       1.0000        0.0697       0.8049        0.4869        0.0292
    258       1.0000        0.0691       0.8049        0.4884        0.0320
    259       1.0000        0.0684       0.8049        0.4888        0.0415
    260       1.0000        0.0678       0.8049        0.4897        0.0404
    261       1.0000        0.0672       0.8049        0.4904        0.0404
    262       1.0000        0.0666       0.8049        0.4910        0.0299
    263       1.0000        0.0660       0.8049        0.4924        0.0314
    264       1.0000        0.0654       0.8049        0.4935        0.0315
    265       1.0000        0.0648       0.8049        0.4949        0.0336
    266       1.0000        0.0643       0.8049        0.4957        0.0369
    267       1.0000        0.0637       0.8049        0.4967        0.0417
    268       1.0000        0.0631       0.8049        0.4976        0.0457
    269       1.0000        0.0626       0.8049        0.4986        0.0458
    270       1.0000        0.0620       0.8049        0.4996        0.0455
    271       1.0000        0.0615       0.8049        0.5010        0.0387
    272       1.0000        0.0609       0.8049        0.5025        0.0297
    273       1.0000        0.0604       0.8049        0.5036        0.0320
    274       1.0000        0.0599       0.8049        0.5045        0.0333
    275       1.0000        0.0593       0.8049        0.5049        0.0357
    276       1.0000        0.0588       0.8049        0.5054        0.0404
    277       1.0000        0.0583       0.8049        0.5059        0.0411
    278       1.0000        0.0578       0.8049        0.5068        0.0296
    279       1.0000        0.0573       0.8049        0.5080        0.0312
    280       1.0000        0.0568       0.8049        0.5092        0.0324
    281       1.0000        0.0564       0.8049        0.5103        0.0348
    282       1.0000        0.0559       0.8049        0.5114        0.0375
    283       1.0000        0.0554       0.8049        0.5123        0.0443
    284       1.0000        0.0549       0.8049        0.5135        0.0398
    285       1.0000        0.0545       0.8049        0.5147        0.0298
    286       1.0000        0.0540       0.8049        0.5157        0.0309
    287       1.0000        0.0536       0.8049        0.5175        0.0316
    288       1.0000        0.0531       0.8049        0.5183        0.0337
    289       1.0000        0.0527       0.7805        0.5203        0.0368
    290       1.0000        0.0523       0.8049        0.5203        0.0422
    291       1.0000        0.0518       0.7805        0.5222        0.0453
    292       1.0000        0.0515       0.8049        0.5221        0.0462
    293       1.0000        0.0511       0.7805        0.5251        0.0463
    294       1.0000        0.0509       0.8049        0.5252        0.0473
    295       1.0000        0.0505       0.7805        0.5296        0.0451
    296       1.0000        0.0505       0.8049        0.5303        0.0383
    297       1.0000        0.0502       0.8049        0.5350        0.0683
    298       1.0000        0.0503       0.8049        0.5374        0.0396
    299       1.0000        0.0501       0.8049        0.5387        0.0285
    300       1.0000        0.0501       0.8049        0.5404        0.0304
Out[89]:
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=Conv1DClassifier(
    (features): Sequential(
      (0): Conv1d(61, 32, kernel_size=(7,), stride=(1,), padding=(3,))
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (4): Conv1d(32, 64, kernel_size=(5,), stride=(1,), padding=(2,))
      (5): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (6): ReLU()
      (7): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (8): Conv1d(64, 128, kernel_size=(3,), stride=(1,), padding=(1,))
      (9): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (10): ReLU()
    )
    (pool): AdaptiveAvgPool1d(output_size=1)
    (classifier): Sequential(
      (0): Flatten(start_dim=1, end_dim=-1)
      (1): Linear(in_features=128, out_features=2, bias=True)
    )
  ),
)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
<class 'skorch.classifier.NeuralNetClassifier'>[initialized](
  module_=Conv1DClassifier(
    (features): Sequential(
      (0): Conv1d(61, 32, kernel_size=(7,), stride=(1,), padding=(3,))
      (1): BatchNorm1d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (2): ReLU()
      (3): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (4): Conv1d(32, 64, kernel_size=(5,), stride=(1,), padding=(2,))
      (5): BatchNorm1d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (6): ReLU()
      (7): MaxPool1d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
      (8): Conv1d(64, 128, kernel_size=(3,), stride=(1,), padding=(1,))
      (9): BatchNorm1d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (10): ReLU()
    )
    (pool): AdaptiveAvgPool1d(output_size=1)
    (classifier): Sequential(
      (0): Flatten(start_dim=1, end_dim=-1)
      (1): Linear(in_features=128, out_features=2, bias=True)
    )
  ),
)
In [90]:
y_pred = net.predict(X_test)
accuracy_score(y_test, y_pred)
Out[90]:
0.7609756097560976
In [91]:
history = net.history
plt.plot(history[:, 'train_loss'], label='train_loss')
plt.plot(history[:, 'valid_loss'], label='valid_loss')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show()

plt.plot(history[:, 'train_acc'], label='train_acc')
plt.plot(history[:, 'valid_acc'], label='valid_acc')
plt.legend()
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.show()
No description has been provided for this image
No description has been provided for this image

Rocket Variants¶

In [71]:
from aeon.classification.convolution_based import MiniRocketClassifier, MultiRocketHydraClassifier
In [72]:
clf = MiniRocketClassifier(n_jobs=-1)
In [73]:
clf.fit(X_train, y_train)
Out[73]:
MiniRocketClassifier(n_jobs=-1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MiniRocketClassifier(n_jobs=-1)
In [74]:
clf.score(X_test, y_test)
Out[74]:
0.7560975609756098
In [75]:
clf = MultiRocketHydraClassifier(n_jobs=-1)
In [76]:
clf.fit(X_train, y_train)
Out[76]:
MultiRocketHydraClassifier(n_jobs=-1)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
MultiRocketHydraClassifier(n_jobs=-1)
In [77]:
clf.score(X_test, y_test)
Out[77]:
0.7414634146341463

Exercises¶

  • 1D-CNN: build a 1d cnn for the ECG200 dataset (see previous notebooks for how to import it)
  • 2D-CNN: build a 2d cnn for the Pneumonia MNIST dataset (https://zenodo.org/records/10519652)
In [ ]:
 
In [ ]: