【问题标题】:Pytorch "Unfold" equivalent in Tensorflow [duplicate]Tensorflow中的Pytorch“展开”等效项[重复]
【发布时间】:2021-07-04 09:54:38
【问题描述】:

假设我有大小为 (50*50) 的灰度图像,在这种情况下批量大小为 2,我使用 Pytorch 展开功能如下:

import numpy as np
from torch import nn
from torch import tensor

image1 = np.random.rand(1,50,50)
image2 = np.random.rand(1,50,50)
image = np.stack((image1,image2))
image = tensor(image)

ds = nn.Unfold(kernel_size=(2,2),stride=2)
x = ds(image).numpy()
x.shape

## OUTPUT: (2, 4, 625)

什么是等效的 tensorflow 实现,以使 tensorflow 实现的输出与“x”完全匹配?我试过使用 tf.image.extract_patches 函数,但它似乎没有给我我想要的东西。

那么问题来了:Unfold 的 tensorflow 实现是什么?

【问题讨论】:

    标签: python tensorflow pytorch


    【解决方案1】:

    tf.image.extract_patches()torch.nn.Unfold 类似,但需要稍微调整一下参数:

    tf.image.extract_patches(image, sizes=[1,2,2,1], strides=[1,2,2,1], rates=[1,1,1,1], padding='SAME')
    

    【讨论】:

    【解决方案2】:

    使用它产生的结果与 Pytorch 的展开完全相同

    testimage = np.rollaxis(image,1,4)
    z = tf.image.extract_patches(testimage, sizes=[1,2,2,1], strides=[1,2,2,1], rates=[1,1,1,1], padding='SAME')
    z = np.reshape(z, (2,625,4))
    

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 2021-11-29
      • 2022-10-12
      • 2020-10-30
      • 2021-08-01
      • 2020-03-28
      • 2021-01-31
      • 1970-01-01
      • 2016-05-23
      相关资源
      最近更新 更多