⽤Python做了⼀个猫狗识别系统~
导语
哈喽吖铁汁萌~今天来教⼤家写个猫狗识别系统
⼩猫⼩狗真的太可爱了
这篇⽂章中我放弃了以往的model.fit()训练⽅法,改⽤ain_on_batch⽅法。两种⽅法的⽐较:model.fit():⽤起来⼗分简单,对新⼿⾮常友好
此外我也引⼊了进度条的显⽰⽅式,更加⽅便我们及时查看模型训练过程中的情况,可以及时打印各项指标。  我的环境:
语⾔环境:Python3.6.5
编译器:jupyter notebook
深度学习环境:TensorFlow2.4.1
black black heart显卡(GPU):NVIDIA GeForce RTX 3080
来⾃专栏:《深度学习100例》
⽂章⽬录
⼀、前期⼯作
1. 设置GPU
2. 导⼊数据
3. 查看数据
⼆、数据预处理
1. 加载数据
2. 再次检查数据
3. 配置数据集
4. 可视化数据
三、构建VG-16⽹络
四、编译
五、训练模型
六、模型评估
七、保存and加载模型
⼋、预测
⼀、前期⼯作
1. 设置GPU
如果使⽤的是CPU可以注释掉这部分的代码。
import tensorflow as tf
gpus = tf.config.list_physical_devices("GPU")
if gpus:
# 打印显卡信息,确认GPU可⽤
print(gpus)
PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
2. 导⼊数据
import matplotlib.pyplot as plt
# ⽀持中⽂
标签
import os,PIL
# 设置随机种⼦尽可能使结果可以重现nu virgos
import numpy as np
np.random.seed(1)
# 设置随机种⼦尽可能使结果可以重现
kraftwerkimport tensorflow as tf
tf.random.set_seed(1)
#隐藏警告
import warnings
warnings.filterwarnings('ignore')
import pathlib
data_dir = "./data/train"
# data_dir = "D:/jupyter notebook/DL-100-days/datasets/017_Eye_dataset"
data_dir = pathlib.Path(data_dir)
3. 查看数据
image_count = len(list(data_dir.glob('*/*')))
print("图⽚总数为:",image_count)
图⽚总数为:3400
⼆、数据预处理
1. 加载数据
使⽤image_dataset_from_directory⽅法将磁盘中的数据加载到tf.data.Dataset中
batch_size = 8
img_height = 224
img_width = 224
TensorFlow版本是2.2.0的同学可能会遇到module 'tensorflow.keras.preprocessing' has no attribute 'image_dataset_from_directory'的报错,升级⼀下TensorFlow就OK了
"""
关于image_dataset_from_directory()的详细介绍可以参考⽂章:mtyjkh.blog.csdn/article/details/117018789
"""
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="training",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
Found 3400 files belonging to 2 classes.
Using 2720 files for training.
"""
关于image_dataset_from_directory()的详细介绍可以参考⽂章:mtyjkh.blog.csdn/article/details/117018789
"""
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
data_dir,
validation_split=0.2,
subset="validation",
seed=12,
image_size=(img_height, img_width),
batch_size=batch_size)
Found 3400 files belonging to 2 classes.
Using 680 files for validation.
沈星整容我们可以通过class_names输出数据集的标签。标签将按字母顺序对应于⽬录名称。
class_names = train_ds.class_names
print(class_names)
['cat', 'dog']
2. 再次检查数据
for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break
(8, 224, 224, 3)
(8,)
Image_batch是形状的张量(8, 224, 224, 3)。这是⼀批形状224x224x3的8张图⽚(最后⼀维指的是彩⾊通道RGB)。
Label_batch是形状(8,)的张量,这些标签对应8张图⽚
3. 配置数据集
prefetch() :预取数据,加速运⾏,其详细介绍可以参考我前两篇⽂章,⾥⾯都有讲解。
cache() :将数据集缓存到内存当中,加速运⾏
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().prefetch(buffer_size=AUTOTUNE)
如果报 AttributeError: module 'tensorflow._api.v2.data' has no attribute 'AUTOTUNE' 错误,就将 AUTOTUNE = tf.data.AUTOTUNE 更换为 AUTOTUNE = perimental.AUTOTUNE,这个错误是由于版本问题引起的。
4. 可视化数据
plt.figure(figsize=(15, 10))  # 图形的宽为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")
三、构建VG-16⽹络
VGG优缺点分析:
VGG优点
VGG的结构⾮常简洁,整个⽹络都使⽤了同样⼤⼩的卷积核尺⼨(3x3)和最⼤池化尺⼨(2x2)。
VGG缺点
当你孤单你会想起谁吉他谱
1)训练时间过长,调参难度⼤。2)需要的存储容量⼤,不利于部署。例如存储VGG-16权重值⽂件的⼤⼩为500多MB,不利于安装到嵌⼊式系统中。
结构说明:
13个卷积层(Convolutional Layer),分别⽤blockX_convX表⽰
3个全连接层(Fully connected Layer),分别⽤fcX与predictions表⽰
5个池化层(Pool layer),分别⽤blockX_pool表⽰
知否主题曲
VGG-16包含了16个隐藏层(13个卷积层和3个全连接层),故称为VGG-16