Skip to content

Commit 34065a6

Browse files
committed
AI: Allow to use defaults when having a custom vision.yml #127 photoprism#5011
Signed-off-by: Michael Mayer <michael@photoprism.app>
1 parent b241fa6 commit 34065a6

4 files changed

Lines changed: 34 additions & 0 deletions

File tree

internal/ai/vision/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
// Model represents a computer vision model configuration.
2626
type Model struct {
2727
Type ModelType `yaml:"Type,omitempty" json:"type,omitempty"`
28+
Default bool `yaml:"Default,omitempty" json:"default,omitempty"`
2829
Name string `yaml:"Name,omitempty" json:"name,omitempty"`
2930
Version string `yaml:"Version,omitempty" json:"version,omitempty"`
3031
System string `yaml:"System,omitempty" json:"system,omitempty"`

internal/ai/vision/models.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
var (
1010
NasnetModel = &Model{
1111
Type: ModelTypeLabels,
12+
Default: true,
1213
Name: "nasnet",
1314
Version: VersionMobile,
1415
Resolution: 224,
@@ -39,6 +40,7 @@ var (
3940
}
4041
NsfwModel = &Model{
4142
Type: ModelTypeNsfw,
43+
Default: true,
4244
Name: "nsfw",
4345
Version: VersionLatest,
4446
Resolution: 224,
@@ -61,6 +63,7 @@ var (
6163
}
6264
FacenetModel = &Model{
6365
Type: ModelTypeFace,
66+
Default: true,
6467
Name: "facenet",
6568
Version: VersionLatest,
6669
Resolution: 160,

internal/ai/vision/options.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,33 @@ func (c *Options) Load(fileName string) error {
4545
return err
4646
}
4747

48+
// 1. Ensure that there is at least one configuration for each model type,
49+
// so that adding a copy of the default configuration to the vision.yml file
50+
// is not required. We could alternatively require a model to included in
51+
// the "vision.yml" file, but set the defaults if the "Default" flag is set.
52+
// 2. Use the default "Thresholds" if no custom thresholds are configured.
53+
54+
for i, model := range c.Models {
55+
if !model.Default {
56+
continue
57+
}
58+
59+
switch model.Type {
60+
case ModelTypeLabels:
61+
c.Models[i] = NasnetModel
62+
case ModelTypeNsfw:
63+
c.Models[i] = NsfwModel
64+
case ModelTypeFace:
65+
c.Models[i] = FacenetModel
66+
case ModelTypeCaption:
67+
c.Models[i] = CaptionModel
68+
}
69+
}
70+
71+
if c.Thresholds.Confidence <= 0 || c.Thresholds.Confidence > 100 {
72+
c.Thresholds.Confidence = DefaultThresholds.Confidence
73+
}
74+
4875
return nil
4976
}
5077

internal/ai/vision/testdata/vision.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Models:
22
- Type: labels
3+
Default: true
34
Name: nasnet
45
Version: mobile
56
Resolution: 224
@@ -19,6 +20,7 @@ Models:
1920
Name: predictions/Softmax
2021
Outputs: 1000
2122
- Type: nsfw
23+
Default: true
2224
Name: nsfw
2325
Version: latest
2426
Resolution: 224
@@ -33,6 +35,7 @@ Models:
3335
Name: nsfw_cls_model/final_prediction
3436
Outputs: 5
3537
- Type: face
38+
Default: true
3639
Name: facenet
3740
Version: latest
3841
Resolution: 160

0 commit comments

Comments
 (0)