基于深度卷积神经网络的水果蔬菜图片分类预测

资料来源

前言

本次主要使用的为深度卷积神经网络,对36种水果蔬菜图片进行模型训练,预测可视化等

首先对数据集进行分析查看,数据集包含2个G的图片,包含3个文件夹,分为训练集、测试集、验证集

其次对数据集图像进行预处理,具体使用为图像增强

接下来使用增强后的数据集对深度卷积神经网络模型进行训练,卷积神经网络对多分类图像处理还是比较强的

最后使用训练的模型使用测试集进行预测评估,随机选取图像可视化结果并展示

1. 导包

import os
import warnings
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, Model, load_model
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.optimizers import Adam
from keras.preprocessing import image
import matplotlib.pyplot as plt
from keras.layers import concatenate

2. 查看分析数据集

data_dir = '/home/mw/input/fruable7115/frutable/frutable'
train_dir = os.path.join(data_dir, 'train')
test_dir = os.path.join(data_dir, 'test')
validation_dir = os.path.join(data_dir, 'validation')

2.1 查看数据集类别数目

def num_of_classes(folder_dir, folder_name) :
    classes = [class_name for class_name in os.listdir(train_dir)]
    print(f'数据集 {folder_name} 中的类别数 : {len(classes)}')
num_of_classes(train_dir, 'train')
num_of_classes(validation_dir, 'validation')
num_of_classes(test_dir, 'test')

在这里插入图片描述

2.2 类别数目

对各个类别进行计数

classes = [class_name for class_name in os.listdir(train_dir)]
count = []
for class_name in classes :
    count.append(len(os.listdir(os.path.join(train_dir, class_name))))

plt.figure(figsize=(15, 4))
ax = sns.barplot(x=classes, y=count, color='navy')
plt.xticks(rotation=285)
for i, value in enumerate(count):
    plt.text(i, value, str(value), ha='center', va='bottom', fontsize=10)
plt.title('每个类别数量', fontsize=25, fontweight='bold')
plt.xlabel('类别', fontsize=15)
plt.ylabel('数量', fontsize=15)
plt.yticks(np.arange(0, 105, 10))
plt.show()

在这里插入图片描述

2.3 图像数量

查看各个集的图像总数量

def create_df(folder_path) :
    all_images = []    
    for class_name in classes :
        class_path = os.path.join(folder_path, class_name)
        all_images.extend([(os.path.join(class_path, file_name), class_name) for file_name in os.listdir(class_path)])
    df = pd.DataFrame(all_images, columns=['file_path', 'label'])
    return df
train_df = create_df(train_dir)
validation_df = create_df(validation_dir)
test_df = create_df(test_dir)
print(f'训练集总图像数 : {len(train_df)}')
print(f'验证集总图像数 : {len(validation_df)}')
print(f'测试集总图像数 : {len(test_df)}')

在这里插入图片描述

2.4 查看部分图像

df_unique = train_df.copy().drop_duplicates(subset=["label"]).reset_index()

fig, axes = plt.subplots(nrows=6, ncols=6, figsize=(8, 7),
                        subplot_kw={'xticks': [], 'yticks': []})

for i, ax in enumerate(axes.flat):
    ax.imshow(plt.imread(df_unique.file_path[i]))
    ax.set_title(df_unique.label[i], fontsize = 12)
plt.tight_layout(pad=0.5)
plt.show()

在这里插入图片描述

3. 图像增强

对数据集进行图像增强,具体意义看下一个cell右边的注释

train_datagen = ImageDataGenerator(
    rescale=1./255,                 # 对图像进行缩放,将像素值标准化到一个较小的范围
    rotation_range=20,              # 随机旋转图像的角度范围,最多旋转 20 度
    width_shift_range=0.2,          # 水平方向上随机移动图像的比例,最多移动图像宽度的 20%
    height_shift_range=0.2,         # 垂直方向同上
    zoom_range=0.1,                 # 随机缩放图像的范围,最多缩放图像 10%
    horizontal_flip=True,           # 随机对图像进行水平翻转
    shear_range=0.1,                # 随机错切变换图像的范围,最多错切图像的 10%
    fill_mode='nearest',            # 对图像进行增强处理时的填充模式,这里设置为最近邻插值
    )
train_generator = train_datagen.flow_from_dataframe(
    dataframe=train_df, 
    x_col='file_path',
    y_col='label',
    target_size=(224, 224),         # 指定将图像调整为的目标大小
    color_mode='rgb',               # 指定图像的颜色模式
    class_mode='categorical',       # 指定分类问题的类型
    batch_size=32, 
    shuffle=True,                 	# 指定是否在每个时期之后打乱数据
    seed=42,
)
validation_datagen = ImageDataGenerator(rescale=1./255,)

validation_generator = validation_datagen.flow_from_dataframe(
    dataframe=validation_df,
    x_col='file_path',
    y_col='label',
    target_size=(224, 224),
    class_mode='categorical',
    batch_size=32,
    seed=42,
    shuffle=False
)
test_datagen = ImageDataGenerator(rescale=1./255,)

test_generator = test_datagen.flow_from_dataframe(
    dataframe=test_df,
    x_col='file_path',
    y_col='label',
    target_size=(224, 224),
    class_mode='categorical',
    batch_size=32,
    seed=42,
    shuffle=False
)

在这里插入图片描述

4. 模型训练

选择的是深度卷积神经网络模型,其中输入为(224,224)的RGB图片,输出36个神经元对应36总类别,多分类激活函数选择softmax

# 创建深度卷积神经网络模型
model = Sequential([
    Conv2D(32, (3,3), activation='relu', input_shape=(224, 224, 3)),
    MaxPooling2D((2,2)),
    Conv2D(64, (3,3), activation='relu'),
    MaxPooling2D((2,2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(36, activation='softmax') 
])

# 编译模型
model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy', metrics=['accuracy'])

4.1 模型训练

训练过程太长隐藏了,训练也挺久的,可以直接使用我训练好保存的/home/mw/project/fruit.h5

# 训练模型
history = model.fit_generator(
    train_generator,
    steps_per_epoch=100,
    epochs=10,
    validation_data=validation_generator,
    validation_steps=50
)
# 模型保存
model.save('fruit.h5')

在这里插入图片描述

4.2 模型评估

查看模型的准确率和损失值

plt.plot(history.history['acc'], label='Train Accuracy')
plt.plot(history.history['val_acc'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend()
plt.show()
# 部分版本使用的是accuracy
# plt.plot(history.history['accuracy'], label='Train Accuracy')
# plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
# plt.title('Model Accuracy')
# plt.ylabel('Accuracy')
# plt.xlabel('Epoch')
# plt.legend()
# plt.show()

在这里插入图片描述

plt.plot(history.history['loss'], label='Train Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend()
plt.show()

在这里插入图片描述

4.3 使用测试集进行预测评估

可以看出来准确率达到了0.977

# 预测和评估
loss, accuracy = model.evaluate_generator(test_generator, steps=50)
print("Test accuracy:", accuracy)
# 加载模型
model = load_model('fruit.h5')

5. 结果展示

随机选择图片进行预测,可视化展示图片、原类别和预测类别
由于每次随机选择的图片不一样,所以多预测几次其实就会发现模型对部分存在明显特征的水果或者蔬菜预测准确率比较好,比如说颜色单一不单一,图片干扰项多不多什么的,感觉很明显

val_images, val_labels = next(validation_generator)

# 进行预测
predictions = model.predict(val_images)
pred_labels = np.argmax(predictions, axis=1)
true_labels = np.argmax(val_labels, axis=1)

# 获取类别映射
class_indices = validation_generator.class_indices
class_names = {v: k for k, v in class_indices.items()}

# 定义显示图像的函数
def display_images(images, true_labels, pred_labels, class_names, num_images=9):
    plt.figure(figsize=(15, 15))
    for i in range(num_images):
        plt.subplot(3, 3, i + 1)
        plt.imshow(images[i])
        true_label = class_names[int(true_labels[i])]
        pred_label = class_names[int(pred_labels[i])]
        plt.title(f"True: {true_label}\nPred: {pred_label}")
        plt.axis('off')
    plt.tight_layout()
    plt.show()

# 调用显示函数
display_images(val_images, true_labels, pred_labels, class_names, num_images=9)

在这里插入图片描述

资料来源

### 关于卷积神经网络在水果蔬菜分类中的应用 卷积神经网络(Convolutional Neural Network, CNN)作为一种深度学习模型,在图像处理领域表现出卓越的能力。其核心优势在于能够自动提取特征并减少人工干预,这使得它成为水果蔬菜分类的理想工具[^1]。 #### 数据集与实验设置 在基于CNN水果蔬菜分类研究中,通常会采用公开的数据集或自建数据集来验证模型的有效性。例如,引用提到的一个项目利用了包含多种水果蔬菜类别的图像数据集,并结合数据增强技术提升模型泛化能力[^2]。此外,另一个案例展示了如何应对类别间样本分布不平衡的问题,这对于实际应用场景尤为重要[^3]。 #### 不同模型结构比较 为了评估不同CNN架构的表现,研究人员常选用经典模型如VGG、ResNet以及轻量化模型MobileNet等进行对比测试。具体而言: - **ResNet50**:由于引入残差连接机制解决了深层网络训练困难问题,因此被广泛应用于复杂场景下的目标识别任务中[^2]。 - **YOLO系列**:该家族算法以其快速推理速度著称,在实时性要求较高的场合下表现优异[^1]。 以下是两种典型框架之间的性能差异分析表: | 模型名称 | 准确率 (%) | 推理时间 (ms/image) | |----------|-------------|----------------------| | ResNet50 | 94 | 25 | | YOLOv5 | 92 | 15 | 从表格可见,虽然ResNet50提供了更高的预测精度,但相应的计算成本也更高;而YOLOv5则牺牲了一定程度上的准确性换取更快的速度,适合资源受限环境部署。 #### 特殊硬件支持下的优化方案 除了纯软件层面改进外,还有些工作探索了嵌入式设备上运行AI模型的可能性。比如某篇论文描述了一种基于STM32微控制器配合特定CNN实现的智能果蔬电子秤系统,不仅完成了重量测量还实现了品种辨别功能[^4]。这种跨学科融合为未来便携式农产品质检仪器开发开辟新思路。 ```python import tensorflow as tf from tensorflow.keras.applications import ResNet50 # 加载预训练模型 base_model = ResNet50(weights='imagenet', include_top=False) # 构建顶层全连接层用于分类 x = base_model.output predictions = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) ``` 以上代码片段展示了一个简单的迁移学习过程,其中`num_classes`应根据具体任务调整为目标类别数。 ---
基于卷积神经网络的水果蔬菜识别系统是一种利用深度学习技术实现的果蔬菜品种分类预测系统。该系统使用卷积神经网络作为模型,前端界面使用Vue实现,FastApi为后端服务器。用户可以通过上传待预测水果蔬菜图片到服务器,利用已经训练好的模型进行预测,准确率能达到90%。此外,该系统还实现了模型架构分析、数据处理分析以及结果分析功能。 具体实现效果如下: 1. 种类预测界面:用户可以通过上传待预测水果蔬菜图片到服务器,系统会自动进行预测,并返回预测结果。 2. 数据预处理分析:该系统还实现了数据预处理分析功能,可以对数据进行处理和分析,以提高模型的准确率。 代码示例: ```python # 导入必要的库 import tensorflow as tf from tensorflow.keras.preprocessing.image import ImageDataGenerator # 定义数据生成器 train_datagen = ImageDataGenerator(rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator(rescale=1./255) # 加载训练集和测试集数据 train_set = train_datagen.flow_from_directory('train', target_size=(64, 64), batch_size=32, class_mode='categorical') test_set = test_datagen.flow_from_directory('test', target_size=(64, 64), batch_size=32, class_mode='categorical') # 定义模型 model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(64, 64, 3)), tf.keras.layers.MaxPooling2D((2, 2)), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(5, activation='softmax') ]) # 编译模型 model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # 训练模型 model.fit(train_set, epochs=10, validation_data=test_set) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值