先进行设备检查,tensorflow和pytorch的cuda不一样,pytorch上的cuda有效不代表tf上就可以
import tensorflow as tf
# 列出所有物理GPU设备
gpus = tf.config.list_physical_devices('GPU')
if gpus:
print("检测到了GPU设备:")
for gpu in gpus:
try:
# 设置GPU显存用量按需使用
tf.config.experimental.set_memory_growth(gpu, True)
print(f"GPU设备: {gpu}")
except RuntimeError as e:
print(e)
else:
print("没有检测到GPU设备,将使用CPU。")
import matplotlib.pyplot as plt
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
import os
import numpy as np
# 设置随机种子尽可能使结果可以重现
np.random.seed(1)
tf.random.set_seed(1)
from tensorflow.keras import layers, models
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import pathlib
数据集选择imagenet上的100类别,可以直接下载,不用额外传入
# 数据目录
data_dir = "D:/pythonproject/imagenet/dataset/train"
data_dir = pathlib.Path(data_dir)
# 数据集参数
batch_size = 8
img_height = 224
img_width = 224
# 数据集参数
batch_size = 8
img_height = 224
img_width = 224
加载训练集和验证集,由于imagenet上的训练集很大,可以直接从训练集中划分,或者使用自带的验证集
# 加载训练集和验证集
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size,
shuffle=True
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size,
shuffle=False
)
进行数据增强
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
# 其他数据增强参数...
)
直接检查类别名称,imagenet文件夹自带标签,不用再去额外传入标签
# 类别名称
class_names = train_ds.class_names
print(class_names)
#['1.10burger_2', '1.1hotdog', '1.2bread', '1.3burger', '1.4lemon', '1.5squash', '1.6mushroom', '1.7apple_2', '1.8simit', '1.9hotdog_2', '2.10ship', '2.1car_2', '2.2tank', '2.3wheel', '2.4wheel_2', '2.5scooter', '2.6forklift', '2.7bike', '2.8traffic_light', '2.9warplane', '3.10vase', '3.1sofa_2', '3.2crate', '3.3toaster', '3.4chair', '3.5bathing', '3.6sofa_3', '3.7piano', '3.8chair_2', '3.9washer', '4.10type', '4.1mic', '4.2computer', '4.3mouse', '4.4camera', '4.5control', '4.6lamp', '4.7guitar', '4.8iron', '4.9keyboard', '5.10manhole', '5.1trash_bin', '5.2bottle', '5.3shoe', '5.4basketball', '5.5shoe_2', '5.6pool_table', '5.7sign', '5.8sign_2', '5.9sign_3', '6.10volleyball', '6.11barrel', '6.12mic_2', '6.13scooter_2', '6.14tool', '6.15shoe_3', '6.16candle', '6.17coffeepot', '6.18vase_2', '6.19teapot', '6.1clock', '6.20plane', '6.21car_4', '6.22soap', '6.23ipod', '6.24cassette', '6.25sofa_5', '6.26lip', '6.27sax', '6.28cart', '6.29cart_2', '6.2clock_2', '6.30barrel_2', '6.31light', '6.32desk', '6.33coffee', '6.34cup', '6.35goblet', '6.36umbrella', '6.37helmet', '6.38syringe', '6.39swing', '6.3warplane_2', '6.40teddy', '6.41screwdriver', '6.42bank', '6.43lock', '6.44abacus', '6.45knife', '6.46hourglass', '6.47hammer', '6.48telephone', '6.49binoculars', '6.4chair_3', '6.50barbell', '6.5car_3', '6.6sofa_4', '6.7glass', '6.8phone', '6.9guitar_2']
配置数据集
# 配置数据集
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
然后就到了最重要的构建resnet50网络的环节,如果要使用预训练的训练权重有完全不同的方法,预训练的权重在torchvision上有传入的代码,imagenet同理也有
# 只从 tensorflow.keras 导入
from tensorflow.keras import layers, models
from tensorflow.keras.layers import Input, Activation, BatchNormalization, Flatten
from keras.layers import Dense, Conv2D, MaxPooling2D, ZeroPadding2D, AveragePooling2D
def identity_block(input_tensor, kernel_size, filters, stage, block):
filters1, filters2, filters3 = filters
name_base = str(stage) + block + '_identity_block_block_'
x = Conv2D(filters1, (1,1), name=name_base + 'conv1')(input_tensor)
x = BatchNormalization(name=name_base + 'bn1')(x)
x = Activation('relu', name=name_base + 'relu1')(x)
x = Conv2D(filters2, kernel_size, padding='same', name=name_base + 'conv2')(x)
x = BatchNormalization(name=name_base + 'bn2')(x)
x = Activation('relu', name=name_base + 'relu2')(x)
x = Conv2D(filters3, (1,1), name=name_base + 'conv3')(x)
x = BatchNormalization(name=name_base + 'bn3')(x)
x = layers.add([x, input_tensor], name=name_base + 'add')
x = Activation('relu', name=name_base + 'relu4')(x)
return x
def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):
filters1, filters2, filters3 = filters
res_name_base = str(stage) + block + '_conv_block_res_'
name_base = str(stage) + block + '_conv_block_'
x = Conv2D(filters1, (1,1), strides=strides, name=name_base + 'conv1')(input_tensor)
x = BatchNormalization(name=name_base + 'bn1')(x)
x = Activation('relu', name=name_base + 'relu1')(x)
x = Conv2D(filters2, kernel_size, padding='same', name=name_base + 'conv2')(x)
x = BatchNormalization(name=name_base + 'bn2')(x)
x = Activation('relu', name=name_base + 'relu2')(x)
x = Conv2D(filters3, (1,1), name=name_base + 'conv3')(x)
x = BatchNormalization(name=name_base + 'bn3')(x)
shortcut = Conv2D(filters3, (1,1), strides=strides, name=res_name_base + 'conv')(input_tensor)
shortcut = BatchNormalization(name=name_base + 'bn')(shortcut)
x = layers.add([x, shortcut], name=name_base + 'add')
x = Activation('relu', name=name_base + 'relu4')(x)
return x
def ResNet50(input_shape=[224,224,3], classes=1000):
img_input = Input(shape=input_shape)
x = ZeroPadding2D((3, 3))(img_input)
x = Conv2D(64, (7,7), strides=(2,2), name='conv1')(x)
x = BatchNormalization(name='bn_conv1')(x)
x = Activation('relu')(x)
x = MaxPooling2D((3, 3), strides=(2,2))(x)
x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')
x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')
x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')
x = AveragePooling2D((7,7), name='avg_pool')(x)
x = Flatten()(x)
# 修改输出层以匹配您的类别数
x = Dense(classes, activation='softmax', name='fc' + str(classes))(x)
model = models.Model(inputs=img_input, outputs=x, name='resnet50')
return model
model = ResNet50()
model.summary()
然后进行编译与训练,训练轮次其实很小就能取得不错的效果,在如此臃肿的架构下太高的训练轮次很容易造成过拟合:
# 编译模型
opt = tf.keras.optimizers.Adam(learning_rate=1e-5) # 调整学习率
model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 训练模型
epochs = 5
history = model.fit(train_ds, validation_data=val_ds, epochs=epochs)
保存模型,下次可直接调用预训练的权重:
# 指定保存模型的路径
model_save_path = 'path_to_save_model/resnet50_ten_epochs'
# 保存整个模型,包括架构、权重和编译信息
tf.keras.models.save_model(model, model_save_path, save_format='tf')
print(f"模型已保存到 {model_save_path}")
可视化模型训练效果:
# 评估模型并绘制结果
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss = history.history['loss']
val_loss = history.history['val_loss']
# 绘制平滑曲线(可选,这里简单使用移动平均)
import numpy as np
smoothed_acc = np.convolve(acc, np.ones(5)/5, mode='valid')
smoothed_val_acc = np.convolve(val_acc, np.ones(5)/5, mode='valid')
smoothed_loss = np.convolve(loss, np.ones(5)/5, mode='valid')
smoothed_val_loss = np.convolve(val_loss, np.ones(5)/5, mode='valid')
# 绘制训练和验证的准确率和损失
plt.figure(figsize=(14, 8))
plt.subplot(1, 2, 1)
plt.plot(range(len(smoothed_acc)), smoothed_acc, label='Training Accuracy (Smoothed)')
plt.plot(range(len(smoothed_val_acc)), smoothed_val_acc, label='Validation Accuracy (Smoothed)')
plt.plot(range(len(acc)), acc, alpha=0.5, label='Training Accuracy')
plt.plot(range(len(val_acc)), val_acc, alpha=0.5, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1, 2, 2)
plt.plot(range(len(smoothed_loss)), smoothed_loss, label='Training Loss (Smoothed)')
plt.plot(range(len(smoothed_val_loss)), smoothed_val_loss, label='Validation Loss (Smoothed)')
plt.plot(range(len(loss)), loss, alpha=0.5, label='Training Loss')
plt.plot(range(len(val_loss)), val_loss, alpha=0.5, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
# 打印每个epoch的详细信息
for epoch, (train_acc, val_acc) in enumerate(zip(acc, val_acc)):
print(f'Epoch {epoch+1}, Train Acc: {train_acc:.4f}, Val Acc: {val_acc:.4f}')
最后绘制验证的图形:
# 绘制验证图像和预测
plt.figure(figsize=(10, 5))
for images, labels in val_ds.take(1):
for i in range(8):
ax = plt.subplot(2, 4, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
img_array = tf.expand_dims(images[i], 0)
predictions = model.predict(img_array)
plt.title(class_names[np.argmax(predictions)])
plt.axis('off')
plt.show()