第T6周:Tensorflow实现好莱坞明星识别

目标

具体实现

(一)环境

语言环境 :Python 3.10
编 译 器: PyCharm
框 架:

(二)具体步骤
1.查询TF版本和设置使用GPU
复制代码
from tensorflow import keras  
from tensorflow.keras import models, layers  
import os, PIL, pathlib  
import matplotlib.pyplot as plt  
import tensorflow as tf  
import numpy as np  
  
# 查询tensorflow版本  
print("Tensorflow Version:", tf.__version__)  
  
# 设置使用GPU  
gpus = tf.config.list_physical_devices("GPU")  
print(gpus)  
  
if gpus:  
    gpu0 = gpus[0]  # 如果有多个GPU,仅使用第0个GPU  
    tf.config.experimental.set_memory_growth(gpu0, True)    # 设置GPU显存按需使用  
    tf.config.set_visible_devices([gpu0], "GPU")

Tensorflow Version: 2.10.0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2.导入数据

目录结构如下:

复制代码
# 导入数据  
data_dir = './datasets/hollywood/'  
data_dir = pathlib.Path(data_dir)  
  
# 查看数据  
image_count = len(list(data_dir.glob('*/*.jpg')))  
print('图片总数为:', image_count)

图片总数为: 1800

# 看看Brad Pitt(布拉德·皮特)的图片  
brad_pitt = list(data_dir.glob('Brad Pitt/*.jpg'))  
im = PIL.Image.open(brad_pitt[0])  
im.show()
3.数据预处理
复制代码
# 数据预处理  
batch_size = 32  
image_height = 224  
image_width = 224  
  
train_ds = tf.keras.preprocessing.image_dataset_from_directory(  
    directory=data_dir,  
    validation_split=0.1,  
    subset="training",  
    label_mode="categorical",  
    seed=123,  
    image_size=(image_height, image_width),  
    batch_size=batch_size  
)  
val_ds = tf.keras.preprocessing.image_dataset_from_directory(  
    directory=data_dir,  
    validation_split=0.1,  
    subset="validation",  
    label_mode="categorical",  
    seed=123,  
    image_size=(image_height, image_width),  
    batch_size=batch_size  
)

Found 1800 files belonging to 17 classes.
Using 1620 files for training.
Found 1800 files belonging to 17 classes.
Using 180 files for validation.

# 输出数据集的标签(标签按字母顺序对应于目录名称)  
class_names = train_ds.class_names  
print(class_names)

['Angelina Jolie', 'Brad Pitt', 'Denzel Washington', 'Hugh Jackman', 'Jennifer Lawrence', 'Johnny Depp', 'Kate Winslet', 'Leonardo DiCaprio', 'Megan Fox', 'Natalie Portman', 'Nicole Kidman', 'Robert Downey Jr', 'Sandra Bullock', 'Scarlett Johansson', 'Tom Cruise', 'Tom Hanks', 'Will Smith']

# 可视化数据  
plt.figure(figsize=(20, 10))  
for images, labels in train_ds.take(1):  
    for i in range(20):  
        ax = plt.subplot(5, 10, i + 1)  
        plt.imshow(images[i].numpy().astype("uint8"))  
        plt.title(class_names[np.argmax(labels[i])])  
  
        plt.axis("off")  
plt.show()
复制代码
# 再次检查数据  
for image_batch, labels_batch in train_ds:  
    print(image_batch.shape)  
    print(labels_batch.shape)  
    break

(32, 224, 224, 3)
(32, 17)
4.配置数据集
复制代码
# 配置数据集  
AUTOTUNE = tf.data.AUTOTUNE  
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)  
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
5.构建CNN网络
复制代码
# 构建CNN网络  
model = models.Sequential([  
    layers.experimental.preprocessing.Rescaling(1./255, input_shape=(image_height, image_width, 3)),  
    layers.Conv2D(16, (3, 3), 1, activation='relu', input_shape=(image_height, image_width)),  
    layers.AveragePooling2D((2, 2)),  
    layers.Conv2D(32, (3, 3), activation='relu'),  
    layers.AveragePooling2D((2, 2)),  
    layers.Dropout(0.5),  
    layers.Conv2D(64, (3, 3), activation='relu'),  
    layers.AveragePooling2D((2, 2)),  
    layers.Dropout(0.5),  
    layers.Conv2D(128, (3, 3), activation='relu'),  
    layers.Dropout(0.5),  
  
    layers.Flatten(),  
    layers.Dense(128, activation='relu'),  
    layers.Dense(len(class_names))  
])  
print(model.summary())
6.训练模型
复制代码
# 训练模型  
# 1. 设置动态学习率  
initial_learning_rate = 1e-4    # 设置初始学习率  
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(  # 对学习率使用指数衰减  
    initial_learning_rate=initial_learning_rate,  
    decay_steps=60,  # 学习率衰减的步数。在经过 decay_steps 步后,学习率将按照指数函数衰减。  
    decay_rate=0.96,  # 学习率的衰减率。它决定了学习率如何衰减。通常,取值在 0 到 1 之间。  
    staircase=True  
)  
# 2. 将指数衰减学习率送入优化器  
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)  
  
# 3. 设置损失函数  
loss = tf.keras.losses.CategoricalCrossentropy(from_logits=True)    # 多分类的对数损失函数  
  
model.compile(  
    optimizer=optimizer,  
    loss=loss,  
    metrics=['accuracy']  
)  
  
epochs = 100  
  
# 保存模型最佳参数  
checkpointer = ModelCheckpoint(  
    filepath='./models/hollywood-best.h5',  
    monitor='val_accuracy',  
    verbose=1,  
    save_best_only=True,  
    save_weights_only=True  
)  
  
# 设置早停  
earlystopper = EarlyStopping(  
    monitor='val_accuracy',  
    min_delta=0.001,  
    patience=20,  
    verbose=1  
)  
  
# 训练  
history = model.fit(  
    x=train_ds,  
    validation_data=val_ds,  
    epochs=epochs,  
    callbacks=[checkpointer, earlystopper]  
)  
  
# 模型评估  
  
acc = history.history['accuracy']  
val_acc = history.history['val_accuracy']  
  
loss = history.history['loss']  
val_loss = history.history['val_loss']  
  
epochs_range = range(len(loss))  
  
plt.figure(figsize=(12, 4))  
plt.subplot(1, 2, 1)  
plt.plot(epochs_range, acc, label="Training Accuracy")  
plt.plot(epochs_range, val_acc, label="Validation Accuracy")  
plt.legend(loc="lower right")  
plt.title("Training and validation accuracy")  
  
plt.subplot(1, 2, 2)  
plt.plot(epochs_range, loss, label="Loss Accuracy")  
plt.plot(epochs_range, val_loss, label="Validation Loss")  
plt.legend(loc="lower right")  
plt.title("Training and validation Loss")  
  
plt.show()
7.真实图片预测
复制代码
# 加载效果最好的模型权重  
model.load_weights('./models/hollywood-best.h5')  
# img = PIL.Image.open("./datasets/hollywood/Jennifer Lawrence/021_2eaafb9f.jpg")  
# image = tf.image.resize(img, [image_height, image_width])  
  
image = tf.keras.utils.load_img('./datasets/hollywood/Jennifer Lawrence/021_2eaafb9f.jpg', target_size=(image_height, image_width))  
image_array = tf.keras.utils.img_to_array(image)  # 将PIL对象转换成numpy数组  
img_array = tf.expand_dims(image_array, 0)    # /255.0  # 记得做归一化处理(与训练集处理方式保持一致)  
  
predictions =model.predict(img_array)  
  
print("预测结果为:", class_names[np.argmax(predictions)])

预测结果为: Jennifer Lawrence
(三)总结

实际预测会发现结果有很大概率会出错的。其次看模型评估,其实训练的结果是比较差的, val_accuracy最好只有40%左右。如何提高准确率,还需要研究,后续出实践总结。

相关推荐
音视频牛哥39 分钟前
超清≠清晰:视频系统里的分辨率陷阱与秩序真相
人工智能·机器学习·计算机视觉·音视频·大牛直播sdk·rtsp播放器rtmp播放器·smartmediakit
Coovally AI模型快速验证1 小时前
当视觉语言模型接收到相互矛盾的信息时,它会相信哪个信号?
人工智能·深度学习·算法·机器学习·目标跟踪·语言模型
sensen_kiss3 小时前
INT305 Machine Learning 机器学习 Pt.5 神经网络(Neural network)
人工智能·神经网络·机器学习
Victory_orsh5 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
长桥夜波5 小时前
机器学习日报10
人工智能·机器学习
电鱼智能的电小鱼6 小时前
基于电鱼 ARM 边缘网关的智慧工地数据可靠传输方案——断点续传 + 4G/5G冗余通信,保障数据完整上传
arm开发·人工智能·嵌入式硬件·深度学习·5g·机器学习
却道天凉_好个秋7 小时前
目标检测算法与原理(二):Tensorflow实现迁移学习
算法·目标检测·tensorflow
武子康7 小时前
AI研究-121 DeepSeek-OCR 研究路线:无限上下文、跨模态抽取、未来创意点、项目创意点
人工智能·深度学习·机器学习·ai·ocr·deepseek·deepseek-ocr
番茄撒旦在上7 小时前
1.每日机器学习——Logits
机器学习
woshihonghonga8 小时前
Deepseek在它擅长的AI数据处理领域还有是有低级错误【k折交叉验证中每折样本数计算】
人工智能·python·深度学习·机器学习