File size: 7,266 Bytes
b42f393
607c616
b42f393
 
 
 
 
 
607c616
b42f393
 
 
 
607c616
 
 
 
 
 
b42f393
607c616
b42f393
607c616
b42f393
 
 
 
607c616
 
b42f393
607c616
 
b42f393
607c616
b42f393
607c616
b42f393
607c616
 
 
 
 
 
 
b42f393
 
607c616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42f393
 
 
 
 
 
607c616
b42f393
 
 
607c616
b42f393
 
607c616
b42f393
607c616
 
 
 
 
 
 
 
 
546990d
607c616
546990d
607c616
 
 
 
b42f393
607c616
 
 
546990d
607c616
546990d
607c616
 
 
b898d37
607c616
 
 
 
 
 
 
b898d37
607c616
 
 
 
 
 
 
 
 
 
546990d
607c616
546990d
607c616
 
 
 
 
 
546990d
607c616
546990d
607c616
 
 
 
 
 
546990d
607c616
546990d
607c616
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b42f393
f4451e1
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
---
pretty_name: KSDD2 (Kolektor Surface-Defect Dataset 2)  manual-download loader
license: cc-by-nc-sa-4.0
tags:
  - computer-vision
  - image-classification
  - anomaly-detection
  - industrial-inspection
  - image-segmentation
task_categories:
  - image-classification
  - image-segmentation
configs:
  - config_name: image_only
    description: "Flat train/test folders with images only (no labels)."
  - config_name: classification_from_list
    description: "Flat train/test + defect_list.txt (or .csv) ⇒ labels."
  - config_name: classification_from_pyb
    description: "Flat train/test + split_weakly_*.pyb ⇒ labels (auto union)."
  - config_name: classification
    description: "ok/ and defect/ subfolders ⇒ labels."
  - config_name: with_mask
    description: "classification + optional mask_path matched by name from masks folders."
---

# KSDD2 (manual-download loader)

This repo provides a **manual-download loading script** for **Kolektor Surface-Defect Dataset 2 (KSDD2)**.
No images are hosted here. Users must download KSDD2 from the **official page** and load locally via `load_dataset(..., data_dir=...)`.

- Official page (license & download): https://www.vicos.si/resources/kolektorsdd2/
- Dataset license: **CC BY-NC-SA 4.0** (non‑commercial; attribution; share‑alike). For commercial usage, follow the authors’ instructions on the official page.

> **What this loader does:** read your local KSDD2 folder, return a standard **DatasetDict** with images and metadata, without moving or copying files.

---

## Recommended environment (important)

This dataset card uses a **custom loading script** (`trust_remote_code=True`). Newer versions of `datasets` (v4.x) **do not execute** loading scripts from the Hub.  
To load this dataset from the Hub script, please install the **tested versions**:

```bash
pip install "datasets==3.2.0" "huggingface_hub<0.27"
```

If you must use newer releases, consider the built‑in `imagefolder` loader (labels/masks & pyb auto‑labeling will not be available), or run a local helper to produce Arrow/Parquet then `load_from_disk`. For best UX, we recommend the tested versions above.

> Windows note: you may see a harmless warning about symlinks from `huggingface_hub`. It can be ignored, or disable via `HF_HUB_DISABLE_SYMLINK_WARNING=1`, or enable Windows Developer Mode / run as admin.

---

## Features by config

| Config                      | Features                                  | When to use                                                                 |
|----------------------------|-------------------------------------------|------------------------------------------------------------------------------|
| `image_only`               | `{"image", "path"}`                       | You have **flat** `train/` and `test/` (images only), no labels yet.        |
| `classification_from_list` | `{"image", "label", "path"}`              | Flat folders + you provide `defect_list.txt` (one filename per line).       |
| `classification_from_pyb`  | `{"image", "label", "path"}`              | Flat folders + you have `split_weakly_*.pyb` files (labels **auto‑derived**). |
| `classification`           | `{"image", "label", "path"}`              | Your data is already split into `ok/` and `defect/` subfolders.             |
| `with_mask`                | `{"image", "label", "path", "mask_path"}` | Same as `classification`, and you also have a masks folder with same names. |

`label` is a `ClassLabel` with `["good", "defect"]`.  
`mask_path` is a string (empty if not found).

---

## Folder layouts (examples)

### A) Flat layout (no subfolders under split)

```
KSDD2/
  train/*.png|jpg
  test/*.png|jpg
  split_weakly_0.pyb
  split_weakly_16.pyb
  ...
```

### B) Labeled subfolders

```
KSDD2/
  train/
    ok/*.png|jpg
    defect/*.png|jpg
    [masks | masks_defect | ground_truth | gt | label | labels]/*.png   # optional, for with_mask
  test/
    ok/*.png|jpg
    defect/*.png|jpg
    [masks | masks_defect | ground_truth | gt | label | labels]/*.png
```

---

## Quickstart

> All snippets below assume the **tested versions** mentioned above and `trust_remote_code=True`.

### 1) Flat, images only

```python
from datasets import load_dataset

ds = load_dataset("OliverOnHF/ksdd2",
                  name="image_only",
                  data_dir=r"<Your Local KSDD2 dataset path>",
                  trust_remote_code=True)
print(ds)
print(ds["train"][0])  # {"image": ..., "path": "..."}
```

### 2) Flat + auto labels from pyb

```python
ds = load_dataset("OliverOnHF/ksdd2",
                  name="classification_from_pyb",
                  data_dir=r"<Your Local KSDD2 dataset path>",
                  trust_remote_code=True)
print(ds["train"].features)  # ClassLabel(names=['good','defect'])
print(ds["train"][0])        # {"image": ..., "label": 0/1, "path": "..."}
```

**How it works:** the loader scans all `split_weakly_*.pyb` next to your `train/` and `test/`, extracts filename strings and/or numeric IDs, matches them to your image basenames (e.g. `10023``10023.png`), and takes the **union** across all pyb files: if a name appears in any pyb, it is labeled as `defect`.

### 3) Flat + your defect list

Place a `defect_list.txt` (or `.csv`) **inside each split**:

```
KSDD2/
  train/
    defect_list.txt    # one filename per line; comments (#) and blanks ignored
  test/
    defect_list.txt
```

Then:

```python
ds = load_dataset("OliverOnHF/ksdd2",
                  name="classification_from_list",
                  data_dir=r"<Your Local KSDD2 dataset path>",
                  trust_remote_code=True)
```

### 4) Labeled subfolders

```python
ds = load_dataset("OliverOnHF/ksdd2",
                  name="classification",
                  data_dir=r"<Your Local KSDD2 dataset path>",
                  trust_remote_code=True)
```

### 5) Labeled subfolders + masks

```python
ds = load_dataset("OliverOnHF/ksdd2",
                  name="with_mask",
                  data_dir=r"<Your Local KSDD2 dataset path>",
                  trust_remote_code=True)
```

The loader looks up masks by **same filename** under any of:
`masks`, `masks_defect`, `ground_truth`, `gt`, `label`, `labels`. If not found, `mask_path` is an empty string.

---

## Troubleshooting

- **“Dataset scripts are no longer supported”** or cannot `trust_remote_code`: use the tested versions shown above (`datasets==3.2.0`, `huggingface_hub<0.27`).  
- **“Cannot find class folders … expect ok and defect”**: you selected `classification`/`with_mask` but your layout is flat. Use `classification_from_pyb` / `classification_from_list`, or reorganize into `ok/` and `defect/`.  
- **No labels produced in `classification_from_pyb`**: make sure `split_weakly_*.pyb` sits next to `train/` and `test/`, and that image basenames contain numeric IDs or exact names referenced by the pyb files.  
- **Windows symlink warning from `huggingface_hub`**: harmless; can be ignored.

---

## License

- **Dataset (KSDD2)**: CC BY-NC-SA 4.0 — see the official KSDD2 page. This repo **does not redistribute** any images.  
- **Loader code in this repo**: MIT (see `LICENSE`).

## Citation

Please cite KSDD2 as requested by the authors on the official page.