# Changing default padding

**URL:** <https://community.ultralytics.com/t/changing-default-padding/1552>\
**Category:** YOLO\
**Tags:** question\
**Created:** [October 15, 2025, 12:04pm UTC](https://community.ultralytics.com/t/changing-default-padding/1552 "2025-10-15T12:04:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigo\_M](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/rodrigo_m/32/705_2.png) [@Rodrigo\_M](https://community.ultralytics.com/u/Rodrigo_M)\
**Post date:** [October 15, 2025, 12:04pm UTC](https://community.ultralytics.com/t/changing-default-padding/1552/1 "2025-10-15T12:04:50Z")

</div>

> Hi, can someone confirm if I’m correctly modifying the YOLO convolution module? I’m editing the `ultralytics/nn/modules/conv.py` file directly (inside the `modules` folder) to change how Conv blocks behave. Is that the proper way to apply custom changes to YOLO layers?
> 
> ChatGPT recommend the following, changing from the default:
> 
> ```
> def \__init_\_(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
> 
> """
> 
> Initialize Conv layer with given parameters.
> 
> Args:
> 
> c1 (int): Number of input channels.
> 
> c2 (int): Number of output channels.
> 
> k (int): Kernel size.
> 
> s (int): Stride.
> 
> p (int, optional): Padding.
> 
> g (int): Groups.
> 
> d (int): Dilation.
> 
> act (bool | nn.Module): Activation function.
> 
> """
> 
> super().\__init_\_()
> 
> self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
> 
> self.bn = nn.BatchNorm2d(c2)
> 
> self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
> 
> def forward(self, x):
> 
> """
> 
> Apply convolution, batch normalization and activation to input tensor.
> 
> Args:
> 
> x (torch.Tensor): Input tensor.
> 
> Returns:
> 
> (torch.Tensor): Output tensor.
> 
> """
> 
> return self.act(self.bn(self.conv(x)))
> 
> def forward_fuse(self, x):
> 
> """
> 
> Apply convolution and activation without batch normalization.
> 
> Args:
> 
> x (torch.Tensor): Input tensor.
> 
> Returns:
> 
> (torch.Tensor): Output tensor.
> 
> """
> 
> return self.act(self.conv(x))
> 
> ```

to:

```auto
def __init__ (self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
    super(). __init__ ()
    # amount of padding we need to add manually
    self.pad = autopad(k, p, d) # int or list[int] for asymmetric kernels
    # set Conv2d padding=0; we will pad explicitly in forward()
    self.conv = nn.Conv2d(c1, c2, k, s, padding=0, groups=g, dilation=d, bias=False)
    self.bn = nn.BatchNorm2d(c2)
    self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()

def _reflect_pad(self, x):
    # support int or [px_l, px_r, py_t, py_b] (autopad returns int for common cases)
    if isinstance(self.pad, int):
        if self.pad:
            x = F.pad(x, [self.pad, self.pad, self.pad, self.pad], mode="reflect")
    else:
        px = self.pad
        # px is [pad_x, pad_y] or [l, r, t, b] depending on upstream; normalize to 4-tuple
        if len(px) == 2:
            l = r = px[0] // 2
            t = b = px[1] // 2
        else:
            l, r, t, b = px[0], px[1], px[2], px[3]
        if any(v > 0 for v in (l, r, t, b)):
            x = F.pad(x, [l, r, t, b], mode="reflect")
    return x

def forward(self, x):
    x = self._reflect_pad(x)
    return self.act(self.bn(self.conv(x)))

def forward_fuse(self, x):
    x = self._reflect_pad(x)
    return self.act(self.conv(x))

```

My main objective is to improve model performance on border objects. Is this the correct way to change the padding and do you think my strategy could yield good results?

Thank you

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [October 15, 2025, 12:53pm UTC](https://community.ultralytics.com/t/changing-default-padding/1552/2 "2025-10-15T12:53:01Z")

</div>

Before modifying the convolution padding, I would suspect there could be other, simpler tactics that you could test first. What have you tried so far?

---

<div class="post-metadata">

**Author:** ![Rodrigo\_M](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/rodrigo_m/32/705_2.png) [@Rodrigo\_M](https://community.ultralytics.com/u/Rodrigo_M)\
**Post date:** [October 15, 2025, 1:09pm UTC](https://community.ultralytics.com/t/changing-default-padding/1552/3 "2025-10-15T13:09:52Z")

</div>

I am doing a master thesis so I am basically exploring as many options as I can, it is not for practical purposes, just for research. But if it returns interesting results that would be great haha.

But I have tried mostly specitic data augmentations, zone-specific.

In the future I will also be trying to change the loss function to deal with certain bboxes differently and I will for sure ask many questions for that too.

Any help is very appreciated haha 🙂

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [October 16, 2025, 12:54pm UTC](https://community.ultralytics.com/t/changing-default-padding/1552/4 "2025-10-16T12:54:42Z")

</div>

So I will be upfront and say I don’t know a lot about modifying these parameters. Since that’s the case, I threw your changes into an LLM (a local one) and it seemed to think that the changes were reasonable, but that it would add some computational overhead. It suspects that it _could_ help improve feature representation by better preserving texture or edge information than zero-padding.

That said, I would probably try just increasing the image resolution for inference as a start. If you’re using the default `imgsz=640` you could experiments where you increase the resolution to find the best setting for your use case. You could also try using tiled inference with SAHI

> **[SAHI Tiled Inference](https://docs.ultralytics.com/guides/sahi-tiled-inference/)**
>
> Learn how to implement YOLO11 with SAHI for sliced inference. Optimize memory usage and enhance detection accuracy for large-scale applications.

as it could help better preserve edge information. Another thing you could try is copying the edge pixels of the input image during a pre-processing step and keeping the default-zero padding. It’s not the most ideal solution, but it’s something that you could test if you wanted.

---

<div class="post-metadata">

**Author:** ![Rodrigo\_M](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/rodrigo_m/32/705_2.png) [@Rodrigo\_M](https://community.ultralytics.com/u/Rodrigo_M)\
**Post date:** [October 16, 2025, 2:20pm UTC](https://community.ultralytics.com/t/changing-default-padding/1552/5 "2025-10-16T14:20:47Z")

</div>

Thank you so much for all the information! I will definitely look into every possibility you have suggested. Can you just please explain a bit deeper the strategy of “copying the edge pixels of the input image during a pre-processing step”

---

<div class="post-metadata">

**Author:** ![BurhanQ](https://sea1.discourse-cdn.com/flex001/user_avatar/community.ultralytics.com/burhanq/32/7_2.png) [@BurhanQ](https://community.ultralytics.com/u/BurhanQ)\
**Post date:** [October 17, 2025, 11:28am UTC](https://community.ultralytics.com/t/changing-default-padding/1552/6 "2025-10-17T11:28:48Z")

</div>

You can use the OpenCV [`cv2.copyMakeBorder`](https://docs.opencv.org/4.12.0/d2/de8/group __core__ array.html#ga2ac1049c2c3dd25c2b41bffe17658a36) function with the [`cv2.BORDER_REPLICATE`](https://docs.opencv.org/4.12.0/d2/de8/group __core__ array.html#ga209f2f4869e304c82d07739337eae7c5) border type. This will add one extra row/column of pixels to the image, duplicating the adjacent pixel values.

The example from the OpenCV docs:

```auto
# Input image pixels:
[[0 1 2 3 4 5]
 [6 7 8 9 10 11]
 [12 13 14 15 16 17]
 [18 19 20 21 22 23]]
 
 
# Border type: cv2.ORDER_REPLICATE
[[0 0 0 1 2 3 4 5 5 5]
 [0 0 0 1 2 3 4 5 5 5]
 [0 0 0 1 2 3 4 5 5 5]
 [6 6 6 7 8 9 10 11 11 11]
 [12 12 12 13 14 15 16 17 17 17]
 [18 18 18 19 20 21 22 23 23 23]
 [18 18 18 19 20 21 22 23 23 23]
 [18 18 18 19 20 21 22 23 23 23]]

```
