photoprism/internal/ai/tensorflow
2026-06-24 17:20:30 +02:00
..
testdata AI: Add TensorFlow model shape detection #127 #5164 2025-08-16 15:55:59 +02:00
gc.go TensorFlow: Trigger explicit GC to free C-allocated tensor memory #5394 2025-12-23 12:06:26 +01:00
image.go Clean-up: Drop imaging and pigo library integrations #5353 #5508 #668 2026-04-01 13:47:42 +02:00
image_test.go Clean-up: Drop imaging and pigo library integrations #5353 #5508 #668 2026-04-01 13:47:42 +02:00
info.go Go: Apply go fix modernizations across backend packages 2026-02-20 03:54:33 +01:00
info_test.go CI: Apply Go linter recommendations to "ai/tensorflow" package #5330 2025-11-22 11:47:17 +01:00
labels.go Backend: Clean up error handling and unit tests in internal/ packages 2026-02-09 12:00:33 +01:00
model.go CI: Apply Go linter recommendations to "ai/tensorflow" package #5330 2025-11-22 11:47:17 +01:00
model_test.go CI: Apply Go linter recommendations to "ai/tensorflow" package #5330 2025-11-22 11:47:17 +01:00
op.go CI: Apply Go linter recommendations to "ai/tensorflow" package #5330 2025-11-22 11:47:17 +01:00
README.md Vision/AI: Document FaceNet corruption/reinstall guidance 2026-03-03 14:41:05 +01:00
tensor_builder.go CI: Apply Go linter recommendations to "ai/tensorflow" package #5330 2025-11-22 11:47:17 +01:00
tensorflow.go Links: Use canonical trailing-slash form for website URLs 2026-06-24 17:20:30 +02:00
util.go CI: Apply Go linter recommendations to "ai/tensorflow" package #5330 2025-11-22 11:47:17 +01:00

PhotoPrism — TensorFlow Package

Last Updated: March 3, 2026

Overview

internal/ai/tensorflow provides the shared TensorFlow helpers used by PhotoPrisms built-in AI features (labels, NSFW, and FaceNet embeddings). It wraps SavedModel loading, input/output discovery, image tensor preparation, and label handling so higher-level packages can focus on domain logic.

Key Components

  • Model LoadingSavedModel, GetModelTagsInfo, and GetInputAndOutputFromSavedModel discover and load SavedModel graphs with appropriate tags.
  • Input PreparationImage, ImageTransform, and ImageTensorBuilder convert JPEG images to tensors with the configured resolution, color order, and resize strategy.
  • Output HandlingAddSoftmax can insert a softmax op when a model exports logits.
  • LabelsLoadLabels loads label lists for classification models.

Model Loading Notes

  • Built-in models live under assets/models/ and are accessed via helpers in internal/ai/vision and internal/ai/classify.
  • When a model lacks explicit tags or signatures, the helpers attempt to infer input/output operations. Logs will show when inference kicks in.
  • Classification models may emit logits; if ModelInfo.Output.Logits is true, a softmax op is injected at load time.

Memory & Garbage Collection

TensorFlow tensors are allocated in C memory and freed by Go GC finalizers in the TensorFlow bindings. Long-running inference can therefore show increasing RSS even when the Go heap is small. PhotoPrism periodically triggers garbage collection to return freed C-allocated tensor buffers to the OS. Control this behavior with:

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

Troubleshooting Tips

  • Model fails to load: Verify the SavedModel path, tags, and that saved_model.pb plus variables/ exist under assets/models/<name>.
  • FaceNet load error Read less bytes than requested: The local assets/models/facenet/saved_model.pb file is usually incomplete or corrupted. Remove cached/downloaded files and reinstall:
    • rm -f /tmp/photoprism/facenet.zip
    • rm -rf assets/models/facenet
    • make dep-tensorflow (or scripts/download-facenet.sh)
    • Re-run the face tests (go test ./internal/ai/face -run TestNet -count=1)
  • Input/output mismatch: Check logs for inferred inputs/outputs and confirm vision.yml overrides (name, resolution, and TensorFlow.Input/Output).
  • Unexpected probabilities: Ensure logits are handled correctly and labels match output indices.
  • High memory usage: Confirm PHOTOPRISM_TF_GC_EVERY is set appropriately; model weights remain resident for the life of the process by design.