>- **🍨 本文为[🔗365天深度学习训练营](https://mp.weixin.qq.com/s/k-vYaC8l7uxX51WoypLkTw) 中的学习记录博客**
>- **🍦 参考文章:365天深度学习训练营-第6周:好莱坞明星识别(训练营内部成员可读)**
>- **🍖 原作者:[K同学啊|接辅导、项目定制](https://mtyjkh.blog.csdn.net/)**
目录
环境:python3.7,1080ti,tensorflow2.5(网上租的环境😂😂)
由于电脑问题,现在在jupytr上跑了,所以代码风格发生变化。
设置cpu(电脑gpu跑不动就用这个将就一下)
- from tensorflow import keras
- from tensorflow.keras import layers, models
- import os, PIL, pathlib
- import matplotlib.pyplot as plt
- import tensorflow as tf
- import numpy as np
- import tensorflow as tf
- import tensorflow as tf
- import os,PIL
- # os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
- #os.environ['CUDA_VISIBLE_DEVICES']='0'
- os.environ['CUDA_VISIBLE_DEVICES']='2'
- # os.environ['TF_CPP_MIN_LOG_LEVEL']='2'#屏蔽通知和警告信息
- import os,PIL
- os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
- from tensorflow import keras
- keras.backend.clear_session()
设置gpu
- from tensorflow import keras
- from tensorflow.keras import layers,models
- import os, PIL, pathlib
- import matplotlib.pyplot as plt
- import tensorflow as tf
- import numpy as np
-
- gpus = tf.config.list_physical_devices("GPU")
-
- if gpus:
- gpu0 = gpus[0] #如果有多个GPU,仅使用第0个GPU
- tf.config.experimental.set_memory_growth(gpu0, True) #设置GPU显存用量按需使用
- tf.config.set_visible_devices([gpu0],"GPU")
-
- gpus
- data_dir = "./48-data/"
-
- data_dir = pathlib.Path(data_dir)
- image_count = len(list(data_dir.glob('*/*.jpg')))
-
- print("图片总数为:",image_count)
显示图片
- roses = list(data_dir.glob('Jennifer Lawrence/*.jpg'))
- PIL.Image.open(str(roses[0]))
设置数据尺寸
- batch_size = 32
- img_height = 224
- img_width = 224
设置dataset
- train_ds = tf.keras.preprocessing.image_dataset_from_directory(
- data_dir,
- validation_split=0.1,
- subset="training",
- label_mode = "categorical",
- seed=123,
- image_size=(img_height, img_width),
- batch_size=batch_size)
- val_ds = tf.keras.preprocessing.image_dataset_from_directory(
- data_dir,
- validation_split=0.1,
- subset="validation",
- label_mode = "categorical",
- seed=123,
- image_size=(img_height, img_width),
- batch_size=batch_size)
关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789

输出经过image_dataset_from_directory()分类后的标签
- class_names = train_ds.class_names
- print(class_names)
打印部分图片
- 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")

输出数据的尺寸
- for image_batch, labels_batch in train_ds:
- print(image_batch.shape)
- print(labels_batch.shape)
- break

32:通道数 224x224为尺寸 3为rgb彩色通道(灰色为1)
32 :对应上面的32 17为标签种类的个数
shuffle:打乱数据集
prefetch:加速处理
- AUTOTUNE = tf.data.AUTOTUNE
-
- train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
- val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
本文神经网络为官方vgg16模型,我们需要做的是对最后一层按我们的类别进行分类即可。
调用官方
- model = keras.applications.VGG16(include_top=False,weights=None)
- global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
- prediction_layer = tf.keras.layers.Dense(len(class_names),activation='softmax')
- model = tf.keras.Sequential([
- model,
- global_average_layer,
- prediction_layer
- ])
关于VGG16函数参考下面文章
(8条消息) keras 自带VGG16 net 参数分析_vola9527的博客-CSDN博客
自己搭建
- model = models.Sequential([
- layers.experimental.preprocessing.Rescaling(1. / 255, input_shape=(img_height, img_width, 3)),
- layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same'), # 卷积层1
- layers.BatchNormalization(), # BN层1
- layers.Activation('relu'), # 激活层1
- layers.Conv2D(filters=64, kernel_size=(3, 3), padding='same', ),
- layers.BatchNormalization(), # BN层1
- layers.Activation('relu') , # 激活层1
- layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='same'),
- layers.Dropout(0.2), # dropout层
- #
- layers.Conv2D(filters=128, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization(), # BN层1
- layers.Activation('relu'), # 激活层1
- layers.Conv2D(filters=128, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization(), # BN层1
- layers.Activation('relu'), # 激活层1
- layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='same'),
- layers.Dropout(0.2), # dropout层
- #
- layers.Conv2D(filters=256, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization() , # BN层1
- layers.Activation('relu'), # 激活层1
- layers.Conv2D(filters=256, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization() , # BN层1
- layers.Activation('relu') , # 激活层1
- layers.Conv2D(filters=256, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization(),
- layers.Activation('relu'),
- layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='same'),
- layers.Dropout(0.2),
- #
- layers.Conv2D(filters=512, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization() , # BN层1
- layers.Activation('relu') , # 激活层1
- layers.Conv2D(filters=512, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization() , # BN层1
- layers.Activation('relu'), # 激活层1
- layers.Conv2D(filters=512, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization(),
- layers.Activation('relu'),
- layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='same'),
- layers.Dropout(0.2),
- #
- layers.Conv2D(filters=512, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization() , # BN层1
- layers.Activation('relu'), # 激活层1
- layers.Conv2D(filters=512, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization(), # BN层1
- layers.Activation('relu'), # 激活层1
- layers.Conv2D(filters=512, kernel_size=(3, 3), padding='same'),
- layers.BatchNormalization(),
- layers.Activation('relu'),
- layers.MaxPool2D(pool_size=(2, 2), strides=2, padding='same'),
- layers.Dropout(0.2),
- #
- # self.flatten = Flatten()
- # self.f1 = Dense(512, activation='relu')
- # self.d6 = Dropout(0.2)
- # self.f2 = Dense(512, activation='relu')
- # self.d7 = Dropout(0.2)
- # self.f3 = Dense(10, activation='softmax')
-
- layers.Flatten(), # Flatten层,连接卷积层与全连接层
- layers.Dense(128, activation='relu'), # 全连接层,特征进一步提取
- layers.Dense(len(class_names)) # 输出层,输出预期结果
- ])
模型训练时,需要完成如下设置
损失函数(loss):衡量模型准确率
优化器(optimizer):根据损失函数进行优化更新
指标(metrics):监控训练过程,保存最优模型
- # 设置初始学习率
- initial_learning_rate = 1e-4
-
- lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
- initial_learning_rate,
- decay_steps=60, # 敲黑板!!!这里是指 steps,不是指epochs
- decay_rate=0.96, # lr经过一次衰减就会变成 decay_rate*lr
- staircase=True)
-
- # 将指数衰减学习率送入优化器
- optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
-
- model.compile(optimizer=optimizer,
- loss=tf.keras.losses.CategoricalCrossentropy(from_logits=True),
- metrics=['accuracy'])
这里的loss是设置为这个是因为:在dataset设置中
label_mode = "categorical",loss则为CategoricalCrossentropy
对于损失函数可以参考下面文章
(8条消息) tensorflow损失函数详解_重邮研究森的博客-CSDN博客
关于ModelCheckpoint参考下面文章(8条消息) ModelCheckpoint 讲解【TensorFlow2入门手册】_K同学啊的博客-CSDN博客_modelcheckpoint函数

- from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
-
- epochs = 100
-
- # 保存最佳模型参数
- checkpointer = ModelCheckpoint('best_model.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(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='Training Loss')
- plt.plot(epochs_range, val_loss, label='Validation Loss')
- plt.legend(loc='upper right')
- plt.title('Training and Validation Loss')
- plt.show()

- # 加载效果最好的模型权重
- model.load_weights('best_model.h5')
- from PIL import Image
- import numpy as np
-
- img = Image.open("./48-data/Jennifer Lawrence/003_963a3627.jpg") #这里选择你需要预测的图片
- img=np.array(img)
- image = tf.image.resize(img, [img_height, img_width])
-
- img_array = tf.expand_dims(image, 0)
-
- predictions = model.predict(img_array) # 这里选用你已经训练好的模型
- print("预测结果为:",class_names[np.argmax(predictions)])
1.设置官方vgg16的weights=imagenet:准确率达到74,weights=None,准确率50-60
2.自搭建vgg16,最大池化:准确率40,平均池化:准确率57
3.损失函数为CategoricalCrossentropy比SparseCategoricalCrossentropy效果好