Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-2380 Check if labels file is split by spaces or commas #2178

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .artifact/tree.json
Original file line number Diff line number Diff line change
Expand Up @@ -51473,6 +51473,10 @@
"hash": "90790cb342b79585a67dc2e4ad5d8be6",
"size": 88663
},
"lorem.txt": {
"hash": "f668c7c4651f39df473a8355fef9fa38",
"size": 1162
},
"object_classifier.tflite": {
"hash": "947c25596dab54a519e08a3d7dfaf6fd",
"size": 4276352
Expand Down
7 changes: 7 additions & 0 deletions services/vision/builtin/tflite_classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"go.viam.com/rdk/vision/classification"
)

var LABEL_OUTPUT_MISMATCH = errors.New("Invalid Label File: Number of labels does not match number of model outputs. Labels must be separated by a newline, comma or space.")

// TFLiteClassifierConfig specifies the fields necessary for creating a TFLite classifier.
type TFLiteClassifierConfig struct {
// this should come from the attributes part of the classifier config
Expand Down Expand Up @@ -112,8 +114,13 @@ func unpackClassificationTensor(ctx context.Context, tensor []interface{},
default:
return nil, errors.New("output type not valid. try uint8 or float32")
}

out := make(classification.Classifications, 0, len(outConf))
if len(labels) > 0 {
if len(labels) != len(outConf) {
return nil, LABEL_OUTPUT_MISMATCH
}

for i, c := range outConf {
out = append(out, classification.NewClassification(c, labels[i]))
}
Expand Down
10 changes: 10 additions & 0 deletions services/vision/builtin/tflite_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ func loadLabels(filename string) ([]string, error) {
for scanner.Scan() {
labels = append(labels, scanner.Text())
}

// if the labels come out as one line, try splitting that line by spaces or commas to extract labels
if len(labels) == 1 {
labels = strings.Split(labels[0], ",")
}

if len(labels) == 1 {
labels = strings.Split(labels[0], " ")
}

return labels, nil
}

Expand Down
29 changes: 29 additions & 0 deletions services/vision/builtin/tflite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/edaniels/golog"
"github.com/nfnt/resize"
"go.viam.com/test"
"go.viam.com/utils/artifact"

Expand Down Expand Up @@ -254,3 +255,31 @@ func TestMoreClassifierModels(t *testing.T) {
test.That(t, bestClass[0].Label(), test.ShouldResemble, "292")
test.That(t, bestClass[0].Score(), test.ShouldBeGreaterThan, 0.93)
}

func TestInvalidLabels(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test of it successfully unpacking a label file where all the labels are on one string, comma separated?

ctx := context.Background()

pic, err := rimage.NewImageFromFile(artifact.MustPath("vision/tflite/redpanda.jpeg"))
test.That(t, err, test.ShouldBeNil)

modelLoc := artifact.MustPath("vision/tflite/mobilenetv2_class.tflite")
labelPath := artifact.MustPath("vision/classification/object_labels.txt")
numThreads := 2

labels, err := loadLabels(labelPath)
model, err := addTFLiteModel(ctx, modelLoc, &numThreads)
resizedImg := resize.Resize(100, 100, pic, resize.Bilinear)
outTensor, err := tfliteInfer(ctx, model, resizedImg)

classifications, err := unpackClassificationTensor(ctx, outTensor, model, labels)
test.That(t, err, test.ShouldResemble, LABEL_OUTPUT_MISMATCH)
test.That(t, classifications, test.ShouldBeNil)
}

func TestSpaceDelineatedLabels(t *testing.T) {
labelPath := artifact.MustPath("vision/classification/lorem.txt")

labels, err := loadLabels(labelPath)
test.That(t, err, test.ShouldBeNil)
test.That(t, len(labels), test.ShouldEqual, 10)
}