2020-01-09 01:21:09 +01:00
|
|
|
package classify
|
2018-09-14 12:44:15 +02:00
|
|
|
|
|
|
|
import (
|
2019-04-29 20:09:10 +02:00
|
|
|
"io/ioutil"
|
2018-09-14 12:44:15 +02:00
|
|
|
"testing"
|
2018-10-31 07:14:33 +01:00
|
|
|
|
2019-12-11 14:10:20 +01:00
|
|
|
tensorflow "github.com/tensorflow/tensorflow/tensorflow/go"
|
|
|
|
|
2019-05-06 23:18:10 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2018-10-31 07:14:33 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-14 12:44:15 +02:00
|
|
|
)
|
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
func TestTensorFlow_LabelsFromFile(t *testing.T) {
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("chameleon_lime.jpg", func(t *testing.T) {
|
2019-07-17 10:48:23 +02:00
|
|
|
conf := config.TestConfig()
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
result, err := tensorFlow.File(conf.ExamplesPath() + "/chameleon_lime.jpg")
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Nil(t, err)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Log(err.Error())
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotNil(t, result)
|
|
|
|
assert.IsType(t, Labels{}, result)
|
|
|
|
assert.Equal(t, 1, len(result))
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Log(result)
|
|
|
|
|
|
|
|
assert.Equal(t, "chameleon", result[0].Name)
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, 7, result[0].Uncertainty)
|
|
|
|
})
|
|
|
|
t.Run("not existing file", func(t *testing.T) {
|
|
|
|
conf := config.TestConfig()
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2018-09-14 12:44:15 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
result, err := tensorFlow.File(conf.ExamplesPath() + "/notexisting.jpg")
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Contains(t, err.Error(), "no such file or directory")
|
|
|
|
assert.Empty(t, result)
|
|
|
|
})
|
2018-09-14 12:44:15 +02:00
|
|
|
}
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-06-04 18:26:35 +02:00
|
|
|
func TestTensorFlow_Labels(t *testing.T) {
|
2019-04-29 20:09:10 +02:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping test in short mode.")
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("chameleon_lime.jpg", func(t *testing.T) {
|
2019-07-17 10:48:23 +02:00
|
|
|
conf := config.TestConfig()
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/chameleon_lime.jpg"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Log(result)
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.NotNil(t, result)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.IsType(t, Labels{}, result)
|
|
|
|
assert.Equal(t, 1, len(result))
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, "chameleon", result[0].Name)
|
2019-04-29 20:09:10 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, 100-93, result[0].Uncertainty)
|
|
|
|
}
|
|
|
|
})
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("dog_orange.jpg", func(t *testing.T) {
|
2019-07-17 10:48:23 +02:00
|
|
|
conf := config.TestConfig()
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/dog_orange.jpg"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
t.Log(result)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.NotNil(t, result)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.IsType(t, Labels{}, result)
|
2019-12-16 20:22:46 +01:00
|
|
|
assert.Equal(t, 1, len(result))
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-12-13 03:07:26 +01:00
|
|
|
assert.Equal(t, "dog", result[0].Name)
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
assert.Equal(t, 34, result[0].Uncertainty)
|
|
|
|
}
|
|
|
|
})
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("Random.docx", func(t *testing.T) {
|
2019-07-17 10:48:23 +02:00
|
|
|
conf := config.TestConfig()
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-04-30 13:17:01 +02:00
|
|
|
|
2019-07-17 10:48:23 +02:00
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/Random.docx"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
assert.Contains(t, err.Error(), "invalid image")
|
|
|
|
}
|
|
|
|
})
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("6720px_white.jpg", func(t *testing.T) {
|
2019-07-17 10:48:23 +02:00
|
|
|
conf := config.TestConfig()
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 10:48:23 +02:00
|
|
|
|
|
|
|
if imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/6720px_white.jpg"); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
result, err := tensorFlow.Labels(imageBuffer)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTensorFlow_LoadModel(t *testing.T) {
|
|
|
|
t.Run("model path exists", func(t *testing.T) {
|
2020-01-06 02:14:17 +01:00
|
|
|
conf := config.TestConfig()
|
2019-07-17 10:48:23 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 10:48:23 +02:00
|
|
|
|
|
|
|
result := tensorFlow.loadModel()
|
|
|
|
assert.Nil(t, result)
|
|
|
|
})
|
|
|
|
t.Run("model path does not exist", func(t *testing.T) {
|
|
|
|
conf := config.NewTestErrorConfig()
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 10:48:23 +02:00
|
|
|
|
|
|
|
result := tensorFlow.loadModel()
|
|
|
|
assert.Contains(t, result.Error(), "Could not find SavedModel")
|
|
|
|
})
|
2019-04-29 20:09:10 +02:00
|
|
|
}
|
2019-07-17 10:59:19 +02:00
|
|
|
|
2019-07-17 11:52:26 +02:00
|
|
|
func TestTensorFlow_BestLabels(t *testing.T) {
|
|
|
|
t.Run("labels not loaded", func(t *testing.T) {
|
2020-01-06 02:14:17 +01:00
|
|
|
conf := config.TestConfig()
|
2019-07-17 11:52:26 +02:00
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 11:52:26 +02:00
|
|
|
|
|
|
|
p := make([]float32, 1000)
|
|
|
|
|
|
|
|
p[666] = 0.5
|
|
|
|
|
|
|
|
result := tensorFlow.bestLabels(p)
|
|
|
|
assert.Empty(t, result)
|
|
|
|
})
|
2019-07-17 12:22:50 +02:00
|
|
|
t.Run("labels loaded", func(t *testing.T) {
|
2020-01-06 02:14:17 +01:00
|
|
|
conf := config.TestConfig()
|
2019-07-17 11:52:26 +02:00
|
|
|
path := conf.TensorFlowModelPath()
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 11:52:26 +02:00
|
|
|
tensorFlow.loadLabels(path)
|
|
|
|
|
|
|
|
p := make([]float32, 1000)
|
|
|
|
|
|
|
|
p[8] = 0.7
|
|
|
|
p[1] = 0.5
|
|
|
|
|
|
|
|
result := tensorFlow.bestLabels(p)
|
2019-12-15 18:36:27 +01:00
|
|
|
assert.Equal(t, "chicken", result[0].Name)
|
2019-07-17 11:52:26 +02:00
|
|
|
assert.Equal(t, "bird", result[0].Categories[0])
|
2019-12-28 23:06:44 +01:00
|
|
|
assert.Equal(t, "animal", result[1].Categories[1])
|
2019-07-17 11:52:26 +02:00
|
|
|
assert.Equal(t, "image", result[0].Source)
|
2019-12-16 20:22:46 +01:00
|
|
|
assert.Equal(t, "fish", result[1].Name)
|
2019-07-17 11:52:26 +02:00
|
|
|
assert.Equal(t, "image", result[1].Source)
|
|
|
|
t.Log(result)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 12:22:50 +02:00
|
|
|
func TestTensorFlow_MakeTensor(t *testing.T) {
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("cat_brown.jpg", func(t *testing.T) {
|
2019-07-17 12:22:50 +02:00
|
|
|
conf := config.TestConfig()
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 12:22:50 +02:00
|
|
|
|
|
|
|
imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/cat_brown.jpg")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
result, err := tensorFlow.makeTensor(imageBuffer, "jpeg")
|
|
|
|
assert.Equal(t, tensorflow.DataType(0x1), result.DataType())
|
|
|
|
assert.Equal(t, int64(1), result.Shape()[0])
|
|
|
|
assert.Equal(t, int64(224), result.Shape()[2])
|
|
|
|
})
|
2020-01-09 01:21:09 +01:00
|
|
|
t.Run("Random.docx", func(t *testing.T) {
|
2019-07-17 12:22:50 +02:00
|
|
|
conf := config.TestConfig()
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
tensorFlow := New(conf.ResourcesPath(), conf.TensorFlowDisabled())
|
2019-07-17 12:22:50 +02:00
|
|
|
|
|
|
|
imageBuffer, err := ioutil.ReadFile(conf.ExamplesPath() + "/Random.docx")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
result, err := tensorFlow.makeTensor(imageBuffer, "jpeg")
|
|
|
|
assert.Empty(t, result)
|
|
|
|
assert.Equal(t, "image: unknown format", err.Error())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-17 11:52:26 +02:00
|
|
|
func Test_ConvertTF(t *testing.T) {
|
|
|
|
result := convertTF(uint32(98765432))
|
|
|
|
assert.Equal(t, float32(3024.898), result)
|
|
|
|
}
|