convolutions_rs/lib.rs
1//! This package provides normal convolutions as well as transposed convolutions.
2//! We provide both in the form of free functions as well as something resembling a neural network layer.
3//! This crate also requires ndarray to use the functions, as input and output are in the form of ndarrays.
4//!
5//! In all implementations, we conform to the Pytorch implementation of convolutions (which agrees with the Tensorflow implementation, up to shapes).
6//! We have used the technique described in this blog to achieve a fast implementation:
7//! - <https://leonardoaraujosantos.gitbook.io/artificial-inteligence/machine_learning/deep_learning/convolution_layer/making_faster>
8//!
9//! Example:
10//! ```
11//! use convolutions_rs::convolutions::*;
12//! use ndarray::*;
13//! use convolutions_rs::Padding;
14//!
15//! // Input has shape (channels, height, width)
16//! let input = Array::from_shape_vec(
17//! (1, 4, 4),
18//! vec![1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.]
19//! )
20//! .unwrap();
21//!
22//! // Kernel has shape (channels out, channels in, height, width)
23//! let kernel: Array4<f32> = Array::from_shape_vec(
24//! (2,1,2,2),
25//! vec![1.,1.,1.,1.,1.,1.,1.,1.]
26//! )
27//! .unwrap();
28//!
29//! let conv_layer = ConvolutionLayer::new(kernel.clone(), None, 1, Padding::Valid);
30//! let output_layer: Array3<f32> = conv_layer.convolve(&input);
31//! let output_free = conv2d(&kernel, None, &input, Padding::Valid, 1);
32//!
33//! println!("Layer: {:?}", output_layer);
34//! println!("Free: {:?}", output_free);
35//! ```
36
37use ndarray::{Array3, Array4};
38
39pub mod convolutions;
40pub mod transposed_convolutions;
41
42pub type DataRepresentation<F> = Array3<F>;
43pub type ConvKernel<F> = Array4<F>;
44
45/// Padding (specific way of adding zeros to the input matrix) kind used in the convolution.
46#[derive(PartialEq, Debug, Clone, Copy)]
47pub enum Padding {
48 /// Output has the same shape as input.
49 Same,
50 /// Padding is only used to make input fit the kernel.
51 Valid,
52}