tf.image.crop_and_resize 与 torchvision.ops.roi_align 的区别

问题描述

我发现这两个 API 之间明显不同(tf.image.crop_and_resize 与 torchvision.ops.roi_align)。我在这里提供示例:

import numpy as np
b = np.arange(100*100).reshape(1,100,1)
Boxes = [12,30,42,53]
x1 = Boxes[0] / (100-1)
x2 = Boxes[2] / (100-1)
y1 = Boxes[1] / (100-1)
y2 = Boxes[3] / (100-1)

import tensorflow as tf
res = sess.run(tf.image.crop_and_resize(b,[[y1,x1,y2,x2]],[0],[7,7])).transpose([0,3,1,2])
# transpose is for comparison with the result of pytorch
print("tensorflow:",res)

# pytorch 1.7
from torchvision.ops import roi_pool,roi_align
res1 = roi_align(torch.from_numpy(b.transpose([0,2]).astype(np.float32)),torch.from_numpy(np.asarray([[0,Boxes[0],Boxes[1],Boxes[2],Boxes[3]]]).astype(np.float32)),output_size=(7,7))
print(res1)

两个结果是:

tf:

[[[[3012.     3017.     3022.     3027.     3032.     3037.
3042.    ]
[3395.3333 3400.3333 3405.3333 3410.3333 3415.3333 3420.3333
3425.3333]
[3778.6667 3783.6667 3788.6667 3793.6667 3798.6667 3803.6667
3808.6667]
[4162.     4167.     4172.     4177.     4182.     4187.
4192.    ]
[4545.333  4550.333  4555.333  4560.333  4565.333  4570.333
4575.333 ]
[4928.6665 4933.6665 4938.6665 4943.6665 4948.6665 4953.6665
4958.6665]
[5312.     5317.     5322.     5327.     5332.     5337.
5342.    ]]]]

pytorch:

tensor([[[[3178.4287,3182.7141,3187.0000,3191.2856,3195.5713,3199.8569,3204.1428],[3507.0000,3511.2856,3515.5710,3519.8569,3524.1426,3528.4285,3532.7141],[3835.5715,3839.8579,3844.1426,3848.4285,3852.7144,3857.0000,3861.2859],[4164.1426,4168.4287,4172.7139,4177.0000,4181.2856,4185.5713,4189.8574],[4492.7139,4497.0000,4501.2856,4505.5713,4509.8569,4514.1426,4518.4287],[4821.2861,4825.5718,4829.8574,4834.1431,4838.4287,4842.7144,4847.0000],[5149.8574,5154.1421,5158.4292,5162.7144,5167.0005,5171.2856,5175.5713]]]])

结果真的不一样!

有人知道原因吗?提前致谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)