在用自己的训练集训练DeeplabV3+的时候,遇到了这样的问题。
报错解析:
这个错误表明你的 CrossEntropyLoss
期望输入的 target
是 2D 空间维度的张量 (即 [batch_size, height, width]
),而你传递的 target
是一个 4D 张量,维度是 [batch_size, height, width, 3]
。这是因为你的目标张量有 3 个通道,而 CrossEntropyLoss
只需要单通道(类别标签)。
解决方案:
-
目标张量通道数问题: 你的目标张量有 3 个通道,通常来说,
CrossEntropyLoss
只需要单通道的标签,即每个像素点的类别索引。如果目标张量是 RGB 图像,可能是数据预处理的问题,需要将其转换为单通道的类别标签。 -
确保
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)