python如何画神经网络特征图

1.构造绘制特征图的函数

width, height为特征图的宽和高,x为数据,savename为保存的图片路径:

def draw_features(width, height, x, savename):
    tic = time.time()
    fig = plt.figure(figsize=(16, 16))
    fig.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95, wspace=0.05, hspace=0.05)
    for i in range(width*height): #
        plt.subplot(height, width,i+1)
        plt.axis('off')
        img = x[0, i, :, :] # b c h w
        pmin = np.min(img)
        pmax = np.max(img)

        img = (img - pmin) / (pmax - pmin + 0.000001)
        plt.imshow(img, cmap='gray')
        print("{}/{}".format(i, width * height))
    fig.savefig(savename, dpi=100)
    fig.clf()
    plt.close()
    print("time:{}".format(time.time() - tic))

2.调用绘制函数

以RPN网络为例:

class UP(RPN):
    def __init__(self, anchor_num=5, feature_in=256, feature_out=256):
        super(UP, self).__init__()

        self.anchor_num = anchor_num
        self.feature_in = feature_in
        self.feature_out = feature_out

        self.cls_output = 2 * self.anchor_num
        self.loc_output = 4 * self.anchor_num

        self.cls = DepthCorr(feature_in, feature_out, self.cls_output)
        self.loc = DepthCorr(feature_in, feature_out, self.loc_output)

    def forward(self, z_f, x_f):
        cls = self.cls(z_f, x_f)

        loc = self.loc(z_f, x_f)
        draw_features(3, 3, loc.detach().numpy(), "{}/loc_map.png".format(savepath))
        return cls, loc

此处可能会根据数据格式对第三个参数loc.detach().numpy()进行一定的修改,否则可能会有错误产生。产生的特征图如下所示:python如何画神经网络特征图_第1张图片


你可能感兴趣的:(计算机视觉,神经网络)