AI: Allow configuration of custom classification models - #5011
Conversation
These new functions allows us to inspect the saved models, get the tags and try to guess the inputs and outputs.
By inspecting existing models we saw that many times logits are returned instead of probabilities. Photoprism uses probabilites to rank the quality of the results, so we need to transform those logits. Our approach is to add a new layer at runtime to the graph that performs the softmax operation.
EnsureShape operation may seem unnecesary, but PredictCosts fails it it is not added to the graph.
Vision input parameters have also been changed to support the new parameters needed for the models.
nsfw/model.go and vision/model.go modified to make it work. Testing remains undone.
Now when loading labels internal/ai/tensorflow package will try to look for all the files that match the glob label*.txt and will return the labels that match the expected number. Some models add a first label called background, which is a bias. Also, a new parameter has been added to models to allow a second path to look for the label files. This path is set to nasnet asset on internal/ai/vision.
They were broken when the constructors changed to include metadata.
The tests has a envar check to avoid running it by default, as it downloads the models and generates a lot of errors.
This parameter allows us to rescale the input of the models because some of them need values between [0, 1] and other between [-1, 1].
The definition of the models now contain the expected resolution. This has improved the results for efficientnet-m.
As we are decompressing, we have to check the paths before writing to disk.
It seems to be standarized, so it is now used as an additional check for input signatures.
* vision_list command failed silently for models without meta. * face model panicked for models without meta.
The test was broken because of the new parameters
| MODEL_HASH="f18b801354e95cade497b4f12e8d2537d04c04f6 $MODEL_ZIP" | ||
| MODEL_VERSION="$MODEL_PATH/version.txt" | ||
| MODEL_BACKUP="storage/backup/nasnet-$TODAY" | ||
| MODEL_21K_LABELS_URL="https://dl.photoprism.app/tensorflow/vision/labels-imagenet21k.txt" |
There was a problem hiding this comment.
It probably makes sense to comment this out (or remove it) until 21k labels are actually supported? As I mentioned in my last issue comment, I believe it would be easier to generate CLIP embeddings and implement a search with those than to generate 21,000 different labels suitable only for regular text-based searches?
There was a problem hiding this comment.
Yes, you are right. I modified the script so as to have some kind of classification (although the rules were not being applied), but if we are thinking of alternative ways of doing the ruling, it makes more sense to remove this.
|
@raystlin Thanks a lot! That's awesome :) After taking a (very) quick look at your changes, I have a few questions regarding their intended scope and functionality:
Also, I'd like to ask the following just to be sure:
|
| Version string `yaml:"Version,omitempty" json:"version,omitempty"` | ||
| Prompt string `yaml:"Prompt,omitempty" json:"prompt,omitempty"` | ||
| Resolution int `yaml:"Resolution,omitempty" json:"resolution,omitempty"` | ||
| Meta *tensorflow.ModelInfo `yaml:"Meta,omitempty" json:"meta,omitempty"` |
There was a problem hiding this comment.
Having a vision.yml configuration example with your changes applied could help with testing. This would clarify what it should look like and enable us to easily compare it with the existing format and available options.
There was a problem hiding this comment.
Of course. I missed this one. This is the example I used for the transformer one, but I have to change it on the PR.
---
Models:
- Type: labels
Name: transformer
Version: Mobile
Resolution: 224
Meta:
Input:
Interval:
Start: -1.0
End: 1.0
Output:
OutputsLogits: true
- Type: nsfw
Name: Nsfw
Resolution: 224
- Type: face
Name: FaceNet
Resolution: 160
- Type: caption
Resolution: 224
Service:
Uri: http://photoprism-vision:5000/api/v1/vision/caption
FileScheme: https
RequestFormat: url
ResponseFormat: vision
Thresholds:
Confidence: 10
|
make fmt-go was applied
This version adds the new fields to the test configuration
|
@raystlin Sorry for not getting back to you sooner! I was traveling, but am now back in the office. Please let me know if you consider this stable enough for release. I will then merge your changes, perform final testing, and create a new preview build :) |
|
@raystlin All the unit tests are green, so that's great! As a general note, the inline comments should end with a period because they are used to automatically generate API documentation (not a big issue, though, because I can easily update them): For our end user and developer documentation, it would also be helpful to have more information on the newly added configuration options, as well as a list of the models that you have tested and found to work? Thank you very much! π€ |
Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>
|
@raystlin I've merged your changes with a few edits for further testing and documentation. Thank you so much for working on this! β€οΈ It would be good to have some specific configuration and usage examples ready for end users before releasing these improvements in the stable version. @graciousgrey and @omerdduran are happy to help with this. |
|
Sure, I will do it this afternoon. I will also bear it in mind for future
contributions.
|
|
You can now test these changes with the updated preview build: |
Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>
This also renames the Meta option to TensorFlow so it is clear these values are to configure TensorFlow models only. Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>
Signed-off-by: Michael Mayer <michael@photoprism.app>

This commit adds the possibility of using custom models for classification via
vision.ymlconfiguration file.The code will inspect the models and try to find their variable layout, inputs, number of outputs and so on, however, if the model returns
logitsinstead of probabilities it must be specified on the configuration to add asoftmaxlayer after the last layer of the model.Notes
download-nasnet.shscript has been modified to includelabels21k.txttonasnetpath, so as to have a default implementation of 21k labels, although the rules for them have not been written yet.internal/ai/classify/model_external_test.gohas been set as optional via an environment var, as it is not an acceptance but one to perform on test photos against external models.Related Issues