CNN Weather Recognition Exercise


Event address: CSDN 21-day learning challenge

If an error occurs during operation, such as the following error, please update tensorflow:
AttributeError: module 'tensorflow.python.keras.api._v1.keras.preprocessing' has no attribute 'image_dataset_from_directory'
The following code requires tensorflow version greater than 2.5

pip install --upgrade tensorflow==2.5或pip install --upgrade tensorflow-gpu==2.5

Dataset acquisition link
Link: https://pan.baidu.com/s/1cE3GNeCY4bFaYCZpjM1-VA
Extraction code: orej

full code

# import tensorflow as tf

# 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")

# 导入数据
import matplotlib.pyplot as plt
import os,PIL

# 设置随机种子尽可能使结果可以重现
import numpy as np
np.random.seed(1)

# 设置随机种子尽可能使结果可以重现
import tensorflow as tf
# tf.random.set_seed(1)

from tensorflow import keras
from tensorflow.keras import layers,models

import pathlib
data_dir = "G:\BaiduNetdiskDownload\climate\weather_photos/"

data_dir = pathlib.Path(data_dir)

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

print("图片总数为:",image_count)
roses = list(data_dir.glob('sunrise/*.jpg'))
PIL.Image.open(str(roses[0]))

# 加载数据
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 = 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[labels[i]])

        plt.axis("off")

# 检验数据集
for image_batch, labels_batch in train_ds:
    print(image_batch.shape)
    print(labels_batch.shape)
    break

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

train_ds = train_ds.cache().shuffle(1000).prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)
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()  # 打印网络结构

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

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

history = model.fit(
  train_ds,
  validation_data=val_ds,
  epochs=epochs
)
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()

Prediction effect
insert image description here
The accuracy and loss value curve process curve is as follows:
insert image description here
the effect of iterating 10 times is not very good, you can increase the number of iterations, the following is the accuracy and loss value curve of 50 iterations: Conclusion: From
insert image description herethis figure, we can see that The network is really not good. I also got it from other places. To make the recognition more accurate, the network model needs to be adjusted. Here is just an exercise to try the feasibility of this set of codes, and does not involve in-depth adjustments. It really proves that different research objects require targeted network structure optimization, and those who are interested in adjusting the network structure can give it a try.

Due to modulating this code and updating tensorflow, my GPU cannot be recognized and the dll file is missing. I haven't had time to debug this yet. I have Baidu enough for reference. It's really slow without calling the GPU. The reference website is as follows: https://blog.csdn.net/qq1198768105/article/details/124004146

Guess you like

Origin blog.csdn.net/self_Name_/article/details/126215534