Files
photoprism/internal/ai/tensorflow/image_test.go
raystlin 519a6ab34a AI: Add TensorFlow model shape detection #127 #5164
* AI: Added support for non BHWC models

Tensorflow models use BHWC by default, however, if we are using
converted models, we can find that the expected input is BCHW. Now the
input is configurable (although the restriction of being dimesion 4 is
still there) via Shape parameter on the input definition. Also, the
model instrospection will try to deduce the input shape from the model
signature.

* AI: Added more tests for enum parsing

ShapeComponent was missing from the tests

* AI: Modified external tests to the new url

The path has been moved from tensorflow/vision to tensorflow/models

* AI: Moved the builder to the model to reuse it

It should reduce the amount of allocations done

* AI: fixed errors after merge

Mainly incorrect paths and duplicated variables
2025-08-16 15:55:59 +02:00

59 lines
1.4 KiB
Go

package tensorflow
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wamuir/graft/tensorflow"
)
var defaultImageInput = &PhotoInput{
Height: 224,
Width: 224,
Shape: DefaultPhotoInputShape(),
}
var examplesPath = filepath.Join(assetsPath, "examples")
func TestConvertValue(t *testing.T) {
result := convertValue(uint32(98765432), &Interval{Start: -1, End: 1})
assert.Equal(t, float32(3024.8982), result)
}
func TestConvertStdMean(t *testing.T) {
mean := float32(1.0 / 127.5)
stdDev := float32(-1.0)
result := convertValue(uint32(98765432), &Interval{Mean: &mean, StdDev: &stdDev})
assert.Equal(t, float32(3024.8982), result)
}
func TestImageFromBytes(t *testing.T) {
t.Run("CatJpeg", func(t *testing.T) {
imageBuffer, err := os.ReadFile(examplesPath + "/cat_brown.jpg")
if err != nil {
t.Fatal(err)
}
result, err := ImageFromBytes(imageBuffer, defaultImageInput, nil)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tensorflow.DataType(0x1), result.DataType())
assert.Equal(t, int64(1), result.Shape()[0])
assert.Equal(t, int64(224), result.Shape()[2])
})
t.Run("Document", func(t *testing.T) {
imageBuffer, err := os.ReadFile(examplesPath + "/Random.docx")
assert.Nil(t, err)
result, err := ImageFromBytes(imageBuffer, defaultImageInput, nil)
assert.Empty(t, result)
assert.EqualError(t, err, "image: unknown format")
})
}