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非线性激活效果:

最大池化效果

平均池化效果:

相关推荐
星辰AI15 分钟前
多模态记忆:让 AI Agent 记忆各种类型的信息
人工智能·ai·语言模型
jiayong2316 分钟前
AI架构师面试题库 - 完整汇总文档
人工智能·面试·职场和发展
bigfootyazi19 分钟前
python爬虫-基本库-urllib库(常用速查)
开发语言·爬虫·python
后端小肥肠29 分钟前
效率狂飙9000%!Codex + HyperFrames 让一篇文章 5 分钟变视频
人工智能·aigc·agent
阿里云大数据AI技术33 分钟前
最佳实践:用 EMR Serverless StarRocks AI Function 实现金融行业文本分类
人工智能
瑶总迷弟33 分钟前
使用 mis-tei 在昇腾310P上部署 bge-m3模型
pytorch·python·华为·语言模型·自然语言处理·cnn·unix
belong_my_offer1 小时前
认识到精通函数
开发语言·python
miaowmiaow1 小时前
PSD2Code 近期更新与深度解析:从设计稿到生产级代码的完整技术栈
前端·人工智能·ai编程
云烟成雨TD1 小时前
Spring AI 1.x 系列【33】RAG Advisor 组件与四大分层架构
java·人工智能·spring
lifallen1 小时前
第一章 Agent 为什么会出现
人工智能·ai·ai编程