Skip to content

feat: allow prediction directly on in-memory numpy arrays and PIL images (closes #47) - #89

Open
ashishsoni-ai wants to merge 1 commit into
DevoLearn:masterfrom
ashishsoni-ai:feat/predict-numpy-arrays-issue-47
Open

ashishsoni-ai wants to merge 1 commit into
DevoLearn:masterfrom
ashishsoni-ai:feat/predict-numpy-arrays-issue-47

Conversation

@ashishsoni-ai

Copy link
Copy Markdown

Description

Resolves #47.

Currently, models in \devolearn\ only allow inference on images by specifying a file path on disk. If users have loaded image data in memory as a NumPy array (e.g. from OpenCV, video frames, preprocessing pipelines, or synthetic generators) or a PIL Image, they previously had to write the image to a temporary file on disk first before calling .predict().

This PR extends the .predict()\ method across all models (\cell_membrane_segmentor, \cell_nucleus_segmentor, and \lineage_population_model) to directly accept in-memory NumPy arrays and PIL Images in addition to file paths, while maintaining full backward compatibility.

Changes Made

  1. **Added \InferenceEngine.load_image()**:

    • Centralized image loading and standardization in \devolearn.base_inference_engine.InferenceEngine.
    • Supports:
      • File paths (\str\ / \os.PathLike)
      • In-memory NumPy arrays (2D grayscale, 3D BGR/RGB, 4D BGRA/RGBA, and float [0.0, 1.0]\ normalized arrays)
      • PIL Images (\PIL.Image.Image)
    • Performs proper format validation and error handling (\FileNotFoundError, \ValueError, \TypeError).
  2. Updated Model \predict()\ Signatures & Implementations:

    • Updated \cell_membrane_segmentor.predict(image_path=None, ..., image=None).
    • Updated \cell_nucleus_segmentor.predict(image_path=None, ..., image=None).
    • Updated \lineage_population_model.predict(image_path=None, image=None).
    • Users can now call either \model.predict('path/to/img.png'), \model.predict(img_array), or explicitly pass \model.predict(image=img_array).
  3. **Fixed \InferenceEngine.repr**:

    • Fixed _repr_\ to return a string (\str(self.model)) instead of returning an
      n.Module\ object, avoiding \TypeError: repr returned non-string\ during debugging, logging, or test failure reporting.
  4. Testing:

    • Added unit tests in \devolearn/tests/test.py\ validating NumPy array prediction (both positional and keyword arguments) across all three inference models.
    • Added a dedicated test case (\ est_predict_numpy_and_pil_inputs) verifying:
      • 2D grayscale arrays
      • 3D BGR arrays
      • Float [0, 1]\ arrays
      • PIL Images
      • Centroid mode with NumPy array inputs
      • Exception handling on invalid inputs
    • Verified that all tests pass (\pytest devolearn/tests/test.py).
  5. Documentation:

    • Added an example in \README.md\ demonstrating how to predict directly on in-memory NumPy arrays.

…ges (closes DevoLearn#47)

- Added load_image utility to InferenceEngine base class to standardize input across file paths, 2D/3D/float numpy arrays, and PIL Images.
- Updated cell_membrane_segmentor.predict, cell_nucleus_segmentor.predict, and lineage_population_model.predict to accept in-memory arrays as well as paths.
- Preserved backward compatibility for image_path argument.
- Fixed InferenceEngine.__repr__ to return str instead of nn.Module object.
- Added comprehensive unit tests in devolearn/tests/test.py covering numpy and PIL predictions across all models.
- Updated README.md documentation with an in-memory array prediction example.
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.

Enhancement: Allow prediction on numpy arrays present as local variable

1 participant