Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@students

Tensorflowでpytorchのreflectionpaddingを実現する

More than 1 year has passed since last update.

詰まったので備忘録です。
まだ動かしていないため、もし間違いなどがあれば教えてくださると嬉しいです。

pytorchにおいて、ReflectionPadding2Dは以下のような挙動をします。
詳しいことについては公式ドキュメントを見るとわかると思います。

>>> m = nn.ReflectionPad2d(2)
>>> input = torch.arange(9, dtype=torch.float).reshape(1, 1, 3, 3)
>>> input
tensor([[[[0., 1., 2.],
          [3., 4., 5.],
          [6., 7., 8.]]]])
>>> m(input)
tensor([[[[8., 7., 6., 7., 8., 7., 6.],
          [5., 4., 3., 4., 5., 4., 3.],
          [2., 1., 0., 1., 2., 1., 0.],
          [5., 4., 3., 4., 5., 4., 3.],
          [8., 7., 6., 7., 8., 7., 6.],
          [5., 4., 3., 4., 5., 4., 3.],
          [2., 1., 0., 1., 2., 1., 0.]]]])
>>> # using different paddings for different sides
>>> m = nn.ReflectionPad2d((1, 1, 2, 0))
>>> m(input)
tensor([[[[7., 6., 7., 8., 7.],
          [4., 3., 4., 5., 4.],
          [1., 0., 1., 2., 1.],
          [4., 3., 4., 5., 4.],
          [7., 6., 7., 8., 7.]]]])

pytorch 公式ドキュメント https://pytorch.org/docs/stable/nn.html

これをtensorflowで実現しようとすると、tensorflowのpadを使うこととなります。
公式ドキュメントに書いてあるんですけどね・・・)
自分のググりかたじゃなかなか出なかったので記事にしました。

tf.pad(
    tensor,
    paddings,
    mode='REFLECT',
    constant_values=0,
    name=None
)

例としては以下のようになります。

t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
tf.pad(t, paddings, "REFLECT")  
# [[6, 5, 4, 5, 6, 5, 4],
#  [3, 2, 1, 2, 3, 2, 1],
#  [6, 5, 4, 5, 6, 5, 4],
#  [3, 2, 1, 2, 3, 2, 1]]

以上2例公式ドキュメントより https://www.tensorflow.org/api_docs/python/tf/pad

1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
students
M1 | 研究分野:Human Agent Interaction | 趣味:表情生成、Talking head?生成、行動生成、TTS、雑談生成、感情分析、3D生成 | どこかの会社でパートタイムエンジニア

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
1
Help us understand the problem. What is going on with this article?