今天在添加网络中添加深监督的时候遇到了这个错误,先看出现错误的位置,是在计算交叉熵的时候:

    def forward(self, inputs_scales, targets_scales):losses = []for inputs, targets in zip(inputs_scales, targets_scales):# mask = targets > 0targets_m = targets.clone() #深拷贝targets_m -= 1 #减去loss_all = self.ce_loss(inputs, targets_m.long())

在网上冲浪一个小时,发现了各路大神解决方案:参考方案

1:检查输出预测图与标签的尺寸是否一样

2:没有区分背景及边缘:

3:分割类别与分割标签种类不一样:

我想在深监督中,使用多尺度标签进行监督,在decoder中输出经过self.side_output,得到了一个通道为37的语义分割图。

self.side_output = nn.Conv2d(in_channels//2,out_channels=37,kernel_size=1)

在图像的预处理中,我们对标签进行缩放,使标签的大小和decoder输出的大小一样,这样才可以进行损失计算。

#
class MultiScaleLabel:def __init__(self, downsampling_rates=None):if downsampling_rates is None:self.downsampling_rates = [8, 4, 2]else:self.downsampling_rates = downsampling_ratesdef __call__(self, sample):label = sample['label']h, w = label.shapesample['label_down'] = dict()# Nearest neighbor interpolationfor rate in self.downsampling_rates:label_down = cv2.resize(label.numpy(), (w // rate, h // rate),interpolation=cv2.INTER_NEAREST)sample['label_down'][rate] = torch.from_numpy(label_down)return sample

而在NYUDepth-v2中,标签的类别为40,我的分割类别只有37,因此问题就出现了。即上面的解决办法3。

所以我们将输出的分割类别调整37为标签类别40就可以了。

self.side_output = nn.Conv2d(in_channels//2,out_channels=40,kernel_size=1)

RuntimeError: weight tensor should be defined either for all or no classes相关推荐

  1. torchserve 错误:RuntimeError: Expected tensor for argument #1 ‘input’ to have the same device as tenso

    RuntimeError: Expected tensor for argument #1 'input' to have the same device as tensor for argument ...

  2. RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAType

    RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda ...

  3. RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAFloatT

    使用torchsummary 中的 summary函数时,模型中有Embedding层的时候,会报错.原因是Embedding 需要整型的Tensor. pip install 的torchsumma ...

  4. Expected tensor for argument #1 'input' to have the same device as tensor for argument #2 'weight';

    解决了题目所述问题. 报错完整信息为: RuntimeError: Expected tensor for argument #1 'input' to have the same device as ...

  5. Tensor unsqueeze 以 broadcast

    如果对 torch.unsqueeze 了解不多,建议先阅读博主的这篇文章. 1. Tensor 和 Scalar import torchX = torch.tensor([[1,2,3],[4,5 ...

  6. PytorchRuntimeError: inconsistent tensor sizes at /pytorch/torch/lib/TH/generic/THTensorMath.c:2709

    1.pytorch在得到dataset时没有问题,得到DataLoader时也没有问题,以batch size = 512遍历数据时,出现了: RuntimeError: inconsistent t ...

  7. Expected tensor for argument #1 ‘indices‘ to have one of the following scalar types: Long, Int; but

    RuntimeError: Expected tensor for argument #1 'indices' to have one of the following scalar types: L ...

  8. pytorch 定义torch类型数据_PyTorch官方中文文档:torch.Tensor

    torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...

  9. 3、TensorFlow 的数据模型-----张量(Tensor)

    一.Tensor 类简介 Tensor 定义 A Tensor is a symbolic handle to one of the outputs of an Operation. It does ...

  10. pytorch学习五、深度学习计算

    来自于 https://tangshusen.me/Dive-into-DL-PyTorch/#/ 官方文档 https://pytorch.org/docs/stable/tensors.html ...

最新文章

  1. Ubuntu x64安装Android studio 全部步骤和问题解决
  2. 定时PING下IP地址,检测该服务器是否还活着。 smokeping
  3. 图像处理基本算法 形状特征
  4. 为什么我们会看到 SAP Spartacus 服务器端渲染 `rendering in process` 的日志
  5. java语言50到100之间素数和_用JAVA语言编写一程序,求100以内的所有素数
  6. 程序员如何避免身体被掏空?
  7. Python之手把手教你用JS逆向爬取网易云40万+评论并用stylecloud炫酷词云进行情感分析
  8. Skyline软件二次开发初级——2如何在WEB页面中控制三维地图的观察点坐标和角度...
  9. vue教程 html表单美化 与 vue表单数据的自动搜集
  10. w10系统 怎么快捷搜索服务器,w10系统怎么远程连接服务器
  11. “21天好习惯“ 第四期 — 4
  12. 共享文件 麒麟系统_分享中标麒麟系统的安装教程
  13. SEM 与 SEO 之间的区别与联系
  14. Python提取PDF中的信息,写入Excel
  15. linux翻页查看,【转载】linux-查看日志
  16. 数显之家快讯:【SHIO世硕心语】便携显示器可能是智能手机变身电脑最好的配件了!
  17. 好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can't be established. 的问题
  18. Java中List集合过滤出符合条件的List元素集合
  19. 分布式存储基础之kad
  20. codeforces 1100D-Dasha and Chess

热门文章

  1. android 解压jar,解压和打包Jar - 海阔天空玩世不恭的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. 在 word 中加入 Mathtype 公式编辑器 解决办法
  3. 洞悉物联网发展1000问之透过华为山海经看如何突破物联网关键技术?
  4. ubuntu安装vbox虚拟机
  5. 冰川时代5:星际碰撞Ice Age: Collision Course迅雷下载
  6. 计算机显示器黑屏首先检查,电脑显示器黑屏怎么回事?教你处理方案
  7. Linux文件比较工具
  8. python输出奇数个数_Python实践|输出0-7组成八位奇数总数
  9. 高等代数中涉及到“正交”的名词总结
  10. IP地址归属地查询算法