Skip to content

[optimizing] vectorize, cache and precompute#49

Merged
davidfischer-ch merged 15 commits intosmartcrop:masterfrom
derVedro:vectorize-cache-and-precompute
Feb 16, 2026
Merged

[optimizing] vectorize, cache and precompute#49
davidfischer-ch merged 15 commits intosmartcrop:masterfrom
derVedro:vectorize-cache-and-precompute

Conversation

@derVedro
Copy link
Contributor

@derVedro derVedro commented Feb 4, 2026

Vectorized importance map: The get_importance() method now generates a composite weighting map using numpy, replacing the slow per-pixel computations with efficient array operations.

Caching for repeated calculations: Importance maps are cached per crop dimensions, avoiding redundant computations across multiple crop candidates.

Precomputed features: The new precompute_features() method consolidates scaling, biasing, and weighting operations into a single, vectorized step. This eliminates repetitive calculations during scoring.

Other small things:

  • added docstrings
  • vectorize debug_crop()

@derVedro derVedro marked this pull request as draft February 4, 2026 02:02
@derVedro derVedro marked this pull request as ready for review February 5, 2026 01:42
@coveralls
Copy link

coveralls commented Feb 13, 2026

Pull Request Test Coverage Report for Build 21993256552

Details

  • 47 of 60 (78.33%) changed or added relevant lines in 2 files are covered.
  • 16 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.6%) to 80.392%

Changes Missing Coverage Covered Lines Changed/Added Lines %
smartcrop/library.py 45 58 77.59%
Files with Coverage Reduction New Missed Lines %
library.py 16 90.64%
Totals Coverage Status
Change from base Build 21250558729: 0.6%
Covered Lines: 328
Relevant Lines: 408

💛 - Coveralls

@derVedro
Copy link
Contributor Author

@davidfischer-ch so, that part is done, what do you think about it?

@davidfischer-ch
Copy link
Member

This looks great,

I am interested in getting some speed, CPU and memory usage metrics.

For example you may run this and previous version using /usr/bin/time -v (...) on some pictures and show the results?

Thank you.

@derVedro
Copy link
Contributor Author

derVedro commented Feb 15, 2026

For example you may run this and previous version using /usr/bin/time -v (...) on some pictures and show the results?

I already use some kind of benchmark on project's test pix. Here a gist with the code. If you want to run it, just put it into the project directory, create an 'othercrops' directory with directories of other versions of SmartCrop (or just import somehow another SmartCrop from somewhere and fix the import).

Here are the timings
Task                             [b] base        mine   Δ time  speedup
-----------------------------------------------------------------------
business-work-1.jpg (100x400)   1.062696s   0.048699s  -95.42%  +21.82x
business-work-1.jpg (300x300)   0.310685s   0.038552s  -87.59%   +8.06x
business-work-1.jpg (350x50)    0.163094s   0.031437s  -80.72%   +5.19x
nature-1.jpg (100x400)          1.174951s   0.175214s  -85.09%   +6.71x
nature-1.jpg (300x300)          0.414470s   0.166441s  -59.84%   +2.49x
nature-1.jpg (350x50)           0.286055s   0.154219s  -46.09%   +1.85x
orientation.jpg (100x400)       0.919778s   0.127922s  -86.09%   +7.19x
orientation.jpg (300x300)       0.292235s   0.114656s  -60.77%   +2.55x
orientation.jpg (350x50)        0.277473s   0.110509s  -60.17%   +2.51x
travel-1.jpg (100x400)          1.067218s   0.067895s  -93.64%  +15.72x
travel-1.jpg (300x300)          0.305897s   0.055855s  -81.74%   +5.48x
travel-1.jpg (350x50)           0.175847s   0.048551s  -72.39%   +3.62x

average times of 5 shuffeled runs
3 cases: 100x400, 300x300, 350x50 crops
[b] base - current version of SmartCrop from github
mine - this PR version
Δ time means "mine took less time in respect to base"

If you're interested in memory usage, I haven't measured anything yet, but my gut feeling is that there aren't that many differences. Nothing crazy happens in the code, a few importance maps are cached, but it's not a thing at all.

@davidfischer-ch davidfischer-ch merged commit 05df417 into smartcrop:master Feb 16, 2026
3 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