RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [8,

在用自己的训练集训练DeeplabV3+的时候,遇到了这样的问题。

报错解析:

这个错误表明你的 CrossEntropyLoss 期望输入的 target 是 2D 空间维度的张量 (即 [batch_size, height, width]),而你传递的 target 是一个 4D 张量,维度是 [batch_size, height, width, 3]。这是因为你的目标张量有 3 个通道,而 CrossEntropyLoss 只需要单通道(类别标签)。

解决方案:

  1. 目标张量通道数问题: 你的目标张量有 3 个通道,通常来说,CrossEntropyLoss 只需要单通道的标签,即每个像素点的类别索引。如果目标张量是 RGB 图像,可能是数据预处理的问题,需要将其转换为单通道的类别标签。

  2. 确保 target 是单通道的类别标签: 通常情况下,目标张量 target 的形状应该是 [batch_size, height, width],其中每个像素的值是类别索引。

原本我的标签是RGB三通道的标签(batch,w,h,channel),在运算过程中要求使用的是单通道灰度标签(batch,w,h),需要进行转化。

遂重新将标签制作为单值灰度图。

import numpy as np
import os
import cv2
import time
 
 
def color2gray(img_path, color_map):
    # 读取图片
    color_img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
    # 计算时间
    t0 = time.time()
    gray_img = np.zeros(shape=(color_img.shape[0], color_img.shape[1]), dtype=np.uint8)
    for i in range(color_map.shape[0]):
        index = np.where(np.all(color_img == color_map[i], axis=-1))  # np.all true false
        gray_img[index] = i
    t1 = time.time()
    time_cost = round(t1 - t0, 3)
    print(f"color2label  cost time {time_cost}")
    # 保存图片
    dir, name = os.path.split(img_path)
    save_dir='xxxxx'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)
    save_path = os.path.join(save_dir, name)
    cv2.imwrite(save_path, gray_img)
 
if __name__ == '__main__':
    # 你的colormap  注意:这个是BGR的!(RGB的反一下)
    cmap = np.array(
        [
            #你的标签有几类就写几个
            #(x,x,x)格式
        ]
    )
    # 文件路径
    img_dir = 'xxxxx'
    if not os.path.exists(img_dir):
        os.mkdir(img_dir)
    for img in os.listdir(img_dir):
        if not img.endswith((".png", ".jpg")):
            continue
        img_path = os.path.join(img_dir, img)
        color2gray(img_path, color_map=cmap)

参考:语义分割,将RGB三通道的lable转为单通道_语义分割 彩色标签转化为单通道-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值