RuntimeError: Output 0 of UnbindBackward is a view and is being modified inplace.
1. 问题描述:
在使用utils中的make_grid函数时
utils.make_grid(fmap, normalize=True, scale_each=True, nrow=nrow)
如果出现:
Traceback (most recent call last):
File "visualization.py", line 94, in <module>
fmap_grid = vutils.make_grid(fmap, normalize=True, scale_each=True, nrow=nrow)
File "/home/lab/anaconda3/envs/instagan/lib/python3.6/site-packages/torchvision/utils.py", line 75, in make_grid
norm_range(t, range)
File "/home/lab/anaconda3/envs/instagan/lib/python3.6/site-packages/torchvision/utils.py", line 71, in norm_range
norm_ip(t, float(t.min()), float(t.max()))
File "/home/lab/anaconda3/envs/instagan/lib/python3.6/site-packages/torchvision/utils.py", line 64, in norm_ip
img.clamp_(min=min, max=max)
RuntimeError: Output 0 of UnbindBackward is a view and is being modified inplace. This view is the output of a function that returns multiple views. Such functions do not allow the output views to be modified inplace. You should replace the inplace operation by an out-of-place one.
2. 问题解释:
为pytorch版本问题,在1.7 之后使用该函数汇报错。
3.解决:
进入utils.py文件,将第74行进行修改:
if scale_each is True:
for t in tensor: # loop over mini-batch dimension
norm_range(t, range)
修改后:
if scale_each is True:
######## modified by yuss in 2022.02.15, for RuntimeError
for idx, _ in enumerate([jj for jj in tensor]):
t = tensor[idx]
推荐阅读
-
RuntimeError: Output 0 of UnbindBackward is a view and is being modified inplace.
-
runtimeerror: output 0 of unbindbackward is a view and is being modified inplace.
-
RuntimeError: Output 0 of SelectBackward is a view and is being modified inplace.
-
RuntimeError: Output 0 of UnbindBackward is a view and is being modified inplace.