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))
相关推荐
古人诚不我欺12 小时前
jmeter常用配置元件介绍总结之后置处理器
人工智能·jmeter·tensorflow
小馒头学python14 小时前
【机器学习】突破分类瓶颈:用逻辑回归与Softmax回归解锁多分类世界
人工智能·python·算法·机器学习·分类·回归·逻辑回归
天蓝蓝235281 天前
机器学习中的分类:决策树、随机森林及其应用
决策树·机器学习·分类
友大冰1 天前
前端开发中的CSS框架:昔日辉煌与新兴潮流
前端·css·算法·开源·tensorflow
极客代码1 天前
【Python TensorFlow】进阶指南(续篇一)
开发语言·人工智能·python·深度学习·ai·tensorflow
极客代码1 天前
【Python TensorFlow】进阶指南(续篇二)
开发语言·人工智能·python·深度学习·tensorflow
严文文-Chris1 天前
【卷积神经网络】
人工智能·神经网络·cnn
代码猪猪傻瓜coding2 天前
关于几种卷积
深度学习·神经网络·cnn
义小深2 天前
TensorFlow|猫狗识别
人工智能·python·tensorflow
Matlab程序猿小助手2 天前
【MATLAB源码-第209期】基于matlab的MSK调制解调仿真,对比三种解调方法的误码率分别是相干解调,1比特差分,2比特差分。
开发语言·算法·matlab·cnn·智能电视