# Alternatives to zero-padding conv layers?

**URL:** <https://discourse.julialang.org/t/alternatives-to-zero-padding-conv-layers/73488>\
**Category:** Machine Learning\
**Tags:** flux\
**Created:** [December 22, 2021, 1:39pm UTC](https://discourse.julialang.org/t/alternatives-to-zero-padding-conv-layers/73488 "2021-12-22T13:39:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![czexana](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@czexana](https://discourse.julialang.org/u/czexana)\
**Post date:** [December 22, 2021, 1:39pm UTC](https://discourse.julialang.org/t/alternatives-to-zero-padding-conv-layers/73488/1 "2021-12-22T13:39:13Z")

</div>

Is there a way to use same-padding or reflection-padding in Flux convolutional layers, such as NNlib.jl supplies in [https://github.com/FluxML/NNlib.jl/blob/master/src/padding.jl](https://github.com/FluxML/NNlib.jl/blob/master/src/padding.jl), rather than only using zero-padding?

I’m seeing significant edge-effects in an image GAN I’m trying to compose and wondered if using one of these other padding regimens would help.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [December 22, 2021, 9:24pm UTC](https://discourse.julialang.org/t/alternatives-to-zero-padding-conv-layers/73488/2 "2021-12-22T21:24:21Z")

</div>

Can you file an issue on NNlib so we can track this? In the meantime, applying the `pad_*` functions before a conv layer is not too bad of a workaround.

---

<div class="post-metadata">

**Author:** ![czexana](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@czexana](https://discourse.julialang.org/u/czexana)\
**Post date:** [February 24, 2022, 5:42pm UTC](https://discourse.julialang.org/t/alternatives-to-zero-padding-conv-layers/73488/3 "2022-02-24T17:42:23Z")

</div>

I finally got back to looking at this project a couple of weeks ago. This is the best construction I could come up with which let me use it the same as a standard convolutional layer:

```julia
struct rConv
    a
    layer
end

@functor rConv

function rConv(k::NTuple{N,Integer}, ch::Pair{<:Integer,<:Integer}, σ = identity;
        init = Flux.glorot_uniform, stride = 1, pad = 0, dilation = 1, groups = 1,
        bias = true) where N
    
    a = Flux.calc_padding(Flux.Conv,pad,k,dilation,stride)
    weight = Flux.convfilter(k, ch; init, groups)

    layer = Flux.Conv(weight, bias, σ; stride=stride, pad=0, dilation=dilation, groups=groups)

    rConv(a,layer)
end

function (m::rConv)(x)
    m.layer(NNlib.pad_reflect(x,m.a))
end

```

Not sure if it’s the most elegant solution, but it works for me and definitely reduces the edge effects in my specific project.

Edit to say: I’ve not tried using this on a gpu, so not sure if it will work on that.

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [March 4, 2022, 4:59pm UTC](https://discourse.julialang.org/t/alternatives-to-zero-padding-conv-layers/73488/4 "2022-03-04T16:59:12Z")

</div>

Glad you figured it out, I don’t see any reason it wouldn’t work on GPU 🙂. We’ve talked about what the interface for fancier padding in `Conv` might look like, but never arrived at a design. If you have any ideas there, let us know on GitHub!
