以下仅是个人的学习笔记 ,内容可能是错误
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))