我是pytorch初学者。pytorch中的RoIAlign模块好像有bug。代码很简单,但结果出乎我的意料。
代码:
import torch
from torchvision.ops import RoIAlign
if __name__ == '__main__':
output_size = (3,3)
spatial_scale = 1/4
sampling_ratio = 2
#x.shape:(1,1,6,6)
x = torch.FloatTensor([[
[[1,2,3,4,5,6],
[7,8,9,10,11,12],
[13,14,15,16,17,18],
[19,20,21,22,23,24],
[25,26,27,28,29,30],
[31,32,33,34,35,36],],
]])
rois = torch.tensor([
[0,0.0,0.0,20.0,20.0],
])
channel_num = x.shape[1]
roi_num = rois.shape[0]
a = RoIAlign(output_size, spatial_scale=spatial_scale, sampling_ratio=sampling_ratio)
ya = a(x, rois)
print(ya)
Run Code Online (Sandbox Code Playgroud)
输出:
tensor([[[[ 6.8333, 8.5000, 10.1667],
[16.8333, 18.5000, 20.1667],
[26.8333, 28.5000, 30.1667]]]])
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,它不应该是每个 2x2 单元上的平均池化操作,例如:
tensor([[[[ 4.5000, 6.5000, 8.5000],
[16.5000, 18.5000, 20.5000],
[28.5000, 30.5000, 32.5000]]]])
Run Code Online (Sandbox Code Playgroud)
我的火炬版本是 …
现在我正在使用 训练模型torch.distributed
,但我不确定如何设置随机种子。例如,这是我当前的代码:
def main():
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
cudnn.enabled = True
cudnn.benchmark = True
cudnn.deterministic = True
mp.spawn(main_worker, nprocs=args.ngpus, args=(args,))
Run Code Online (Sandbox Code Playgroud)
我应该移动
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
cudnn.enabled = True
cudnn.benchmark = True
cudnn.deterministic = True
Run Code Online (Sandbox Code Playgroud)
进入函数main_worker()
以确保每个进程都有正确的种子和 cudnn 设置?顺便说一句,我已经尝试过这个,这种行为会让训练速度慢2倍,这真的让我很困惑。
非常感谢您的帮助!