• 不同天气状况识别


    活动地址:CSDN21天学习挑战赛

    学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。各位小伙伴,如果您:
    想系统/深入学习某技术知识点…
    一个人摸索学习很难坚持,想组团高效学习…
    想写博客但无从下手,急需写作干货注入能量…
    热爱写作,愿意让自己成为更好的人…

    创作计划

    **
    1,机缘

    A,分享创作经验
    B,学习TensorFlow
    C,熟悉Python

    2,收获

    A,获得了10粉丝的关注
    B,获得了20多赞、阅读量
    C,认识了TensorFlow同行

    3,日常

    1. 创作已经是我学习的一部分
    2. 制定创作计划,例如,每周二、四、六晚写博客。其余时间学习

    4,憧憬

    创作规划是最近学习TensorFlow编程基本流程,从识别文字到图片。再到实现自己的机器学习模型。

    **

    学习计划

    **
    1,学习目标

    这几天掌握 TensorFlow入门知识,安装,运行,常见错误处理。

    2,学习内容

    A,搭建 TensorFlow开发环境
    B,掌握 TensorFlow 基本语法
    C,掌握加载数据集的方法
    D,掌握查看数据集的方法

    3,学习时间

    周一至周五晚上6 点—晚上9点
    周六下午 6 点-下午 9 点
    周日下午 6 点-下午 9 点

    4,学习产出

    技术笔记 1 遍
    查看CSDN技术博客 10 篇
    学习的 视频 2个

    **

    学习日记

    **
    1,学习知识点

    TensorFlow加载、导入数据集,设置验证集、测试集,查看数据集,训练等

    2,学习遇到的问题

    matplotlib的使用,pathlib如何选择图片问题,TensorFlow提高读取性能的方法

    3,学习的收获

    学会了如何使用TensorFlow运行机器学习,TensorFlow运行的基本流程。什么是卷积神经网络,如何卷积。构建卷积神经网络的过程。

    4,实操

    import matplotlib.pyplot as plt
    import os,PIL
    import numpy as np
    import tensorflow as tf
    from tensorflow import keras
    from tensorflow.keras import layers,models
    import pathlib
    
    
    data_dir = "D:/BaiduNetdiskDownload/weather_photos"
    #为调用Path的glob函数
    data_dir = pathlib.Path(data_dir)
    image_count = len(list(data_dir.glob('*/*.jpg')))
    print("图片总数为:",image_count)
    #查看图片
    rises = list(data_dir.glob('sunrise/*.jpg'))
    PIL.Image.open(str(rises[0])).show()
    #数据预处理
    batch_size = 32
    img_height = 180
    img_width = 180
    
    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)
    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))  #指定figure的宽和高
    
    for images, labels in train_ds.take(1):
        for i in range(25):
            ax = plt.subplot(5, 5, i + 1) #指的是在一个5行5列共25个子图的图中,定位第i个图来进行操作。
    
            plt.imshow(images[i].numpy().astype("uint8"))
            plt.title(class_names[labels[i]])
    
            plt.axis("off")
    plt.show()
    
    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
    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.Conv2D(64, (3, 3), activation='relu'),  # 卷积层
        layers.Dropout(0.3),
    # Flatten层
        layers.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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    运行结果:
    请添加图片描述
    可以看到模型在训练集上表现得好,在验证集上表现得不好,说明出现了过拟合。使用layers.Dropout(0.3) 消除一些神经元可以减少过拟合。

  • 相关阅读:
    4.3每日一题(知全微分求函数本身)
    前后端RSA加密梳理
    大学生网页作业成品——基于HTML网上书城项目的设计与实现
    Docker桥接网络分析
    使用JAVA pdf转word
    STM32F411配置GPIO输入输出(标准库)
    强化学习和torchrl
    【生成对抗网络 论文泛读】……StarganV1 & StarganV2……
    基于SSH的商城管理系统
    Pytorch中批量定义模型中相同结构的子模型(动态定义子模型变量名)
  • 原文地址:https://blog.csdn.net/misterfm/article/details/126103864