use burn::prelude::{
Backend,
Tensor,
};
pub fn get_same_padding(
size: usize,
kernel_size: usize,
stride: usize,
dilation: usize,
) -> usize {
let reach = size.div_ceil(stride) * stride + kernel_size * dilation + 1;
reach.saturating_sub(size + stride + dilation)
}
pub fn pad_same<B: Backend>(
input: Tensor<B, 4>,
kernel_size: [usize; 2],
stride: [usize; 2],
dilation: [usize; 2],
value: f32,
) -> Tensor<B, 4> {
let ih = input.shape()[2];
let iw = input.shape()[3];
let pad_h = get_same_padding(ih, kernel_size[0], stride[0], dilation[0]);
let pad_w = get_same_padding(iw, kernel_size[1], stride[1], dilation[1]);
input.pad(
(pad_w / 2, pad_w - pad_w / 2, pad_h / 2, pad_h - pad_h / 2),
value,
)
}
#[cfg(test)]
mod tests {
use serial_test::serial;
use super::*;
use crate::support::testing::{
DeviceMemoryGuard,
PerformanceBackend,
default_device,
};
const TIMM_REFERENCE: &[(usize, usize, usize, [usize; 12])] = &[
(1, 1, 1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
(1, 2, 1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
(1, 3, 1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
(1, 4, 1, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
(2, 1, 1, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
(2, 2, 1, [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]),
(2, 3, 1, [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0]),
(2, 4, 1, [1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]),
(3, 1, 1, [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]),
(3, 2, 1, [2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1]),
(3, 3, 1, [2, 1, 0, 2, 1, 0, 2, 1, 0, 2, 1, 0]),
(3, 4, 1, [2, 1, 0, 0, 2, 1, 0, 0, 2, 1, 0, 0]),
(7, 1, 1, [6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6]),
(7, 2, 1, [6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5]),
(7, 3, 1, [6, 5, 4, 6, 5, 4, 6, 5, 4, 6, 5, 4]),
(7, 4, 1, [6, 5, 4, 3, 6, 5, 4, 3, 6, 5, 4, 3]),
(3, 1, 2, [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]),
(3, 2, 2, [4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3]),
(3, 3, 2, [4, 3, 2, 4, 3, 2, 4, 3, 2, 4, 3, 2]),
(3, 4, 2, [4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1]),
];
#[test]
fn test_get_same_padding_matches_timm_reference() {
for &(kernel_size, stride, dilation, pads) in TIMM_REFERENCE {
for (size, expected) in (1..=12).zip(pads) {
assert_eq!(
get_same_padding(size, kernel_size, stride, dilation),
expected,
"size={size} kernel_size={kernel_size} stride={stride} dilation={dilation}"
);
}
}
}
#[test]
fn test_get_same_padding_counts_a_partial_last_window() {
for (size, kernel_size, stride, dilation, expected) in [
(4, 7, 3, 1, 6),
(10, 7, 3, 1, 6),
(5, 7, 4, 1, 6),
(9, 7, 4, 1, 6),
(4, 3, 3, 2, 4),
(5, 3, 4, 2, 4),
] {
assert_eq!(
get_same_padding(size, kernel_size, stride, dilation),
expected,
"size={size} kernel_size={kernel_size} stride={stride} dilation={dilation}"
);
}
}
#[test]
#[serial]
fn test_pad_same_puts_the_odd_pixel_bottom_right() {
type B = PerformanceBackend;
let device = default_device();
let _memory = DeviceMemoryGuard::<B>::new(&device);
let input = Tensor::<B, 4>::ones([1, 1, 10, 8], &device);
let output = pad_same(input.clone(), [3, 3], [3, 3], [1, 1], 0.0);
assert_eq!(output.dims(), [1, 1, 12, 9]);
output
.clone()
.slice([0..1, 0..1, 1..11, 0..8])
.to_data()
.assert_eq(&input.to_data(), true);
assert_eq!(output.sum().into_scalar(), 80.0);
}
}