Datasets:
File size: 2,991 Bytes
9082623 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "1bba141c-5345-4833-960e-59a5e65f08b8",
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import shutil\n",
"import random"
]
},
{
"cell_type": "markdown",
"id": "57ee26a6-ba9b-4228-847a-8fdb9dca42bd",
"metadata": {
"tags": []
},
"source": [
"https://harminder.dev/projects/ai-powered-property-surveillance/training/#create-a-data-configuration-file"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f4ee3ad6-2555-403f-a68b-c2a102649fe0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dataset successfully split into train, val, and test sets.\n"
]
}
],
"source": [
"# Set the seed for reproducibility\n",
"\n",
"random.seed(42)\n",
"\n",
"# Paths\n",
"\n",
"base_path = 'yolo'\n",
"images_path = os.path.join(base_path, 'images')\n",
"labels_path = os.path.join(base_path, 'labels')\n",
"\n",
"# Split Ratios\n",
"test_ratio = 0.20\n",
"\n",
"# Create directories for train, val, and test sets\n",
"\n",
"for set_type in ['train', 'test']:\n",
" for content_type in ['images', 'labels']:\n",
" os.makedirs(os.path.join(base_path, set_type, content_type), exist_ok=True)\n",
"\n",
"# Get all image filenames\n",
"\n",
"all_files = [f for f in os.listdir(images_path) if os.path.isfile(os.path.join(images_path, f))]\n",
"random.shuffle(all_files)\n",
"\n",
"# Calculate split indices\n",
"\n",
"total_files = len(all_files)\n",
"train_end = int(total_files*test_ratio)\n",
"\n",
"# Split files\n",
"\n",
"test_files = all_files[:train_end]\n",
"train_files = all_files[train_end:]\n",
"\n",
"# Function to copy files\n",
"\n",
"def copy_files(files, set_type):\n",
" for file in files: # Copy image\n",
" shutil.copy(os.path.join(images_path, file), os.path.join(base_path, set_type, 'images')) # Copy corresponding label\n",
" label_file = file.rsplit('.', 1)[0] + '.txt'\n",
" shutil.copy(os.path.join(labels_path, label_file), os.path.join(base_path, set_type, 'labels'))\n",
"\n",
"# Copy files to respective directories\n",
"\n",
"copy_files(train_files, 'train')\n",
"copy_files(test_files, 'test')\n",
"\n",
"print(\"Dataset successfully split into train, val, and test sets.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "torch",
"language": "python",
"name": "torch"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|