• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

LR233

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

pytorch_debug

1、报错信息

1.1、出错位置

1 image = Image.open('./img.png')
2 # 图像预处理
3 transforms = transforms.Compose([transforms.Resize(256),
4                                                          transforms.CenterCrop(224),
5                                                          transforms.ToTensor(),   # 转换为张量
6                                                          # 归一化
7                                                          transforms.Normalize([0.485, 0.456, 0.406],
8                                                                                              [0.229, 0.224, 0.225])])
9 image = transforms(image)

1.2、最后一行报错

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

1.3、解决方案

使用PIL.Image读取图像后,要用 .convert("RGB") 转为RGB三个通道。

image = Image.open('./img.png').convert("RGB")

 


 

posted on 2023-03-03 10:49  LR233  阅读(27)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3