OSV-q The size of tensor a (3) must match the size of tensor b (320) at non-singleton dimension 3

    lap = -(cmul(fft2(grd_x), cconj(fft2(grd_x), inplace=False)) + cmul(fft2(grd_y), cconj(fft2(grd_y), inplace=False)))
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 132, in cconj
    c = t.clone() if not inplace else t
AttributeError: 'numpy.ndarray' object has no attribute 'clone'

 把‘clone’ 改成 ‘copy’ 统一类型,然后编写了新的代码

 lap = -(conjoDx * otfDx + conjoDy * otfDy)
 pdf1 = miu2 * (lap * fft2(f - output_net - b2)) - karma * (conjoDx * fft2(q1) + conjoDy * fft2(q2))

有问题,维度不一致,

把fft2中的每个参数维度都改一致了,还是报错

  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 391, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 320, in train
    pdf1 = miu2 * (lap * fft2(f - output_net - b2)) - karma * (conjoDx * fft2(q1) + conjoDy * fft2(q2))
RuntimeError: The size of tensor a (3) must match the size of tensor b (320) at non-singleton dimension 3

Process finished with exit code 1

3.22 因为代码抑郁了好久了,学校还封校,心情非常不好

今天又好好看了看,决定不能再瞎搞,还是得找方法,不能凭感觉,终于被我找到了,

http://t.csdn.cn/mqK82

果然静下心来就会好的。仔细分析了这个错误。发现了报错中几个数维度就是不匹配

b1 = {Tensor:(1,3,320,320)} 
b2 = {Tensor:(1,3,320,320)} 

f = {Tensor:(1,320,320,3)} 

output_net = {Tensor:(1,3,320,320)} 

搞了这么久原来是f的维度问题啊,气死了,搞它!

因为b1 b2再代码里是和f相等的,所以只修改了f的维度就好。

 f = img_in.clone()
 f = f.permute(2, 0, 1)
 f = f.unsqueeze(0)

没有维度错误了,但是还在这一行报错

====>> Sat Mar 12 11:01:15 2022   Pass time: 0:00:17.422007
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 392, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 321, in train
    pdf1 = miu2 * (lap * fftn(f - output_net - b2)) - karma * (conjoDx * fftn(q1) + conjoDy * fftn(q2))
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

Process finished with exit code 1

改动

    f = img_in.clone()
    f = f.permute(2, 0, 1)
    f = f.unsqueeze(0).type(torch.LongTensor).to(device)

不确定对不对,但是问题变了 

====>> Sat Mar 12 11:45:10 2022   Pass time: 0:00:16.835837
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 390, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 319, in train
    pdf1 = miu2 * (lap * fftn(f - output_net - b2)) - karma * (conjoDx * fftn(q1) + conjoDy * fftn(q2))
ValueError: operands could not be broadcast together with shapes (348,348) (1,3,320,320) 

Process finished with exit code 1

 报错这里,我定义的lap值是(348,348),应该是tensor广播的错误,继续研究这个问题吧

找到啦

  h, w, c = img_in.shape
  nw, nh = w - w % d, h - h % d

  # 原来是这样的
 otfDx = psf2otf(Dh_psf, [h, w])
 otfDy = psf2otf(Dv_psf, [h, w])

  # 改为
 otfDx = psf2otf(Dh_psf, [nh, nw])
 otfDy = psf2otf(Dv_psf, [nh, nw])

这一行终于不报错啦

====>> Sat Mar 12 15:13:59 2022   Pass time: 0:00:17.143842
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 390, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 322, in train
    z = z.real.detach().clone()
AttributeError: 'numpy.ndarray' object has no attribute 'detach'

Process finished with exit code 1

你可能感兴趣的:(python,深度学习,机器学习)