番茄成熟程度检测与分割数据集/西红柿成熟程度检测与分割数据集,检测集yolo格式或者voc格式均可 yolo格式或者voc 7800张 番茄数据集

 

西红柿成熟程度检测与分割数据集,其中,检测数据集yolo格式或者voc格式均可,标签分为unripe,semiripe,fullyripe和rotten,分割提供json格式标注,标签分为fully_ripened ,half_ripened ,green ,分割图像约1000张,检测图像约7800张

西红柿成熟程度检测与分割数据集介绍

数据集概览
  • 总图像数量:
    • 检测图像: 约7800张
    • 分割图像: 约1000张
  • 标签分类:
    • 检测任务:
      • unripe(未成熟)
      • semiripe(半成熟)
      • fullyripe(完全成熟)
      • rotten(腐烂)
    • 分割任务:
      • fully_ripened(完全成熟)
      • half_ripened(半成熟)
      • green(绿色,未成熟)
  • 格式:
    • 检测任务: YOLO格式或VOC格式
    • 分割任务: JSON格式标注
  • 用途: 用于西红柿成熟程度的检测和分割任务。
数据集结构

假设数据集文件夹结构如下:

tomato_dataset/
├── detection/
│   ├── images/
│   │   ├── train/
│   │   ├── val/
│   │   └── test/
│   ├── labels/
│   │   ├── train/  # YOLO或VOC格式
│   │   ├── val/
│   │   └── test/
├── segmentation/
│   ├── images/
│   │   ├── train/
│   │   ├── val/
│   │   └── test/
│   ├── annotations/
│   │   ├── train/  # JSON格式
│   │   ├── val/
│   │   └── test/
└── README.md
  • detection/images/ 和 segmentation/images/ 目录下分别存放检测和分割任务的图像。
  • detection/labels/ 目录下存放检测任务的标注文件(YOLO或VOC格式)。
  • segmentation/annotations/ 目录下存放分割任务的JSON格式标注文件。
  • README.md 文件包含数据集的使用说明和字段解释。
标注文件示例
检测任务 (YOLO格式)
# 示例:detection/labels/train/00001.txt
0 0.456 0.321 0.123 0.089  # unripe
1 0.678 0.456 0.154 0.123  # semiripe
2 0.789 0.567 0.189 0.156  # fullyripe
3 0.910 0.678 0.212 0.189  # rotten
检测任务 (VOC格式)
<annotation>
    <folder>train</folder>
    <filename>00001.jpg</filename>
    <size>
        <width>640</width>
        <height>480</height>
        <depth>3</depth>
    </size>
    <object>
        <name>unripe</name>
        <bndbox>
            <xmin>292</xmin>
            <ymin>154</ymin>
            <xmax>415</xmax>
            <ymax>242</ymax>
        </bndbox>
    </object>
    <object>
        <name>semiripe</name>
        <bndbox>
            <xmin>435</xmin>
            <ymin>220</ymin>
            <xmax>590</xmax>
            <ymax>350</ymax>
        </bndbox>
    </object>
    <!-- 其他对象 -->
</annotation>
分割任务 (JSON格式)
{
    "imagePath": "00001.jpg",
    "shapes": [
        {
            "label": "fully_ripened",
            "points": [[100, 100], [200, 100], [200, 200], [100, 200]],
            "group_id": null,
            "shape_type": "polygon"
        },
        {
            "label": "half_ripened",
            "points": [[300, 150], [400, 150], [400, 250], [300, 250]],
            "group_id": null,
            "shape_type": "polygon"
        },
        {
            "label": "green",
            "points": [[500, 200], [600, 200], [600, 300], [500, 300]],
            "group_id": null,
            "shape_type": "polygon"
        }
    ]
}
使用场景
  • 成熟度检测: 用于自动识别和分类西红柿的成熟程度。
  • 质量控制: 帮助农民和食品加工厂快速筛选出不同成熟度的西红柿。
  • 智能农业: 结合其他技术如机器人采摘,实现自动化收割和分拣。
Keras 训练代码示例

以下是一个使用Keras框架进行模型训练的代码示例。我们将分别展示检测任务和分割任务的代码示例。

检测任务 (YOLO格式)

import os
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications.efficientnet import EfficientNetB0
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

# 数据集路径
data_dir = '/path/to/tomato_dataset/detection'
train_images_dir = os.path.join(data_dir, 'images/train')
val_images_dir = os.path.join(data_dir, 'images/val')

# 图像生成器
datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)

# 训练集生成器
train_generator = datagen.flow_from_directory(
    train_images_dir,
    target_size=(224, 224),
    batch_size=32,
    class_mode='categorical'
)

# 验证集生成器
val_generator = datagen.flow_from_directory(
    val_images_dir,
    target_size=(224, 224),
    batch_size=32,
    class_mode='categorical'
)

# 加载预训练的EfficientNetB0模型
base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(224, 224, 3))

# 添加全局平均池化层
x = base_model.output
x = GlobalAveragePooling2D()(x)

# 添加全连接层
x = Dense(1024, activation='relu')(x)
predictions = Dense(4, activation='softmax')(x)  # 4个类别

# 构建最终模型
model = Model(inputs=base_model.input, outputs=predictions)

# 冻结基础模型的层
for layer in base_model.layers:
    layer.trainable = False

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

# 打印模型概要
model.summary()

# 设置回调函数
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
checkpoint = ModelCheckpoint('best_detection_model.h5', save_best_only=True, monitor='val_accuracy', mode='max')

# 训练模型
history = model.fit(
    train_generator,
    steps_per_epoch=len(train_generator),
    epochs=20,
    validation_data=val_generator,
    validation_steps=len(val_generator),
    callbacks=[early_stopping, checkpoint]
)

# 保存模型
model.save('tomato_detection_model.h5')

# 可视化训练过程
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.legend()
plt.title('Accuracy')

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.title('Loss')

plt.show()

分割任务 (JSON格式)

为了处理分割任务,我们可以使用U-Net架构。这里我们使用Keras来构建一个简单的U-Net模型,并加载JSON格式的标注文件。

import os
import json
import numpy as np
import cv2
import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, Concatenate
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from sklearn.model_selection import train_test_split

# 数据集路径
data_dir = '/path/to/tomato_dataset/segmentation'
images_dir = os.path.join(data_dir, 'images')
annotations_dir = os.path.join(data_dir, 'annotations')

# 读取图像和标注
def load_image_and_mask(image_path, annotation_path):
    image = cv2.imread(image_path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (256, 256))  # 调整大小
    image = image / 255.0  # 归一化
    
    with open(annotation_path, 'r') as f:
        annotation = json.load(f)
    
    mask = np.zeros((256, 256, 3), dtype=np.uint8)
    
    for shape in annotation['shapes']:
        points = np.array(shape['points'], dtype=np.int32)
        if shape['label'] == 'fully_ripened':
            color = (255, 0, 0)  # 红色
        elif shape['label'] == 'half_ripened':
            color = (0, 255, 0)  # 绿色
        elif shape['label'] == 'green':
            color = (0, 0, 255)  # 蓝色
        cv2.fillPoly(mask, [points], color)
    
    mask = mask / 255.0  # 归一化
    return image, mask

# 加载所有图像和标注
images = []
masks = []

for filename in os.listdir(images_dir):
    if filename.endswith('.jpg'):
        image_path = os.path.join(images_dir, filename)
        annotation_path = os.path.join(annotations_dir, filename.replace('.jpg', '.json'))
        if os.path.exists(annotation_path):
            image, mask = load_image_and_mask(image_path, annotation_path)
            images.append(image)
            masks.append(mask)

images = np.array(images)
masks = np.array(masks)

# 划分训练集和验证集
X_train, X_val, y_train, y_val = train_test_split(images, masks, test_size=0.2, random_state=42)

# 构建U-Net模型
def build_unet(input_shape):
    inputs = Input(input_shape)
    
    # 编码器
    conv1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
    conv1 = Conv2D(64, 3, activation='relu', padding='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
    
    conv2 = Conv2D(128, 3, activation='relu', padding='same')(pool1)
    conv2 = Conv2D(128, 3, activation='relu', padding='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
    
    # 中间层
    conv3 = Conv2D(256, 3, activation='relu', padding='same')(pool2)
    conv3 = Conv2D(256, 3, activation='relu', padding='same')(conv3)
    
    # 解码器
    up4 = UpSampling2D(size=(2, 2))(conv3)
    merge4 = Concatenate()([up4, conv2])
    conv4 = Conv2D(128, 3, activation='relu', padding='same')(merge4)
    conv4 = Conv2D(128, 3, activation='relu', padding='same')(conv4)
    
    up5 = UpSampling2D(size=(2, 2))(conv4)
    merge5 = Concatenate()([up5, conv1])
    conv5 = Conv2D(64, 3, activation='relu', padding='same')(merge5)
    conv5 = Conv2D(64, 3, activation='relu', padding='same')(conv5)
    
    outputs = Conv2D(3, 1, activation='sigmoid')(conv5)
    
    model = Model(inputs=inputs, outputs=outputs)
    return model

input_shape = (256, 256, 3)
model = build_unet(input_shape)

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

# 打印模型概要
model.summary()

# 设置回调函数
early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True)
checkpoint = ModelCheckpoint('best_segmentation_model.h5', save_best_only=True, monitor='val_accuracy', mode='max')

# 训练模型
history = model.fit(
    X_train, y_train,
    batch_size=32,
    epochs=20,
    validation_data=(X_val, y_val),
    callbacks=[early_stopping, checkpoint]
)

# 保存模型
model.save('tomato_segmentation_model.h5')

# 可视化训练过程
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.legend()
plt.title('Accuracy')

plt.subplot(1, 2, 2)
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.legend()
plt.title('Loss')

plt.show()

代码说明

  1. 数据生成器:

    • 对于检测任务,使用ImageDataGenerator进行数据增强。
    • 对于分割任务,手动加载图像和JSON标注文件,并进行预处理。
  2. 模型构建:

    • 检测任务: 使用预训练的EfficientNetB0模型作为基础,添加全连接层进行分类。
    • 分割任务: 构建一个简单的U-Net模型进行语义分割。
  3. 模型编译:

    • 使用Adam优化器,学习率为0.0001。
    • 损失函数为交叉熵损失,评估指标为准确率。
  4. 模型训练:

    • 使用fit方法进行训练,设置早停机制和最佳模型保存。
    • 训练过程中会记录训练集和验证集的准确率和损失。
  5. 可视化:

    • 训练完成后,绘制训练集和验证集的准确率和损失曲线,帮助分析模型的训练效果。

希望这段代码对你有所帮助!如果有任何问题或需要进一步的帮助,

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值