Equivalence between Patch Merging and Conv.

Author: AHHOZPCreated Aug 29, 2022Updated Dec 25, 2024

Hello, after looking at the code in patch merging part, we found the complex operation that slice the feature and concatenate them then go through the linear layer to reduce dimension from 4C to 2C is completely equal to a conv layer of kernel size 2 and stride 2.

The operation you did is concatenate 4 pixels from a 2x2 patch in to 1 pixel, but quadrupled channel. Every 2x2 patch shared the same weight with other patches in your linear layer (self.reduction). The conv(kernel size=2, stride=2) does the same thing.

Amount of parameters of this linear layer is equal to this conv layer. linear layer params = input channel * output channel = 4C * 2C = 8 * C^2 conv layer params = kernel size * kernel size * input channel * output channel = 2 * 2 * C * (2 * C) = 8 * C^2 SO, linear layer params == conv layer params

Source: microsoft/Swin-Transformer