use burn::{
config::Config,
module::Module,
prelude::Tensor,
};
#[doc(inline)]
pub use crate::ops::drop::DropBlockOptions;
use crate::ops::drop::drop_block_2d;
#[derive(Config, Debug)]
pub struct DropBlock2dConfig {
#[config(default = "DropBlockOptions::default()")]
pub options: DropBlockOptions,
}
impl From<DropBlockOptions> for DropBlock2dConfig {
fn from(options: DropBlockOptions) -> Self {
Self { options }
}
}
impl DropBlock2dConfig {
pub fn init(&self) -> DropBlock2d {
DropBlock2d {
options: self.options.clone(),
}
}
}
#[derive(Module, Debug)]
pub struct DropBlock2d {
#[module(skip)]
pub options: DropBlockOptions,
}
impl DropBlock2d {
pub fn forward(
&self,
tensor: Tensor<4>,
) -> Tensor<4> {
if tensor.device().is_autodiff() {
drop_block_2d(tensor.clone(), &self.options)
} else {
tensor
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::support::testing::cpu_device;
#[test]
fn test_module_inference() {
let device = cpu_device();
let config = DropBlock2dConfig::new();
let module = config.init();
let batch_size = 2;
let channels = 3;
let height = 100;
let width = height;
let shape = [batch_size, channels, height, width];
let tensor: Tensor<4> = Tensor::ones(shape, &device);
assert_eq!(tensor.device().is_autodiff(), false);
let result = module.forward(tensor.clone());
result.to_data().assert_eq(&tensor.to_data(), false);
}
#[test]
fn test_module_training() {
let device = cpu_device().autodiff();
let drop_prob = 0.1;
let config = DropBlock2dConfig::new().with_options(
DropBlockOptions::default()
.with_drop_prob(drop_prob)
.with_kernel([2, 3]),
);
let module = config.init();
let batch_size = 2;
let channels = 3;
let height = 100;
let width = height;
let shape = [batch_size, channels, height, width];
let tensor: Tensor<4> = Tensor::ones(shape, &device);
assert_eq!(tensor.device().is_autodiff(), true);
let drop = module.forward(tensor.clone());
let numel = drop.shape().num_elements();
let keep_count = drop
.clone()
.greater_elem(1.0)
.int()
.sum()
.into_scalar::<i64>() as usize;
let drop_count = numel - keep_count;
let drop_ratio = drop_count as f64 / numel as f64;
assert!((drop_ratio - drop_prob).abs() < 0.15);
let total = drop.sum().into_scalar::<f64>();
let norm = total / numel as f64;
assert!((norm - 1.0).abs() < 0.01);
}
}