{"cells":[{"cell_type":"markdown","metadata":{"id":"BMiGd7RXtecz"},"source":["Code snippet to load your image datasets and their corresponding labels from JSON files, using a utility structure that organizes the images and labels for efficient processing."]},{"cell_type":"code","execution_count":139,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Requirement already satisfied: pillow in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (11.0.0)Note: you may need to restart the kernel to use updated packages.\n"]},{"name":"stderr","output_type":"stream","text":["\n","[notice] A new release of pip available: 22.2.1 -> 24.3.1\n","[notice] To update, run: python.exe -m pip install --upgrade pip\n"]},{"name":"stdout","output_type":"stream","text":["\n"]}],"source":["pip install pillow\n"]},{"cell_type":"code","execution_count":140,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Requirement already satisfied: numpy in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (2.0.2)\n","Note: you may need to restart the kernel to use updated packages.\n"]},{"name":"stderr","output_type":"stream","text":["\n","[notice] A new release of pip available: 22.2.1 -> 24.3.1\n","[notice] To update, run: python.exe -m pip install --upgrade pip\n"]}],"source":["pip install numpy"]},{"cell_type":"code","execution_count":141,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"vwyh7IqVhhjo","jupyter":{"is_executing": true},"outputId":"b1b5c385-f079-4f73-e540-ea6649db26d9"},"outputs":[{"name":"stdout","output_type":"stream","text":["Processed train batch size: 10\n","Processed train batch size: 10\n","Processed train batch size: 10\n","Processed train batch size: 10\n",...."Processed test batch size: 9\n"]}],"source":["import os\n","import json\n","from pathlib import Path\n","from PIL import Image\n","import numpy as np\n","\n","# Define paths\n","test_path = Path('D:/work/plant-seedlings/test')\n","train_path = Path('D:/work/plant-seedlings/train')\n","val_path = Path('D:/work/plant-seedlings/val')\n","train_label_path = Path('D:/work/plant-seedlings/train_label.json')\n","val_label_path = Path('D:/work/plant-seedlings/val_label.json')\n","\n","# Load JSON labels\n","def load_labels(label_path):\n"," try:\n"," with open(label_path, 'r') as file:\n"," labels = json.load(file)\n"," return labels\n"," except Exception as e:\n"," print(f\"Error loading labels: {e}\")\n"," return []\n","\n","# Load images and labels into a structured dictionary in batches\n","def load_images_and_labels(image_dir, labels, batch_size=10):\n"," data = []\n"," for i, label in enumerate(labels):\n"," if i % batch_size == 0 and data:\n"," yield data # Yield the current batch\n"," data = [] # Reset for next batch\n","\n"," img_path = image_dir / label.get(\"文件名\", \"\")\n"," if img_path.is_file():\n"," try:\n"," img = Image.open(img_path).convert('RGB')\n"," img_array = np.array(img)\n"," data.append({\n"," \"image\": img_array,\n"," \"bbox\": label.get(\"边界框\", (0, 0, 0, 0)), # Default bbox if missing\n"," \"label\": label.get(\"标签\", \"Unknown\") # Default label if missing\n"," })\n"," except Exception as e:\n"," print(f\"Error loading image {img_path}: {e}\")\n"," if data:\n"," yield data # Yield the last batch if not empty\n","\n","# Example usage with batch processing\n","train_labels = load_labels(train_label_path)\n","val_labels = load_labels(val_label_path)\n","\n","# Load train data in batches\n","for train_batch in load_images_and_labels(train_path, train_labels, batch_size=10):\n"," print(\"Processed train batch size:\", len(train_batch))\n","\n","# Load validation data in batches\n","for val_batch in load_images_and_labels(val_path, val_labels, batch_size=10):\n"," print(\"Processed validation batch size:\", len(val_batch))\n","\n","# Test data does not have labels, so load only images in batches\n","def load_test_images(image_dir, batch_size=10):\n"," data = []\n"," for i, img_path in enumerate(image_dir.iterdir()):\n"," if i % batch_size == 0 and data:\n"," yield data # Yield the current batch\n"," data = [] # Reset for next batch\n","\n"," if img_path.is_file():\n"," try:\n"," img = Image.open(img_path).convert('RGB')\n"," img_array = np.array(img)\n"," data.append(img_array)\n"," except Exception as e:\n"," print(f\"Error loading image {img_path}: {e}\")\n"," if data:\n"," yield data # Yield the last batch if not empty\n","\n","# Load test data in batches\n","for test_batch in load_test_images(test_path, batch_size=10):\n"," print(\"Processed test batch size:\", len(test_batch))\n"]},{"cell_type":"markdown","metadata":{"id":"8vb-mtJFr0F2"},"source":["**1. Target Detection Model for Crops and Weeds**\n","\n","This code snippet designs a YOLOv5-based object detection model to detect crops and weeds in images, training it on the provided dataset."]},{"cell_type":"code","execution_count":142,"metadata":{},"outputs":[{"name":"stdout","output_type":"stream","text":["Requirement already satisfied: torch in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (2.5.0)\n","Requirement already satisfied: sympy==1.13.1 in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from torch) (1.13.1)\n","Requirement already satisfied: jinja2 in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from torch) (3.1.4)\n","Requirement already satisfied: filelock in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from torch) (3.16.1)\n","Requirement already satisfied: networkx in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from torch) (3.4.2)\n","Requirement already satisfied: fsspec in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from torch) (2024.10.0)\n","Requirement already satisfied: typing-extensions>=4.8.0 in c:\\users\\dell\\appdata\\roaming\\python\\python310\\site-packages (from torch) (4.12.2)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in c:\\users\\dell\\appdata\\local\\programs\\python\\python310\\lib\\site-packages (from sympy==1.13.1->torch) (1.3.0)\n","Require