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

最大池化效果

平均池化效果:

相关推荐
GEO实战经验分享3 分钟前
王涛:GEO 服务商能力评估清单——基础、结构、战略、风控四层怎么核验
大数据·人工智能·chatgpt
干就完事了4 分钟前
机器学习-音乐艺人流行趋势预测
人工智能·机器学习·阿里云
财经科技社5 分钟前
从卧室到卫生间,流感季家庭环境消毒怎么做?
人工智能·科技
量化吞吐机5 分钟前
会写代码之后,量化学习还要补上交易判断
人工智能·python
IvorySQL6 分钟前
PostgreSQL 日报|在线校验和特性被回退(9 月 17 日)
数据库·人工智能·postgresql
nvvas10 分钟前
Spring AI 2.0正式GA:Token砍掉64%,Java的AI反击战打响了
java·人工智能
PPIO派欧云10 分钟前
DeepSeek V4.1-Flash 更新后,企业如何管理模型版本、成本与稳定性?
人工智能
ZPC821019 分钟前
YOLO 识别串果西红柿果梗(果柄)完整方案(适配你的采摘机器人)
人工智能
lie..23 分钟前
30天从零开始学AI应用开发(Day 1):机器学习、深度学习、大模型,到底是个啥关系
人工智能·python·大模型
Ivanqhz26 分钟前
Unigram 算法
开发语言·人工智能·python·深度学习·mlir