Skip to content

Conversation

@DivergeAI
Copy link

This commit introduces an adaptive superpixel generation mechanism into the SuiT architecture.

Key changes:

  1. Parameter Predictor (models/parameter_predictor.py): A lightweight CNN is implemented to predict superpixel parameters (number of segments K and compactness m) dynamically based on the input image.

  2. Superpixel Generation (utils/superpixels.py): A new utility function generate_superpixels uses skimage.segmentation.slic to generate superpixel maps based on the predicted K and m. It supports batch processing and handles cases where scikit-image might not be available.

  3. Adaptive SuiT Model (models/suit_adaptive.py): The SuperpixelVisionTransformer has been refactored into SuitAdaptive. This new model incorporates the ParameterPredictor and generate_superpixels to dynamically create superpixels for each input image. The predicted parameters K and m are 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.

  4. Main Script (main.py): The main training script has been updated to use the new SuitAdaptive models. Command-line arguments for fixed superpixel parameters (--compactness, --n-spix-segments) have been removed as these are now predicted.

  5. Unit Tests (tests/test_adaptive.py): Comprehensive unit tests have been added for the ParameterPredictor, generate_superpixels function (including skimage availability checks and fallback testing), and the SuitAdaptive model integration (including forward pass and parameter clipping tests).

  6. 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.

This commit introduces an adaptive superpixel generation mechanism into the
SuiT architecture.

Key changes:

1.  **Parameter Predictor (`models/parameter_predictor.py`)**:
    A lightweight CNN is implemented to predict superpixel parameters
    (number of segments `K` and compactness `m`) dynamically based on the
    input image.

2.  **Superpixel Generation (`utils/superpixels.py`)**:
    A new utility function `generate_superpixels` uses `skimage.segmentation.slic`
    to generate superpixel maps based on the predicted `K` and `m`.
    It supports batch processing and handles cases where scikit-image
    might not be available.

3.  **Adaptive SuiT Model (`models/suit_adaptive.py`)**:
    The `SuperpixelVisionTransformer` has been refactored into `SuitAdaptive`.
    This new model incorporates the `ParameterPredictor` and
    `generate_superpixels` to dynamically create superpixels for each input
    image. The predicted parameters `K` and `m` are 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.

4.  **Main Script (`main.py`)**:
    The main training script has been updated to use the new `SuitAdaptive`
    models. Command-line arguments for fixed superpixel parameters
    (`--compactness`, `--n-spix-segments`) have been removed as these are
    now predicted.

5.  **Unit Tests (`tests/test_adaptive.py`)**:
    Comprehensive unit tests have been added for the `ParameterPredictor`,
    `generate_superpixels` function (including skimage availability checks
    and fallback testing), and the `SuitAdaptive` model integration
    (including forward pass and parameter clipping tests).

6.  **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.
This commit refactors the codebase to integrate adaptive superpixel
functionality directly into the existing `suit.py` and `utils.py` files,
removing the `models/` and `utils/` subdirectories that were
previously introduced. This addresses your feedback and resolves
`ModuleNotFoundError` issues related to package imports.

Key changes:

1.  **File Structure Flattening**:
    *   The `ParameterPredictor` class definition has been moved directly
        into `suit.py`.
    *   The `generate_superpixels` function and its `SKIMAGE_AVAILABLE`
        helper constant have been moved directly into `utils.py`.
    *   The `SuitAdaptive` model definition (previously
        `models/suit_adaptive.py`) now constitutes the main content of
        `suit.py`, replacing the original `SuperpixelVisionTransformer` logic.
        Registered model names (e.g., `suit_tiny_224_adaptive`) are
        preserved.
    *   The `models/` and `utils/` directories and their specific files
        (`parameter_predictor.py`, `suit_adaptive.py`, `superpixels.py`)
        have been removed.

2.  **Import Adjustments**:
    *   All import statements in `main.py`, `tests/test_adaptive.py`, and
        `visualize/adaptive_debug.py` have been updated to reflect the
        new locations of `ParameterPredictor` (in `suit.py`),
        `generate_superpixels` (in `utils.py`), and the `SuitAdaptive`
        model variants (in `suit.py`).
    *   Mock paths in tests have also been updated.

This resolves the previously reported `ModuleNotFoundError` and aligns
the codebase with the preferred flat directory structure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant