10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PyTorch で conv2d + padding='same' 相当を行うメモ

Posted at

pytorch で, numpy や tensorflow などでの padding='same'(名前がややこしいが, 畳み込んだ結果が入力画像と同じサイズになる)で畳み込み処理したい.

PyTorch 自体には, padding='same' 自体はないため, それ相当を自前で行うか, 上下左右の padding 幅が変わらないのであれば, 最近(少なくとも v1.6.0)では, Conv2d に padding 引数と, padding_mode を指定で対応できます. e.g.

conv = torch.nn.Conv2d(1, 1, kernel_size, padding=[kernel_size[1]//2, kernel_size[0] // 2], bias=False, padding_mode='replicate')

padding の幅は, kernel width size や stride などから自前で計算する必要があります.
discuss にあるように, 奇数の kernel 幅であれば kernel_width//2 で求まります.

padding_mode は, デフォルトの "zeros" だと, 端っこの結果が暗くなるので注意ください.
replicate or reflection あたりを指定がよいかと思います.

10
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?