第T3周:天气识别

前言

一、我的环境

  • 电脑系统:Windows 11
  • 语言环境:Python 3.9.7
  • 编辑器:Jupyter Lab
  • 深度学习环境:TensorFlow2.4.1

二、代码实现

1.前期工作

本文将采用CNN实现多云、下雨、晴、日出四种天气状态的识别。较上篇文章,本文为了增加模型的泛化能力,新增了Dropout层并且将最大池化层调整成了平均池化层。

1.1 导入数据

import tensorflow as tf
#前期工作
#导入数据
import os,PIL,pathlib
import matplotlib.pyplot as plt
import numpy             as np
from tensorflow          import keras
from tensorflow.keras    import layers,models
data_dir = "F:/boshiqijian/kechengxuexi/Deep Learning/365xunlianying/data/weather_photos/"

data_dir = pathlib.Path(data_dir)

1.2 查看数据

数据集一共分为cloudy、rain、shine、sunrise四类,分别存放于weather_photos文件夹中以各自名字命名的子文件夹中。

#查看数据
image_count = len(list(data_dir.glob('*/*.jpg')))

print("图片总数为:",image_count)

在这里插入图片描述

roses = list(data_dir.glob('sunrise/*.jpg'))
PIL.Image.open(str(roses[0]))

在这里插入图片描述

2. 数据预处理

2.1 加载数据

使用image_dataset_from_directory方法将磁盘中的数据加载到tf.data.Dataset中

#数据预处理
#加载数据
batch_size = 32
img_height = 180
img_width = 180
"""
关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789
"""
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

在这里插入图片描述

"""
关于image_dataset_from_directory()的详细介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/117018789
"""
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="validation",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

在这里插入图片描述

我们可以通过class_names输出数据集的标签。标签将按字母顺序对应于目录名称。

class_names = train_ds.class_names
print(class_names)

2.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")

在这里插入图片描述

2.3 再次检查数据

#再次检查数据
for image_batch, labels_batch in train_ds:
    print(image_batch.shape)
    print(labels_batch.shape)
    break

在这里插入图片描述

2.4 配置数据集

● shuffle():打乱数据,关于此函数的详细介绍可以参考:https://zhuanlan.zhihu.com/p/42417456
● prefetch():预取数据,加速运行

prefetch()功能详细介绍:CPU 正在准备数据时,加速器处于空闲状态。相反,当加速器正在训练模型时,CPU 处于空闲状态。因此,训练所用的时间是 CPU 预处理时间和加速器训练时间的总和。prefetch()将训练步骤的预处理和模型执行过程重叠到一起。当加速器正在执行第 N 个训练步时,CPU 正在准备第 N+1 步的数据。这样做不仅可以最大限度地缩短训练的单步用时(而不是总用时),而且可以缩短提取和转换数据所需的时间。如果不使用prefetch(),CPU 和 GPU/TPU 在大部分时间都处于空闲状态:
在这里插入图片描述
使用prefetch()可显著减少空闲时间:

在这里插入图片描述
cache():将数据集缓存到内存当中,加速运行

#配置数据集
AUTOTUNE = tf.data.AUTOTUNE

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

3. 构建CNN网络

卷积神经网络(CNN)的输入是张量 (Tensor) 形式的 (image_height, image_width, color_channels),包含了图像高度、宽度及颜色信息。不需要输入batch size。color_channels 为 (R,G,B) 分别对应 RGB 的三个颜色通道(color channel)。在此示例中,我们的 CNN 输入形状是 (180, 180, 3)。我们需要在声明第一层时将形状赋值给参数input_shape。
网络结构图:
在这里插入图片描述

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Dropout, Flatten, Dense, Rescaling

num_classes = 4
img_height = 180  # 请根据实际情况设置 img_height 和 img_width
img_width = 180

model = Sequential([
    Rescaling(1./255, input_shape=(img_height, img_width, 3)),
    Conv2D(16, (3, 3), activation='relu'),  # 卷积层1,卷积核3*3
    AveragePooling2D((2, 2)),  # 池化层1,2*2采样
    Conv2D(32, (3, 3), activation='relu'),  # 卷积层2,卷积核3*3
    AveragePooling2D((2, 2)),  # 池化层2,2*2采样
    Conv2D(64, (3, 3), activation='relu'),  # 卷积层3,卷积核3*3
    Dropout(0.3),  # 让神经元以一定的概率停止工作,防止过拟合,提高模型的泛化能力。
    Flatten(),  # Flatten层,连接卷积层与全连接层
    Dense(128, activation='relu'),  # 全连接层,特征进一步提取
    Dense(num_classes)  # 输出层,输出预期结果
])

model.summary()  # 打印网络结构

在这里插入图片描述

4. 编译

在准备对模型进行训练之前,还需要再对其进行一些设置。以下内容是在模型的编译步骤中添加的:

● 损失函数(loss):用于衡量模型在训练期间的准确率。
● 优化器(optimizer):决定模型如何根据其看到的数据和自身的损失函数进行更新。
● 指标(metrics):用于监控训练和测试步骤。以下示例使用了准确率,即被正确分类的图像的比率。

#编译
# 设置优化器
opt = tf.keras.optimizers.Adam(learning_rate=0.001)

model.compile(optimizer=opt,
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

5. 训练模型

#训练模型
epochs = 10

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)

在这里插入图片描述

6. 模型评估

#评估模型
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(epochs)

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()

在这里插入图片描述

四、心得体会

加强自主学习能力、善于利用AI解决问题

  • 路径尽量不要有中文
    因为路径中有英文

  • kimi是个好东西
    又报错

#构建CNN网络
num_classes = 4

"""
关于卷积核的计算不懂的可以参考文章:https://blog.csdn.net/qq_38251616/article/details/114278995 

layers.Dropout(0.4) 作用是防止过拟合,提高模型的泛化能力。
在上一篇文章花朵识别中,训练准确率与验证准确率相差巨大就是由于模型过拟合导致的

关于Dropout层的更多介绍可以参考文章:https://mtyjkh.blog.csdn.net/article/details/115826689 
"""

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)), # 卷积层1,卷积核3*3  
    layers.AveragePooling2D((2, 2)),               # 池化层1,2*2采样
    layers.Conv2D(32, (3, 3), activation='relu'),  # 卷积层2,卷积核3*3
    layers.AveragePooling2D((2, 2)),               # 池化层2,2*2采样
    layers.Conv2D(64, (3, 3), activation='relu'),  # 卷积层3,卷积核3*3
    layers.Dropout(0.3),                           # 让神经元以一定的概率停止工作,防止过拟合,提高模型的泛化能力。
    
    layers.Flatten(),                       # Flatten层,连接卷积层与全连接层
    layers.Dense(128, activation='relu'),   # 全连接层,特征进一步提取
    layers.Dense(num_classes)               # 输出层,输出预期结果
])

model.summary()  # 打印网络结构
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
C:\Users\HUZHAO~1\AppData\Local\Temp/ipykernel_32644/2495415754.py in <module>
     12 
     13 model = models.Sequential([
---> 14     layers.experimental.preprocessing.Rescaling(1./255, input_shape=(img_height, img_width, 3)),
     15 
     16     layers.Conv2D(16, (3, 3), activation='relu', input_shape=(img_height, img_width, 3)), # 卷积层1,卷积核3*3

AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental'

kimi的方案初步解决问题

错误信息 AttributeError: module 'keras._tf_keras.keras.layers' has no attribute 'experimental' 表示在尝试访问 keras 模块的 experimental 子模块时出现了问题。这通常意味着你当前的 TensorFlow 版本可能不包含 experimental 子模块,或者你的导入方式有误。

要解决这个问题,请尝试以下步骤:

    确保 TensorFlow 版本:确保你安装的 TensorFlow 版本是最新的,因为 experimental 子模块可能在旧版本中不可用。你可以通过以下命令来更新 TensorFlow:

    bash

pip install --upgrade tensorflow

检查导入方式:如果你的 TensorFlow 版本是最新的,那么请检查你的导入方式是否正确。通常,你应该这样导入 Rescaling 层:

python

    from tensorflow.keras.layers import Rescaling

    而不是使用 layers.experimental.preprocessing。

    直接使用 Rescaling:如果你的 TensorFlow 版本确实包含 experimental 子模块,但仍然出现错误,你可以尝试直接从 tensorflow.keras.layers 导入 Rescaling,而不是通过 layers.experimental.preprocessing。

修改后的代码应该像这样:

python

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Dropout, Flatten, Dense, Rescaling

num_classes = 4
img_height = 28  # 请根据实际情况设置 img_height 和 img_width
img_width = 28

model = Sequential([
    Rescaling(1./255, input_shape=(img_height, img_width, 3)),
    Conv2D(16, (3, 3), activation='relu'),  # 卷积层1,卷积核3*3
    AveragePooling2D((2, 2)),  # 池化层1,2*2采样
    Conv2D(32, (3, 3), activation='relu'),  # 卷积层2,卷积核3*3
    AveragePooling2D((2, 2)),  # 池化层2,2*2采样
    Conv2D(64, (3, 3), activation='relu'),  # 卷积层3,卷积核3*3
    Dropout(0.3),  # 让神经元以一定的概率停止工作,防止过拟合,提高模型的泛化能力。
    Flatten(),  # Flatten层,连接卷积层与全连接层
    Dense(128, activation='relu'),  # 全连接层,特征进一步提取
    Dense(num_classes)  # 输出层,输出预期结果
])

model.summary()  # 打印网络结构

请确保 img_height 和 img_width 被设置为适当的值,这些值应该匹配你的输入数据的维度。如果你不确定 TensorFlow 的版本,可以通过 tensorflow.__version__ 来检查。

再次报错

#训练模型
epochs = 10

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
C:\Users\HUZHAO~1\AppData\Local\Temp/ipykernel_32644/2368475182.py in <module>
      2 epochs = 10
      3 
----> 4 history = model.fit(
      5   train_ds,
      6   validation_data=val_ds,

d:\py\Anaconda3\lib\site-packages\keras\src\utils\traceback_utils.py in error_handler(*args, **kwargs)
    120             # To get the full stack trace, call:
    121             # `keras.config.disable_traceback_filtering()`
--> 122             raise e.with_traceback(filtered_tb) from None
    123         finally:
    124             del filtered_tb

d:\py\Anaconda3\lib\site-packages\keras\src\layers\input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    225                     None,
    226                 }:
--> 227                     raise ValueError(
    228                         f'Input {input_index} of layer "{layer_name}" is '
    229                         f"incompatible with the layer: expected axis {axis} "

ValueError: Exception encountered when calling Sequential.call().

Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 576, but received input with shape (None, 107584)

Arguments received by Sequential.call():
  • inputs=tf.Tensor(shape=(None, 180, 180, 3), dtype=float32)
  • training=True
  • mask=None

kimi再次解决问题

错误信息 ValueError: Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 576, but received input with shape (None, 180, 180, 3) 指出在模型训练时,全连接层(Dense层)接收到的输入形状与预期不符。全连接层期望的输入应该是一个二维的张量,其中最后一个维度是特征向量的长度,但这里模型提供的输入是一个四维的张量。

这个问题通常发生在从卷积层到全连接层(Flatten层)的转换过程中。在你的模型中,可能缺少了一个 Flatten 层,或者 Flatten 层的位置不正确,导致全连接层接收到了错误的输入形状。

要解决这个问题,请确保在最后一个卷积层和第一个全连接层之间正确地添加了 Flatten 层。Flatten 层的作用是将多维的卷积层输出展平成一维,以便可以被全连接层处理。

以下是修改后的模型结构,确保在 Dropout 层和第一个 Dense 全连接层之间正确添加了 Flatten 层:

python

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, AveragePooling2D, Dropout, Flatten, Dense, Rescaling

num_classes = 4
img_height = 180  # 请根据实际情况设置 img_height 和 img_width
img_width = 180

model = Sequential([
    Rescaling(1./255, input_shape=(img_height, img_width, 3)),
    Conv2D(16, (3, 3), activation='relu'),  # 卷积层1,卷积核3*3
    AveragePooling2D((2, 2)),  # 池化层1,2*2采样
    Conv2D(32, (3, 3), activation='relu'),  # 卷积层2,卷积核3*3
    AveragePooling2D((2, 2)),  # 池化层2,2*2采样
    Conv2D(64, (3, 3), activation='relu'),  # 卷积层3,卷积核3*3
    Dropout(0.3),  # 让神经元以一定的概率停止工作,防止过拟合,提高模型的泛化能力。
    Flatten(),  # Flatten层,连接卷积层与全连接层
    Dense(128, activation='relu'),  # 全连接层,特征进一步提取
    Dense(num_classes)  # 输出层,输出预期结果
])

model.summary()  # 打印网络结构

确保 img_height 和 img_width 被设置为适当的值,这些值应该匹配你的输入数据的维度。此外,确保 train_ds 和 val_ds 数据集已经被正确地构建和预处理,以匹配模型的输入要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值