gpt4 book ai didi

python - RuntimeError : Given groups=1, weight of size [64, 3, 7, 7], expected input[3, 1, 224, 224] 有 3 个 channel ,但得到了 1 个 channel

转载 作者:行者123 更新时间:2023-12-05 04:03:43 27 4
gpt4 key购买 nike

在下面的代码中:

    model_ft.eval()
test_data, test_target = image_datasets['train'][idx]
test_data = test_data.cuda()
#test_target = test_target.cuda()
test_target = torch.tensor(test_target)
test_target = test_target.cuda()
test_data.unsqueeze_(1)
test_target.unsqueeze_(0)
print(test_data.shape)
output = model_ft(test_data)

我收到以下错误:

Traceback (most recent call last):
File "test_loocv.py", line 245, in <module>
output = model_ft(test_data)
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torchvision-0.2.1-py3.6.egg/torchvision/models/resnet.py", line 139, in forward
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 477, in __call__
result = self.forward(*input, **kwargs)
File "/scratch/sjn-p3/anaconda/anaconda3/lib/python3.6/site-packages/torch/nn/modules/conv.py", line 301, in forward
self.padding, self.dilation, self.groups)
RuntimeError: Given groups=1, weight of size [64, 3, 7, 7], expected input[3, 1, 224, 224] to have 3 channels, but got 1 channels instead

此外,test_data 的形状为:torch.Size([3, 1, 224, 224])。

我应该如何解决这个问题?

最佳答案

修复方法如下:

test_data, test_target = image_datasets['train'][idx]
test_data = test_data.cuda()
test_target = torch.tensor(test_target)
test_target = test_target.cuda()
test_data.unsqueeze_(0)
test_target.unsqueeze_(0)
output = model_ft(test_data)

我必须将 test_data.unsqueeze_(1) 更改为 test_data.unsqueeze_(0)

关于python - RuntimeError : Given groups=1, weight of size [64, 3, 7, 7], expected input[3, 1, 224, 224] 有 3 个 channel ,但得到了 1 个 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416833/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com