text stringlengths 0 4.99k |
|---|
Setup |
import tensorflow as tf |
from tensorflow import keras |
import matplotlib.pyplot as plt |
# Set seed for reproducibility. |
tf.random.set_seed(42) |
Convolution |
Convolution remains the mainstay of deep neural networks for computer vision. To understand Involution, it is necessary to talk about the convolution operation. |
Imgur |
Consider an input tensor X with dimensions H, W and C_in. We take a collection of C_out convolution kernels each of shape K, K, C_in. With the multiply-add operation between the input tensor and the kernels we obtain an output tensor Y with dimensions H, W, C_out. |
In the diagram above C_out=3. This makes the output tensor of shape H, W and 3. One can notice that the convoltuion kernel does not depend on the spatial position of the input tensor which makes it location-agnostic. On the other hand, each channel in the output tensor is based on a specific convolution filter which ma... |
Involution |
The idea is to have an operation that is both location-specific and channel-agnostic. Trying to implement these specific properties poses a challenge. With a fixed number of involution kernels (for each spatial position) we will not be able to process variable-resolution input tensors. |
To solve this problem, the authors have considered generating each kernel conditioned on specific spatial positions. With this method, we should be able to process variable-resolution input tensors with ease. The diagram below provides an intuition on this kernel generation method. |
Imgur |
class Involution(keras.layers.Layer): |
def __init__( |
self, channel, group_number, kernel_size, stride, reduction_ratio, name |
): |
super().__init__(name=name) |
# Initialize the parameters. |
self.channel = channel |
self.group_number = group_number |
self.kernel_size = kernel_size |
self.stride = stride |
self.reduction_ratio = reduction_ratio |
def build(self, input_shape): |
# Get the shape of the input. |
(_, height, width, num_channels) = input_shape |
# Scale the height and width with respect to the strides. |
height = height // self.stride |
width = width // self.stride |
# Define a layer that average pools the input tensor |
# if stride is more than 1. |
self.stride_layer = ( |
keras.layers.AveragePooling2D( |
pool_size=self.stride, strides=self.stride, padding=\"same\" |
) |
if self.stride > 1 |
else tf.identity |
) |
# Define the kernel generation layer. |
self.kernel_gen = keras.Sequential( |
[ |
keras.layers.Conv2D( |
filters=self.channel // self.reduction_ratio, kernel_size=1 |
), |
keras.layers.BatchNormalization(), |
keras.layers.ReLU(), |
keras.layers.Conv2D( |
filters=self.kernel_size * self.kernel_size * self.group_number, |
kernel_size=1, |
), |
] |
) |
# Define reshape layers |
self.kernel_reshape = keras.layers.Reshape( |
target_shape=( |
height, |
width, |
self.kernel_size * self.kernel_size, |
1, |
self.group_number, |
) |
) |
self.input_patches_reshape = keras.layers.Reshape( |
target_shape=( |
height, |
width, |
self.kernel_size * self.kernel_size, |
num_channels // self.group_number, |
self.group_number, |
) |
) |
self.output_reshape = keras.layers.Reshape( |
target_shape=(height, width, num_channels) |
) |
def call(self, x): |
# Generate the kernel with respect to the input tensor. |
# B, H, W, K*K*G |
kernel_input = self.stride_layer(x) |
kernel = self.kernel_gen(kernel_input) |
# reshape the kerenl |
# B, H, W, K*K, 1, G |
kernel = self.kernel_reshape(kernel) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.