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

二、CV_AlexNet

二、AlexNet

1. AlexNet的模型构建

该网络的特点是:

  • AlexNet包含8层变换,有5层卷积和2层全连接层隐藏层,以及1个全连接输出层
  • AlexNet第一层中的卷积核形状是11×1111 \times 1111×11 。第二层中的卷积核形状减小到5×55 \times 55×5,之后全采用3×33\times 33×3。所有池化层窗口大小为3×33\times 33×3,步幅为2的最大池化
  • AlexNet将sigmoid激活函数改成了ReLU激活函数,使计算更简单,网络更容易训练
  • AlexNet通过dropOut来控制全连接层的模型复杂度
  • AlexNet引入了大量的图像增强,如反转,裁剪和颜色变化,从而进一步扩大数据集(提高数据量)来缓解过拟合

在tf.keras中实现AlexNet模型:

net = tf.keras.models.Sequential([# 卷积层:96(神经元个数)  11*11(卷积大小)  4(步长)  relu(激活函数)tf.keras.layers.Conv2D(filters = 96, kernel_size = 11, strides = 4, activation = "relu"),# 池化层:3*3  2(步长)tf.keras.layers.MaxPool2D(pool_size = 3, strides = 2),# 卷积:256  5*5  1  relu  sametf.keras.layers.Conv2D(filters = 256, kernel_size = 5, strides = 1, activation = "relu", padding = "same"),# 池化:3*3  2tf.keras.layers.MaxPool2D(pool_size = 3, strides = 2),# 卷积:384  3*3  1  relu  sametf.keras.layers.Conv2D(filters = 384, kernel_size = 3, strides = 1, activation = "relu", padding = "same"),# 卷积:384  3*3  1  relu  sametf.keras.layers.Conv2D(filters = 384, kernel_size = 3, strides = 1, activation = "relu", padding = "same"),# 卷积:256  3*3  1  relu  sametf.keras.layers.Conv2D(filters = 256, kernel_size = 3, strides = 1, activation = "relu", padding = "same"),# 池化:3*3  2tf.keras.layers.MaxPool2D(pool_size = 3, strides = 2),# 展开tf.keras.layers.Flatten(),# 全连接层:4096, relutf.keras.layers.Dense(4096, activation = 'relu'),# 随机失活tf.keras.layers.Dropout(0.5),# 输出层tf.keras.layers.Dense(10, activation = "softmax")
])

2. 手写数字识别

(1)数据读取

from tensorflow.keras.datasets import mnist
import numpy as np(train_images, train_labels), (test_images, test_labels) = mnist.load_data()# 维度调整
train_images = np.reshape(train_images, (train_images.shape[0], train_images.shape[1], train_images.shape[2], 1))
test_images = np.reshape(test_images, (test_images.shape[0], test_images.shape[1], test_images.shape[2], 1))# 对训练数据进行抽样
def get_train(size):index = np.random.randint(0, train_images.shape[0], size)# 选择图像进行resizeresizes_images = tf.image.resize_with_pad(train_images[index], 227, 277)return resized_images.numpy(), train_labels[index]# 对测试数据进行抽样
def get_test(size):index = np.random.randint(0, test_images.shape[0], size)resized_images = tf.image.resize_with_pad(test_images[index], 227, 227)return resize_images.numpy(), test_labels[index]train_images, train_labels = get_train(256)
test_images, train_labels = get_test(128)import matplotlib.pyplot as plt
plt.imshow(train_images[4].astype(int8).squeeze(), cmap = 'gray')

(2)模型编译

# 指定优化器,损失函数,评价指标
optimizer = tf.keras.optimizers.SGD(learning_rate = 0.01, momentum = 0.0, nesterov = False)net.compile(optimizer = optimizer,loss = 'sparse_categorical_crossentropy',metrics - ['accuracy']
)

(3)模型训练

# 模型训练:指定训练数据集,batchsize, epoch, 验证集
net.fit(train_images, train_labels, batch_size = 128, epochs = 3, verbose = 1, # 显示整个训练的logvalidation_split = 0.2) # 验证集

(4)模型评估

net.evaluate(test_images, test_labels, verbose = 1)
http://www.dtcms.com/a/282022.html

相关文章:

  • 牛客:HJ25 数据分类处理[华为机考][哈希][字符串]
  • nextjs+react项目如何代理本地请求解决跨域
  • NSSCTF CVE版签到
  • Win11专业工作站版安装配置要求
  • 实训十一——网络通信原理
  • 【Java】【力扣】94.二叉树的中序遍历
  • 通过 Docker 安装 MySQL
  • 手撕Spring底层系列之:IOC、AOP
  • Web前端性能优化原理与方法
  • 力扣面试150(31/150)
  • React之旅-09 useMemo,优化计算性能的利器
  • Python设计模式深度解析:建造者模式(Builder Pattern)完全指南
  • 浅析BLE/MQTT协议的区别
  • React 源码7:Lane、React和schedule优先级转换
  • Ansible 查看PostgreSQL的版本
  • beautiful-react-hooks库——入门实践常用hook详解
  • React Hooks 数据请求库——SWR使用详解
  • 为来时路,OCM拿证学习和考试
  • 产品经理笔试考试回忆集(2025湖南某国企)
  • 消息中间件(Kafka VS RocketMQ)
  • 每天一个前端小知识 Day 33 - 虚拟列表与长列表性能优化实践(Virtual Scroll)
  • 短剧App/小程序/H5多端开发全流程解析:从架构设计到性能优化
  • WPF 加载和显示 GIF 图片的完整指南
  • 借助AI学习开源代码git0.7之编译和使用
  • Gradle vs Maven:构建工具世纪对决 —— 像乐高积木与标准模型之间的选择艺术
  • SQL中对字符串字段模糊查询(LIKE)的索引命中情况
  • Git问题排查与故障解决详解
  • C++---emplace_back与push_back
  • 工业网络协议桥接设计指南:从LIN到CAN/RS-232的毫秒级互通方案
  • Adobe illustrator、klayout绘制光刻图及其尺寸映射