Background Object detection is a fundamental computer vision task with broad…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Background Object detection is a fundamental computer vision task with broad applications. While methods like YOLO achieve high accuracy on standard benchmarks, they are limited to detecting only predefined object classes. Fine-tuning a pretrained detector (e.g., YOLOv11) with a custom dataset may overcome this limitation. However, manually annotating large datasets is impractical due to the time and effort required. An openworld object detector is a computer vision model capable of detecting objects of arbitrary classes, beyond a fixed predefined set. In this task, we will design an auto-annotated food dataset using an open-world object detector and try to implement a Chinese food recognizer. Task 1: Auto-Annotated Custom Dataset Genatration (20 points) • Objective: Generate a custom food detection dataset using any open-world object detector (the following uses Grounding DINO as an example), for automatic annotation. (a) Prepare the environment of Grounding DINO from GroundingDINO. (b) Use pretrained checkpoints to automatically annotate the Food-101 dataset. You can easily load Food-101 dataset using datasets.load_dataset(’food101’) after installing datasets. (c) Implement a pipeline using Grounding DINO to detect food objects in images. You can use the open source models in the code repository. 1(d) Annotate Food-101 dataset following the directory format for fine-tuning YOLO. You can use just ”food” as the prompt. (e) Visualize image samples annotated with bounding boxes and results of other steps in your report. Task 2: Fine-tune YOLOv11n on the Generated Dataset (40 points) • Objective: Practice how to fine-tune visual models and optimization strategies, and understand evaluation metrics. • We recommend using YOLOv11n as base model. You can use any other method you like, the following will use YOLOv11n as an example. • Fine-tune the base model on your auto-annotated dataset. • Please report the following: (a) List the evaluation metrics for this task that you think are important and give reasons. (b) Give the parameter settings that you think are reasonable and how model performance varies with parameters. Give the data for your best results. (c) Present success and failure cases in your pre- and post-fine-tuning tests. (d) Study the architecture design of yolov11 and your chosen open-world object detector, compare the difference between auto-annotated test images and their actual predictions, describe and briefly analyze the differences you find. Task 3: Fine-tune YOLOv11n on ChineseFoodNet (40 points) • Objective: Further refine the model using ChineseFoodNet to enhance accuracy in identifying Chinese cuisine. Construct a subset to reduce the effort required for fine-tuning. • You can download the ChineseFoodNet dataset via ChineseFoodNet and class names. • Please take a photo of meal that you love (web photos are allowed, photos should include at least 3 classes of food) and select the appropriate classes in the ChineseFoodNet dataset. • Select 3 additional categories from ChineseFoodNet with low correlations to construct a subset. Design a process that you think is reasonable for constructing the subset for fine-tuning. • Fine-tune YOLOv11n on your new subset. • Please report the following: (a) Describe your subset building process in detail, and show screenshots of your subsets in detail. (b) Show the detection result of the fine-tuned model on your prepared photo. (c) If the previous step fails, don’t be frustrated, you can also report the results of the corresponding class on the test set evaluation metrics as an alternative.我已经完成了task1和task2,现在请你完成一下task3的编码实现, base model用yolov8
Certainly! Below is a step-by-step guide and code implementation for Task 3 using YOLOv8 as the base model. We'll follow the instructions provided:
First, download the ChineseFoodNet dataset and prepare it for fine-tuning.
import os
import shutil
from PIL import Image
import requests
from zipfile import ZipFile
# Download the dataset
dataset_url = 'http://mmlab.ie.cuhk.edu.hk/projects/ChineseFoodNet/dataset/ChineseFoodNet_dataset.zip'
dataset_zip = 'ChineseFoodNet_dataset.zip'
dataset_dir = 'ChineseFoodNet_dataset'
if not os.path.exists(dataset_dir):
response = requests.get(dataset_url)
with open(dataset_zip, 'wb') as f:
f.write(response.content)
with ZipFile(dataset_zip, 'r') as zip:
zip.extractall()
os.remove(dataset_zip)
Select a photo that includes at least 3 classes of food and 3 additional categories with low correlations.
# Example: Select categories
selected_categories = ['dimsum', 'noodle', 'rice', 'soup', 'vegetable']
# Create directories for the subset
subset_dir = 'ChineseFoodNet_subset'
os.makedirs(subset_dir, exist_ok=True)
os.makedirs(os.path.join(subset_dir, 'images'), exist_ok=True)
os.makedirs(os.path.join(subset_dir, 'annotations'), exist_ok=True)
# Copy selected images and annotations
for category in selected_categories:
src_images_dir = os.path.join(dataset_dir, category)
dest_images_dir = os.path.join(subset_dir, 'images')
src_annotations_dir = os.path.join(dataset_dir, f'{category}_annotations')
dest_annotations_dir = os.path.join(subset_dir, 'annotations')
for img_file in os.listdir(src_images_dir):
shutil.copy(os.path.join(src_images_dir, img_file), dest_images_dir)
for anno_file in os.listdir(src_annotations_dir):
shutil.copy(os.path.join(src_annotations_dir, anno_file), dest_annotations_dir)
Convert the annotations to the YOLO format.
def convert_annotations(src_annotations_dir, dest_annotations_dir):
for anno_file in os.listdir(src_annotations_dir):
if anno_file.endswith('.xml'):
xml_path = os.path.join(src_annotations_dir, anno_file)
yolo_path = os.path.join(dest_annotations_dir, anno_file.replace('.xml', '.txt'))
tree = ET.parse(xml_path)
root = tree.getroot()
img_width = int(root.find('size/width').text)
img_height = int(root.find('size/height').text)
with open(yolo_path, 'w') as f:
for obj in root.findall('object'):
class_name = obj.find('name').text
bbox = obj.find('bndbox')
xmin = int(bbox.find('xmin').text)
ymin = int(bbox.find('ymin').text)
xmax = int(bbox.find('xmax').text)
ymax = int(bbox.find('ymax').text)
x_center = (xmin + xmax) / 2.0 / img_width
y_center = (ymin + ymax) / 2.0 / img_height
width = (xmax - xmin) / img_width
height = (ymax - ymin) / img_height
class_id = selected_categories.index(class_name)
f.write(f"{class_id} {x_center} {y_center} {width} {height}\n")
convert_annotations(os.path.join(dataset_dir, 'annotations'), os.path.join(subset_dir, 'annotations'))
Use the YOLOv8 model from the ultralytics library to fine-tune on the subset.
import ultralytics
from ultralytics import YOLO
# Load a pre-trained YOLOv8 model
model = YOLO('yolov8n.pt')
# Define the path to the dataset
data_yaml = 'data.yaml' # Create a data.yaml file with paths to your images and annotations
# Fine-tune the model
results = model.train(data=data_yaml, epochs=50, imgsz=640)
# Evaluate the model
metrics = model.val()
print(metrics)
Test the fine-tuned model on a photo.
# Load an image
image_path = 'path_to_your_photo.jpg' # Replace with your photo path
results = model(image_path)
# Show results
results.show()
Report the detection results and any observations.
# Example: Report results
print("Detection Results:")
for result in results:
print(result)
Make sure to create a data.yaml file that specifies the paths to your images and annotations, and adjust the category indices and paths as needed.