AttributeError: 'Namespace' object has no attribute 'plots'
不知道你这个问题是否已经解决, 如果还没有解决的话:\qquad这个函数是用来可视化feature map的,而且可以实现可视化网络中任意一层的feature map。
函数代码:
def feature_visualization(x, module_type, stage, n=64):
"""用在yolo.py的Model类中的forward_once函数中 自行选择任意层进行可视化该层feature map
可视化feature map(模型任意层都可以用)
:params x: Features map [bs, channels, height, width]
:params module_type: Module type
:params stage: Module stage within model
:params n: Maximum number of feature maps to plot
"""
batch, channels, height, width = x.shape # batch, channels, height, width
if height > 1 and width > 1:
project, name = 'runs/features', 'exp'
save_dir = increment_path(Path(project) / name) # increment run
save_dir.mkdir(parents=True, exist_ok=True) # make save dir
plt.figure(tight_layout=True)
# torch.chunk: 与torch.cat()原理相反 将tensor x按dim(行或列)分割成channels个tensor块, 返回的是一个元组
# 将第2个维度(channels)将x分成channels份 每张图有三个block batch张图 blocks=len(blocks)=3*batch
blocks = torch.chunk(x, channels, dim=1)
n = min(n, len(blocks)) # 总共可视化的feature map数量
for i in range(n):
feature = transforms.ToPILImage()(blocks[i].squeeze()) # tensor -> PIL Image
ax = plt.subplot(int(math.sqrt(n)), int(math.sqrt(n)), i + 1) # 根号n行根号n列 当前属于第i+1张子图
ax.axis('off')
plt.imshow(feature) # cmap='gray' 可视化当前feature map
f = f"stage_{stage}_{module_type.split('.')[-1]}_features.png"
print(f'Saving {save_dir / f}...')
plt.savefig(save_dir / f, dpi=300)
通常这个函数是把他放在yolo.py的Model类中的forward_once函数中:
自己可以在if中选择要查看的任意一层feature map。
原图:
执行效果:
你好,请问这个问题你已经解决了吗?能请问下是怎么解决的吗?