当前位置: 首页 > article >正文

J1打卡——鸟类识别

  •   🍨 本文为🔗365天深度学习训练营中的学习记录博客
  • 🍖 原作者:K同学啊

1.检查GPU

import tensorflow as tf
gpus=tf.config.list_physical_devices("GPU")
if gpus:
    tf.config.experimental.set_memory_growth(gpus[0],True)  
    tf.config.set_visible_devices([gpus[0]],"GPU")

​​​​

2.查看数据

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']  
plt.rcParams['axes.unicode_minus'] = False 
import os,PIL,pathlib
import numpy as np
from tensorflow import keras
from tensorflow.keras import layers,models

data_dir="D:\\jupyter lab\\训练营\\data\\第8天\\bird_photos"
data_dir=pathlib.Path(data_dir)

image_count = len(list(data_dir.glob('*/*')))
print("图片总数为:",image_count)

3.划分数据集

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=(10, 5))  # 图形的宽为10高为5
plt.suptitle("photo")
for images, labels in train_ds.take(1):
    for i in range(8):
        ax = plt.subplot(2, 4, i + 1)  
        plt.imshow(images[i].numpy().astype("uint8"))
        plt.title(class_names[labels[i]])
        plt.axis("off")

plt.imshow(images[1].numpy().astype("uint8"))

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)

​​​​​​

4.创建模型

​​

from keras import layers

from keras.layers import Input,Activation,BatchNormalization,Flatten
from keras.layers import Dense,Conv2D,MaxPooling2D,ZeroPadding2D,AveragePooling2D
from keras.models import Model

def identity_block(input_tensor, kernel_size, filters, stage, block):

    filters1, filters2, filters3 = filters

    name_base = str(stage) + block + '_identity_block_'

    x = Conv2D(filters1, (1, 1), name=name_base + 'conv1')(input_tensor)
    x = BatchNormalization(name=name_base + 'bn1')(x)
    x = Activation('relu', name=name_base + 'relu1')(x)

    x = Conv2D(filters2, kernel_size,padding='same', name=name_base + 'conv2')(x)
    x = BatchNormalization(name=name_base + 'bn2')(x)
    x = Activation('relu', name=name_base + 'relu2')(x)

    x = Conv2D(filters3, (1, 1), name=name_base + 'conv3')(x)
    x = BatchNormalization(name=name_base + 'bn3')(x)

    x = layers.add([x, input_tensor] ,name=name_base + 'add')
    x = Activation('relu', name=name_base + 'relu4')(x)
    return x

def conv_block(input_tensor, kernel_size, filters, stage, block, strides=(2, 2)):

    filters1, filters2, filters3 = filters

    res_name_base = str(stage) + block + '_conv_block_res_'
    name_base = str(stage) + block + '_conv_block_'

    x = Conv2D(filters1, (1, 1), strides=strides, name=name_base + 'conv1')(input_tensor)
    x = BatchNormalization(name=name_base + 'bn1')(x)
    x = Activation('relu', name=name_base + 'relu1')(x)

    x = Conv2D(filters2, kernel_size, padding='same', name=name_base + 'conv2')(x)
    x = BatchNormalization(name=name_base + 'bn2')(x)
    x = Activation('relu', name=name_base + 'relu2')(x)

    x = Conv2D(filters3, (1, 1), name=name_base + 'conv3')(x)
    x = BatchNormalization(name=name_base + 'bn3')(x)

    shortcut = Conv2D(filters3, (1, 1), strides=strides, name=res_name_base + 'conv')(input_tensor)
    shortcut = BatchNormalization(name=res_name_base + 'bn')(shortcut)

    x = layers.add([x, shortcut], name=name_base+'add')
    x = Activation('relu', name=name_base+'relu4')(x)
    return x

def ResNet50(input_shape=[224,224,3],classes=1000):

    img_input = Input(shape=input_shape)
    x = ZeroPadding2D((3, 3))(img_input)

    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
    x = BatchNormalization(name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

    x =     conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x =     conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x =     conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x =     conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = AveragePooling2D((7, 7), name='avg_pool')(x)

    x = Flatten()(x)
    x = Dense(classes, activation='softmax', name='fc1000')(x)

    model = Model(img_input, x, name='resnet50')
    
    # 加载预训练模型
    model.load_weights("resnet50_weights_tf_dim_ordering_tf_kernels.h5")

    return model

model = ResNet50()
model.summary()

.....


​​​​​​

5.编译及训练模型
 

model.compile(optimizer="adam",
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

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.suptitle("photo")

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

​​​​​7.预测图片

plt.figure(figsize=(10, 5)) 
plt.suptitle("photo")

for images, labels in val_ds.take(1):
    for i in range(8):
        ax = plt.subplot(2, 4, i + 1)  
        
        plt.imshow(images[i].numpy().astype("uint8"))
        
        # 需要给图片增加一个维度
        img_array = tf.expand_dims(images[i], 0) 
        
        predictions = model.predict(img_array)
        plt.title(class_names[np.argmax(predictions)])

        plt.axis("off")

​​总结:

     这些代码展示了如何使用TensorFlow和Keras构建、训练并评估一个基于ResNet50架构的图像分类模型。

  1. 检查GPU:首先,通过TensorFlow检查系统中是否存在可用的GPU,并设置内存增长以优化性能。

  2. 查看数据:定义了数据集的路径,并使用matplotlib和TensorFlow库来探索数据集的基本信息,包括图片总数和类别名称。此外,还展示了一些样本图片及其标签,帮助直观理解数据集的构成。

  3. 划分数据集:利用tf.keras.preprocessing.image_dataset_from_directory函数将数据集划分为训练集和验证集,以便后续模型训练与验证。

  4. 创建模型:实现了经典的ResNet50模型,包含基本块(identity block)和卷积块(convolutional block),这些块是ResNet的核心组成部分。此步骤还包括加载预训练权重,以增强模型的泛化能力。

  5. 编译及训练模型:配置了Adam优化器、损失函数以及评价指标,并对模型进行了训练。这里采用了10个epoch的训练周期,可以根据实际情况调整这个参数以达到更好的效果。

  6. 结果可视化:通过绘制训练和验证的准确率与损失曲线,可以直观地看到模型在训练过程中的表现。这对于分析模型是否过拟合或欠拟合非常有帮助。

  7. 预测图片:最后,选取验证集中的一些图片进行预测,展示模型的实际识别效果。这一步骤有助于评估模型的最终性能,并提供了一个直观的方式来检验模型的有效性。


http://www.kler.cn/a/515446.html

相关文章:

  • 当使用 npm 时,出现 `certificate has expired` 错误通常意味着请求的证书已过期。
  • javaweb之HTML
  • 观察者模式 - 观察者模式的应用场景
  • Golang之Context详解
  • MFC程序设计(二)基于对话框编程
  • 高并发压力测试
  • 智慧公安(实景三维公安基层基础平台)建设方案——第4章
  • Spring的条件加载
  • Github配置ssh详细步骤
  • Linux 系统服务开机自启动指导手册
  • owasp SQL 手工注入 - 02 (技巧)
  • Android 问题00_IncompatibleComposeRuntimeVersionException
  • Fastapi + vue3 自动化测试平台(4)-- fastapi分页查询封装
  • 前端jquery 实现文本框输入出现自动补全提示功能
  • yolov11 推理保存json
  • Windows 环境下 Docker Desktop + Kubernetes 部署项目指南
  • 免费SSL证书申请,springboot 部署证书
  • 【自动化测试】—— Appium使用保姆教程
  • SoftGNSS软件接收机源码阅读(一)程序简介、运行调试、执行流程
  • 数据结构——树和二叉树
  • Linux 下注册分析(1)
  • 用AI生成PPT,办公效率提升新方式
  • 基于 Vue3 + Canvas + Web Worker 实现高性能图像黑白转换工具的设计与实现
  • Linux通过docker部署京东矩阵容器服务
  • canvas基础
  • 【EXCEL_VBA_实战】多工作薄合并深入理解