【跑实验07】RuntimeError: Argument #6: Padding size should be less than the corresponding input dimension

最近在尝试跑实验的时候,我们的部分代码为:

patch_h = 28
patch_w = 28
feat_dim = 768

transform = T.Compose([
    T.GaussianBlur(9, sigma=(0.1, 2.0)),
    T.Resize((patch_h * 14, patch_w * 14)),
    T.CenterCrop((patch_h * 14, patch_w * 14)),
    T.ToTensor(),
    T.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
])

dinov2_vitb14 = torch.hub.load('', 'dinov2_vitb14',source='local').cuda()

features = torch.zeros(4, patch_h * patch_w, feat_dim)
imgs_tensor = torch.zeros(4, 3, patch_h * 14, patch_w * 14).cuda()

img_path = f'/home/wangzhenkuan/val_cropped/cropped_(25, 140, 39, 143)_obj365_val_000000685822.jpg'
img = Image.open(img_path).convert('RGB')
imgs_tensor[0] = transform(img)[:3]
with torch.no_grad():
    features_dict = dinov2_vitb14.forward_features(imgs_tensor)
    features = features_dict['x_norm_patchtokens']

features = features.reshape(4 * patch_h * patch_w, feat_dim).cpu()
pca = PCA(n_components=3)
pca.fit(features)
pca_features = pca.transform(features)
pca_features[:, 0] = (pca_features[:, 0] - pca_features[:, 0].min()) / (pca_features[:, 0].max() - pca_features[:, 0].min())
new_pca_features = pca_features.flatten()
print(new_pca_features, new_pca_features.shape)

遇到了这样的错误:

RuntimeError: Argument #6: Padding size should be less than the corresponding input dimension, but got: padding (4, 4) at dimension 2 of input 4

在这里插入图片描述

根据提供的代码和错误信息,问题出现在T.Resize()操作中。T.Resize()操作调整图像大小时,填充的大小超过了输入图像的对应维度。

在我的代码中,尝试将图像大小调整为(patch_h * 14, patch_w * 14),但输入图像的尺寸仅为14x3。所以导致填充大小为(4, 4),而这超过了图像的高度维度3。

为了解决这个问题,我需要确保图像的尺寸足够大,可以容纳patch_h * 14patch_w * 14的大小。你可以调整输入图像的尺寸,或者修改T.Resize()操作的目标大小。

如果你想调整输入图像的大小,可以使用T.Resize()来设置一个合适的尺寸。如果你希望更改T.Resize()的目标大小,确保目标大小小于输入图像的尺寸。

我们看一下这张照片的尺寸大小:

from PIL import Image

image_path = "/home/wangzhenkuan/val_cropped/cropped_(25, 140, 39, 143)_obj365_val_000000685822.jpg"
img = Image.open(image_path)
width, height = img.size
print(f"图片尺寸:宽度 = {width}px,高度 = {height}px")

可以得到:

图片尺寸:宽度 = 14px,高度 = 3px

我尝试将图片尺寸调整为 14 * 14:

img = Image.open(img_path).convert('RGB').resize((14, 14))

这样的话输出便不再报错!

[ 7.44867728e-01  2.34980489e+00 -2.27559823e-02 ...  3.59724475e-01
  9.42175007e+00  2.56441818e+01] (9408,)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same错误时,通常是因为输入的张量类型和权重的张量类型不一致导致的。这个错误提示表明输入类型应该是torch.cuda.FloatTensor,而权重类型应该是torch.FloatTensor。 为了解决这个问题,我们需要确保输入的张量和权重的张量类型一致。有几种方法可以解决这个问题: 1. 确保输入的张量类型和权重的张量类型一致。你可以使用.to()方法将输入的张量类型转换为与权重类型相同的类型。例如,如果输入的张量是torch.cuda.FloatTensor,你可以使用input = input.to(torch.float)将其转换为torch.FloatTensor。 2. 如果输入的张量是在GPU上运行的,而权重是在CPU上运行的,你可以使用.to()方法将权重的张量类型转换为与输入类型相同的类型。例如,如果输入的张量是torch.cuda.FloatTensor,你可以使用weights = weights.to(torch.cuda.FloatTensor)将权重的张量类型转换为torch.cuda.FloatTensor。 3. 另一种方法是使用torch.nn.Module的.to()方法将整个模型的张量类型一致化。你可以使用model.to(device)将整个模型移动到指定的设备上,其中device可以是"cuda"或"cpu"。这将确保模型的所有张量类型与设备上的张量类型一致。 需要注意的是,如果你在GPU上使用torch.cuda.FloatTensor类型的张量,确保你的硬件和PyTorch版本支持CUDA。否则,你需要将张量类型转换为torch.FloatTensor并在CPU上运行。 综上所述,当出现RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same错误时,你可以使用上述方法之一来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the](https://blog.csdn.net/qq_43369406/article/details/128663487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)](https://blog.csdn.net/qq_46684028/article/details/131138864)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅途中的宽~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值