当前位置: 首页 > news >正文

最棒的网站建设公司seo推广营销网站

最棒的网站建设,公司seo推广营销网站,北京微信网站搭建费用,做烘焙网站案例-图像分类 网络结构: 卷积BN激活池化 数据集介绍 CIFAR-10数据集5万张训练图像、1万张测试图像、10个类别、每个类别有6k个图像,图像大小32323。下图列举了10个类,每一类随机展示了10张图片: 特征图计算 在卷积层和池化层结束后, 将特征…

案例-图像分类

网络结构: 卷积+BN+激活+池化

数据集介绍

CIFAR-10数据集5万张训练图像、1万张测试图像、10个类别、每个类别有6k个图像,图像大小32×32×3。下图列举了10个类,每一类随机展示了10张图片:

特征图计算

在卷积层和池化层结束后, 将特征图变形成一行n列数据, 计算特征图进行变化, 映射到全连接层时输入层特征为最后一层卷积层经池化后的特征图各维度相乘

具体流程-# Acc: 0.728

# 导包
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchsummary import summary
from torchvision.datasets import CIFAR10
from torchvision.transforms import ToTensor, Compose  # Compose: 数据增强(扩充数据集)
import time
import matplotlib.pyplot as plt
​
batch_size = 16
​
​
# 创建数据集
def create_dataset():torch.manual_seed(21)train = CIFAR10(root='data',train=True,transform=Compose([ToTensor()]))test = CIFAR10(root='data',train=False,transform=Compose([ToTensor()]))return train, test
​
​
# 创建模型
class ImgCls(nn.Module):# 定义网络结构def __init__(self):super(ImgCls, self).__init__()# 定义网络层:卷积层+池化层self.conv1 = nn.Conv2d(3, 16, stride=1, kernel_size=3)self.batch_norm_layer1 = nn.BatchNorm2d(num_features=16, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
​self.conv2 = nn.Conv2d(16, 32, stride=1, kernel_size=3)self.batch_norm_layer2 = nn.BatchNorm2d(num_features=32, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool2 = nn.MaxPool2d(kernel_size=2, stride=1)
​self.conv3 = nn.Conv2d(32, 64, stride=1, kernel_size=3)self.batch_norm_layer3 = nn.BatchNorm2d(num_features=64, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool3 = nn.MaxPool2d(kernel_size=2, stride=1)
​self.conv4 = nn.Conv2d(64, 128, stride=1, kernel_size=2)self.batch_norm_layer4 = nn.BatchNorm2d(num_features=128, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)
​self.conv5 = nn.Conv2d(128, 256, stride=1, kernel_size=2)self.batch_norm_layer5 = nn.BatchNorm2d(num_features=256, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool5 = nn.MaxPool2d(kernel_size=2, stride=1)
​# 全连接层self.linear1 = nn.Linear(1024, 2048)self.linear2 = nn.Linear(2048, 1024)self.linear3 = nn.Linear(1024, 512)self.linear4 = nn.Linear(512, 256)self.linear5 = nn.Linear(256, 128)self.out = nn.Linear(128, 10)
​# 定义前向传播def forward(self, x):# 第1层: 卷积+BN+激活+池化x = self.conv1(x)x = self.batch_norm_layer1(x)x = torch.rrelu(x)x = self.pool1(x)
​# 第2层: 卷积+BN+激活+池化x = self.conv2(x)x = self.batch_norm_layer2(x)x = torch.rrelu(x)x = self.pool2(x)
​# 第3层: 卷积+BN+激活+池化x = self.conv3(x)x = self.batch_norm_layer3(x)x = torch.rrelu(x)x = self.pool3(x)
​# 第4层: 卷积+BN+激活+池化x = self.conv4(x)x = self.batch_norm_layer4(x)x = torch.rrelu(x)x = self.pool4(x)
​# 第5层: 卷积+BN+激活+池化x = self.conv5(x)x = self.batch_norm_layer5(x)x = torch.rrelu(x)x = self.pool5(x)
​# 将特征图做成以为向量的形式:相当于特征向量x = x.reshape(x.size(0), -1)  # 将3维特征图转化为1维向量(1, n)
​# 全连接层x = torch.rrelu(self.linear1(x))x = torch.rrelu(self.linear2(x))x = torch.rrelu(self.linear3(x))x = torch.rrelu(self.linear4(x))x = torch.rrelu(self.linear5(x))# 返回输出结果return self.out(x)
​
​
# 训练
def train(model, train_dataset, epochs):torch.manual_seed(21)loss = nn.CrossEntropyLoss()opt = optim.Adam(model.parameters(), lr=1e-4)for epoch in range(epochs):dataloader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)loss_total = 0iter = 0stat_time = time.time()for x, y in dataloader:output = model(x.to(device))loss_value = loss(output, y.to(device))opt.zero_grad()loss_value.backward()opt.step()loss_total += loss_value.item()iter += 1print(f'epoch:{epoch + 1:4d}, loss:{loss_total / iter:6.4f}, time:{time.time() - stat_time:.2f}s')torch.save(model.state_dict(), 'model/img_cls_model.pth')
​
​
# 测试
def test(valid_dataset, model, batch_size):# 构建数据加载器dataloader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
​# 计算精度total_correct = 0# 遍历每个batch的数据,获取预测结果,计算精度for x, y in dataloader:output = model(x.to(device))y_pred = torch.argmax(output, dim=-1)total_correct += (y_pred == y.to(device)).sum()# 打印精度print(f'Acc: {(total_correct.item() / len(valid_dataset))}')
​
​
if __name__ == '__main__':batch_size = 16device = torch.device("cuda" if torch.cuda.is_available() else "cpu")# 获取数据集train_data, test_data = create_dataset()
​# # 查看数据集# print(f'数据集类别: {train_data.class_to_idx}')# print(f'训练集: {train_data.data.shape}')# print(f'验证集: {test_data.data.shape}')# print(f'类别数量: {len(np.unique(train_data.targets))}')# # 展示图像# plt.figure(figsize=(8, 8))# plt.imshow(train_data.data[0])# plt.title(train_data.classes[train_data.targets[0]])# plt.show()
​# 实例化模型model = ImgCls().to(device)
​# 查看网络结构summary(model, (3, 32, 32), device='cuda', batch_size=batch_size)
​# 模型训练train(model, train_data, epochs=60)# 加载训练好的模型参数model.load_state_dict(torch.load('model/img_cls_model.pth'))model.eval()# 模型评估test(test_data, model, batch_size=16)   # Acc: 0.728
​

调整网络结构

第一次调整: 训练50轮, Acc: 0.71

第二次调整: 训练30轮, Acc:0.7351

第三次调整: batch_size=8, epoch=50 => Acc: 0.7644

# 导包
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchsummary import summary
from torchvision.datasets import CIFAR10
from torchvision.transforms import ToTensor, Compose  # Compose: 数据增强(扩充数据集)
import time
import matplotlib.pyplot as plt
​
batch_size = 16
​
​
# 创建数据集
def create_dataset():torch.manual_seed(21)train = CIFAR10(root='data',train=True,transform=Compose([ToTensor()]))test = CIFAR10(root='data',train=False,transform=Compose([ToTensor()]))return train, test
​
​
# 创建模型
class ImgCls(nn.Module):# 定义网络结构def __init__(self):super(ImgCls, self).__init__()# 定义网络层:卷积层+池化层self.conv1 = nn.Conv2d(3, 16, stride=1, kernel_size=3, padding=1)self.batch_norm_layer1 = nn.BatchNorm2d(num_features=16, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
​self.conv2 = nn.Conv2d(16, 32, stride=1, kernel_size=3, padding=1)self.batch_norm_layer2 = nn.BatchNorm2d(num_features=32, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
​self.conv3 = nn.Conv2d(32, 64, stride=1, kernel_size=3, padding=1)self.batch_norm_layer3 = nn.BatchNorm2d(num_features=64, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool3 = nn.MaxPool2d(kernel_size=2, stride=1)
​self.conv4 = nn.Conv2d(64, 128, stride=1, kernel_size=3, padding=1)self.batch_norm_layer4 = nn.BatchNorm2d(num_features=128, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool4 = nn.MaxPool2d(kernel_size=2, stride=1)
​self.conv5 = nn.Conv2d(128, 256, stride=1, kernel_size=3)self.batch_norm_layer5 = nn.BatchNorm2d(num_features=256, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True)self.pool5 = nn.MaxPool2d(kernel_size=2, stride=2)
​# 全连接层self.linear1 = nn.Linear(1024, 2048)self.linear2 = nn.Linear(2048, 1024)self.linear3 = nn.Linear(1024, 512)self.linear4 = nn.Linear(512, 256)self.linear5 = nn.Linear(256, 128)self.out = nn.Linear(128, 10)
​# 定义前向传播def forward(self, x):# 第1层: 卷积+BN+激活+池化x = self.conv1(x)x = self.batch_norm_layer1(x)x = torch.relu(x)x = self.pool1(x)
​# 第2层: 卷积+BN+激活+池化x = self.conv2(x)x = self.batch_norm_layer2(x)x = torch.relu(x)x = self.pool2(x)
​# 第3层: 卷积+BN+激活+池化x = self.conv3(x)x = self.batch_norm_layer3(x)x = torch.relu(x)x = self.pool3(x)
​# 第4层: 卷积+BN+激活+池化x = self.conv4(x)x = self.batch_norm_layer4(x)x = torch.relu(x)x = self.pool4(x)
​# 第5层: 卷积+BN+激活+池化x = self.conv5(x)x = self.batch_norm_layer5(x)x = torch.rrelu(x)x = self.pool5(x)
​# 将特征图做成以为向量的形式:相当于特征向量x = x.reshape(x.size(0), -1)  # 将3维特征图转化为1维向量(1, n)
​# 全连接层x = torch.relu(self.linear1(x))x = torch.relu(self.linear2(x))x = torch.relu(self.linear3(x))x = torch.relu(self.linear4(x))x = torch.rrelu(self.linear5(x))# 返回输出结果return self.out(x)
​
​
# 训练
def train(model, train_dataset, epochs):torch.manual_seed(21)loss = nn.CrossEntropyLoss()opt = optim.Adam(model.parameters(), lr=1e-4)for epoch in range(epochs):dataloader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)loss_total = 0iter = 0stat_time = time.time()for x, y in dataloader:output = model(x.to(device))loss_value = loss(output, y.to(device))opt.zero_grad()loss_value.backward()opt.step()loss_total += loss_value.item()iter += 1print(f'epoch:{epoch + 1:4d}, loss:{loss_total / iter:6.4f}, time:{time.time() - stat_time:.2f}s')torch.save(model.state_dict(), 'model/img_cls_model1.pth')
​
​
# 测试
def test(valid_dataset, model, batch_size):# 构建数据加载器dataloader = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)
​# 计算精度total_correct = 0# 遍历每个batch的数据,获取预测结果,计算精度for x, y in dataloader:output = model(x.to(device))y_pred = torch.argmax(output, dim=-1)total_correct += (y_pred == y.to(device)).sum()# 打印精度print(f'Acc: {(total_correct.item() / len(valid_dataset))}')
​
​
if __name__ == '__main__':batch_size = 8device = torch.device("cuda" if torch.cuda.is_available() else "cpu")# 获取数据集train_data, test_data = create_dataset()
​# # 查看数据集# print(f'数据集类别: {train_data.class_to_idx}')# print(f'训练集: {train_data.data.shape}')# print(f'验证集: {test_data.data.shape}')# print(f'类别数量: {len(np.unique(train_data.targets))}')# # 展示图像# plt.figure(figsize=(8, 8))# plt.imshow(train_data.data[0])# plt.title(train_data.classes[train_data.targets[0]])# plt.show()
​# 实例化模型model = ImgCls().to(device)
​# 查看网络结构summary(model, (3, 32, 32), device='cuda', batch_size=batch_size)
​# 模型训练train(model, train_data, epochs=50)# 加载训练好的模型参数model.load_state_dict(torch.load('model/img_cls_model1.pth', weights_only=True))model.eval()# 模型评估test(test_data, model, batch_size=16)   # Acc: 0.7644
​


文章转载自:
http://orchidist.c7625.cn
http://rhapidosome.c7625.cn
http://aloha.c7625.cn
http://transformism.c7625.cn
http://sonolyse.c7625.cn
http://goss.c7625.cn
http://slangy.c7625.cn
http://nebulium.c7625.cn
http://verruca.c7625.cn
http://corallaceous.c7625.cn
http://circumnutate.c7625.cn
http://lengthwise.c7625.cn
http://wallonian.c7625.cn
http://amalgamative.c7625.cn
http://alacrity.c7625.cn
http://constructivist.c7625.cn
http://epigraphy.c7625.cn
http://muezzin.c7625.cn
http://carbenoxolone.c7625.cn
http://trappean.c7625.cn
http://formosan.c7625.cn
http://honies.c7625.cn
http://napper.c7625.cn
http://plasterer.c7625.cn
http://gop.c7625.cn
http://unkindness.c7625.cn
http://lactiferous.c7625.cn
http://conveyorize.c7625.cn
http://goldbeater.c7625.cn
http://clack.c7625.cn
http://tanager.c7625.cn
http://epochal.c7625.cn
http://adrate.c7625.cn
http://defamation.c7625.cn
http://scabble.c7625.cn
http://shellheap.c7625.cn
http://superelevate.c7625.cn
http://trample.c7625.cn
http://dicotyledonous.c7625.cn
http://thioester.c7625.cn
http://circinus.c7625.cn
http://rad.c7625.cn
http://bijection.c7625.cn
http://lager.c7625.cn
http://illegalize.c7625.cn
http://count.c7625.cn
http://horsemint.c7625.cn
http://plasmolyze.c7625.cn
http://triteness.c7625.cn
http://anaplastic.c7625.cn
http://claqueur.c7625.cn
http://grum.c7625.cn
http://infallibility.c7625.cn
http://kleig.c7625.cn
http://chantry.c7625.cn
http://ragged.c7625.cn
http://guyot.c7625.cn
http://ammonification.c7625.cn
http://berline.c7625.cn
http://prodigy.c7625.cn
http://thermion.c7625.cn
http://roentgenite.c7625.cn
http://snug.c7625.cn
http://viridin.c7625.cn
http://walachian.c7625.cn
http://annihilate.c7625.cn
http://hygristor.c7625.cn
http://hubless.c7625.cn
http://kleptomania.c7625.cn
http://fief.c7625.cn
http://functionary.c7625.cn
http://bierhaus.c7625.cn
http://thermoammeter.c7625.cn
http://jerfalcon.c7625.cn
http://polystichous.c7625.cn
http://incommunicability.c7625.cn
http://tymbal.c7625.cn
http://extine.c7625.cn
http://radiumize.c7625.cn
http://mammock.c7625.cn
http://newsdealer.c7625.cn
http://telaesthesia.c7625.cn
http://countdown.c7625.cn
http://koei.c7625.cn
http://cithaeron.c7625.cn
http://liquefier.c7625.cn
http://corrugated.c7625.cn
http://soldierly.c7625.cn
http://thiophenol.c7625.cn
http://eigenvalue.c7625.cn
http://jay.c7625.cn
http://introspect.c7625.cn
http://underpay.c7625.cn
http://proclamation.c7625.cn
http://grayish.c7625.cn
http://vegetable.c7625.cn
http://spenserian.c7625.cn
http://unmodulated.c7625.cn
http://runnerless.c7625.cn
http://zooid.c7625.cn
http://www.zhongyajixie.com/news/88678.html

相关文章:

  • wordpress开发论坛seo如何挖掘关键词
  • 做网站充值系统巩义网站推广优化
  • 北京建设监理协会网站网络推广方法有几种
  • seo网站优化培训班抖音排名优化
  • 如何做一个网站平台360优化大师旧版
  • 网站风格天天网站
  • 网站建设类型手机网站搜索优化
  • 做网站违反广告法深圳今天重大事件新闻
  • 哪个网站可以做图交易平台sem竞价托管多少钱
  • 网站制作的设备环境营销宝
  • 成都必去景点排名海淀区seo引擎优化
  • 小网站做几个关键词seo技术优化技巧
  • 91人才网赣州招聘网seo基础入门免费教程
  • 网站模板下企业网站建设制作
  • 阿里网站建设工具百度我的订单app
  • 什么样的网站需要备案产品网络推广深圳
  • 17做网站广州如何做网站
  • 昆明做网站建设找谁网络推广技巧
  • 南京网页网站制作如何开通网站
  • b2b建设网站公司广东百度推广的代理商
  • 泉州网站建设价格广东疫情防控措施
  • 正邦网站建设 优帮云搜索引擎营销的案例
  • 考试系统 微网站是什么样的大学生网络营销策划方案书
  • 建个什么网站百度竞价推广开户费用
  • 做推广网站网站收录有什么用
  • 吉林省招标网官方网站做网络销售感觉自己是骗子
  • 京津冀协同发展规划纲要北京seo招聘信息
  • 单位网站建设情况2024疫情最新消息今天
  • 网址是什么系统优化的方法
  • 做搬家网站的素材南宁百度关键词推广