NeuZephyr
Simple DL Framework
nz::nodes::calc::Col2ImgNode Class Reference

Reconstructs spatial tensors from column matrices generated by im2col transformation. More...

Inheritance diagram for nz::nodes::calc::Col2ImgNode:
Collaboration diagram for nz::nodes::calc::Col2ImgNode:

Public Member Functions

 Col2ImgNode (Node *input, Tensor::size_type outputHeight, Tensor::size_type outputWidth)
 Constructor for the Col2ImgNode class.
 
void forward () override
 Performs the forward propagation operation in the Col2ImgNode.
 
void backward () override
 Performs the backward propagation operation in the Col2ImgNode.
 
- Public Member Functions inherited from nz::nodes::Node
virtual void print (std::ostream &os) const
 Prints the type, data, and gradient of the node.
 
void dataInject (Tensor::value_type *data, bool grad=false) const
 Injects data into a relevant tensor object, optionally setting its gradient requirement.
 
template<typename Iterator >
void dataInject (Iterator begin, Iterator end, const bool grad=false) const
 Injects data from an iterator range into the output tensor of the InputNode, optionally setting its gradient requirement.
 
void dataInject (const std::initializer_list< Tensor::value_type > &data, bool grad=false) const
 Injects data from a std::initializer_list into the output tensor of the Node, optionally setting its gradient requirement.
 

Detailed Description

Reconstructs spatial tensors from column matrices generated by im2col transformation.

This node performs the inverse operation of Img2ColNode, converting (N, 1, Hout*Wout, Cout) tensors back to (N, Cout, Hout, Wout) spatial format. Essential for gradient propagation in convolution operations and feature map reconstruction.

Core functionality and characteristics:

  • Spatial Reconstruction: Transforms column-organized data back to original spatial dimensions.
  • Gradient Handling: Reconstructs gradients during backpropagation through reverse im2col.
  • Parameter Consistency: Requires matching kernel/stride/padding with original Img2ColNode.
  • Memory Optimization: Implements efficient gradient summation for overlapping regions.
  • Shape Restoration: Recovers original spatial dimensions through inverse indexing.
  • CUDA Support: GPU-accelerated implementation for both forward and backward passes.

Key implementation aspects:

  • Forward Pass: Reshapes column matrix to spatial format using output dimensions.
  • Backward Pass: Applies im2col transformation to gradients for upstream propagation.
  • Overlap Resolution: Accumulates gradients from overlapping input regions during backward pass.
  • Shape Validation: Verifies input tensor conforms to (N, 1, H*W, C) structure.

Typical use cases:

  • Gradient computation for convolution layers using matrix-based implementations.
  • Feature map reconstruction after channel-wise operations.
  • Custom layer implementations requiring spatial/tensor format conversion.
  • Visualization of intermediate column-organized features.

Critical considerations:

  • Parameter Matching: Must use identical kernel/stride/padding to corresponding Img2ColNode.
  • Memory Footprint: Gradient tensors may require significant memory during backward pass.
  • Input Constraints: Strict 4D input tensor requirement with second dimension = 1.
  • Spatial Resolution: Output dimensions must match original pre-im2col calculations.
Warning
  • Input tensor shape mismatch will cause runtime errors.
  • Incorrect kernel/stride parameters may produce distorted output spatial dimensions.
  • Large kernel sizes combined with small strides may create memory pressure during backward pass.
Note
  • Output dimensions should satisfy: Hout = (H - K_h + 2*padding)/stride + 1
  • Backward pass internally uses Img2ColNode's logic for gradient propagation.
  • For dilated convolutions, ensure parameter compatibility with forward operations.
See also
Img2ColNode For the corresponding forward transformation
ConvolutionNode For typical usage context in convolutional networks

Usage Example:

// Previous im2col transformation
Img2ColNode im2col(&input, 3, 3, 1, 1);
im2col.forward(); // Output shape: (32, 1, 256*256, 64)
// Reconstruct original spatial format
Col2ImNode col2im(&im2col, 3, 3, 1, 1);
col2im.forward();
// Reconstructed shape: (32, 64, 256, 256)
std::cout << "Reconstructed shape: " << col2im.output->shape() << std::endl;
// Backward pass through inverse transformation
col2im.backward();
Implements im2col transformation for efficient convolution operations in neural networks.
Definition Nodes.cuh:3729
Author
Mgepahmge (https://github.com/Mgepahmge)
Date
2023/10/18

Definition at line 3910 of file Nodes.cuh.

Constructor & Destructor Documentation

◆ Col2ImgNode()

nz::nodes::calc::Col2ImgNode::Col2ImgNode ( Node * input,
Tensor::size_type outputHeight,
Tensor::size_type outputWidth )

Constructor for the Col2ImgNode class.

Parameters
inputA pointer to the input Node (host-to-object). This node provides the necessary input data for the Col2ImgNode.
outputHeightThe height of the output image in size_type (host-to-object).
outputWidthThe width of the output image in size_type (host-to-object).
Returns
None

This constructor initializes a Col2ImgNode object. It sets the output height, width, and number of channels based on the input node's output shape. It then adds the input node to the list of input nodes. A new Tensor object is created for the output, with its shape determined by the batch size from the input node's output, the number of output channels, the specified output height, and width. The output tensor also inherits the gradient requirement from the input node's output. Finally, the node type is set to "Col2Img".

Memory management strategy: The constructor uses std::make_shared to allocate memory for the output tensor. The memory will be automatically managed by the smart pointer, and it will be released when the last reference to the tensor is removed. Exception handling mechanism: There is no explicit exception handling in this constructor. However, if memory allocation for the output tensor fails, a std::bad_alloc exception may be thrown.

Exceptions
std::bad_allocIf memory allocation for the output tensor fails.
Note
  • Ensure that the input pointer is valid and points to a properly initialized Node object.
  • The performance of this constructor is mainly determined by the memory allocation for the output tensor, which has a time complexity of O(n), where n is the number of elements in the output tensor.
```cpp
Node* inputNode = new Node();
Tensor::size_type outputHeight = 10;
Tensor::size_type outputWidth = 10;
Col2ImgNode col2ImgNode(inputNode, outputHeight, outputWidth);
```
Base class for nodes in a neural network or computational graph.
Definition Nodes.cuh:114
Reconstructs spatial tensors from column matrices generated by im2col transformation.
Definition Nodes.cuh:3910
Author
Mgepahmge(https://github.com/Mgepahmge)
Date
2024/07/15

Definition at line 642 of file Nodes.cu.

Member Function Documentation

◆ backward()

void nz::nodes::calc::Col2ImgNode::backward ( )
overridevirtual

Performs the backward propagation operation in the Col2ImgNode.

Parameters
None
Returns
None

This function conducts the backward propagation step for the Col2ImgNode. It first checks if the output tensor of the input node requires gradient computation. If it does, the function calls iCol2imgBackward, passing the gradient tensor of the input node's output, the gradient tensor of the output, the output height, output width, output channels, and the batch size from the input node's output. The iCol2imgBackward function is responsible for computing the gradients and propagating them back to the input.

Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing gradient tensors of the input and output. Exception handling mechanism: There is no explicit exception handling in this function. If the iCol2imgBackward function encounters an error, it may throw an exception, and the specific type of exception depends on the implementation of iCol2imgBackward.

Exceptions
[Exceptiontype from iCol2imgBackward] If the iCol2imgBackward function encounters an error during execution.
Note
  • Ensure that the gradient tensors of the input node's output and the output tensor of the Col2ImgNode are properly initialized before calling this function.
  • The performance of this function depends on the implementation of the iCol2imgBackward function. If the iCol2imgBackward function has a time complexity of O(n), where n is the number of elements in the input or output gradient tensors, then this backward propagation step also has a time complexity of O(n).
```cpp
Col2ImgNode col2ImgNode(...); // Assume Col2ImgNode is properly initialized
col2ImgNode.backward();
```
Author
Mgepahmge(https://github.com/Mgepahmge)
Date
2024/07/15

Implements nz::nodes::Node.

Definition at line 660 of file Nodes.cu.

◆ forward()

void nz::nodes::calc::Col2ImgNode::forward ( )
overridevirtual

Performs the forward propagation operation in the Col2ImgNode.

Parameters
None
Returns
None

This function executes the forward propagation step for the Col2ImgNode. It calls the iCol2img function, passing the output tensor's data, the input node's output tensor's data, the output height, output width, output channels, and the batch size from the input node's output. The iCol2img function is responsible for converting the column - formatted data from the input to image - formatted data in the output.

Memory management strategy: This function does not allocate or deallocate any memory directly. It operates on the existing data pointers of the input and output tensors. Exception handling mechanism: There is no explicit exception handling in this function. If the iCol2img function encounters an error, it may throw an exception, but the nature of the exception depends on the implementation of iCol2img.

Exceptions
[Exceptiontype from iCol2img] If the iCol2img function encounters an error during execution.
Note
  • Ensure that the input node's output tensor and the output tensor of the Col2ImgNode are properly initialized before calling this function.
  • The performance of this function depends on the implementation of the iCol2img function. If the iCol2img function has a time complexity of O(n), where n is the number of elements in the input or output tensors, then this forward propagation step also has a time complexity of O(n).
```cpp
Col2ImgNode col2ImgNode(...); // Assume Col2ImgNode is properly initialized
col2ImgNode.forward();
```
Author
Mgepahmge(https://github.com/Mgepahmge)
Date
2024/07/15

Implements nz::nodes::Node.

Definition at line 655 of file Nodes.cu.


The documentation for this class was generated from the following files: