ass-cv-tl 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: ass-cv-tl
3
+ Version: 0.1.0
4
+ Summary: Check a YOLO-style image dataset: count train/valid images and labels, and list images without a .txt label and labels without an image
5
+ Author: Siva Narayana Surya Chandra
6
+ Keywords: yolo,dataset,labels,object-detection,validation,computer-vision
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+
10
+ # ass-cv-tl
11
+
12
+ Check a YOLO-style image dataset before you train. It looks at your `train` and `valid` folders, counts the images and
13
+ the `.txt` label files, and prints exactly which **images have no `.txt` label** and which **`.txt` labels have no image**.
14
+
15
+ ## Install
16
+
17
+ ```
18
+ pip install ass-cv-tl
19
+ ```
20
+
21
+ ## Run it
22
+
23
+ ```
24
+ ass-cv-tl --folder C:\data\my_dataset
25
+ ```
26
+
27
+ (`python -m ass_cv_tl --folder C:\data\my_dataset` does the same.)
28
+
29
+ Example output:
30
+
31
+ ```
32
+ Dataset : C:\data\my_dataset
33
+ Layout : [train] train/images + train/labels
34
+ Layout : [valid] valid/images + valid/labels
35
+
36
+ SPLIT IMAGES LABELS PAIRED NO LABEL NO IMAGE EMPTY BAD LINES BOXES
37
+ ----- ------ ------ ------ -------- -------- ----- --------- -----
38
+ train 7 6 5 2 1 0 0 6
39
+ valid 3 4 3 0 1 0 0 4
40
+ TOTAL 10 10 8 2 2 0 0 10
41
+
42
+ [train] images with NO matching .txt label file (2):
43
+ img_missing_label1.jpg
44
+ img_missing_label2.png
45
+
46
+ [train] .txt label files with NO matching image (1):
47
+ ghost_label.txt
48
+
49
+ [valid] .txt label files with NO matching image (1):
50
+ v_orphan.txt
51
+
52
+ Boxes per class id: 0: 7, 1: 3
53
+
54
+ RESULT: PROBLEMS FOUND - 4 unmatched file(s).
55
+ ```
56
+
57
+ ## Folder layouts it understands
58
+
59
+ ```
60
+ dataset/train/images + dataset/train/labels dataset/images/train + dataset/labels/train dataset/train/ (images and .txt together)
61
+ dataset/valid/images + dataset/valid/labels dataset/images/valid + dataset/labels/valid dataset/valid/
62
+ ```
63
+
64
+ `val` and `validation` count as `valid`; a `test` folder is checked too. Sub-folders are searched, and files are matched by
65
+ their path, so `x/1.jpg` is never confused with `y/1.jpg`. `classes.txt`, `train.txt`, `valid.txt` and similar are not counted as labels.
66
+
67
+ ## What it checks
68
+
69
+ - number of images and label files in each split, and how many are paired
70
+ - images with no `.txt` (printed by name) and `.txt` files with no image (printed by name)
71
+ - empty label files (reported, but fine for images with no objects)
72
+ - label lines that are not valid YOLO (`class x y w h`, or a polygon `class x1 y1 x2 y2 ...`, values between 0 and 1)
73
+ - two images sharing one name (for example `a.jpg` and `a.png`), where only one label can match
74
+ - boxes per class
75
+
76
+ ## Options
77
+
78
+ | option | meaning |
79
+ |---|---|
80
+ | `-f`, `--folder PATH` | the dataset folder (default: the current folder; `ass-cv-tl PATH` also works) |
81
+ | `--all` | list every problem file (default: the first 30 of each kind) |
82
+ | `--max N` | how many files to list of each kind |
83
+ | `--no-format` | skip checking the numbers inside the label files |
84
+ | `--json` | print the result as JSON |
85
+
86
+ The exit code is `0` when everything matches, `1` when problems were found, `2` when no dataset was found,
87
+ so you can use it in scripts.
88
+
89
+ ## From Python
90
+
91
+ ```python
92
+ from ass_cv_tl import check_dataset, print_report
93
+
94
+ result = check_dataset(r"C:\data\my_dataset")
95
+ print_report(result)
96
+ print(result.ok)
97
+ print(result.splits["train"].images_without_label) # image names that have no .txt
98
+ print(result.splits["valid"].labels_without_image) # .txt names that have no image
99
+ ```
@@ -0,0 +1,90 @@
1
+ # ass-cv-tl
2
+
3
+ Check a YOLO-style image dataset before you train. It looks at your `train` and `valid` folders, counts the images and
4
+ the `.txt` label files, and prints exactly which **images have no `.txt` label** and which **`.txt` labels have no image**.
5
+
6
+ ## Install
7
+
8
+ ```
9
+ pip install ass-cv-tl
10
+ ```
11
+
12
+ ## Run it
13
+
14
+ ```
15
+ ass-cv-tl --folder C:\data\my_dataset
16
+ ```
17
+
18
+ (`python -m ass_cv_tl --folder C:\data\my_dataset` does the same.)
19
+
20
+ Example output:
21
+
22
+ ```
23
+ Dataset : C:\data\my_dataset
24
+ Layout : [train] train/images + train/labels
25
+ Layout : [valid] valid/images + valid/labels
26
+
27
+ SPLIT IMAGES LABELS PAIRED NO LABEL NO IMAGE EMPTY BAD LINES BOXES
28
+ ----- ------ ------ ------ -------- -------- ----- --------- -----
29
+ train 7 6 5 2 1 0 0 6
30
+ valid 3 4 3 0 1 0 0 4
31
+ TOTAL 10 10 8 2 2 0 0 10
32
+
33
+ [train] images with NO matching .txt label file (2):
34
+ img_missing_label1.jpg
35
+ img_missing_label2.png
36
+
37
+ [train] .txt label files with NO matching image (1):
38
+ ghost_label.txt
39
+
40
+ [valid] .txt label files with NO matching image (1):
41
+ v_orphan.txt
42
+
43
+ Boxes per class id: 0: 7, 1: 3
44
+
45
+ RESULT: PROBLEMS FOUND - 4 unmatched file(s).
46
+ ```
47
+
48
+ ## Folder layouts it understands
49
+
50
+ ```
51
+ dataset/train/images + dataset/train/labels dataset/images/train + dataset/labels/train dataset/train/ (images and .txt together)
52
+ dataset/valid/images + dataset/valid/labels dataset/images/valid + dataset/labels/valid dataset/valid/
53
+ ```
54
+
55
+ `val` and `validation` count as `valid`; a `test` folder is checked too. Sub-folders are searched, and files are matched by
56
+ their path, so `x/1.jpg` is never confused with `y/1.jpg`. `classes.txt`, `train.txt`, `valid.txt` and similar are not counted as labels.
57
+
58
+ ## What it checks
59
+
60
+ - number of images and label files in each split, and how many are paired
61
+ - images with no `.txt` (printed by name) and `.txt` files with no image (printed by name)
62
+ - empty label files (reported, but fine for images with no objects)
63
+ - label lines that are not valid YOLO (`class x y w h`, or a polygon `class x1 y1 x2 y2 ...`, values between 0 and 1)
64
+ - two images sharing one name (for example `a.jpg` and `a.png`), where only one label can match
65
+ - boxes per class
66
+
67
+ ## Options
68
+
69
+ | option | meaning |
70
+ |---|---|
71
+ | `-f`, `--folder PATH` | the dataset folder (default: the current folder; `ass-cv-tl PATH` also works) |
72
+ | `--all` | list every problem file (default: the first 30 of each kind) |
73
+ | `--max N` | how many files to list of each kind |
74
+ | `--no-format` | skip checking the numbers inside the label files |
75
+ | `--json` | print the result as JSON |
76
+
77
+ The exit code is `0` when everything matches, `1` when problems were found, `2` when no dataset was found,
78
+ so you can use it in scripts.
79
+
80
+ ## From Python
81
+
82
+ ```python
83
+ from ass_cv_tl import check_dataset, print_report
84
+
85
+ result = check_dataset(r"C:\data\my_dataset")
86
+ print_report(result)
87
+ print(result.ok)
88
+ print(result.splits["train"].images_without_label) # image names that have no .txt
89
+ print(result.splits["valid"].labels_without_image) # .txt names that have no image
90
+ ```
@@ -0,0 +1,14 @@
1
+ """ass-cv-tl: check that every image has a .txt label (and every label has an image).
2
+
3
+ from ass_cv_tl import check_dataset, print_report
4
+ result = check_dataset("C:/data/my_dataset")
5
+ print_report(result)
6
+ print(result.ok, result.splits["train"].images_without_label)
7
+
8
+ Command line: ass-cv-tl C:/data/my_dataset
9
+ """
10
+ from .checker import (check_dataset, print_report, Result, SplitResult, DatasetNotFound,
11
+ IMAGE_EXTENSIONS)
12
+
13
+ __all__ = ["check_dataset", "print_report", "Result", "SplitResult", "DatasetNotFound", "IMAGE_EXTENSIONS"]
14
+ __version__ = "0.1.0"
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ raise SystemExit(main())
@@ -0,0 +1,294 @@
1
+ """Core checks for YOLO-style datasets (an image plus a same-named .txt label file)."""
2
+ import os
3
+ import sys
4
+ from collections import Counter
5
+ from dataclasses import dataclass, field
6
+ from typing import Dict, List, Optional, Tuple
7
+
8
+ IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff", ".webp", ".jfif", ".gif"}
9
+ SPLIT_NAMES = {"train": "train", "training": "train", "valid": "valid", "val": "valid", "validation": "valid",
10
+ "test": "test", "testing": "test"}
11
+ IMAGE_DIR_NAMES = {"images", "image", "imgs", "img", "jpegimages"}
12
+ LABEL_DIR_NAMES = {"labels", "label", "annotations", "txt"}
13
+ # text files that live next to labels but are not labels
14
+ NOT_LABELS = {"classes.txt", "class.txt", "names.txt", "obj.names.txt", "train.txt", "val.txt", "valid.txt",
15
+ "test.txt", "readme.txt", "license.txt", "requirements.txt", "notes.txt"}
16
+ MAX_BAD_FORMAT = 1000
17
+
18
+
19
+ class DatasetNotFound(Exception):
20
+ """No train / valid folders (or images and labels) were found in the folder."""
21
+
22
+
23
+ @dataclass
24
+ class SplitResult:
25
+ name: str
26
+ layout: str = ""
27
+ image_dirs: List[str] = field(default_factory=list)
28
+ label_dirs: List[str] = field(default_factory=list)
29
+ images: int = 0
30
+ labels: int = 0
31
+ paired: int = 0
32
+ images_without_label: List[str] = field(default_factory=list)
33
+ labels_without_image: List[str] = field(default_factory=list)
34
+ empty_labels: List[str] = field(default_factory=list)
35
+ duplicate_images: List[str] = field(default_factory=list)
36
+ bad_format: List[Tuple[str, int, str]] = field(default_factory=list) # (label file, line number, problem)
37
+ boxes: int = 0
38
+ class_counts: Dict[int, int] = field(default_factory=dict)
39
+
40
+ @property
41
+ def ok(self) -> bool:
42
+ return not (self.images_without_label or self.labels_without_image or self.bad_format or self.duplicate_images)
43
+
44
+
45
+ @dataclass
46
+ class Result:
47
+ root: str
48
+ splits: Dict[str, SplitResult]
49
+
50
+ @property
51
+ def ok(self) -> bool:
52
+ return all(s.ok for s in self.splits.values())
53
+
54
+ @property
55
+ def totals(self) -> SplitResult:
56
+ t = SplitResult("TOTAL")
57
+ counts = Counter()
58
+ for s in self.splits.values():
59
+ t.images += s.images
60
+ t.labels += s.labels
61
+ t.paired += s.paired
62
+ t.images_without_label += s.images_without_label
63
+ t.labels_without_image += s.labels_without_image
64
+ t.empty_labels += s.empty_labels
65
+ t.duplicate_images += s.duplicate_images
66
+ t.bad_format += s.bad_format
67
+ t.boxes += s.boxes
68
+ counts.update(s.class_counts)
69
+ t.class_counts = dict(counts)
70
+ return t
71
+
72
+ def to_dict(self) -> dict:
73
+ def one(s):
74
+ return {"layout": s.layout, "images": s.images, "labels": s.labels, "paired": s.paired,
75
+ "images_without_label": s.images_without_label, "labels_without_image": s.labels_without_image,
76
+ "empty_labels": s.empty_labels, "duplicate_images": s.duplicate_images,
77
+ "bad_format": [{"file": f, "line": n, "problem": m} for f, n, m in s.bad_format],
78
+ "boxes": s.boxes, "class_counts": s.class_counts, "ok": s.ok}
79
+ return {"root": self.root, "ok": self.ok, "splits": {k: one(v) for k, v in self.splits.items()}, "total": one(self.totals)}
80
+
81
+
82
+ # ---------- finding the folders ----------
83
+ def _subdirs(path):
84
+ try:
85
+ return [e for e in os.scandir(path) if e.is_dir()]
86
+ except OSError:
87
+ return []
88
+
89
+
90
+ def _named(path, names):
91
+ return [e.path for e in _subdirs(path) if e.name.lower() in names]
92
+
93
+
94
+ def _has_images(path):
95
+ for _r, _d, files in os.walk(path):
96
+ if any(os.path.splitext(f)[1].lower() in IMAGE_EXTENSIONS for f in files):
97
+ return True
98
+ return False
99
+
100
+
101
+ def _find_splits(root: str) -> Dict[str, Tuple[List[str], List[str], str]]:
102
+ """split -> (image dirs, label dirs, layout description)"""
103
+ found: Dict[str, Tuple[List[str], List[str], str]] = {}
104
+ top = _subdirs(root)
105
+ # layout 1: root/<split>/images + root/<split>/labels (or images and txt files together in root/<split>)
106
+ for e in top:
107
+ split = SPLIT_NAMES.get(e.name.lower())
108
+ if not split:
109
+ continue
110
+ imgs, lbls = _named(e.path, IMAGE_DIR_NAMES), _named(e.path, LABEL_DIR_NAMES)
111
+ if imgs:
112
+ found[split] = (imgs, lbls, "%s/images + %s/labels" % (e.name, e.name) if lbls else "%s/images (no labels folder)" % e.name)
113
+ elif lbls:
114
+ found[split] = ([e.path], lbls, "%s/<images here> + %s/labels" % (e.name, e.name))
115
+ elif _has_images(e.path):
116
+ found[split] = ([e.path], [e.path], "%s/ (images and .txt together)" % e.name)
117
+ if found:
118
+ return found
119
+ # layout 2: root/images/<split> + root/labels/<split>
120
+ img_roots, lbl_roots = _named(root, IMAGE_DIR_NAMES), _named(root, LABEL_DIR_NAMES)
121
+ for ir in img_roots:
122
+ for e in _subdirs(ir):
123
+ split = SPLIT_NAMES.get(e.name.lower())
124
+ if not split:
125
+ continue
126
+ lbls = [os.path.join(lr, e.name) for lr in lbl_roots if os.path.isdir(os.path.join(lr, e.name))]
127
+ found[split] = ([e.path], lbls, "images/%s + labels/%s" % (e.name, e.name))
128
+ if found:
129
+ return found
130
+ # single folder pair, no splits
131
+ if img_roots:
132
+ return {"all": (img_roots, lbl_roots, "images + labels (no train/valid folders)")}
133
+ if _has_images(root):
134
+ return {"all": ([root], [root], "images and .txt together (no train/valid folders)")}
135
+ raise DatasetNotFound(
136
+ "No images found in %r. Expected a folder like:\n"
137
+ " dataset/train/images + dataset/train/labels\n"
138
+ " dataset/valid/images + dataset/valid/labels\n"
139
+ "(or dataset/images/train + dataset/labels/train, or images and .txt together in dataset/train)" % root)
140
+
141
+
142
+ # ---------- indexing ----------
143
+ def _index(dirs: List[str], images: bool, skip_names=NOT_LABELS) -> Dict[str, List[str]]:
144
+ """key (path relative to its folder, without extension) -> list of file paths"""
145
+ out: Dict[str, List[str]] = {}
146
+ for base in dirs:
147
+ for r, _d, files in os.walk(base):
148
+ for f in files:
149
+ stem, ext = os.path.splitext(f)
150
+ ext = ext.lower()
151
+ if images and ext not in IMAGE_EXTENSIONS:
152
+ continue
153
+ if not images and (ext != ".txt" or f.lower() in skip_names):
154
+ continue
155
+ rel = os.path.relpath(os.path.join(r, stem), base).replace(os.sep, "/")
156
+ out.setdefault(rel, []).append(os.path.join(r, f))
157
+ return out
158
+
159
+
160
+ def _check_label_file(path: str, split: SplitResult, report_format: bool):
161
+ """Count boxes and validate every line: 'class x y w h' (boxes) or 'class x1 y1 x2 y2 ...' (polygons), all in 0..1."""
162
+ try:
163
+ with open(path, "r", encoding="utf-8", errors="replace") as fh:
164
+ lines = fh.read().splitlines()
165
+ except OSError as e:
166
+ if report_format:
167
+ split.bad_format.append((path, 0, "cannot read: %s" % e))
168
+ return
169
+ content = [(i, ln.strip()) for i, ln in enumerate(lines, 1) if ln.strip()]
170
+ if not content:
171
+ split.empty_labels.append(path)
172
+ return
173
+ for i, ln in content:
174
+ parts = ln.split()
175
+ problem = None
176
+ try:
177
+ cls = float(parts[0])
178
+ if cls != int(cls) or cls < 0:
179
+ problem = "class id %r is not a whole number >= 0" % parts[0]
180
+ nums = [float(x) for x in parts[1:]]
181
+ except ValueError:
182
+ problem = "line has text where numbers are expected"
183
+ nums = []
184
+ if problem is None:
185
+ if len(parts) == 5:
186
+ if any(v < 0 or v > 1 for v in nums):
187
+ problem = "box values must be between 0 and 1"
188
+ elif nums[2] <= 0 or nums[3] <= 0:
189
+ problem = "box width/height must be above 0"
190
+ elif len(parts) < 5:
191
+ problem = "only %d values, expected 5 (class x y w h)" % len(parts)
192
+ elif len(parts) % 2 == 0:
193
+ problem = "%d values: a polygon needs class + x y pairs" % len(parts)
194
+ elif any(v < 0 or v > 1 for v in nums):
195
+ problem = "polygon values must be between 0 and 1"
196
+ if problem:
197
+ if report_format and len(split.bad_format) < MAX_BAD_FORMAT:
198
+ split.bad_format.append((path, i, problem))
199
+ else:
200
+ split.boxes += 1
201
+ c = int(float(parts[0]))
202
+ split.class_counts[c] = split.class_counts.get(c, 0) + 1
203
+
204
+
205
+ def check_dataset(root: str = ".", check_format: bool = True) -> Result:
206
+ """Inspect a dataset folder. Returns a Result; use print_report(result) to show it."""
207
+ root = os.path.abspath(root)
208
+ if not os.path.isdir(root):
209
+ raise DatasetNotFound("Folder not found: %s" % root)
210
+ res = Result(root, {})
211
+ for name, (img_dirs, lbl_dirs, layout) in sorted(_find_splits(root).items(), key=lambda kv: ["train", "valid", "test", "all"].index(kv[0]) if kv[0] in ("train", "valid", "test", "all") else 9):
212
+ s = SplitResult(name, layout, img_dirs, lbl_dirs)
213
+ imgs, lbls = _index(img_dirs, True), _index(lbl_dirs, False)
214
+ s.images = sum(len(v) for v in imgs.values())
215
+ s.labels = sum(len(v) for v in lbls.values())
216
+ for key, paths in imgs.items():
217
+ if len(paths) > 1:
218
+ s.duplicate_images += ["%s (same name, different type: %s)" % (os.path.basename(p), ", ".join(os.path.splitext(q)[1] for q in paths)) for p in paths[:1]]
219
+ if key in lbls:
220
+ s.paired += len(paths)
221
+ else:
222
+ s.images_without_label += [_short(p, img_dirs) for p in paths]
223
+ for key, paths in lbls.items():
224
+ if key not in imgs:
225
+ s.labels_without_image += [_short(p, lbl_dirs) for p in paths]
226
+ for key, paths in lbls.items():
227
+ for p in paths:
228
+ _check_label_file(p, s, check_format)
229
+ s.images_without_label.sort()
230
+ s.labels_without_image.sort()
231
+ s.empty_labels = sorted(_short(p, lbl_dirs) for p in s.empty_labels)
232
+ s.bad_format = [(_short(p, lbl_dirs), n, m) for p, n, m in s.bad_format]
233
+ res.splits[name] = s
234
+ return res
235
+
236
+
237
+ def _short(path: str, bases: List[str]) -> str:
238
+ for b in bases:
239
+ try:
240
+ rel = os.path.relpath(path, b)
241
+ except ValueError:
242
+ continue
243
+ if not rel.startswith(".."):
244
+ return rel.replace(os.sep, "/")
245
+ return os.path.basename(path)
246
+
247
+
248
+ # ---------- printing ----------
249
+ def _list(title, items, show_all, max_list, out, indent=" "):
250
+ if not items:
251
+ return
252
+ print("\n%s (%d):" % (title, len(items)), file=out)
253
+ shown = items if show_all else items[:max_list]
254
+ for it in shown:
255
+ print(indent + it, file=out)
256
+ if len(items) > len(shown):
257
+ print("%s... and %d more (use --all to list everything)" % (indent, len(items) - len(shown)), file=out)
258
+
259
+
260
+ def print_report(result: Result, show_all: bool = False, max_list: int = 30, file=None) -> None:
261
+ out = file or sys.stdout
262
+ print("Dataset : %s" % result.root, file=out)
263
+ for s in result.splits.values():
264
+ print("Layout : [%s] %s" % (s.name, s.layout), file=out)
265
+ cols = ["SPLIT", "IMAGES", "LABELS", "PAIRED", "NO LABEL", "NO IMAGE", "EMPTY", "BAD LINES", "BOXES"]
266
+ rows = []
267
+ for s in list(result.splits.values()) + ([result.totals] if len(result.splits) > 1 else []):
268
+ rows.append([s.name, s.images, s.labels, s.paired, len(s.images_without_label), len(s.labels_without_image), len(s.empty_labels), len(s.bad_format), s.boxes])
269
+ w = [max(len(str(c)), *(len(str(r[i])) for r in rows)) for i, c in enumerate(cols)]
270
+ print("", file=out)
271
+ print(" ".join(str(c).ljust(w[i]) if i == 0 else str(c).rjust(w[i]) for i, c in enumerate(cols)), file=out)
272
+ print(" ".join("-" * x for x in w), file=out)
273
+ for r in rows:
274
+ print(" ".join(str(v).ljust(w[i]) if i == 0 else str(v).rjust(w[i]) for i, v in enumerate(r)), file=out)
275
+ for s in result.splits.values():
276
+ _list("[%s] images with NO matching .txt label file" % s.name, s.images_without_label, show_all, max_list, out)
277
+ _list("[%s] .txt label files with NO matching image" % s.name, s.labels_without_image, show_all, max_list, out)
278
+ _list("[%s] images sharing one name (only one label can match)" % s.name, s.duplicate_images, show_all, max_list, out)
279
+ if s.bad_format:
280
+ _list("[%s] label lines with a format problem" % s.name, ["%s line %d: %s" % (f, n, m) for f, n, m in s.bad_format], show_all, max_list, out)
281
+ if s.empty_labels:
282
+ print("\n[%s] %d empty label file(s) (fine for background images with no objects)" % (s.name, len(s.empty_labels)), file=out)
283
+ if show_all:
284
+ for e in s.empty_labels:
285
+ print(" " + e, file=out)
286
+ t = result.totals
287
+ if t.class_counts:
288
+ print("\nBoxes per class id: " + ", ".join("%d: %d" % (k, v) for k, v in sorted(t.class_counts.items())), file=out)
289
+ print("", file=out)
290
+ if result.ok:
291
+ print("RESULT: OK - every image has a label and every label has an image.", file=out)
292
+ else:
293
+ n = len(t.images_without_label) + len(t.labels_without_image)
294
+ print("RESULT: PROBLEMS FOUND - %d unmatched file(s)%s." % (n, (", %d bad label line(s)" % len(t.bad_format)) if t.bad_format else ""), file=out)
@@ -0,0 +1,33 @@
1
+ import argparse
2
+ import json
3
+ import sys
4
+
5
+ from .checker import DatasetNotFound, check_dataset, print_report
6
+
7
+
8
+ def main(argv=None) -> int:
9
+ ap = argparse.ArgumentParser(
10
+ prog="ass-cv-tl",
11
+ description="Check a YOLO-style dataset: counts images and labels in train / valid (and test), "
12
+ "and lists images that have no .txt label and .txt labels that have no image.")
13
+ ap.add_argument("path", nargs="?", default=None, help="dataset folder (same as --folder)")
14
+ ap.add_argument("-f", "--folder", metavar="PATH", help="dataset folder that contains train/ and valid/ (default: current folder)")
15
+ ap.add_argument("--all", action="store_true", help="list every problem file (default: first 30 per list)")
16
+ ap.add_argument("--max", type=int, default=30, metavar="N", help="how many files to list per problem (default 30)")
17
+ ap.add_argument("--no-format", action="store_true", help="skip checking the numbers inside each label file")
18
+ ap.add_argument("--json", action="store_true", help="print the result as JSON")
19
+ a = ap.parse_args(argv)
20
+ try:
21
+ res = check_dataset(a.folder or a.path or ".", check_format=not a.no_format)
22
+ except DatasetNotFound as e:
23
+ print("ERROR: %s" % e, file=sys.stderr)
24
+ return 2
25
+ if a.json:
26
+ print(json.dumps(res.to_dict(), indent=2))
27
+ else:
28
+ print_report(res, show_all=a.all, max_list=a.max)
29
+ return 0 if res.ok else 1
30
+
31
+
32
+ if __name__ == "__main__":
33
+ raise SystemExit(main())
@@ -0,0 +1,99 @@
1
+ Metadata-Version: 2.4
2
+ Name: ass-cv-tl
3
+ Version: 0.1.0
4
+ Summary: Check a YOLO-style image dataset: count train/valid images and labels, and list images without a .txt label and labels without an image
5
+ Author: Siva Narayana Surya Chandra
6
+ Keywords: yolo,dataset,labels,object-detection,validation,computer-vision
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+
10
+ # ass-cv-tl
11
+
12
+ Check a YOLO-style image dataset before you train. It looks at your `train` and `valid` folders, counts the images and
13
+ the `.txt` label files, and prints exactly which **images have no `.txt` label** and which **`.txt` labels have no image**.
14
+
15
+ ## Install
16
+
17
+ ```
18
+ pip install ass-cv-tl
19
+ ```
20
+
21
+ ## Run it
22
+
23
+ ```
24
+ ass-cv-tl --folder C:\data\my_dataset
25
+ ```
26
+
27
+ (`python -m ass_cv_tl --folder C:\data\my_dataset` does the same.)
28
+
29
+ Example output:
30
+
31
+ ```
32
+ Dataset : C:\data\my_dataset
33
+ Layout : [train] train/images + train/labels
34
+ Layout : [valid] valid/images + valid/labels
35
+
36
+ SPLIT IMAGES LABELS PAIRED NO LABEL NO IMAGE EMPTY BAD LINES BOXES
37
+ ----- ------ ------ ------ -------- -------- ----- --------- -----
38
+ train 7 6 5 2 1 0 0 6
39
+ valid 3 4 3 0 1 0 0 4
40
+ TOTAL 10 10 8 2 2 0 0 10
41
+
42
+ [train] images with NO matching .txt label file (2):
43
+ img_missing_label1.jpg
44
+ img_missing_label2.png
45
+
46
+ [train] .txt label files with NO matching image (1):
47
+ ghost_label.txt
48
+
49
+ [valid] .txt label files with NO matching image (1):
50
+ v_orphan.txt
51
+
52
+ Boxes per class id: 0: 7, 1: 3
53
+
54
+ RESULT: PROBLEMS FOUND - 4 unmatched file(s).
55
+ ```
56
+
57
+ ## Folder layouts it understands
58
+
59
+ ```
60
+ dataset/train/images + dataset/train/labels dataset/images/train + dataset/labels/train dataset/train/ (images and .txt together)
61
+ dataset/valid/images + dataset/valid/labels dataset/images/valid + dataset/labels/valid dataset/valid/
62
+ ```
63
+
64
+ `val` and `validation` count as `valid`; a `test` folder is checked too. Sub-folders are searched, and files are matched by
65
+ their path, so `x/1.jpg` is never confused with `y/1.jpg`. `classes.txt`, `train.txt`, `valid.txt` and similar are not counted as labels.
66
+
67
+ ## What it checks
68
+
69
+ - number of images and label files in each split, and how many are paired
70
+ - images with no `.txt` (printed by name) and `.txt` files with no image (printed by name)
71
+ - empty label files (reported, but fine for images with no objects)
72
+ - label lines that are not valid YOLO (`class x y w h`, or a polygon `class x1 y1 x2 y2 ...`, values between 0 and 1)
73
+ - two images sharing one name (for example `a.jpg` and `a.png`), where only one label can match
74
+ - boxes per class
75
+
76
+ ## Options
77
+
78
+ | option | meaning |
79
+ |---|---|
80
+ | `-f`, `--folder PATH` | the dataset folder (default: the current folder; `ass-cv-tl PATH` also works) |
81
+ | `--all` | list every problem file (default: the first 30 of each kind) |
82
+ | `--max N` | how many files to list of each kind |
83
+ | `--no-format` | skip checking the numbers inside the label files |
84
+ | `--json` | print the result as JSON |
85
+
86
+ The exit code is `0` when everything matches, `1` when problems were found, `2` when no dataset was found,
87
+ so you can use it in scripts.
88
+
89
+ ## From Python
90
+
91
+ ```python
92
+ from ass_cv_tl import check_dataset, print_report
93
+
94
+ result = check_dataset(r"C:\data\my_dataset")
95
+ print_report(result)
96
+ print(result.ok)
97
+ print(result.splits["train"].images_without_label) # image names that have no .txt
98
+ print(result.splits["valid"].labels_without_image) # .txt names that have no image
99
+ ```
@@ -0,0 +1,12 @@
1
+ README.md
2
+ pyproject.toml
3
+ ass_cv_tl/__init__.py
4
+ ass_cv_tl/__main__.py
5
+ ass_cv_tl/checker.py
6
+ ass_cv_tl/cli.py
7
+ ass_cv_tl.egg-info/PKG-INFO
8
+ ass_cv_tl.egg-info/SOURCES.txt
9
+ ass_cv_tl.egg-info/dependency_links.txt
10
+ ass_cv_tl.egg-info/entry_points.txt
11
+ ass_cv_tl.egg-info/top_level.txt
12
+ tests/test_checker.py
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ ass-cv-tl = ass_cv_tl.cli:main
@@ -0,0 +1 @@
1
+ ass_cv_tl
@@ -0,0 +1,19 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "ass-cv-tl"
7
+ version = "0.1.0"
8
+ authors = [{ name = "Siva Narayana Surya Chandra" }]
9
+ description = "Check a YOLO-style image dataset: count train/valid images and labels, and list images without a .txt label and labels without an image"
10
+ readme = "README.md"
11
+ requires-python = ">=3.8"
12
+ dependencies = []
13
+ keywords = ["yolo", "dataset", "labels", "object-detection", "validation", "computer-vision"]
14
+
15
+ [project.scripts]
16
+ ass-cv-tl = "ass_cv_tl.cli:main"
17
+
18
+ [tool.setuptools]
19
+ packages = ["ass_cv_tl"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,98 @@
1
+ import json, os, subprocess, sys, tempfile, unittest, io
2
+
3
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
4
+ from ass_cv_tl import check_dataset, print_report, DatasetNotFound # noqa: E402
5
+
6
+
7
+ def touch(path, text=""):
8
+ os.makedirs(os.path.dirname(path), exist_ok=True)
9
+ with open(path, "w") as f:
10
+ f.write(text)
11
+
12
+
13
+ BOX = "0 0.5 0.5 0.2 0.2\n"
14
+
15
+
16
+ class T(unittest.TestCase):
17
+ def make(self):
18
+ return tempfile.mkdtemp()
19
+
20
+ def test_standard_layout_finds_every_problem(self):
21
+ r = self.make()
22
+ for i in range(5):
23
+ touch(f"{r}/train/images/img{i}.jpg"); touch(f"{r}/train/labels/img{i}.txt", BOX)
24
+ touch(f"{r}/train/images/no_label.png") # image without txt
25
+ touch(f"{r}/train/labels/no_image.txt", BOX) # txt without image
26
+ touch(f"{r}/train/labels/empty.txt", ""); touch(f"{r}/train/images/empty.jpg") # background image: fine
27
+ for i in range(3):
28
+ touch(f"{r}/valid/images/v{i}.jpeg"); touch(f"{r}/valid/labels/v{i}.txt", BOX)
29
+ touch(f"{r}/train/labels/classes.txt", "cat\ndog\n") # not a label
30
+ res = check_dataset(r)
31
+ t, v = res.splits["train"], res.splits["valid"]
32
+ self.assertEqual((t.images, t.labels, t.paired), (7, 7, 6)) # classes.txt is not counted
33
+ self.assertEqual(t.images_without_label, ["no_label.png"])
34
+ self.assertEqual(t.labels_without_image, ["no_image.txt"])
35
+ self.assertEqual(t.empty_labels, ["empty.txt"])
36
+ self.assertEqual((v.images, v.labels, v.paired), (3, 3, 3))
37
+ self.assertTrue(v.ok); self.assertFalse(t.ok); self.assertFalse(res.ok)
38
+ buf = io.StringIO(); print_report(res, file=buf); s = buf.getvalue()
39
+ self.assertIn("no_label.png", s); self.assertIn("no_image.txt", s); self.assertIn("PROBLEMS FOUND", s)
40
+
41
+ def test_ok_dataset(self):
42
+ r = self.make()
43
+ for sp in ("train", "val"):
44
+ for i in range(2):
45
+ touch(f"{r}/{sp}/images/a{i}.jpg"); touch(f"{r}/{sp}/labels/a{i}.txt", BOX)
46
+ res = check_dataset(r)
47
+ self.assertTrue(res.ok); self.assertEqual(set(res.splits), {"train", "valid"}) # val == valid
48
+
49
+ def test_images_labels_then_split_layout(self):
50
+ r = self.make()
51
+ touch(f"{r}/images/train/a.jpg"); touch(f"{r}/labels/train/a.txt", BOX); touch(f"{r}/images/valid/b.jpg")
52
+ res = check_dataset(r)
53
+ self.assertEqual(res.splits["train"].paired, 1); self.assertEqual(res.splits["valid"].images_without_label, ["b.jpg"])
54
+
55
+ def test_flat_layout_images_and_txt_together(self):
56
+ r = self.make()
57
+ touch(f"{r}/train/a.jpg"); touch(f"{r}/train/a.txt", BOX); touch(f"{r}/train/b.jpg")
58
+ res = check_dataset(r)
59
+ self.assertEqual(res.splits["train"].images_without_label, ["b.jpg"]); self.assertEqual(res.splits["train"].labels, 1)
60
+
61
+ def test_subfolders_do_not_collide(self):
62
+ r = self.make()
63
+ touch(f"{r}/train/images/x/1.jpg"); touch(f"{r}/train/labels/x/1.txt", BOX)
64
+ touch(f"{r}/train/images/y/1.jpg") # same file name, other folder, no label
65
+ res = check_dataset(r)
66
+ self.assertEqual(res.splits["train"].images_without_label, ["y/1.jpg"])
67
+
68
+ def test_missing_labels_folder_reports_all_images(self):
69
+ r = self.make()
70
+ touch(f"{r}/train/images/a.jpg"); touch(f"{r}/train/images/b.jpg")
71
+ self.assertEqual(check_dataset(r).splits["train"].images_without_label, ["a.jpg", "b.jpg"])
72
+
73
+ def test_label_format_problems(self):
74
+ r = self.make()
75
+ touch(f"{r}/train/images/a.jpg"); touch(f"{r}/train/labels/a.txt", "0 0.5 0.5 0.2\n1 1.5 0.5 0.2 0.2\nx 0.1 0.1 0.1 0.1\n2 0.5 0.5 0.1 0.1\n")
76
+ s = check_dataset(r).splits["train"]
77
+ self.assertEqual([n for _f, n, _m in s.bad_format], [1, 2, 3]); self.assertEqual(s.boxes, 1)
78
+ self.assertEqual(check_dataset(r, check_format=False).splits["train"].bad_format, [])
79
+
80
+ def test_polygon_labels_are_accepted(self):
81
+ r = self.make()
82
+ touch(f"{r}/train/images/a.jpg"); touch(f"{r}/train/labels/a.txt", "0 0.1 0.1 0.9 0.1 0.5 0.9\n")
83
+ self.assertTrue(check_dataset(r).ok)
84
+
85
+ def test_no_dataset(self):
86
+ with self.assertRaises(DatasetNotFound):
87
+ check_dataset(self.make())
88
+
89
+ def test_cli_exit_codes_and_json(self):
90
+ r = self.make(); touch(f"{r}/train/images/a.jpg")
91
+ p = subprocess.run([sys.executable, "-m", "ass_cv_tl", "--folder", r, "--json"], capture_output=True, text=True, cwd=os.path.join(os.path.dirname(__file__), ".."))
92
+ self.assertEqual(p.returncode, 1); self.assertEqual(json.loads(p.stdout)["splits"]["train"]["images_without_label"], ["a.jpg"])
93
+ p = subprocess.run([sys.executable, "-m", "ass_cv_tl", self.make()], capture_output=True, text=True, cwd=os.path.join(os.path.dirname(__file__), ".."))
94
+ self.assertEqual(p.returncode, 2)
95
+
96
+
97
+ if __name__ == "__main__":
98
+ unittest.main(verbosity=2)