Center-crop a U-Net skip connection and concatenate
Why it matters Segmentation networks for medical imaging still use this crop-and-concat skip.
In the original U-Net, unpadded convs make encoder maps larger than the decoder maps they join, so the skip has to be center-cropped first.
Write crop_and_concat(enc, dec). enc is (C1, He, We) and dec is (C2, Hd, Wd) with He >= Hd and We >= Wd. Crop enc to Hd x Wd starting at top = (He - Hd) // 2 and left = (We - Wd) // 2. Return the concatenation [enc_cropped, dec] along the channel axis, shape (C1 + C2, Hd, Wd).
def crop_and_concat(enc, dec)Example
enc = arange(16).reshape(1, 4, 4), dec = zeros((1, 2, 2)) gives [[[5, 6], [9, 10]], [[0, 0], [0, 0]]].
Understand the idea
U-Net has a down path that shrinks the image and an up path that grows it back. Skip connections hand the sharp, detailed encoder features straight to the decoder at the same level. Without padding, convs nibble a border off each map, so the encoder map is a bit larger than its partner. Cropping the middle keeps the two maps lined up pixel for pixel, like trimming the edges of a photo to fit a frame.
Tiny example: encoder 4 x 4 with values 0 to 15, decoder 2 x 2. Offsets (4 - 2) // 2 = 1, so we take rows 1 to 2 and columns 1 to 2: [[5, 6], [9, 10]], then stack the decoder channel after it.
Rule: top = (He - Hd) // 2, left = (We - Wd) // 2, out = concat(enc[:, top:top+Hd, left:left+Wd], dec).
U-Net and its variants are standard in medical image segmentation and in diffusion model denoisers.
Shapes and data flow
enc (C1,He,We) -> crop (C1,Hd,Wd); dec (C2,Hd,Wd) -> (C1+C2,Hd,Wd)