feat: Implement adaptive superpixel generation for SuiT #2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit introduces an adaptive superpixel generation mechanism into the SuiT architecture.
Key changes:
Parameter Predictor (
models/parameter_predictor.py): A lightweight CNN is implemented to predict superpixel parameters (number of segmentsKand compactnessm) dynamically based on the input image.Superpixel Generation (
utils/superpixels.py): A new utility functiongenerate_superpixelsusesskimage.segmentation.slicto generate superpixel maps based on the predictedKandm. It supports batch processing and handles cases where scikit-image might not be available.Adaptive SuiT Model (
models/suit_adaptive.py): TheSuperpixelVisionTransformerhas been refactored intoSuitAdaptive. This new model incorporates theParameterPredictorandgenerate_superpixelsto dynamically create superpixels for each input image. The predicted parametersKandmare clipped to predefined ranges (K: 50-1000, m: 1-40). Internal methods have been updated to use this dynamic superpixel generation. Model registration functions have also been updated.Main Script (
main.py): The main training script has been updated to use the newSuitAdaptivemodels. Command-line arguments for fixed superpixel parameters (--compactness,--n-spix-segments) have been removed as these are now predicted.Unit Tests (
tests/test_adaptive.py): Comprehensive unit tests have been added for theParameterPredictor,generate_superpixelsfunction (including skimage availability checks and fallback testing), and theSuitAdaptivemodel integration (including forward pass and parameter clipping tests).Visualization Utilities (
visualize/adaptive_debug.py): Debugging utilities are provided to visualize superpixel overlays on images and to compare segmentations generated with adaptive parameters versus fixed parameters. These include handling for optional dependencies like matplotlib and skimage.This refactoring allows for more flexible and image-specific superpixel generation, aiming to enhance semantic granularity. The training strategy for the parameter predictor (heuristic loss or end-to-end) is planned as a subsequent step.