--- title: Label-mixing transforms keywords: fastai sidebar: home_sidebar summary: "Callbacks that perform data augmentation by mixing samples in different ways." description: "Callbacks that perform data augmentation by mixing samples in different ways." nb_path: "nbs/018_data.mixed_augmentation.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class MixHandler1d[source]

MixHandler1d(alpha=0.5) :: Callback

A handler class for implementing mixed sample data augmentation

{% endraw %} {% raw %}
{% endraw %} {% raw %}

class MixUp1d[source]

MixUp1d(alpha=0.4) :: MixHandler1d

Implementation of https://arxiv.org/abs/1710.09412

{% endraw %} {% raw %}
{% endraw %} {% raw %}
from tsai.models.utils import *
from tsai.models.ResNet import *
from tsai.learner import *
dsid = 'NATOPS'
X, y, splits = get_UCR_data(dsid, return_split=False)
tfms = [None, Categorize()]
batch_tfms = TSStandardize()
dls = get_ts_dls(X, y, tfms=tfms, splits=splits, batch_tfms=batch_tfms)
model = build_model(ResNet, dls=dls)
{% endraw %} {% raw %}
learn = Learner(dls, model, cbs=MixUp1d(0.4))
learn.fit_one_cycle(1)
epoch train_loss valid_loss time
0 1.848915 1.780299 00:03
{% endraw %} {% raw %}

class CutMix1d[source]

CutMix1d(alpha=1.0) :: MixHandler1d

Implementation of https://arxiv.org/abs/1905.04899

{% endraw %} {% raw %}
{% endraw %} {% raw %}
dsid = 'NATOPS'
X, y, splits = get_UCR_data(dsid, return_split=False)
tfms = [None, Categorize()]
batch_tfms = TSStandardize()
dls = get_ts_dls(X, y, tfms=tfms, splits=splits, batch_tfms=batch_tfms)
model = build_model(ResNet, dls=dls)
learn = Learner(dls, model, cbs=CutMix1d(1.))
learn.fit_one_cycle(1)
epoch train_loss valid_loss time
0 1.842320 1.787130 00:03
/Users/nacho/opt/anaconda3/envs/py36/lib/python3.6/site-packages/torch/_tensor.py:575: UserWarning: floor_divide is deprecated, and will be removed in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values.
To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor'). (Triggered internally at  ../aten/src/ATen/native/BinaryOps.cpp:467.)
  return torch.floor_divide(self, other)
{% endraw %} {% raw %}
dsid = 'NATOPS'
X, y, splits = get_UCR_data(dsid, return_split=False)
tfms = [None, Categorize()]
batch_tfms = TSStandardize()
dls = get_ts_dls(X, tfms=tfms, splits=splits, batch_tfms=batch_tfms)
model = build_model(ResNet, dls=dls)
learn = Learner(dls, model, loss_func=L1LossFlat(), cbs=CutMix1d(1.))
learn.fit_one_cycle(1)
epoch train_loss valid_loss time
0 nan None 00:01
{% endraw %}