DL学习笔记 T5
【TF NOTE】T5 运动鞋品牌识别
前言
🍨 本文为🔗365天深度学习训练营 中的学习记录博客
🍖 原作者:K同学啊
一、设置GPU
# noinspection PyUnresolvedReferences
from tensorflow import keras
# noinspection PyUnresolvedReferences
from tensorflow.keras import layers,models
import os, PIL, pathlib
# noinspection PyUnresolvedReferences
import matplotlib.pyplot as plt
import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")
if gpus:
gpu0 = gpus[0]
tf.config.experimental.set_memory_growth(gpu0, True)
tf.config.set_visible_devices([gpu0],"GPU")
gpus
二、正式开始
1.加载数据
data_dir = "D:/BaiduNetdiskDownload/T5/"
data_dir = pathlib.Path(data_dir)
image_count = len(list(data_dir.glob('*/*/*.jpg')))
print("图片总数为:", image_count)
roses = list(data_dir.glob('train/nike/*.jpg'))
img1=PIL.Image.open(str(roses[0]))
img1.show()
#加载数据
batch_size =32
img_height = 224
img_width = 224
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
"D:/BaiduNetdiskDownload/T5/train/",
seed=123,
image_size = (img_height, img_width),
batch_size = batch_size)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
"D:/BaiduNetdiskDownload/T5/train/",
seed=123,
image_size=(img_height,img_width),
batch_size=batch_size)
class_names = train_ds.class_names
print(class_names)
输出
2.数据可视化
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[labels[i]])
plt.axis("off")
for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
3.配置数据集
#配置数据集
AUTOTUNE = tf.data.AUTOTUNE
train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
4.搭建模型
老演员了
#搭建模型
model = models.Sequential([
layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width,3)),
layers.Conv2D(16, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)),
layers.AveragePooling2D((2, 2)),
layers.Conv2D(32, (3, 3), activation='relu'),
layers.AveragePooling2D((2, 2)),
layers.Dropout(0.3),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.Dropout(0.3),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(len(class_names))
])
model.summary()
5.参数设置
本次加入了保存局部最优模型,并加入了早停机制。
#参数设置
initial_learning_rate = 0.001
lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate,
decay_steps=10,
decay_rate=0.92,
staircase=True)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule)
model.compile(optimizer=optimizer,
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
epochs = 50
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)
6.训练模型
#训练模型
history = model.fit(train_ds,
validation_data=val_ds,
epochs=epochs,
callbacks=[checkpointer, earlystopper])
在第44轮停止,每次训练不一定相同
7.模型评估
#模型评估
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()
输出
8.模型预测
#预测
model.load_weights('best_model.h5')
from PIL import Image
import numpy as np
img = Image.open("D:/BaiduNetdiskDownload/T5/test/adidas/31.jpg")
img = np.array(img)
image = tf.image.resize(img,[img_height, img_width])
img_array = tf.expand_dims(image,0)/255.0
predictions = model.predict(img_array)
print("预测结果为:", class_names[np.argmax(predictions)])
总结
本次加入了早停机制,保存20轮以内没有增长验证集精度的模型参数。