BCUNet¶
- class torcheeg.models.BCUNet(in_channels=4, hid_channels=64, grid_size=(9, 9), beta_timesteps=256, num_classes=2)[source][source]¶
The diffusion model consists of two processes, the forward process, and the backward process. The forward process is to gradually add Gaussian noise to an image until it becomes random noise, while the backward process is the de-noising process. We train an attention-based UNet network at the backward process to start with random noise and gradually de-noise it until an image is generated and use the UNet to generate a simulated image from random noises. In particular, in conditional UNet, additional label information is provided to guide the noise reduction results during the noise reduction process.
It is worth noting that this model is not designed for EEG analysis, but shows good performance and can serve as a good research start.
Paper: Ho J, Jain A, Abbeel P. Denoising diffusion probabilistic models[J]. Advances in Neural Information Processing Systems, 2020, 33: 6840-6851.
Related Project: https://github.com/dome272/Diffusion-Models-pytorch
Below is a recommended suite for use in EEG generation:
import torch from torcheeg.models.ddpm import BCUNet noise = torch.randn(1, 4, 9, 9) t = torch.randint(low=1, high=1000, size=(1, )) y = torch.randint(low=0, high=2, size=(1, )) unet = BCUNet(num_classes=2) fake_X = unet(noise, t, y)
- Parameters:
in_channels (int) – The feature dimension of each electrode. (default:
4
)hid_channels (int) – The basic hidden channels in the network blocks. (default:
64
)grid_size (tuple) – Spatial dimensions of grid-like EEG representation. (default:
(9, 9)
)beta_timesteps (int) – The variance schedule controlling step sizes. (default:
256
)num_classes (int) – The number of classes to predict. (default:
2
)
- forward(x: Tensor, t: Tensor, y: Tensor)[source][source]¶
- Parameters:
x (torch.Tensor) – The random noise to be denoised, which should have the same shape as the simulated EEG expected to be generated, i.e.,
[n, 4, 9, 9]
. Here,n
corresponds to the batch size,4
corresponds toin_channels
, and(9, 9)
corresponds togrid_size
.t (torch.Tensor) – The randomly sampled time steps (int) for denoising a batch of samples. The shape should be
[n,]
. Here,n
corresponds to the batch size.y (torch.Tensor) – Category labels (int) for a batch of samples The shape should be
[n,]
. Here,n
corresponds to the batch size.
- Returns:
the denoised results, which should have the same shape as the input noise, i.e.,
[n, 4, 9, 9]
. Here,n
corresponds to the batch size,4
corresponds toin_channels
, and(9, 9)
corresponds togrid_size
.- Return type:
torch.Tensor[n, 4, 9, 9]