问题描述:
出现问题的地方:
原因:由于在网络模型中用来resize来恢复特征图,而用这个函数得到是tuple的数据类型,在保存成图片的时候,就会出现以下的问题
解决办法:
要解决这个办法涉及到两个知识点:
一、数据类型的转换 tuple--->tensor
利用torch.cat方法
import torch
# 假设模型输出的特征图为 featurep, feature 是一个 tuple
# 获取特征图个数
num_maps = len(feature)
# 打印原来的特征图信息
print("type feature_raw:", type(outs))
for out in feature:
print(out.size())
print("len feature_raw:", num_maps)
# 按第 0 维度拼接特征图
feature = torch.cat([fm for fm in feature], dim=0)
# 检查特征图类型
print("type feature_map:", type(feature))
# 输出: <class 'torch.Tensor'>
# 检查特征图维度
print("size feature:", feature.size())
输出为:
type feature_raw: <class 'tuple'>
torch.Size([8, 32, 640, 640])
len feature_raw: 1
type feature_map: <class 'torch.Tensor'>
feature_map: torch.Size([8, 32, 640, 640])
二,如果在不用卷积的情况下如果将通道数压缩为1
img = torch.rand(4, 64, 192, 192)
print("size img:", img.size())
yasuo = torch.narrow(img, 1, 0, 1)
print("size yasuo:", yasuo.size())
打印的结果:
size img: torch.Size([4, 64, 192, 192])
size yasuo: torch.Size([4, 1, 192, 192])
这里选择不用卷积的情况压缩通道的原因是:此处是我在模型测试的时候需要输出中间特征图以作参考,考虑到卷积是权重并且模型的参数量不一样,故而选择这种不用卷积的情况将通道数压缩。