photoprism/internal/ai/classify
Michael Mayer 28eb11d468 TensorFlow: Trigger explicit GC to free C-allocated tensor memory #5394
Signed-off-by: Michael Mayer <michael@photoprism.app>
2025-12-23 12:06:26 +01:00
..
classify.go
const.go
gen.go
label.go
label_rule.go
label_rule_test.go
label_test.go
labels.go
labels_test.go
model.go TensorFlow: Trigger explicit GC to free C-allocated tensor memory #5394 2025-12-23 12:06:26 +01:00
model_external_test.go
model_test.go
README.md TensorFlow: Trigger explicit GC to free C-allocated tensor memory #5394 2025-12-23 12:06:26 +01:00
rules.go
rules.yml
rules_test.go

PhotoPrism — Classification Package

Last Updated: December 23, 2025

Overview

internal/ai/classify wraps PhotoPrisms TensorFlow-based image classification (labels). It loads SavedModel classifiers (Nasnet by default), prepares inputs, runs inference, and maps output probabilities to label rules.

How It Works

  • Model Loading — The classifier loads a SavedModel under assets/models/<name> and resolves model tags and input/output ops (see vision.yml overrides for custom models).
  • Input Preparation — JPEGs are decoded and resized/cropped to the models expected input resolution.
  • Inference — The model outputs probabilities; Rules apply thresholds and priority to produce final labels.

Memory & Performance

TensorFlow tensors allocate C memory and are freed by Go GC finalizers. To keep RSS bounded during long runs, PhotoPrism periodically triggers garbage collection to return freed tensor memory to the OS. Tune with:

  • PHOTOPRISM_TF_GC_EVERY (default 200, 0 disables).
    Lower values reduce peak RSS but increase GC overhead and can slow indexing.

Troubleshooting Tips

  • Labels are empty: Verify the model labels file and that Rules thresholds are not too strict.
  • Model load failures: Ensure saved_model.pb and variables/ exist under the configured model path.
  • Unexpected outputs: Check TensorFlow.Input/Output settings in vision.yml for custom models.