rgb-to-segmentation 0.1.6__py3-none-any.whl
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.
- rgb_to_segmentation/__init__.py +10 -0
- rgb_to_segmentation/api.py +123 -0
- rgb_to_segmentation/clean.py +315 -0
- rgb_to_segmentation/cli.py +180 -0
- rgb_to_segmentation/models/__init__.py +5 -0
- rgb_to_segmentation/models/base_classifier.py +77 -0
- rgb_to_segmentation/models/cnn_decoder.py +103 -0
- rgb_to_segmentation/models/pixelwise_classifier.py +31 -0
- rgb_to_segmentation/nn.py +164 -0
- rgb_to_segmentation/train.py +212 -0
- rgb_to_segmentation/utils.py +40 -0
- rgb_to_segmentation-0.1.6.dist-info/METADATA +213 -0
- rgb_to_segmentation-0.1.6.dist-info/RECORD +15 -0
- rgb_to_segmentation-0.1.6.dist-info/WHEEL +4 -0
- rgb_to_segmentation-0.1.6.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: rgb-to-segmentation
|
|
3
|
+
Version: 0.1.6
|
|
4
|
+
Summary: Tools for processing and cleaning segmentation images using palette mapping and neural networks
|
|
5
|
+
Author: Alex Senden
|
|
6
|
+
Maintainer: Alex Senden
|
|
7
|
+
License: MIT
|
|
8
|
+
Keywords: image-processing,machine-learning,pytorch,segmentation
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
18
|
+
Requires-Python: >=3.8
|
|
19
|
+
Requires-Dist: numpy>=1.21.0
|
|
20
|
+
Requires-Dist: pillow>=8.0.0
|
|
21
|
+
Requires-Dist: pytorch-lightning>=1.5.0
|
|
22
|
+
Requires-Dist: scipy>=1.7.0
|
|
23
|
+
Requires-Dist: torch>=1.9.0
|
|
24
|
+
Requires-Dist: torchvision>=0.10.0
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# RGB to Segmentation
|
|
28
|
+
|
|
29
|
+
A Python package for processing and cleaning segmentation images. This package provides tools to convert RGB images to segmentation masks using palette-based color mapping and neural network-based refinement.
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- **Palette-based Cleaning**: Clean noisy segmentation images by mapping pixels to the nearest colors in a predefined palette, with optional morphological operations to refine boundaries.
|
|
34
|
+
- **Strict Palette Mapping**: Directly map RGB values to class indices with strict validation - throws an error if any pixel value is not in the colour map.
|
|
35
|
+
- **Neural Network Refinement**: Use trained neural network models to refine segmentation masks using PyTorch Lightning:
|
|
36
|
+
- **Pixelwise Classifier**: MLP-based pixel-by-pixel classification
|
|
37
|
+
- **CNN Decoder**: Convolutional encoder-decoder architecture for spatial context
|
|
38
|
+
- **Flexible Input Types**: Accept both NumPy arrays and PyTorch tensors, with output type matching input type.
|
|
39
|
+
- **Command-Line Interface**: Unified CLI for cleaning with method selection, plus separate training command.
|
|
40
|
+
- **Programmatic API**: Direct access to cleaning and training functions for integration into other workflows.
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
Install from PyPI:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install rgb-to-segmentation
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Or install from source:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
git clone https://github.com/alexsenden/rgb-to-segmentation.git
|
|
54
|
+
cd rgb-to-segmentation
|
|
55
|
+
pip install .
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
### Cleaning Noisy Segmentation Images
|
|
61
|
+
|
|
62
|
+
Use the `segment-clean` command to clean segmentation images using various methods:
|
|
63
|
+
|
|
64
|
+
#### Palette-based cleaning:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
segment-clean --method palette --input_dir /path/to/input --output_dir /path/to/output --colour_map "0,0,0;255,0,0;0,255,0" --output_type rgb
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
#### Neural network-based cleaning:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
# Works with both pixelwise and CNN decoder models
|
|
74
|
+
segment-clean --method nn --input_dir /path/to/input --output_dir /path/to/output --model_path /path/to/model.ckpt --colour_map "0,0,0;255,0,0;0,255,0" --output_type index
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
#### Strict palette mapping:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
segment-clean --method strict_palette --input_dir /path/to/input --output_dir /path/to/output --colour_map "0,0,0;255,0,0;0,255,0" --output_type index
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
You can also provide colours via file with `--colour_map_file /path/to/colours.txt` (one `r,g,b` per line). The CLI parses colours and constructs the palette/colour map internally, mirroring the Python API which accepts parsed structures (NumPy array for palette, dictionary for colour map).
|
|
84
|
+
|
|
85
|
+
Options:
|
|
86
|
+
|
|
87
|
+
- `--method`: Cleaning method ('palette', 'nn', or 'strict_palette')
|
|
88
|
+
- `--input_dir`: Path to input directory containing images
|
|
89
|
+
- `--output_dir`: Directory where cleaned images will be written
|
|
90
|
+
- `--inplace`: Overwrite input images in place
|
|
91
|
+
- `--exts`: Comma-separated list of allowed image extensions
|
|
92
|
+
- `--name_filter`: Only process files whose name contains this substring
|
|
93
|
+
- `--output_type`: Output format ('rgb' or 'index')
|
|
94
|
+
- `--colour_map`: Semicolon-separated list of RGB triples
|
|
95
|
+
- `--colour_map_file`: Path to a file listing RGB triples
|
|
96
|
+
|
|
97
|
+
For palette method:
|
|
98
|
+
|
|
99
|
+
- `--morph_kernel_size`: Size of morphological kernel for boundary cleaning
|
|
100
|
+
|
|
101
|
+
For nn method (both pixelwise and CNN decoder models):
|
|
102
|
+
|
|
103
|
+
- `--model_path`: Path to trained model file
|
|
104
|
+
|
|
105
|
+
The strict_palette method requires no additional options beyond the common ones.
|
|
106
|
+
|
|
107
|
+
### Training the Neural Network Model
|
|
108
|
+
|
|
109
|
+
Train a neural network model to refine segmentation masks. Choose between pixelwise MLP or CNN decoder:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Train pixelwise classifier (default)
|
|
113
|
+
segment-train --image_dir /path/to/noisy_images --label_dir /path/to/labels --output_dir /path/to/model_output --colour_map "0,0,0;255,0,0;0,255,0"
|
|
114
|
+
|
|
115
|
+
# Train CNN decoder
|
|
116
|
+
segment-train --image_dir /path/to/noisy_images --label_dir /path/to/labels --output_dir /path/to/model_output --model_type cnn_decoder --colour_map "0,0,0;255,0,0;0,255,0"
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Options:
|
|
120
|
+
|
|
121
|
+
- `--image_dir`: Path to directory containing noisy images
|
|
122
|
+
- `--label_dir`: Path to directory containing target RGB labels
|
|
123
|
+
- `--output_dir`: Directory where model weights will be saved
|
|
124
|
+
- `--colour_map`: Semicolon-separated list of RGB triples
|
|
125
|
+
- `--colour_map_file`: Path to a file listing RGB triples
|
|
126
|
+
- `--model_type`: The type of model to train ('pixel_decoder' or 'cnn_decoder', default: pixel_decoder)
|
|
127
|
+
|
|
128
|
+
Note that one label image may have multiple corresponding noisy masks. Labels are matched to noisy masks whose filenames contain the label file basename (pre-extension name, i.e. `my_image.png` -> `my_image`).
|
|
129
|
+
|
|
130
|
+
## API
|
|
131
|
+
|
|
132
|
+
You can also use the package programmatically:
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
import numpy as np
|
|
136
|
+
from rgb_to_segmentation import clean, nn, train, utils, clean_image
|
|
137
|
+
|
|
138
|
+
# Palette cleaning
|
|
139
|
+
colours = utils.parse_colours_from_string("0,0,0;255,0,0;0,255,0")
|
|
140
|
+
palette = np.asarray(colours, dtype=np.uint8)
|
|
141
|
+
clean.clean_segmentation(input_dir="/path/to/input", output_dir="/path/to/output", palette=palette, output_type="index")
|
|
142
|
+
|
|
143
|
+
# NN inference (works with both pixelwise and CNN decoder models)
|
|
144
|
+
colours = utils.parse_colours_from_string("0,0,0;255,0,0;0,255,0")
|
|
145
|
+
colour_map = {i: rgb for i, rgb in enumerate(colours)}
|
|
146
|
+
nn.run_inference(input_dir="/path/to/input", output_dir="/path/to/output", model_path="/path/to/model.ckpt", colour_map=colour_map, output_type="rgb")
|
|
147
|
+
|
|
148
|
+
# Train pixelwise model
|
|
149
|
+
colours = utils.parse_colours_from_string("0,0,0;255,0,0;0,255,0")
|
|
150
|
+
colour_map = {i: rgb for i, rgb in enumerate(colours)}
|
|
151
|
+
train.train_model(image_dir="/path/to/images", label_dir="/path/to/labels", output_dir="/path/to/output", colour_map=colour_map)
|
|
152
|
+
|
|
153
|
+
# Train CNN decoder model
|
|
154
|
+
train.train_model(image_dir="/path/to/images", label_dir="/path/to/labels", output_dir="/path/to/output", colour_map=colour_map, model_type="cnn_decoder")
|
|
155
|
+
|
|
156
|
+
# Single-image cleaning (programmatic-only API)
|
|
157
|
+
# Accepts both numpy arrays and torch tensors; output type matches input type
|
|
158
|
+
|
|
159
|
+
# Palette method (returns RGB)
|
|
160
|
+
import numpy as np
|
|
161
|
+
rgb_out = clean_image(
|
|
162
|
+
image_array=np.zeros((512, 512, 3), dtype=np.uint8),
|
|
163
|
+
method="palette",
|
|
164
|
+
colour_map=colour_map,
|
|
165
|
+
morph_kernel_size=3,
|
|
166
|
+
output_type="rgb",
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
# Strict palette method (returns index mask, validates all pixels are in colour_map)
|
|
170
|
+
index_out = clean_image(
|
|
171
|
+
image_array=np.zeros((512, 512, 3), dtype=np.uint8),
|
|
172
|
+
method="strict_palette",
|
|
173
|
+
colour_map=colour_map,
|
|
174
|
+
output_type="index",
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
# Pixel decoder method (returns index mask, works with pixelwise or CNN models)
|
|
178
|
+
index_out = clean_image(
|
|
179
|
+
image_array=np.zeros((512, 512, 3), dtype=np.uint8),
|
|
180
|
+
method="pixel_decoder",
|
|
181
|
+
model=None, # Provide a loaded model instance
|
|
182
|
+
colour_map=colour_map,
|
|
183
|
+
output_type="index",
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
# CNN decoder method (returns index mask)
|
|
187
|
+
index_out = clean_image(
|
|
188
|
+
image_array=np.zeros((512, 512, 3), dtype=np.uint8),
|
|
189
|
+
method="cnn_decoder",
|
|
190
|
+
model=None, # Provide a loaded CNN decoder model instance
|
|
191
|
+
colour_map=colour_map,
|
|
192
|
+
output_type="index",
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
# Using PyTorch tensors
|
|
196
|
+
import torch
|
|
197
|
+
tensor_input = torch.zeros((512, 512, 3), dtype=torch.uint8)
|
|
198
|
+
tensor_out = clean_image(
|
|
199
|
+
image_array=tensor_input,
|
|
200
|
+
method="strict_palette",
|
|
201
|
+
colour_map=colour_map,
|
|
202
|
+
output_type="rgb",
|
|
203
|
+
)
|
|
204
|
+
# tensor_out will be a torch.Tensor with same dtype and device as tensor_input
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Contributing
|
|
208
|
+
|
|
209
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
210
|
+
|
|
211
|
+
## License
|
|
212
|
+
|
|
213
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
rgb_to_segmentation/__init__.py,sha256=pO_LJNKZcojkxZuEmzSYLY4Xohzs35LHfanQbcWjBu4,135
|
|
2
|
+
rgb_to_segmentation/api.py,sha256=IXFAbr64XgT8I_p0DtL9Fzmyqul1h_LzZpLP0hE7Id0,3925
|
|
3
|
+
rgb_to_segmentation/clean.py,sha256=JUJjSaTG3LvKl8PUwDv_2YsQyaapqvjOebIt16Ot5MI,10533
|
|
4
|
+
rgb_to_segmentation/cli.py,sha256=Z2bEtiIaovw8RF3MZn6pO7_K4zCDebzyIWWwApwySN0,4989
|
|
5
|
+
rgb_to_segmentation/nn.py,sha256=5rjolEa0RLZiVGnAzzyPOVKlUpIIGmnY0FaWiNRtHXs,5344
|
|
6
|
+
rgb_to_segmentation/train.py,sha256=fvO80541witZ-jE53Ux70GgTUGH44Nuq_aUpXDI-swo,6475
|
|
7
|
+
rgb_to_segmentation/utils.py,sha256=BS3fedeRvXx38F7867JwVDJfyY9JO1ERZtbowE5IJzI,1120
|
|
8
|
+
rgb_to_segmentation/models/__init__.py,sha256=WEncbl3DIsz5kdB0n6xNSXY6IaxX03tXKddBykU932g,203
|
|
9
|
+
rgb_to_segmentation/models/base_classifier.py,sha256=VCvuyTYTVH3qPvVbg3E-ps7jOSizqRO3TwmkjoZHPMg,2627
|
|
10
|
+
rgb_to_segmentation/models/cnn_decoder.py,sha256=n7ilv5hH_gr6HauFoQdl423_V5GLXK8Ti9snJBcBURA,3169
|
|
11
|
+
rgb_to_segmentation/models/pixelwise_classifier.py,sha256=L8X0AX3z0AiyenDsmazJbgVPa-DjF9uZUg9nlyDUiuk,895
|
|
12
|
+
rgb_to_segmentation-0.1.6.dist-info/METADATA,sha256=l2dd99g0s8O38K41_jf5UVdHIww7eSOo9jt8b_vdhdE,8309
|
|
13
|
+
rgb_to_segmentation-0.1.6.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
14
|
+
rgb_to_segmentation-0.1.6.dist-info/entry_points.txt,sha256=ret3OuYyUWvHQmy5gJplbqhJMho34VQCaGIbBCx8eOQ,120
|
|
15
|
+
rgb_to_segmentation-0.1.6.dist-info/RECORD,,
|