Files
photoprism/internal/ai/tensorflow/image_test.go
raystlin adc4dc0f74 Added new parameters to model input.
New parameters have been added to define the input of the models:
* ResizeOperation: by default center-crop was being performed, now it is
  configurable.
* InputOrder: by default RGB was being used as the order for the array
  values of the input tensor, now it can be configured.
* InputInterval has been changed to InputIntervals (an slice). This
  means that every channel can have its own interval conversion.
* InputInterval can define now stddev and mean, because sometimes
  instead of adjusting the interval, the stddev and mean of the training
data should be use.
2025-07-15 13:31:31 +00:00

56 lines
1.4 KiB
Go

package tensorflow
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/wamuir/graft/tensorflow"
"github.com/photoprism/photoprism/pkg/fs"
)
var defaultImageInput = &PhotoInput{
Height: 224,
Width: 224,
}
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) {
var assetsPath = fs.Abs("../../../assets")
var examplesPath = assetsPath + "/examples"
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)
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)
assert.Empty(t, result)
assert.EqualError(t, err, "image: unknown format")
})
}