问题描述
我想创建一个4x4
矩阵来变形我的4 x N
张量图像并将其优化为损失函数。损失函数可以工作,但是即使我使用纯矢量演算来确保它应该是可微的,但我初始化变形的方式还是有些错误。
def affine_transform(
points1,points2,stepSize=0.01,iterations=2000):
def zero_grads(elements):
for elem in elements:
elem.grad.data.zero_()
# Constructing the affine transformation matrix
## translation in x and y which should be optimised
tx = tc.zeros((1,1),requires_grad=True).float()
ty = tc.zeros((1,requires_grad=True).float()
# Construct vectors from the single variables size 4x1
v1 = tc.from_numpy(np.array([[0,1]]).T).float()
vx = v1.mm(tx)
v2 = tc.from_numpy(np.array([[0,1,0]]).T).float()
vy = v2.mm(ty)
## use outer product to increase dimentionality to 4x4
u = tc.from_numpy(np.array([[1,0]]).T).float()
T = u.mm(vx.T) + u.mm(vy.T).float()
I = tc.from_numpy(np.eye(4)).float()
R = I + T
# Add ones to data
n = points1.shape[1]
one = np.ones(n)
p1 = tc.from_numpy(np.vstack((points1,one))).float()
p2 = tc.from_numpy(np.vstack((points2,one))).float()
# Setup optimizer to tx and ty only
optimizer = optim.Adam([tx,ty],stepSize)
count = 0
first = True
while True:
# Constructing the affine transformation matrix
R = I + T # + P
# Calculate transition image (works as expected)
transImg = bilinear_interpolate_torch(
p1,tc.mm(R,p1))
# Calculate loss
loss = tc_ssd(p2,transImg)
if first:
loss.backward(retain_graph=True)
else:
loss.backward()
# take a step
optimizer.step()
# Zero the gradients
zero_grads([tx,ty])
if count >= iterations and iterations != -1:
print("Max iterations reached!")
break
count += 1
R = I + T
H3 = bilinear_interpolate_torch(p1.float(),p1.float()))
return (H3,R)
它引发错误
[W python_anomaly_mode.cpp:60] Warning: Error detected in MmBackward. Traceback of forward call that caused the error:
File "<stdin>",line 1,in <module>
File "/home/fuzie/DropBox/KU/7.aar/MIA/Handins/hw4/code/affine_im.py",line 314,in <module>
b1mesh.T,b2mesh.T)
File "/home/fuzie/DropBox/KU/7.aar/MIA/Handins/hw4/code/affine_im.py",line 225,in affine_transform
vy = v2.mm(ty)
(function print_stack)
Traceback (most recent call last):
File "<stdin>",line 257,in affine_transform
# Stepping
File "/home/fuzie/.pyenv/versions/mia/lib/python3.8/site-packages/torch/tensor.py",line 185,in backward
torch.autograd.backward(self,gradient,retain_graph,create_graph)
File "/home/fuzie/.pyenv/versions/mia/lib/python3.8/site-packages/torch/autograd/__init__.py",line 125,in backward
Variable._execution_engine.run_backward(
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [1,1]] is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that Failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!
但是我不明白为什么不允许这样构造矩阵T
。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)