pytorch+tensorboard+可视化CNN

数据预处理:

python 复制代码
transform = transforms.Compose([
    transforms.Resize((224,224)),
    transforms.ToTensor(),
    transforms.Normalize(
        mean=[0.5, 0.5, 0.5],
        std=[0.5, 0.5, 0.5]
    )
])

改变了尺寸、归一化

加载数据集:

python 复制代码
fold_path = '../images'
dataset = ImageFolder(fold_path,transform=transform)
dataloader = DataLoader(dataset,batch_size=1)

定义网络结构并实例化

python 复制代码
class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = nn.Conv2d(3,6,3,1,0)
        self.bn1 = nn.BatchNorm2d(6)
        self.relu1 = nn.ReLU()
        self.pool1 = nn.MaxPool2d(2,2)
        #self.pool2 = nn.AvgPool2d(2,2)
        self.flatten1 = nn.Flatten()
        self.linear = nn.Linear(111*111*6,2)

    def forward(self,x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu1(x)
        x = self.pool1(x)
        #x = self.pool2(x)
        x = self.flatten1(x)
        x = self.linear(x)
        return x

#实例化网络
net = Net()

效果展示:

output = torch.reshape(output,(-1,3,111,111))

这个地方是池化之后是这样的

池化之前是

output = torch.reshape(output,(-1,3,222,222))

python 复制代码
writer = SummaryWriter('../hcy_logs')

cnt = 0
for data in dataloader:
    img,label = data
    print(img.shape)
    output = net(img)
    print(output.shape)
    #writer.add_images('input',img,cnt)
    output = torch.reshape(output,(-1,3,111,111))
    writer.add_images('output',output,cnt)
    cnt += 1

writer.close()

原图:(量变临界点 强推 wyy可听)

原图归一化后效果:

卷积后效果 卷积核是3*3 stride=1 padding=0

BN 批量归一化效果:

relu非线性激活效果:

最大池化效果

平均池化效果:

相关推荐
大飞记Python12 分钟前
【2026更新】Python基础学习指南(AI版)——04数据类型
开发语言·人工智能·python
Marvel__Dead16 分钟前
AI 大模型时代:验证码如何用「通用识别」解决?
人工智能·ai 大模型·ai 验证码识别·ai 爬虫
生成论实验室22 分钟前
《事件关系阴阳博弈动力学:识势应势之道》第四篇:降U动力学——认知确定度的自驱演化
人工智能·科技·神经网络·算法·架构
不懂的浪漫28 分钟前
把 AI Skill 做成系统:路由、领域技能、自我复盘和进化飞轮
人工智能·ai·skill
等风来不如迎风去35 分钟前
【win11】最佳性能:fix 没有壁纸,一直黑屏
网络·人工智能
AI科技星35 分钟前
全域数学·72分册:场计算机卷【乖乖数学】
算法·机器学习·数学建模·数据挖掘·量子计算
云云只是个程序马喽38 分钟前
AI漫剧创作系统开发定制指南
人工智能·小程序·php
Elastic 中国社区官方博客1 小时前
Elastic 和 Cursor 合作 加速 上下文工程 与 coding agents
大数据·人工智能·elasticsearch·搜索引擎·全文检索
迦南的迦 亚索的索1 小时前
AI_12_Dify_平台介绍
人工智能
HIT_Weston1 小时前
68、【Agent】【OpenCode】用户对话提示词(任务执行流程)
人工智能·agent·opencode