Ethan Cao commited on
Commit
913e0c1
Β·
1 Parent(s): c9d225b

24/6/20 18:27

Browse files
Files changed (3) hide show
  1. .gitignore +9 -0
  2. app.ipynb +149 -0
  3. labels/classes.txt +0 -4
.gitignore ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ train
3
+ valid
4
+ data.yaml
5
+
6
+ runs
7
+ wandb
8
+ YOLOv8
9
+ *.pt
app.ipynb ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [],
8
+ "source": [
9
+ "import os\n",
10
+ "import yaml\n",
11
+ "import shutil\n",
12
+ "from pathlib import Path\n",
13
+ "\n",
14
+ "import torch\n",
15
+ "from tqdm import tqdm\n",
16
+ "from ultralytics import YOLO\n",
17
+ "from sklearn import model_selection"
18
+ ]
19
+ },
20
+ {
21
+ "cell_type": "code",
22
+ "execution_count": 5,
23
+ "metadata": {},
24
+ "outputs": [],
25
+ "source": [
26
+ "image_files = sorted(os.listdir(\"images\"))\n",
27
+ "label_files = sorted(os.listdir(\"labels\"))\n",
28
+ "train_images, valid_images, train_labels, valid_labels = (\n",
29
+ " model_selection.train_test_split(image_files, label_files, test_size=0.1)\n",
30
+ ")"
31
+ ]
32
+ },
33
+ {
34
+ "cell_type": "code",
35
+ "execution_count": 11,
36
+ "metadata": {},
37
+ "outputs": [],
38
+ "source": [
39
+ "def create_folder(\n",
40
+ " image_names: list[str],\n",
41
+ " label_names: list[str],\n",
42
+ " img_src_dir: str,\n",
43
+ " label_src_dir: str,\n",
44
+ " img_dest_dir: str,\n",
45
+ " label_dest_dir: str,\n",
46
+ "):\n",
47
+ " if os.path.exists(img_dest_dir) == True:\n",
48
+ " shutil.rmtree(img_dest_dir)\n",
49
+ " os.makedirs(img_dest_dir)\n",
50
+ "\n",
51
+ " if os.path.exists(label_dest_dir) == True:\n",
52
+ " shutil.rmtree(label_dest_dir)\n",
53
+ " os.makedirs(label_dest_dir)\n",
54
+ "\n",
55
+ " for i in tqdm(range(len(image_names))):\n",
56
+ " img_path = Path(img_src_dir) / image_names[i]\n",
57
+ " img_dest = Path(img_dest_dir) / image_names[i]\n",
58
+ " if os.path.exists(img_dest) == False:\n",
59
+ " shutil.copy(img_path, img_dest)\n",
60
+ "\n",
61
+ " label_path = Path(label_src_dir) / label_names[i]\n",
62
+ " label_dest = Path(label_dest_dir) / label_names[i]\n",
63
+ " if os.path.exists(label_dest) == False:\n",
64
+ " shutil.copy(label_path, label_dest)\n",
65
+ "\n",
66
+ " assert img_path.stem == label_path.stem"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "code",
71
+ "execution_count": 12,
72
+ "metadata": {},
73
+ "outputs": [
74
+ {
75
+ "name": "stderr",
76
+ "output_type": "stream",
77
+ "text": [
78
+ " 0%| | 0/690 [00:00<?, ?it/s]"
79
+ ]
80
+ },
81
+ {
82
+ "name": "stderr",
83
+ "output_type": "stream",
84
+ "text": [
85
+ "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 690/690 [00:00<00:00, 8986.54it/s]\n",
86
+ "100%|β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ| 77/77 [00:00<00:00, 8794.77it/s]\n"
87
+ ]
88
+ }
89
+ ],
90
+ "source": [
91
+ "create_folder(\n",
92
+ " image_names=train_images,\n",
93
+ " label_names=train_labels,\n",
94
+ " img_src_dir=\"images\",\n",
95
+ " label_src_dir=\"labels\",\n",
96
+ " img_dest_dir=\"train/images\",\n",
97
+ " label_dest_dir=\"train/labels\",\n",
98
+ ")\n",
99
+ "\n",
100
+ "create_folder(\n",
101
+ " image_names=valid_images,\n",
102
+ " label_names=valid_labels,\n",
103
+ " img_src_dir=\"images\",\n",
104
+ " label_src_dir=\"labels\",\n",
105
+ " img_dest_dir=\"valid/images\",\n",
106
+ " label_dest_dir=\"valid/labels\",\n",
107
+ ")"
108
+ ]
109
+ },
110
+ {
111
+ "cell_type": "code",
112
+ "execution_count": 13,
113
+ "metadata": {},
114
+ "outputs": [],
115
+ "source": [
116
+ "dict_file = {\n",
117
+ " \"train\": f\"{Path(\"./train\").resolve()}\",\n",
118
+ " \"val\": f\"{Path(\"./valid\").resolve()}\",\n",
119
+ " \"nc\": 3,\n",
120
+ " \"names\": {0: \"circle\", 1: \"oval\", 2: \"teardrop\"},\n",
121
+ "}\n",
122
+ "\n",
123
+ "with open(\"data.yaml\", \"w\") as f:\n",
124
+ " yaml.dump(dict_file, f)"
125
+ ]
126
+ }
127
+ ],
128
+ "metadata": {
129
+ "kernelspec": {
130
+ "display_name": "base",
131
+ "language": "python",
132
+ "name": "python3"
133
+ },
134
+ "language_info": {
135
+ "codemirror_mode": {
136
+ "name": "ipython",
137
+ "version": 3
138
+ },
139
+ "file_extension": ".py",
140
+ "mimetype": "text/x-python",
141
+ "name": "python",
142
+ "nbconvert_exporter": "python",
143
+ "pygments_lexer": "ipython3",
144
+ "version": "3.12.3"
145
+ }
146
+ },
147
+ "nbformat": 4,
148
+ "nbformat_minor": 2
149
+ }
labels/classes.txt DELETED
@@ -1,4 +0,0 @@
1
- circle
2
- oval
3
- teardrop
4
- drop