Etiqueta STL10 Segmentation

#1
by semihyagli - opened
.github/funding.yml DELETED
@@ -1,10 +0,0 @@
1
- # Support Etiqueta by using the links below
2
- # Etiqeuta and the public datasets shared in this repo are developed and maintained by Dr. Semih Yagli.
3
- # GitHub Sponsors: https://github.com/sponsors/semihyagli
4
- # Buy Me a Coffee: https://buymeacoffee.com/semihyagli
5
-
6
- github: semihyagli
7
- buy_me_a_coffee: semihyagli
8
-
9
- # Your support helps me in continuing to develop many other labeled opensource datasets.
10
- # Thank you very much!
 
 
 
 
 
 
 
 
 
 
 
.gitignore DELETED
@@ -1,217 +0,0 @@
1
- # Compiled source #
2
- ###################
3
- *.com
4
- *.class
5
- *.dll
6
- *.exe
7
- *.o
8
- *.so
9
-
10
- # Packages #
11
- ############
12
- # it's better to unpack these files and commit the raw source
13
- # git has its own built in compression methods
14
- *.7z
15
- *.dmg
16
- *.gz
17
- *.iso
18
- *.jar
19
- *.rar
20
- *.tar
21
- *.zip
22
-
23
- # Logs and databases #
24
- ######################
25
- *.log
26
- *.sql
27
- *.sqlite
28
-
29
- # OS generated files #
30
- ######################
31
- .DS_Store
32
- .DS_Store?
33
- ._*
34
- .Spotlight-V100
35
- .Trashes
36
- ehthumbs.db
37
- Thumbs.db
38
- .vscode
39
-
40
-
41
- # Byte-compiled / optimized / DLL files
42
- __pycache__/
43
- *.py[cod]
44
- *$py.class
45
-
46
- # C extensions
47
- *.so
48
-
49
- # Distribution / packaging
50
- .Python
51
- build/
52
- develop-eggs/
53
- dist/
54
- downloads/
55
- eggs/
56
- .eggs/
57
- lib/
58
- lib64/
59
- parts/
60
- sdist/
61
- var/
62
- wheels/
63
- share/python-wheels/
64
- *.egg-info/
65
- .installed.cfg
66
- *.egg
67
- MANIFEST
68
-
69
- # PyInstaller
70
- # Usually these files are written by a python script from a template
71
- # before PyInstaller builds the exe, so as to inject date/other infos into it.
72
- *.manifest
73
- *.spec
74
-
75
- # Installer logs
76
- pip-log.txt
77
- pip-delete-this-directory.txt
78
-
79
- # Unit test / coverage reports
80
- htmlcov/
81
- .tox/
82
- .nox/
83
- .coverage
84
- .coverage.*
85
- .cache
86
- nosetests.xml
87
- coverage.xml
88
- *.cover
89
- *.py,cover
90
- .hypothesis/
91
- .pytest_cache/
92
- cover/
93
-
94
- # Translations
95
- *.mo
96
- *.pot
97
-
98
- # Django stuff:
99
- *.log
100
- local_settings.py
101
- db.sqlite3
102
- db.sqlite3-journal
103
-
104
- # Flask stuff:
105
- instance/
106
- .webassets-cache
107
-
108
- # Scrapy stuff:
109
- .scrapy
110
-
111
- # Sphinx documentation
112
- docs/_build/
113
-
114
- # PyBuilder
115
- .pybuilder/
116
- target/
117
-
118
- # Jupyter Notebook
119
- .ipynb_checkpoints
120
-
121
- # IPython
122
- profile_default/
123
- ipython_config.py
124
-
125
- # gcloud
126
- .gcloudignore
127
-
128
- # git
129
-
130
- # pyenv
131
- # For a library or package, you might want to ignore these files since the code is
132
- # intended to run in multiple environments; otherwise, check them in:
133
- # .python-version
134
-
135
- # pipenv
136
- # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
137
- # However, in case of collaboration, if having platform-specific dependencies or dependencies
138
- # having no cross-platform support, pipenv may install dependencies that don't work, or not
139
- # install all needed dependencies.
140
- #Pipfile.lock
141
-
142
- # poetry
143
- # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
144
- # This is especially recommended for binary packages to ensure reproducibility, and is more
145
- # commonly ignored for libraries.
146
- # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
147
- #poetry.lock
148
-
149
- # pdm
150
- # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
151
- #pdm.lock
152
- # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
153
- # in version control.
154
- # https://pdm.fming.dev/#use-with-ide
155
- .pdm.toml
156
-
157
- # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
158
- __pypackages__/
159
-
160
- # Celery stuff
161
- celerybeat-schedule
162
- celerybeat.pid
163
-
164
- # SageMath parsed files
165
- *.sage.py
166
-
167
- # Environments
168
- .env
169
- .venv
170
- env/
171
- venv/
172
- ENV/
173
- env.bak/
174
- venv.bak/
175
- .conda
176
-
177
- # Spyder project settings
178
- .spyderproject
179
- .spyproject
180
-
181
- # Rope project settings
182
- .ropeproject
183
-
184
- # mkdocs documentation
185
- /site
186
-
187
- # mypy
188
- .mypy_cache/
189
- .dmypy.json
190
- dmypy.json
191
-
192
- # Pyre type checker
193
- .pyre/
194
-
195
- # pytype static type analyzer
196
- .pytype/
197
-
198
- # Cython debug symbols
199
- cython_debug/
200
-
201
- # dummy ipynb
202
- dummy.ipynb
203
-
204
- # PyCharm
205
- # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
206
- # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
207
- # and can be added to the global gitignore or merged into this file. For a more nuclear
208
- # option (not recommended) you can uncomment the following to ignore the entire idea folder.
209
- #.idea/
210
-
211
- # stl10 data
212
- img/
213
- data/
214
- *.npy
215
-
216
- # tests and verifications
217
- *.ipynb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Semih
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -1,117 +1,3 @@
1
- # STL10 - Segmentation
2
-
3
- Please consider sponsoring this repo so that we can continue to develop high-quality datasets for the AI and ML research.
4
-
5
- To become a sponsor:
6
-
7
- [GitHub Sponsors](https://github.com/sponsors/semihyagli) <br/>
8
- [Buy me a coffee](https://buymeacoffee.com/semihyagli) <br/>
9
-
10
- You can also sponsor us by downloading our free application, **_Etiqueta_**, to your devices:
11
-
12
- [Etiqueta on iOS or Apple Chip Macs](https://apps.apple.com/us/app/etiqueta/id6504646776) <br/>
13
- [Etiqueta on Android](https://play.google.com/store/apps/details?id=com.aidatalabel.etiqueta) <br/>
14
-
15
-
16
- This repo contains segmented images for the labeled part of the [STL-10 Dataset](https://www.google.com/url?sa=t&source=web&rct=j&opi=89978449&url=https://cs.stanford.edu/~acoates/stl10/&ved=2ahUKEwidyq31o9-LAxUQbvUHHeAOIx0QFnoECAgQAQ&usg=AOvVaw1TdzbOwKAoAz_-kLc5rghT). <br/>
17
- If you are looking for `STL10-Labeled` variant of the dataset, refer here: [STL10-Labeled](https://github.com/semihyagli/STL10-Labeled).<br/>
18
- More information on the original STL-10 dataset can be found [here](https://cs.stanford.edu/~acoates/stl10/).<br/>
19
- Thanks to Martin Tutek, the original STL-10 dataset can be downloaded via the python code in this [repo](https://github.com/mttk/STL10). For convenience, this code is copied in `stl10.py` in this repo.
20
-
21
-
22
- If you use this dataset in your research please do not forget to cite: <br/>
23
-
24
- ```
25
- @techreport{yagli2025etiqueta,
26
- author = {Semih Yagli},
27
- title = {Etiqueta: AI-Aided, Gamified Data Labeling to Label and Segment Data},
28
- year = {2025},
29
- number = {TR-2025-0001},
30
- address = {NJ, USA},
31
- month = Apr.,
32
- url = {https://www.aidatalabel.com/technical_reports/aidatalabel_tr_2025_0001.pdf},
33
- institution = {AI Data Label},
34
- }
35
- ```
36
- ```
37
- @inproceedings{coates2011analysis,
38
- title = {An analysis of single-layer networks in unsupervised feature learning},
39
- author = {Coates, Adam and Ng, Andrew and Lee, Honglak},
40
- booktitle = {Proceedings of the fourteenth international conference on artificial intelligence and statistics},
41
- pages = {215--223},
42
- year = {2011},
43
- organization={JMLR Workshop and Conference Proceedings}
44
- }
45
- ```
46
-
47
- **Note: If you notice any errors and/or if you have comments/ideas relevant to this dataset or Etiqueta in general, please reach me out at [contact@aidatalabel.com](mailto:contact@aidatalabel.com).**
48
-
49
- ## Instructions
50
-
51
- #### For Original Data:
52
- You can download the stl10 image data by running
53
- ```bash
54
- python stl10.py
55
- ```
56
- This will:
57
- 1. create a folder named `data` download and extract the stl10 dataset inside that folder.
58
- 2. show one example picture in a new window.
59
- <!-- 3. Once you close this example picture, images will then be saved under a folder called `img` -->
60
- How to read images and their labels are also exemplified inside the `stl10.py`. For example, you can load all test images and their labels to a numpy array by using:
61
- ```python
62
- import stl10
63
-
64
- img_test_X_bin_loc = "./data/stl10_binary/test_X.bin"
65
- img_test_y_bin_loc = "./data/stl10_binary/test_y.bin"
66
-
67
- test_X = stl10.read_all_images(img_test_X_bin_loc)
68
- test_y = stl10.read_labels(img_test_y_bin_loc)
69
- ```
70
- Additionally, other useful functions are readily defined inside `stl10.py`. <br/>
71
-
72
-
73
- #### For Segmentation Data:
74
- Segmentation of each image in the `test_X.bin` file can be found inside the provided `.json` files. Note that images that contain more than a single segment are segmented using different labels. <br/>
75
- The combined segmentations can be recovered running:
76
-
77
- ```bash
78
- python recoverSegmentations.py
79
- ```
80
- By default, this will create `test_X_segmented.npy`, which contains the cutouts of images in the test part of stl10 as depicted in the examples before.
81
- - Depending on your device specifications, the above should take about *~10 minutes* to complete. The segmentation progress will be printed out so that you can go get a coffee while the segmented data is being saved. Feel free to modify the script depending on your needs.
82
-
83
- You can load this numpy array by using:
84
- ```python
85
- from numpy import load
86
-
87
- DEFAULT_SEGMENTED_TEST_X_SAVE_LOC = "./test_X_segmented.npy"
88
-
89
- test_X_segmented = load(DEFAULT_SEGMENTED_TEST_X_SAVE_LOC)
90
- ```
91
- Observe that the arrays inside `test_X_segmented` are now sparse.
92
-
93
- Enjoy!
94
-
95
- ## Examples
96
-
97
- Class | airplane | bird | car | cat | deer |
98
- --- | --- | --- | --- | --- | --- |
99
- original | ![airplane](examples/original_airplane_4599.png) | ![bird](examples/original_bird_2297.png) | ![car](examples/original_car_2606.png) | ![cat](examples/original_cat_546.png) | ![deer](examples/original_deer_3472.png) |
100
- segmented | ![airplane](examples/segmented_airplane_4599.png) | ![bird](examples/segmented_bird_2297.png) | ![car](examples/segmented_car_2606.png) | ![cat](examples/segmented_cat_546.png) | ![deer](examples/segmented_deer_3472.png) |
101
-
102
- Class | dog | horse | monkey | ship | truck |
103
- --- | --- | --- | --- | --- | --- |
104
- original | ![dog](examples/original_dog_2537.png ) | ![horse](examples/original_horse_6056.png) | ![monkey](examples/original_monkey_2987.png) | ![ship](examples/original_ship_1628.png) | ![truck](examples/original_truck_2999.png) |
105
- segmented | ![dog](examples/segmented_dog_2537.png) | ![horse](examples/segmented_horse_6056.png) | ![monkey](examples/segmented_monkey_2987.png) | ![ship](examples/segmented_ship_1628.png) | ![truck](examples/segmented_truck_2999.png) |
106
-
107
-
108
- ### Notes:
109
- We have caught the following errors in the test part of the STL-10 dataset: <br/>
110
-
111
- `1495`: `cat_0` mark is in fact a `dog_0`. <br/>
112
- `6417`: `cat_0` mark is in fact a `dog_0`. <br/>
113
- `1718`: `cat_1` mark is in fact a `dog_0`. <br/>
114
- `1138`: `dog_1` mark is in fact a `cat_0`. <br/>
115
- `1484`: `dog_1`, `dog_2`, and `dog_3` are in fact `sheep_0`, `sheep_1`, `sheep_2`. <br/>
116
- `6566`: `dog_0` and `dog_1` marks are in fact `cat_0`, and `dog_0`. <br/>
117
- `7902`: `dog_0` and `dog_1` marks are in fact `cat_0`, and `dog_0`. <br/>
 
1
+ ---
2
+ license: mit
3
+ ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
examples/original_airplane_4599.png DELETED

Git LFS Details

  • SHA256: 9e03f4d334049643b9f7e8c1762011d34d59b3911ff3ec3319e2ca844df6de47
  • Pointer size: 130 Bytes
  • Size of remote file: 15 kB
examples/original_bird_2297.png DELETED

Git LFS Details

  • SHA256: 541e4d724c48488ffb7ca477aca71ad9921544d63d7525e5c587c8d501dab2ef
  • Pointer size: 130 Bytes
  • Size of remote file: 17.2 kB
examples/original_car_2606.png DELETED

Git LFS Details

  • SHA256: 1e6754ddedafc8a5d0a8e71c4419e09bf33e22b07ca6c59d9792f16720731df4
  • Pointer size: 130 Bytes
  • Size of remote file: 13.6 kB
examples/original_cat_546.png DELETED

Git LFS Details

  • SHA256: 69b6bfea23409888e19f5ab956bec04a8d5c141a81545e9031e17d522c13f673
  • Pointer size: 130 Bytes
  • Size of remote file: 10.1 kB
examples/original_deer_3472.png DELETED

Git LFS Details

  • SHA256: bc238cac73e6d9f328241fc89d516267f270b0670b768d9cf0d004d9d9bb7bae
  • Pointer size: 130 Bytes
  • Size of remote file: 22.9 kB
examples/original_dog_2537.png DELETED

Git LFS Details

  • SHA256: 7d0708d2fa9319d01ca3b16c8f5270f08825eb07852115c71b7cd941c5c7c872
  • Pointer size: 130 Bytes
  • Size of remote file: 15.6 kB
examples/original_horse_6056.png DELETED

Git LFS Details

  • SHA256: eab6528589d44e0641bb475f3818f1af0a275aba871cb27cea03dbc921dd44aa
  • Pointer size: 130 Bytes
  • Size of remote file: 17.5 kB
examples/original_monkey_2987.png DELETED

Git LFS Details

  • SHA256: d2d33b93b1a223693da2c4d5af41689c57ecd3f2e5d9b8d53cfe873787c7bf74
  • Pointer size: 130 Bytes
  • Size of remote file: 17.2 kB
examples/original_ship_1628.png DELETED

Git LFS Details

  • SHA256: 5783dfb0a3fa7f9077fb5bc916990905316ebebdcfe6e94ce665c22acab647f5
  • Pointer size: 130 Bytes
  • Size of remote file: 17.8 kB
examples/original_truck_2999.png DELETED

Git LFS Details

  • SHA256: 06387274c8f383b5dca262f86a2f660b8270dc0a406d421791312ee12a65e1e6
  • Pointer size: 130 Bytes
  • Size of remote file: 19.3 kB
examples/segmented_airplane_4599.png DELETED

Git LFS Details

  • SHA256: 6cdff2ed0ff42950ed24f69e9e6a99e72ecb0944299a323160941ce737a0b514
  • Pointer size: 129 Bytes
  • Size of remote file: 5.91 kB
examples/segmented_bird_2297.png DELETED

Git LFS Details

  • SHA256: 669b23f97b7784479fd1798440c1f114dd9f24744e7c5b17ad821cc04414f430
  • Pointer size: 130 Bytes
  • Size of remote file: 10.6 kB
examples/segmented_car_2606.png DELETED

Git LFS Details

  • SHA256: b95b3463e51ee35ae3e71a70a557cd87eb4fe32fa8ef51d938535ed3895bd227
  • Pointer size: 129 Bytes
  • Size of remote file: 8.22 kB
examples/segmented_cat_546.png DELETED

Git LFS Details

  • SHA256: 0c3b637cfc5331c3fc7e64f55e404d617ffd1c8db00e07637406a85d070d25ea
  • Pointer size: 129 Bytes
  • Size of remote file: 8.59 kB
examples/segmented_deer_3472.png DELETED

Git LFS Details

  • SHA256: de878bbc65d4696e68aded828e65b3846a8fb234d8ef98392d8e6e0fc7573ca9
  • Pointer size: 130 Bytes
  • Size of remote file: 15.3 kB
examples/segmented_dog_2537.png DELETED

Git LFS Details

  • SHA256: 89dd579642684197a7b1efa206974c8d8ad2c0273fc6aa7465d97c2d09fda116
  • Pointer size: 130 Bytes
  • Size of remote file: 10.1 kB
examples/segmented_horse_6056.png DELETED

Git LFS Details

  • SHA256: eb976e5a01f3b7e285e9cb31c827fb0e8614b3a264822b3d78e78371bbab439e
  • Pointer size: 129 Bytes
  • Size of remote file: 8.15 kB
examples/segmented_monkey_2987.png DELETED

Git LFS Details

  • SHA256: ed8fbb303f49a162f1075901e897d6a3a63a7af68c4422ab077d44d1ee87134e
  • Pointer size: 130 Bytes
  • Size of remote file: 15 kB
examples/segmented_ship_1628.png DELETED

Git LFS Details

  • SHA256: 23062074dca2f5ad5af904cebeaa48b0e801e4204df43600fdded2b8d86fd2d0
  • Pointer size: 130 Bytes
  • Size of remote file: 15.7 kB
examples/segmented_truck_2999.png DELETED

Git LFS Details

  • SHA256: f92a22a0df8be4bb9cbcbe0de0f461ec102417fe5f7aeddae79e8e3bf8b532de
  • Pointer size: 130 Bytes
  • Size of remote file: 10 kB
img_segmented/2/4391.png DELETED

Git LFS Details

  • SHA256: 88452620dc552cb555a9a53f57a34f24bfde7bea1f8fa40add4b96f8e4ef53a8
  • Pointer size: 130 Bytes
  • Size of remote file: 21.6 kB
recoverSegmentations.py DELETED
@@ -1,182 +0,0 @@
1
- import argparse
2
- import stl10
3
- from PIL import Image, ImageDraw
4
- import json
5
- from numpy import array, arange, save
6
- from matplotlib.pyplot import subplots
7
- from itertools import chain
8
- import os
9
-
10
- ALL = "all"
11
-
12
- DEFAULT_TEST_X_BIN_LOC = "./data/stl10_binary/test_X.bin"
13
- DEFAULT_TEST_Y_BIN_LOC = "./data/stl10_binary/test_y.bin"
14
-
15
- DEFAULT_SEGMENTED_TEST_X_SAVE_LOC = "./test_X_segmented.npy"
16
-
17
- DEFAULT_NUM_SQUARES = 16
18
- DEFAULT_TRANSPARENCY = 0.4
19
- DEFAULT_DATA_LENGTH = 8000
20
-
21
-
22
- LAYER_COLORS = {
23
- "0": "#ff8080",
24
- "1": "#ffd480",
25
- "2": "#d4ff80",
26
- "3": "#80ff80",
27
- "4": "#80ffd4",
28
- "5": "#80d4ff",
29
- "6": "#8080ff",
30
- "7": "#d580ff",
31
- "8": "#ff80d4",
32
- }
33
-
34
- LABEL_DICTIONARY = {
35
- 1: "airplane",
36
- 2: "bird",
37
- 3: "car",
38
- 4: "cat",
39
- 5: "deer",
40
- 6: "dog",
41
- 7: "horse",
42
- 8: "monkey",
43
- 9: "ship",
44
- 10: "truck",
45
- }
46
-
47
-
48
- def combine_lists(list_of_lists):
49
- flattened_list = chain.from_iterable(list_of_lists)
50
- return list(
51
- list(tuple_item) for tuple_item in set(
52
- tuple(list_item) for list_item in flattened_list
53
- )
54
- )
55
-
56
- def cutout(
57
- img_index,
58
- combine_segments=True,
59
- create_cutout=True,
60
- show_segmented_image=False,
61
- save=False,
62
- to_array=False,
63
- img_bin_loc=DEFAULT_TEST_X_BIN_LOC,
64
- lbl_bin_loc=DEFAULT_TEST_Y_BIN_LOC,
65
- ):
66
-
67
- label = stl10.read_labels(lbl_bin_loc)[int(img_index)]
68
- segment = LABEL_DICTIONARY[label]
69
- segment_marks_json_loc = f"./stl10_test_{segment}.json"
70
-
71
- with open(segment_marks_json_loc, 'r') as f:
72
- segment_marks = json.load(f)
73
-
74
- marked_squares = segment_marks[str(img_index)]
75
- combined_marked_squares = {f"{segment}_combined": combine_lists(marked_squares.values())}
76
- marked_squares_dictionary = combined_marked_squares if combine_segments else marked_squares
77
-
78
- all_images = stl10.read_all_images(img_bin_loc)
79
- img = Image.fromarray(all_images[int(img_index)]).convert("RGBA")
80
-
81
- directory_OriginalImage = f"./{segment}/original/"
82
- directory_SegmentedImage = f"./{segment}/cutout/"
83
-
84
- if save:
85
- if not os.path.exists(directory_OriginalImage):
86
- os.makedirs(directory_OriginalImage, exist_ok=True)
87
- img.save(os.path.join(directory_OriginalImage, f"{img_index}.png"))
88
- imgWidth, imgHeight = img.size
89
-
90
- overlay = Image.new("RGBA", img.size, (255, 255, 255, 0))
91
- draw = ImageDraw.Draw(overlay)
92
-
93
- squareSize = min(imgWidth, imgHeight)/DEFAULT_NUM_SQUARES
94
-
95
- for lbl, segmentation in marked_squares_dictionary.items():
96
- overlay = Image.new("RGBA", img.size, (255, 255, 255, 0))
97
- draw = ImageDraw.Draw(overlay)
98
- for y in arange(0, imgHeight, squareSize):
99
- for x in arange(0, imgWidth, squareSize):
100
- top_left = (x, y)
101
- bottom_right = (x + squareSize, y + squareSize)
102
-
103
- if [int(x / squareSize), int(y / squareSize)] not in segmentation:
104
- if create_cutout:
105
- draw.rectangle([top_left, bottom_right], fill="#000000")
106
- else:
107
- if not create_cutout:
108
- draw.rectangle([top_left, bottom_right],
109
- fill=LAYER_COLORS.get(lbl[-1], "#ff8080") + format(int(DEFAULT_TRANSPARENCY * 255), '02X'),
110
- )
111
-
112
- segmented_image = Image.alpha_composite(img, overlay)
113
- if show_segmented_image:
114
- fig, ax = subplots()
115
- ax.imshow(segmented_image)
116
- if save:
117
- if not os.path.exists(directory_SegmentedImage):
118
- os.makedirs(directory_SegmentedImage, exist_ok=True)
119
- segmented_image.save(os.path.join(directory_SegmentedImage, f"{img_index}_{lbl}.png"))
120
-
121
- if to_array:
122
- return array(segmented_image.convert("RGB"))
123
-
124
- def get_segmented_images_array():
125
-
126
- combined_segments = []
127
- for i in range(DEFAULT_DATA_LENGTH):
128
- print(f"Segmentation Progress: {100*(i+1)/DEFAULT_DATA_LENGTH:0f}%", end="\r")
129
- combined_segments.append(
130
- cutout(
131
- img_index=i,
132
- combine_segments=True,
133
- create_cutout=True,
134
- show_segmented_image=False,
135
- save=False,
136
- to_array=True,
137
- )
138
- )
139
-
140
- output = array(combined_segments)
141
-
142
- return output
143
-
144
-
145
-
146
-
147
- if __name__ == "__main__":
148
-
149
- ## Note: Although I created many arguments for future use, midway through writing the script, I realized that use cases maybe vastly different for those who may use this dataset.
150
- ## If the dataset turns out to be popular, I may revisit this file and continue working on this, otherwise I fear that this file will remain in this state forever...
151
-
152
- parser = argparse.ArgumentParser()
153
-
154
- parser.add_argument("--img_bin_loc", type=str, default=DEFAULT_TEST_X_BIN_LOC, help="Location of the original stl10 images.")
155
-
156
- parser.add_argument("--lbl_bin_loc", type=str, default=DEFAULT_TEST_Y_BIN_LOC, help="Location of the original stl10 image labels.")
157
-
158
- parser.add_argument("--combine_segments", type=bool, default=True, help="Whether to combine segments within a single image.")
159
-
160
- parser.add_argument("--create_cutout", type=bool, default=True, help="Whether to create cutout from segments. If this is set to false, segments will be overlayed with transparent colors.")
161
-
162
- parser.add_argument("--show_segmented_image", type=bool, default=False, help="Whether to show segmented image.")
163
-
164
- parser.add_argument("--save", type=bool, default=True, help="Whether to save original and segmented under {segment}/original and {segment}/cutout directories, respectively.")
165
-
166
- parser.add_argument("--to_array", type=bool, default=True, help="Whether to combine segments within a single image.")
167
-
168
- parser.add_argument("--img_index", type=str, default=ALL, help="Select image index.")
169
-
170
- parser.add_argument("--save_segmented_img_arr", type=bool, default=True, help="Whether to save the segmented images array.")
171
-
172
- parser.add_argument("--save_segmented_img_loc", type=str, default=DEFAULT_SEGMENTED_TEST_X_SAVE_LOC, help="Where to save the segmented images array.")
173
-
174
- args = parser.parse_args()
175
-
176
- if args.img_index == ALL:
177
- segmented_images_array = get_segmented_images_array()
178
- if args.save_segmented_img_arr:
179
- save(args.save_segmented_img_loc, segmented_images_array)
180
-
181
-
182
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt DELETED
@@ -1,4 +0,0 @@
1
- argparse
2
- numpy
3
- matplotlib
4
- PIL
 
 
 
 
 
stl10.py DELETED
@@ -1,161 +0,0 @@
1
- from __future__ import print_function
2
-
3
- import sys
4
- import os, sys, tarfile, errno
5
- import numpy as np
6
- import matplotlib.pyplot as plt
7
-
8
- if sys.version_info >= (3, 0, 0):
9
- import urllib.request as urllib # ugly but works
10
- else:
11
- import urllib
12
-
13
- try:
14
- from imageio import imsave
15
- except:
16
- from scipy.misc import imsave
17
-
18
- print(sys.version_info)
19
-
20
- # image shape
21
- HEIGHT = 96
22
- WIDTH = 96
23
- DEPTH = 3
24
-
25
- # size of a single image in bytes
26
- SIZE = HEIGHT * WIDTH * DEPTH
27
-
28
- # path to the directory with the data
29
- DATA_DIR = './data'
30
-
31
- # url of the binary data
32
- DATA_URL = 'http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gz'
33
-
34
- # path to the binary train file with image data
35
- DATA_PATH = './data/stl10_binary/train_X.bin'
36
-
37
- # path to the binary train file with labels
38
- LABEL_PATH = './data/stl10_binary/train_y.bin'
39
-
40
- def read_labels(path_to_labels):
41
- """
42
- :param path_to_labels: path to the binary file containing labels from the STL-10 dataset
43
- :return: an array containing the labels
44
- """
45
- with open(path_to_labels, 'rb') as f:
46
- labels = np.fromfile(f, dtype=np.uint8)
47
- return labels
48
-
49
-
50
- def read_all_images(path_to_data):
51
- """
52
- :param path_to_data: the file containing the binary images from the STL-10 dataset
53
- :return: an array containing all the images
54
- """
55
-
56
- with open(path_to_data, 'rb') as f:
57
- # read whole file in uint8 chunks
58
- everything = np.fromfile(f, dtype=np.uint8)
59
-
60
- # We force the data into 3x96x96 chunks, since the
61
- # images are stored in "column-major order", meaning
62
- # that "the first 96*96 values are the red channel,
63
- # the next 96*96 are green, and the last are blue."
64
- # The -1 is since the size of the pictures depends
65
- # on the input file, and this way numpy determines
66
- # the size on its own.
67
-
68
- images = np.reshape(everything, (-1, 3, 96, 96))
69
-
70
- # Now transpose the images into a standard image format
71
- # readable by, for example, matplotlib.imshow
72
- # You might want to comment this line or reverse the shuffle
73
- # if you will use a learning algorithm like CNN, since they like
74
- # their channels separated.
75
- images = np.transpose(images, (0, 3, 2, 1))
76
- return images
77
-
78
-
79
- def read_single_image(image_file):
80
- """
81
- CAREFUL! - this method uses a file as input instead of the path - so the
82
- position of the reader will be remembered outside of context of this method.
83
- :param image_file: the open file containing the images
84
- :return: a single image
85
- """
86
- # read a single image, count determines the number of uint8's to read
87
- image = np.fromfile(image_file, dtype=np.uint8, count=SIZE)
88
- # force into image matrix
89
- image = np.reshape(image, (3, 96, 96))
90
- # transpose to standard format
91
- # You might want to comment this line or reverse the shuffle
92
- # if you will use a learning algorithm like CNN, since they like
93
- # their channels separated.
94
- image = np.transpose(image, (2, 1, 0))
95
- return image
96
-
97
-
98
- def plot_image(image):
99
- """
100
- :param image: the image to be plotted in a 3-D matrix format
101
- :return: None
102
- """
103
- plt.imshow(image)
104
- plt.show()
105
-
106
- def save_image(image, name):
107
- imsave("%s.png" % name, image, format="png")
108
-
109
- def download_and_extract():
110
- """
111
- Download and extract the STL-10 dataset
112
- :return: None
113
- """
114
- dest_directory = DATA_DIR
115
- if not os.path.exists(dest_directory):
116
- os.makedirs(dest_directory)
117
- filename = DATA_URL.split('/')[-1]
118
- filepath = os.path.join(dest_directory, filename)
119
- if not os.path.exists(filepath):
120
- def _progress(count, block_size, total_size):
121
- sys.stdout.write('\rDownloading %s %.2f%%' % (filename,
122
- float(count * block_size) / float(total_size) * 100.0))
123
- sys.stdout.flush()
124
- filepath, _ = urllib.urlretrieve(DATA_URL, filepath, reporthook=_progress)
125
- print('Downloaded', filename)
126
- tarfile.open(filepath, 'r:gz').extractall(dest_directory)
127
-
128
- def save_images(images, labels):
129
- print("Saving images to disk")
130
- i = 0
131
- for image in images:
132
- label = labels[i]
133
- directory = './img/' + str(label) + '/'
134
- try:
135
- os.makedirs(directory, exist_ok=True)
136
- except OSError as exc:
137
- if exc.errno == errno.EEXIST:
138
- pass
139
- filename = directory + str(i)
140
- print(filename)
141
- save_image(image, filename)
142
- i = i+1
143
-
144
- if __name__ == "__main__":
145
- # download data if needed
146
- download_and_extract()
147
-
148
- # test to check if the image is read correctly
149
- with open(DATA_PATH) as f:
150
- image = read_single_image(f)
151
- plot_image(image)
152
-
153
- # test to check if the whole dataset is read correctly
154
- images = read_all_images(DATA_PATH)
155
- print(images.shape)
156
-
157
- labels = read_labels(LABEL_PATH)
158
- print(labels.shape)
159
-
160
- # save images to disk
161
- # save_images(images, labels)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
stl10_test_airplane.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_bird.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_car.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_cat.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_deer.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_dog.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_horse.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_monkey.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_ship.json DELETED
The diff for this file is too large to render. See raw diff
 
stl10_test_truck.json DELETED
The diff for this file is too large to render. See raw diff