OSV-q ValueError: axes don‘t match array

z 子问题报错 AttributeError: ‘numpy.ndarray‘ object has no attribute ‘clone‘

于是转类型,在代码里加了一行

z = torch.from_numpy(z)

又回到损失函数了呀,这行代码好像见过,尝试按原来的改一下

torch.Size([1, 3, 320, 320]) torch.Size([1, 3, 320, 320])
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 396, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 303, in train
    loss = loss_func(input_net, output_net, miu2, b2)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 99, in loss_func
    b2 = b2.transpose(2, 0, 1)
ValueError: axes don't match array

Process finished with exit code 1
 b2 = b2.permute(2, 0, 1)

好吧,不对,和之前的错误不一样

torch.Size([1, 3, 320, 320]) torch.Size([1, 3, 320, 320])
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 396, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 303, in train
    loss = loss_func(input_net, output_net, miu2, b2)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 99, in loss_func
    b2 = b2.permute(2, 0, 1)
AttributeError: 'numpy.ndarray' object has no attribute 'permute'

Process finished with exit code 1

 改回去,研究上面的问题

ValueError: axes don't match array,翻译过来是:轴不匹配数组

http://t.csdn.cn/el99Q  关于tranpose的解释

 ,因b2定义的是和f相等,维度已经不需要改变了,所以把关于b2的这几行代码注释掉了

  # dive = divergence(zx, zy)
    # b2 = b2.transpose(2, 0, 1)
    # b2 = b2[np.newaxis,:,:,:]   # 给数组增加维度
    # b2 = np_to_torch(b2).to(device)

    f1 = f1.cuda()
    dive1 = dive1.cuda()
    # b2 = b2.cuda()

然后就有了新错误,cuda和cpu不统一

File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 303, in train
    loss = loss_func(input_net, output_net, miu2, b2)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 108, in loss_func
    loss1 = miu2 * mse(u1, u11)
  File "D:\ProgramData\Anaconda3\envs\python36\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "D:\ProgramData\Anaconda3\envs\python36\lib\site-packages\torch\nn\modules\loss.py", line 446, in forward
    return F.mse_loss(input, target, reduction=self.reduction)
  File "D:\ProgramData\Anaconda3\envs\python36\lib\site-packages\torch\nn\functional.py", line 2660, in mse_loss
    return torch._C._nn.mse_loss(expanded_input, expanded_target, _Reduction.get_enum(reduction))
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

这一行的错误, loss1 = miu2 * mse(u1, u11),然后调用这个函数时u1的值是网络输出。

 input_net = input_net.to(device)
  # 原来的
 output_net = output_net

  # 改为
 output_net = output_net.to(device)

还是不对,同样的报错

  b2 = torch.tensor(b2).cuda()

可以了,下面是数组类型问题

====>> Sun Mar 13 11:06:48 2022   Pass time: 0:00:17.732670
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 396, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 345, in train
    b2 = b2 + f - output_net - divergence(zx, zy)
TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'Tensor'

Process finished with exit code 1

z的类型是numpy, output_net 是 Tensor,梯度函数定义的是对 Tensor形式的.clone(),

于是在对z求梯度时使用了新的梯度函数 ,定义中使用numpy的函数 .copy()

还是不对,Debug发现,q1,q2,b1,b2,conjoDx,conjoDy,lap都是numpy.ndarry形式的,所以

  # 原来
    q1 = np.zeros(f.shape)
    q2 = np.zeros(f.shape)

    b1 = np.zeros(f.shape)
    b2 = np.zeros(f.shape)

# 改为
    q1 = torch.zeros(f.shape)
    q2 = torch.zeros(f.shape)

    b1 = torch.zeros(f.shape)
    b2 = torch.zeros(f.shape)

报错

====>> Sun Mar 13 11:37:09 2022   Pass time: 0:00:17.131145
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 395, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 317, 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

决定统一为tensor类型,所以把定义的fft,divgence函数内部都改为torch了,另外,定义的复共轭这里也改成torch,试一下结果吧

    Dh_psf = np.array([[0, 0, 0], [1, -1, 0], [0, 0, 0]])
    Dv_psf = np.array([[0, 1, 0], [0, -1, 0], [0, 0, 0]])
 
# 新加入的两行
    Dh_psf = torch.from_numpy(Dh_psf)
    Dh_psf = torch.from_numpy(Dh_psf)

    otfDx = psf2otf(Dh_psf, [nh, nw])
    otfDy = psf2otf(Dv_psf, [nh, nw])
    conjoDx = conj(otfDx)
    conjoDy = conj(otfDy)

还是不行

====>>   Build Net

Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 397, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 245, in train
    Dh_psf = torch.from_numpy(Dh_psf)
TypeError: expected np.ndarray (got Tensor)

Process finished with exit code 1

http://t.csdn.cn/rFoeH

在q1,q2,b1,b2,后面都加了.to(device)

可以了,代码继续运行,还需要检查一下定义的散度函数

Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 396, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 345, in train
    b2 = b2 + f - output_net - divergence(zx, zy)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 88, in divergence
    div = divx + divy
ValueError: operands could not be broadcast together with shapes (1,2,320,320) (1,3,319,320) 

Process finished with exit code 1

 重新定义了散度函数,然后主程序已经可以运行了,后续还有一点小问题

def divergence(zx, zy):
    zxx, zxy = getGrd2(zx)
    zyx, zyy = getGrd2(zy)
    dive = zxx + zyy
    return dive
====>> Sun Mar 13 15:10:04 2022   Pass time: 0:00:16.247797
Traceback (most recent call last):
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 387, in 
    train(opt)
  File "C:/Users/shang/Desktop/STDN_LI/TVGnet/OSV_q.py", line 353, in train
    img_v = (v.cpu().data[0].numpy().astype(np.float32)) * 255.0
AttributeError: 'numpy.ndarray' object has no attribute 'cpu'

Process finished with exit code 1

因为v是numpy数组,所以将v转为tensor类型的

 v = torch.tensor(v)

运行应该是可以了,运行时间也明显增加 ,但是又回到了一个之前的问题。

====>> Sun Mar 13 16:37:38 2022   Pass time: 0:00:31.486364
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 309, 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

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