深度学习——pytorch卷积神经网络中间特征层可视化

前言

在我们使用神经网络的过程中,经常会好奇中间的网络到底学到了些什么,所以常常想用可视化的方法来输出这些特征层,所以惊天带大家用一个简易的网络来输出这些特征层。

搭建网络

from torch import nn
import torch
from torch.nn import functional as F
import cv2
from matplotlib import pyplot as plt
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1=nn.Conv2d(3,6,5)
        self.pool1=nn.MaxPool2d(4,4)
        self.conv2=nn.Conv2d(6,16,5)
        self.pool2=nn.MaxPool2d(4,4)

    def forward(self,x):
        output=[]     #保存特征层的列表,在最后的时候返回
        x=self.conv1(x)
        output.append(x)   #加入output中
        x=F.relu(x)
        output.append(x)
        x=self.pool1(x)
        output.append(x)
        x=self.conv2(x)
        output.append(x)
        x=F.relu(x)
        output.append(x)
        x=self.pool2(x)
        return x,output

net=Net()   #实例化网络,当然也可以选择导入预训练的权重,这里就不导入了

导入图像,并进行前向传播

path="dog.png"   #图像路径
img=cv2.imread(path)  #读取图片
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  #opencv读取的图片默认是bgr需要转换成rgb
img=torch.tensor(img,dtype=torch.float32) #opencv采用的是numpy,所以需要转成tensor
# img=torch.unsqueeze(img,0)
# img=torch.permute(img,(0,3,1,2))
img=torch.permute(img,(2,0,1))  #[348,348,3] --->[3,348,348]
print(img.shape)
_,output=net(img)  #前向传播,并返回特征层

打印特征层

for layer in output:
    fig = plt.figure()  # 定义画布
    # layer=torch.squeeze(layer)
    #print(layer.shape)
    layer = layer.detach().numpy() #将tensor转成numpy
    print(layer.shape)
    plt.subplots_adjust(wspace=0.05, hspace=0)  # 无缝布局,间距可改
    for i in range(layer.shape[0]):      #画出每一个通道的特征
        # plt.tight_layout() #紧密布局
        ax = fig.add_subplot(2, layer.shape[0]//2, i + 1, xticks=[], yticks=[])  
        plt.imshow(layer[i,:,:],cmap="gray")   #使用灰度图
    plt.show()

效果展示

卷积层输出
在这里插入图片描述
Relu之后
在这里插入图片描述
池化之后
在这里插入图片描述

完整源代码

from torch import nn
import torch
from torch.nn import functional as F
import cv2
from matplotlib import pyplot as plt
class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1=nn.Conv2d(3,6,5)
        self.pool1=nn.MaxPool2d(4,4)
        self.conv2=nn.Conv2d(6,16,5)
        self.pool2=nn.MaxPool2d(4,4)

    def forward(self,x):
        output=[]     #保存特征层的列表,在最后的时候返回
        x=self.conv1(x)
        output.append(x)   #加入output中
        x=F.relu(x)
        output.append(x)
        x=self.pool1(x)
        output.append(x)
        x=self.conv2(x)
        output.append(x)
        x=F.relu(x)
        output.append(x)
        x=self.pool2(x)
        return x,output

net=Net()   #实例化网络,当然也可以选择导入预训练的权重,这里就不导入了

path="dog.png"
img=cv2.imread(path)
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img=torch.tensor(img,dtype=torch.float32)
# img=torch.unsqueeze(img,0)
# img=torch.permute(img,(0,3,1,2))
img=torch.permute(img,(2,0,1))
print(img.shape)
_,output=net(img)
# print(output)
for layer in output:
    fig = plt.figure()  # 定义画布
    # layer=torch.squeeze(layer)
    #print(layer.shape)
    layer = layer.detach().numpy()
    print(layer.shape)
    plt.subplots_adjust(wspace=0.05, hspace=0)  # 无缝布局,间距可改
    for i in range(layer.shape[0]):
        # plt.tight_layout() #紧密布局
        ax = fig.add_subplot(2, layer.shape[0]//2, i + 1, xticks=[], yticks=[])  # 2行4列,坐标为空
        plt.imshow(layer[i,:,:],cmap="gray")
    plt.show()

  • 1
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千禧皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值