当前位置: 代码网 > 科技>人工智能>神经网络 > 基于卷积神经网络的农作物病虫害图像识别(Opencv,Pytorch,Tensorflow,MobileNetV3)

基于卷积神经网络的农作物病虫害图像识别(Opencv,Pytorch,Tensorflow,MobileNetV3)

2024年08月05日 神经网络 我要评论
最近做了一个农作物虫害图像识别的程序,在此分享一下。本文用到的深度学习框架为Tensorflow2,Opencv等等!使用的数据集共有61种类别,分别代表不同的虫害类别。使用的网络模型为moblienetv3.

前言:

最近做了一个农作物虫害图像识别的程序,在此分享一下。本文用到的深度学习框架为tensorflow2,opencv等等!使用的数据集共有61种类别,分别代表不同的虫害类别。使用的网络模型为moblienetv3.

bi 设 dai 坐

效果视频如下所示:

农作物虫害图像识别

搭建mobilenetv3模型

代码如下所示:

# 根据tf.keras的官方代码修改的mobilenetv3的网络模型
import tensorflow as tf
from keras import layers, models

"""
    reference:
    - [searching for mobilenetv3](https://arxiv.org/pdf/1905.02244.pdf) (iccv 2019)
    the following table describes the performance of mobilenets v3:
    ------------------------------------------------------------------------
    macs stands for multiply adds
    |classification checkpoint|macs(m)|parameters(m)|top1 accuracy|pixel1 cpu(ms)|
    |---|---|---|---|---|
    | mobilenet_v3_large_1.0_224              | 217 | 5.4 |   75.6   |   51.2  |
    | mobilenet_v3_large_0.75_224             | 155 | 4.0 |   73.3   |   39.8  |
    | mobilenet_v3_large_minimalistic_1.0_224 | 209 | 3.9 |   72.3   |   44.1  |
    | mobilenet_v3_small_1.0_224              | 66  | 2.9 |   68.1   |   15.8  |
    | mobilenet_v3_small_0.75_224             | 44  | 2.4 |   65.4   |   12.8  |
    | mobilenet_v3_small_minimalistic_1.0_224 | 65  | 2.0 |   61.9   |   12.2  |
    for image classification use cases, see
    [this page for detailed examples](https://keras.io/api/applications/#usage-examples-for-image-classification-models).
    for transfer learning use cases, make sure to read the
    [guide to transfer learning & fine-tuning](https://keras.io/guides/transfer_learning/).
"""


##################################################################################################################################
# 定义v3的完整模型 #################################################################################################################
##################################################################################################################################
def mobilenetv3(input_shape=[224, 224, 3], classes=1000, dropout_rate=0.2, alpha=1.0, weights=none,
                model_type='large', minimalistic=false, classifier_activation='softmax', include_preprocessing=false):
    # 如果有权重文件,那就意味着要迁移学习,那就意味着需要让bn层始终处于infer状态,否则解冻整个网络后,会出现acc下降loss上升的现象,终其原因是解冻网络之
    # 前,网络bn层用的是之前数据集的均值和方差,解冻后虽然维护着新的滑动平均和滑动方差,但是单次训练时使用的是当前batch的均值和方差,差异太大造成特征崩塌
    if weights:
        bn_training = false
    else:
        bn_training = none
    bn_decay = 0.99  # bn层的滑动平均系数,这个值的设置需要匹配steps和batchsize否则会出现奇怪现象
    # 确定通道所处维度
    channel_axis = -1
    # 根据是否为mini设置,修改部分配置参数
    if minimalistic:
        kernel = 3
        activation = relu
        se_ratio = none
        name = "mini"
    else:
        kernel = 5
        activation = hard_swish
        se_ratio = 0.25
        name = "norm"
    # 定义模型输入张量
    img_input = layers.input(shape=input_shape)
    # 是否包含预处理层
    if include_preprocessing:
        x = layers.rescaling(scale=1. / 127.5, offset=-1.)(img_input)
    else:
        x = img_input
    # 定义整个模型的第一个特征提取层
    x = layers.conv2d(16, kernel_size=3, strides=(2, 2), padding='same', use_bias=false, name='conv')(x)
    x = layers.batchnormalization(axis=channel_axis, epsilon=1e-3, momentum=bn_decay, name='conv/batchnorm')(x,
                                                                                                             training=bn_training)
    x = activation(x)
    # 定义整个模型的骨干特征提取
    if model_type == 'large':
        x = mobilenetv3large(x, kernel, activation, se_ratio, alpha, bn_training, bn_decay)
        last_point_ch = 1280
    else:
        x = mobilenetv3small(x, kernel, activation, se_ratio, alpha, bn_training, bn_decay)
        last_point_ch = 1024
    # 定义整个模型的后特征提取
    last_conv_ch = _depth(x.shape[channel_axis] * 6)
    # if the width multiplier is greater than 1 we increase the number of output channels
    if alpha > 1.0:
        last_point_ch = _depth(last_point_ch * alpha)
    x = layers.conv2d(last_conv_ch, kernel_size=1, padding='same', use_bias=false, name='conv_1')(x)
    x = layers.batchnormalization(axis=channel_axis, epsilon=1e-3, momentum=bn_decay, name='conv_1/batchnorm')(x,
                                                                                                               training=bn_training)
    x = activation(x)
    # 如果tf版本大于等于2.6则直接使用下面第一句就可以了,否则使用下面2~3句
    # x = layers.globalaveragepooling2d(data_format='channels_last', keepdims=true)(x)
    x = layers.globalaveragepooling2d(data_format='channels_last')(x)
    x = tf.expand_dims(tf.expand_dims(x, 1), 1)
    # 定义第一个特征分类层
    x = layers.conv2d(last_point_ch, kernel_size=1, padding=
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com