Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embeddings Models #159

Merged
merged 66 commits into from
Jan 29, 2025
Merged

Embeddings Models #159

merged 66 commits into from
Jan 29, 2025

Conversation

kozlov721
Copy link
Collaborator

@kozlov721 kozlov721 commented Jan 25, 2025

Overview

Re-opening of #141

  • Added GhostFaceNetV2 network for embeddings learning together with corresponding losses, metrics and visualizer
    • ClosestIsPositiveAccuracy and MedianDistance metrics
    • Most losses from pytorch-metric-learning
    • EmbeddingVisualizer
  • Integrated the "metadata" labels introduced in #211
  • Groundwork for type checking of the metadata labels and better automatic inference of correct tasks

Example:

from luxonis_train.enums import Metadata

class Node(BaseNode):
    tasks = [Metadata("id")]

class Loss(BaseLoss):
    supported_tasks = [Metadata("id")]

The specific name of the metadata field can be overridden in the config file using metadata_task_override field.

- name: GhostFaceNetHead
  alias: color-embeddings
  metadata_task_override: color

This will cause the model to look for "task_name/metadata/color" labels instead of "task_name/metadata/id"

Open questions

Predefined model

There are a lot of options to specify for the embedding models, we support:

  • 21 losses
  • 5 distance measures
  • 10 miners
  • 9 reducers
  • 5 regularizers

It would be good to do a large benchmarking on some real dataset to determine good combinations that we can use in the predefined models

Visualizer

Visualizing the embedding results is challenging. The current implementation uses PCA to reduce the dimensionality to 2d and then plot the points using seaborn.scatterplot and seaborn.kdeplot so we can see whether the points representing different embedding classes are getting grouped together.

This works somewhat good for tasks with just a few embedding classes, but is not great for anything more complex

  • The plots get very convoluted and not really readable
  • Visualizers are called per batch, but with many embedding classes the batch will usually contain only one example per class
    • We can use a little hack to accumulate the batches (the commented code in the PR)
    • We could generalize the visualizers so they work like metrics with reset, update, and compute methods which would allow natural batch accumulation
      • This would also allow visualizing closest matches using database and query images
      • Could be eventually useful for other visualizers as well

Comparison of simple embeddings of vehicle images based on color (red, green, blue)

Untrained model on top, trained on the bottom

image

Dataset with many classes (VeRI dataset)

Untrained model on the right, trained on the left

image

CaptainTrojan and others added 30 commits December 6, 2024 18:54
…earning (loss). Added custom reID/embedding metrics. Implemented a test to verify trainability, exportability and inference.

Removed GhostFaceNetsV2 from the backbone tests as it only generates embeddings instead of the usual features.
…locks and variants. Added tests for all supported pytorch metric learning losses.
@kozlov721 kozlov721 requested a review from a team as a code owner January 25, 2025 03:28
@kozlov721 kozlov721 requested review from klemen1999, tersekmatija and conorsim and removed request for a team January 25, 2025 03:28
@github-actions github-actions bot added the enhancement New feature or request label Jan 25, 2025
@github-actions github-actions bot added the documentation Improvements or additions to documentation label Jan 25, 2025
Copy link
Collaborator

@klemen1999 klemen1999 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally LGTM

On the open questions:

Predefined Model

  • I think currently predefined model can use the same setup as it is definened in the embeddings_model.yaml (same loss, miner, distance, reducer, etc.). This was used before already for a successful training and works. But agree, we should also test out others and give some pointers in the docs which combination is better for specific cases - this can be addressed as a separate PR IMO

Visualizer

I like the second idea of generalizing visualizers to same interface as we have for metrics. I agree that longterm this would be the best solution. I wouldn't make this a blocking requirement but we can note internally as a nice to have improvement.

And for the visualization of embedding models for datasets with a lot of classes I think to some degree this is still unsolved problem. There are multiple methods for reducing dimensionality (PCA, t-SNE, UMAP, etc.) and we likely can't catch all as the best one is usually decided case by case. But we could provide several options to choose from. But also nice-to-have, for initial implementation current one is nice.

@kozlov721 kozlov721 mentioned this pull request Jan 28, 2025
@kozlov721 kozlov721 merged commit 3b68c9b into main Jan 29, 2025
5 of 6 checks passed
@kozlov721 kozlov721 deleted the feature/embeddings-models branch January 29, 2025 00:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants