Tensorflow2.0:CNN、ResNet实现MNIST分类识别

以下仅是个人的学习笔记 ,内容可能是错误

CNN:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# 导入数据
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# 数据预处理
x_train = x_train.reshape(-1, 28, 28, 1) / 255.0
x_test = x_test.reshape(-1, 28, 28, 1) / 255.0

# 构建模型
model = keras.Sequential([
    layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
    layers.MaxPooling2D(pool_size=(2, 2)),
    layers.Flatten(),
    layers.Dense(10, activation='softmax')
])

# 编译模型
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))

# 评估模型
test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc)

ResNet18:

import tensorflow as tf
from keras import layers, models, datasets
import os

# 定义gpu
os.environ['CUDA_VISIBLE_DEVICES'] = '0'  # 指定GPU编号
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
    try:
        tf.config.experimental.set_memory_growth(gpus[0], True)  # 动态申请显存
    except RuntimeError as e:
        print(e)

# 加载数据集
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# 数据预处理
train_images, test_images = train_images / 255.0, test_images / 255.0


# 搭建残差模块
def resnet_block(inputs, num_filters=16, kernel_size=3, strides=1, activation='relu'):
    x = layers.Conv2D(num_filters, kernel_size=kernel_size, strides=strides, padding='same')(inputs)
    x = layers.BatchNormalization()(x)
    if activation:
        x = layers.Activation(activation)(x)
    return x


# 定义resnet
def resnet18():
    inputs = layers.Input(shape=(32, 32, 3))
    num_filters = 64
    t = layers.BatchNormalization()(inputs)
    t = resnet_block(t, num_filters=num_filters)
    for i in range(2):
        t = resnet_block(t, num_filters=num_filters, activation=None)
        t = layers.Add()([t, layers.Activation('relu')(t)])
    t = resnet_block(t, num_filters=num_filters * 2, strides=2, activation=None)
    t = layers.Add()([t, resnet_block(t, num_filters=num_filters * 2)])
    num_filters *= 2
    for i in range(2):
        t = resnet_block(t, num_filters=num_filters, activation=None)
        t = layers.Add()([t, layers.Activation('relu')(t)])
    t = resnet_block(t, num_filters=num_filters * 2, strides=2, activation=None)
    t = layers.Add()([t, resnet_block(t, num_filters=num_filters * 2)])
    num_filters *= 2
    for i in range(2):
        t = resnet_block(t, num_filters=num_filters, activation=None)
        t = layers.Add()([t, layers.Activation('relu')(t)])
    t = layers.AveragePooling2D()(t)
    outputs = layers.Dense(10, activation='softmax')(layers.Flatten()(t))
    model = models.Model(inputs, outputs)
    return model


# 定义模型
model = resnet18()
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

# 训练 CPU
# history = model.fit(train_images, train_labels, epochs=10,
#                     validation_data=(test_images, test_labels))

with tf.device('GPU:0'):  # 指定使用GPU
    history = model.fit(train_images, train_labels, epochs=10,
                        validation_data=(test_images, test_labels))
相关推荐
love you joyfully1 小时前
目标检测与R-CNN——paddle部分
人工智能·目标检测·cnn·paddle
AI视觉网奇2 小时前
Detected at node ‘truediv‘ defined at (most recent call last): Node: ‘truediv‘
人工智能·python·tensorflow
IT古董2 小时前
【机器学习】机器学习的基本分类-半监督学习(Semi-supervised Learning)
学习·机器学习·分类·半监督学习
-芒果酱-2 小时前
KNN分类算法 HNUST【数据分析技术】(2025)
分类·数据挖掘·数据分析
深圳市青牛科技实业有限公司 小芋圆6 小时前
开关电源特点、分类、工作方式
前端·科技·单片机·物联网·分类·数据挖掘·新能源
落魄君子7 小时前
BP分类-反向传播神经网络(Backpropagation Neural Network)
神经网络·分类
Easy数模8 小时前
基于LR/GNB/SVM/KNN/DT算法的鸢尾花分类和K-Means算法的聚类分析
算法·机器学习·支持向量机·分类·聚类
落魄君子9 小时前
RF分类-随机森林(Random Forest)
神经网络·分类
Kenneth風车11 小时前
【机器学习(九)】分类和回归任务-多层感知机(Multilayer Perceptron,MLP)算法-Sentosa_DSML社区版 (1)11
算法·机器学习·分类
Kenneth風车14 小时前
【机器学习(九)】分类和回归任务-多层感知机(Multilayer Perceptron,MLP)算法-Sentosa_DSML社区版 (1)111
算法·机器学习·分类