>- **🍨 本文为[🔗365天深度学习训练营](https://mp.weixin.qq.com/s/xLjALoOD8HPZcH563En8bQ) 中的学习记录博客**
>- ** 参考文章:365天深度学习训练营-第5周:运动鞋品牌识别(训练营内部成员可读)**
>- **🍖 原作者:[K同学啊](https://mp.weixin.qq.com/s/xLjALoOD8HPZcH563En8bQ)**
本文主要进行猫狗识别,通过本文的手工搭建VGG网络和训练流程,增强了我我们对端到端模型的掌握和了解,并超低的损失展示了模型的学习情况和泛化能力等相关指标
1.实验准备
设置GPU:
import tensorflow as tf
gpus=tf.config.list_physical_devices("GPU")
if gpus:
gpus0=gpus[0]
tf.config.experimental.set_memory_growth(gpu0,True)
tf.config.set_visible_devices([gpus0],"GPUS")
设置数据路径
data_dir="./365-7-data"
data_dir=pathlib.Path(data_dir)
image_count=len(list(data_dir.glob('*/*.png')))
print("图片总数:",image_count)
batch_size=8
img_height=224
img_width=224
数据探索
image_count=len(list(data_dir.glob('*/*.png')))
print("图片总数:",image_count)
#图片总数3400
打印所有的数据类别
['cat', 'dog']
加载数据到tensorflow中:
train_ds=tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=12,
image_size=(img_hegiht,img_width),
batch_size=batch_size
)
val_ds=tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=12,
image_size=(img_hegiht,img_width),
batch_size=batch_size
)
查看训练数据的形状:
for image_batch,labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
数据预处理:
AUTOTUNE=tf.data.AUTOTUNE
def preprocess_image(image,label):
return (image/255.0,label)
train_ds=train_ds.map(preprocess_image,num_parallel_calls=AUTOTUNE)
val_ds=val_ds.map(preprocess_image,num_parallel_calls=AUTOTUNE)
train_ds=train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds=val_ds.cache().cache().prefetch(buffer_size=AUTOTUNE)
数据可视化
plt.figure(figsize=(15,10))
for images,labels in train_ds.take(1):
for i in range(8):
ax=plt.subplot(5,8,i+1)
plt.imshow(images[i])
plt.title(class_names[labels[i]])
plt.axis("off")
网络搭建:
from tensorflow.keras import layers,models,Input
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Dense,Flatten,Dropout
def VGG16(nb_classes,input_shape):
input_tensor=Input(shape=input_shape)
x=Conv2D(64,(3,3),activation='relu',padding='same',name='block1_conv1')(input_tensor)
x=Conv2D(64,(3,3),activation='relu',padding='same',name='block1_conv2')(x)
x=MaxPooling2D((2,2),strides=(2,2),name='block1_pool')(x)
x=Conv2D(128,(3,3),activation='relu',padding='same',name='block2_conv1')(x)
x=Conv2D(128,(3,3),activation='relu',padding='same',name='block2_conv2')(x)
x=MaxPooling2D((2,2),strides=(2,2),name='block2_pool')(x)
x=Conv2D(256,(3,3),activation='relu',padding='same',name='block3_conv1')(x)
x=Conv2D(256,(3,3),activation='relu',padding='same',name='block3_conv2')(x)
x=Conv2D(256,(3,3),activation='relu',padding='same',name='block3_conv3')(x)
x=MaxPooling2D((2,2),strides=(2,2),name='block3_pool')(x)
x=Conv2D(512,(3,3),activation='relu',padding='same',name='block4_conv1')(x)
x=Conv2D(512,(3,3),activation='relu',padding='same',name='block4_conv2')(x)
x=Conv2D(2512,(3,3),activation='relu',padding='same',name='block4_conv3')(x)
x=MaxPooling2D((2,2),strides=(2,2),name='block4_pool')(x)
x=Conv2D(512,(3,3),activation='relu',padding='same',name='block5_conv1')(x)
x=Conv2D(512,(3,3),activation='relu',padding='same',name='block5_conv2')(x)
x=Conv2D(512,(3,3),activation='relu',padding='same',name='block5_conv3')(x)
x=MaxPooling2D((2,2),strides=(2,2),name='block5_pool')(x)
x=Flatten()(x)
x=Dense(4096,activation='relu',name='fc1')(x)
x=Dense(4096,activation='relu',name='fc2')(x)
output_tensor=Dense(nb_classes,activation='softmax',name='precisions')(x)
model=Model(input_tensor,output_tensor)
return model
model=VGG16(1000,(img_width,img_hegiht,3))
model.summary()
网络模型图:
模型编译:
model.compile(optimizer="adam",
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
训练和测试,并设置进度条:
from tqdm import tqdm
import tensorflow,keras.backend as k
epochs=10
lr=1e-4
history_train_loss=[]
history_train_accuracy=[]
history_val_loss=[]
history_val_accuracy=[]
for epoch in range(epochs):
train_total=len(train_ds)
val_total=len(val_ds)
with tqdm(total=train_total,desc=f'Epoch{epoch+1}/{epochs}',mininterval=1,ncols=100) as pbar:
lr=lr*0.92
k.set_value(model.optimizer.lr,lr)
for image,label in train_ds:
history=model.train_on_batch(image,label)
train_loss=history[0]
train_accuracy=history[1]
pbar.set_postfix({"loss":"%.4f"%train_loss,
"accuracy":"%.4f"%train_accuracy,
"lr":k.get_value(model.optimizer.lr)
})
pbar.update(1)
history_train_loss.append(train_loss)
history_train_accuracy.append(train_accuracy)
print("开始验证")
with tqdm(total=val_total,desc=f'Epoch{epoch+1}/{epochs}',mininterval=0.3,ncols=100) as pbar:
for image,label in val_ds:
history=model.test_on_batch(image,label)
val_loss=history[0]
val_accuracy=history[1]
pbar.set_postfix({"loss":"%.4f"%val_loss,
"accuracy":"%.4f"%val_accuracy
})
pbar.update(1)
history_val_loss.append(val_loss)
history_val_accuracy.append(val_accuracy)
print("结束验证!")
print("验证loss为:%.4f"%val_loss)
print("验证准确率为:%.4f"%val_accuracy)
训练结果可视化:
epochs_range=range(epochs)
plt.figure(figsize=(12,4))
plt.subplot(1,2,1)
plt.plot(epochs_range,history_train_accuracy,label='Training Accuracy')
plt.plot(epochs_range,history_val_accuracy,label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')
plt.subplot(1,2,2)
plt.plot(epochs_range,history_train_loss,label='Training Loss')
plt.plot(epochs_range,history_val_loss,label='Validation Accuracy')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()
基于已经完成训练的模型进行预测:
import numpy as np
plt.figure(figsize=(18,3))
plt.suptitle("预测结果展示")
for images,labels in val_ds.take(1):
for i in range(8):
ax=plt.subplot(1,8,i+1)
plt.imshow(images[i].numpy())
img_array=tf.expand_dims(images[i],0)
predictions=model.predict(img_array)
plt.title(class_names[np.argmax(predictions)])
plt.axis("off")
总结:
学习使用模型和搭建计算机的图像识别网络加深了我对VGG模型的熟悉和了解!