col2im

Function col2im 

Source
pub fn col2im<F>(
    cols: &ArrayView2<'_, F>,
    outputshape: (usize, usize, usize, usize),
    kernelsize: (usize, usize),
    stride: (usize, usize),
    padding: (usize, usize),
    dilation: (usize, usize),
) -> LinalgResult<Array4<F>>
Expand description

Convert a column matrix back to an input tensor using the col2im algorithm

This function implements the col2im (column to image) algorithm, which converts the column matrix back to the original input tensor format.

§Arguments

  • cols - Column matrix of shape (kernel_h * kernel_w * channels, output_h * output_w * batchsize)
  • outputshape - Shape of the output tensor as (batchsize, channels, height, width)
  • kernelsize - Size of the kernel as (kernel_height, kernel_width)
  • stride - Stride as (stride_height, stride_width)
  • padding - Padding as (padding_height, padding_width)
  • dilation - Dilation as (dilation_height, dilation_width)

§Returns

  • Output tensor of shape (batchsize, channels, height, width)

§Examples

use scirs2_core::ndarray::{Array4, ArrayView4};
use scirs2_linalg::convolution::{im2col, col2im};

// Create a 1x3x4x4 input tensor
let mut input = Array4::<f32>::zeros((1, 3, 4, 4));
// Fill with sample data
for c in 0..3 {
    for h in 0..4 {
        for w in 0..4 {
            input[[0, c, h, w]] = (c * 16 + h * 4 + w) as f32;
        }
    }
}

// Convert to columns with im2col
let cols = im2col(&input.view(), (3, 3), (1, 1), (0, 0), (1, 1)).unwrap();

// Convert back to image with col2im
let output = col2im(
    &cols.view(),
    (1, 3, 4, 4),
    (3, 3),
    (1, 1),
    (0, 0),
    (1, 1),
).unwrap();

// Verify output shape
assert_eq!(output.shape(), &[1, 3, 4, 4]);