Skip to content
tezvyn:

Zero-padding vs reflect vs replicate padding and their visual artifacts

Source: shadecoder.comMediumHow cards are made

Zero-padding vs reflect vs replicate padding and their visual artifacts

This tests boundary assumptions in convolution. Zero-padding adds black borders causing dark vignettes; reflect padding mirrors edges for continuity; replicate padding repeats edge values outward. A red flag is saying padding choice does not affect outputs.

What's really being asked

This question probes whether you understand that padding is not just a tensor-shape mechanism but a boundary-condition assumption that directly affects the receptive field and visual output at image edges. Interviewers want to see that you can connect numerical padding values to perceptual artifacts and task-specific tradeoffs.

The full answer

A strong response defines zero-padding as adding zeros around the input, which tells the kernel there is no signal beyond the border; in image space this often creates dark vignettes or black bleeding because zeros map to minimum intensity. It then describes reflect-padding as mirroring edge pixels inward, which assumes continuity across the boundary and typically produces smoother transitions with fewer visible seams. Next it explains replicate-padding as clamping the nearest border pixel outward, which creates flat streaking or frozen edges because the same value is repeated. Finally it contrasts when each is used: zero-padding is the default in most deep learning frameworks because it is simple and works well for classification where edge artifacts are less critical, while reflect and replicate are favored in image-to-image tasks like super-resolution, inpainting, or style transfer where border fidelity matters.

The mistakes people make

A red flag is claiming that padding mode does not matter as long as spatial dimensions are preserved. Another weak pattern is confusing reflect and replicate, or stating that zero-padding is always best because it is the framework default. Saying that valid padding avoids all artifacts without acknowledging the dimension shrinkage is also a miss.

What usually comes next

An interviewer might ask how circular padding differs and when it is useful, or how padding affects the effective receptive field at the border versus the center. They may also ask you to compute the exact output size given kernel size K, stride S, and padding P, or to discuss why some generative models use reflect padding explicitly in the decoder.

A concrete example

Consider a 5x5 patch of bright sky at the top border of a photo. With a 3x3 kernel, zero-padding pulls the average down toward black at the edge, creating a faint dark line. Reflect-padding copies the top row downward, so the kernel sees a smooth gradient and preserves the sky tone. Replicate-padding repeats the top row exactly, which avoids darkening but can create a subtle flat band if the kernel weights emphasize the repeated pixel.

Interview question

When applying a 3x3 convolution to the top border of a bright photo, which padding mode preserves spatial dimensions while avoiding dark vignettes and flat streaking?

  • a.Replicate-padding, because it repeats the nearest border pixel outward
  • b.Valid padding, because it computes convolution only where the kernel fits fully
  • c.Reflect-padding, because it mirrors edge pixels to assume continuity across the boundaryCorrect
  • d.Zero-padding, because zeros represent minimum intensity beyond the border
Why?

Reflect-padding mirrors edge pixels to maintain continuity across the boundary, avoiding both the dark vignettes caused by zero-padding and the flat streaking caused by replicate-padding. Replicate-padding is tempting because it avoids darkening, but it creates frozen-edge artifacts by repeating the same pixel value outward.

Just read this? Test yourself on what you have been reading.

Read the original → shadecoder.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on computer vision — each one lists the topics its interview covers.

See open roles