Skip to content

Conversation

@thomasmarchioro3
Copy link
Contributor

This PR addresses some minor fixes in the SMoG example scripts and notebook (examples/notebooks/pytorch/smog.ipynb, examples/notebooks/pytorch_lightning/smog.ipynb, examples/pytorch/smog.py, examples/pytorch_lightning/smog.py):

OS and enviroment:

  • Ubuntu 24.04.2 LTS
  • Python 3.12.7
  • lightly==1.5.22
  • scikit-learn==1.7.1
  • torch==2.7.1
  • pytorch-lightning==2.5.2

Fix 1: Missing scikit-learn requirement

  • Issue: scikit-learn was not listed in the example dependencies, leading to ImportError when running the code.
  • Fix: Added scikit-learn>=1.7.1 to the example installation instructions.
    Note: Consider also adding this to the default dependencies in pyproject.toml.

Fix 2: KMeans ValueError due to incorrect tensor shape

  • Issue: The KMeans call fails with:
kmeans = KMeans(self.n_groups).fit(features) raises
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 ValueError: n_samples=128 should be >= n_clusters=300.

caused by an incorrect array shape — features were shaped (num_features, memory_size) instead of (memory_size, num_features).

  • Fix: Corrected array shape by transposing the feature array:
features = features.cpu().numpy().T

Fix 3: Incorrect import in pytorch_lightning examples

  • Issue: Outdated MemoryBankModule + missing feature shape
self.memory_bank = loss.memory_bank.MemoryBankModule(size=memory_bank_size)
  • Fix: Used correct module (same as pytorch examples)

Additional minor fixes

  • Added #type: ignore when converting features from tensor to numpy array to avoid warnings from the language server

@liopeer
Copy link
Contributor

liopeer commented Aug 7, 2025

Hey @thomasmarchioro3 and thank you for the contribution! We will have a look at your PR in the next few days :)

@codecov
Copy link

codecov bot commented Aug 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (master@1d2eb09). Learn more about missing BASE report.
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1876   +/-   ##
=========================================
  Coverage          ?   86.07%           
=========================================
  Files             ?      167           
  Lines             ?     6966           
  Branches          ?        0           
=========================================
  Hits              ?     5996           
  Misses            ?      970           
  Partials          ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yutong-xiang-97
Copy link
Contributor

yutong-xiang-97 commented Aug 13, 2025

Hi @thomasmarchioro3, thank you for your contribution!

Quick note: in example/ the notebooks are auto-generated from the example scripts for pytorch, pytorch-lightning, and pytorch-lightning-distributed. So whenever you add or modify examples, make sure that:

  1. you add or update the script examples for all three of them, then
  2. generate corresponding notebooks with make generate-example-notebooks following our contributing guide: https://github.com/lightly-ai/lightly/blob/master/CONTRIBUTING.md

Then the "check example notebooks" test will pass.

Copy link
Contributor

@yutong-xiang-97 yutong-xiang-97 left a comment

Choose a reason for hiding this comment

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

Great catch in finding the dimension mistake!

For scikit-learn dependency, your solution is sufficient since SMoG is not officially added to LightlySSL yet, and it is not used anywhere else.

@thomasmarchioro3
Copy link
Contributor Author

Thanks for the suggestions! I hadn't notice that the feature tensor was being transposed in _reset_group_features.

I also have removed the # type: ignore. It causes some warnings from LSPs (like Pylance or Pyright) but that's not an actual issue, since examples are not type checked.

Copy link
Contributor

@yutong-xiang-97 yutong-xiang-97 left a comment

Choose a reason for hiding this comment

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

Approved!

@yutong-xiang-97 yutong-xiang-97 merged commit e201e00 into lightly-ai:master Aug 14, 2025
15 checks passed
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.

3 participants