水果蔬菜数据集,

简介: 该数据集包含各种水果和蔬菜的图像,为图像识别任务提供了多样化的集合。 其中包括的食物有:
水果:香蕉、苹果、梨、葡萄、橙子、猕猴桃、西瓜、石榴、菠萝、芒果
蔬菜:黄瓜、胡萝卜、辣椒、洋葱、土豆、柠檬、西红柿、小萝卜、甜菜、卷心菜、生菜、菠菜、大豆、花椰菜、灯笼椒、辣椒、芜菁、玉米、甜玉米、红薯、辣椒粉、墨西哥胡椒、生姜、大蒜、豌豆、茄子
内容
数据集分为三个主要文件夹:
训练:每个类别包含 100 张图像。
测试:每个类别包含 10 张图像。
验证:每个类别包含 10 张图像。
每个文件夹又细分为针对每种水果和蔬菜的特定文件夹,包含相应的图像。
好的,这个水果和蔬菜数据集非常适合用于深度学习的图像分类任务。下面是一个详细的指南,帮助你从头开始准备数据、训练模型、评估模型和可视化结果。我们将使用TensorFlow和Keras进行模型训练。
1. 环境准备
首先,确保你已经安装了必要的库和工具。你可以使用以下命令安装所需的库:
pip install tensorflow
pip install numpy
pip install matplotlib
pip install scikit-learn
2. 数据集准备
假设你的数据集目录结构如下:
fruit_vegetable_dataset/
├── train/
│ ├── banana/
│ ├── apple/
│ ├── ...
│ └── ginger/
├── test/
│ ├── banana/
│ ├── apple/
│ ├── ...
│ └── ginger/
├── validation/
│ ├── banana/
│ ├── apple/
│ ├── ...
│ └── ginger/
每个类别文件夹包含相应的图像。
3. 数据加载和预处理
使用Keras的ImageDataGenerator来加载和预处理数据。
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# 设置图像尺寸和批次大小
img_height = 224
img_width = 224
batch_size = 32
# 数据生成器
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
validation_split=0.2
)
validation_datagen = ImageDataGenerator(rescale=1./255)
# 加载训练数据
train_generator = train_datagen.flow_from_directory(
'fruit_vegetable_dataset/train',
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical',
subset='training'
)
# 加载验证数据
validation_generator = validation_datagen.flow_from_directory(
'fruit_vegetable_dataset/validation',
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical'
)
# 加载测试数据
test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory(
'fruit_vegetable_dataset/test',
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical'
)
4. 构建模型
使用预训练的MobileNetV2模型进行迁移学习。
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
# 加载预训练的MobileNetV2模型
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(img_height, img_width, 3))
# 冻结基础模型的层
for layer in base_model.layers:
layer.trainable = False
# 构建模型
model = Sequential([
base_model,
GlobalAveragePooling2D(),
Dense(256, activation='relu'),
Dense(28, activation='softmax') # 28个类别
])
# 编译模型
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# 打印模型摘要
model.summary()
5. 训练模型
使用训练数据和验证数据进行模型训练。
# 训练模型
history = model.fit(
train_generator,
steps_per_epoch=len(train_generator),
epochs=50,
validation_data=validation_generator,
validation_steps=len(validation_generator)
)
6. 评估模型
使用测试数据评估模型性能。
# 评估模型
evaluation = model.evaluate(test_generator)
print(f'Test Loss: {evaluation[0]}')
print(f'Test Accuracy: {evaluation[1]}')
7. 可视化训练过程
使用Matplotlib绘制训练过程中的损失和准确率曲线。
import matplotlib.pyplot as plt
# 绘制训练和验证损失
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
# 绘制训练和验证准确率
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
8. 保存模型
保存训练好的模型以供后续使用。
# 保存模型
model.save('fruit_vegetable_model.h5')
9. 总结
通过以上步骤,你可以成功地使用TensorFlow和Keras对水果和蔬菜数据集进行训练、评估和可视化。
1631



被折叠的 条评论
为什么被折叠?



