-
Notifications
You must be signed in to change notification settings - Fork 111
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
DATA-3467: Cloud Inference CLI #4748
Open
vpandiarajan20
wants to merge
9
commits into
viamrobotics:main
Choose a base branch
from
vpandiarajan20:DATA-3467
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−10
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79a8b3f
first pass
vpandiarajan20 a3d684c
move some stuff
vpandiarajan20 fbb6b65
works, need to clean up output format
vpandiarajan20 d757dc9
working
vpandiarajan20 91e33ad
lint
vpandiarajan20 e9bbe3e
pr broken?
vpandiarajan20 648a3cd
address comments
vpandiarajan20 1b111f9
change command to viam infer
vpandiarajan20 92e169c
adjust comment
vpandiarajan20 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/urfave/cli/v2" | ||
v1 "go.viam.com/api/app/data/v1" | ||
mlinferencepb "go.viam.com/api/app/mlinference/v1" | ||
) | ||
|
||
const ( | ||
inferenceFlagFileOrgID = "file-org-id" | ||
inferenceFlagFileID = "file-id" | ||
inferenceFlagFileLocationID = "file-location-id" | ||
inferenceFlagModelOrgID = "model-org-id" | ||
inferenceFlagModelName = "model-name" | ||
inferenceFlagModelVersion = "model-version" | ||
) | ||
|
||
type mlInferenceInferArgs struct { | ||
OrgID string | ||
FileOrgID string | ||
FileID string | ||
FileLocationID string | ||
ModelOrgID string | ||
ModelName string | ||
ModelVersion string | ||
} | ||
|
||
// MLInferenceInferAction is the corresponding action for 'inference infer'. | ||
func MLInferenceInferAction(c *cli.Context, args mlInferenceInferArgs) error { | ||
client, err := newViamClient(c) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = client.mlRunInference( | ||
args.OrgID, args.FileOrgID, args.FileID, args.FileLocationID, | ||
args.ModelOrgID, args.ModelName, args.ModelVersion) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// mlRunInference runs inference on an image with the specified parameters. | ||
func (c *viamClient) mlRunInference(orgID, fileOrgID, fileID, fileLocation, modelOrgID, | ||
modelName, modelVersion string, | ||
) (*mlinferencepb.GetInferenceResponse, error) { | ||
if err := c.ensureLoggedIn(); err != nil { | ||
return nil, err | ||
} | ||
|
||
req := &mlinferencepb.GetInferenceRequest{ | ||
OrganizationId: orgID, | ||
BinaryId: &v1.BinaryID{ | ||
FileId: fileID, | ||
OrganizationId: fileOrgID, | ||
LocationId: fileLocation, | ||
}, | ||
RegistryItemId: fmt.Sprintf("%s:%s", modelOrgID, modelName), | ||
RegistryItemVersion: modelVersion, | ||
} | ||
|
||
resp, err := c.mlInferenceClient.GetInference(context.Background(), req) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "received error from server") | ||
} | ||
c.printInferenceResponse(resp) | ||
return resp, nil | ||
} | ||
|
||
// printInferenceResponse prints a neat representation of the GetInferenceResponse. | ||
func (c *viamClient) printInferenceResponse(resp *mlinferencepb.GetInferenceResponse) { | ||
printf(c.c.App.Writer, "Inference Response:") | ||
printf(c.c.App.Writer, "Output Tensors:") | ||
if resp.OutputTensors != nil { | ||
for name, tensor := range resp.OutputTensors.Tensors { | ||
printf(c.c.App.Writer, " Tensor Name: %s", name) | ||
printf(c.c.App.Writer, " Shape: %v", tensor.Shape) | ||
if tensor.Tensor != nil { | ||
var sb strings.Builder | ||
for i, value := range tensor.GetDoubleTensor().GetData() { | ||
if i > 0 { | ||
sb.WriteString(", ") | ||
} | ||
sb.WriteString(fmt.Sprintf("%.4f", value)) | ||
} | ||
printf(c.c.App.Writer, " Values: [%s]", sb.String()) | ||
} else { | ||
printf(c.c.App.Writer, " No values available.") | ||
} | ||
} | ||
} else { | ||
printf(c.c.App.Writer, " No output tensors.") | ||
} | ||
|
||
printf(c.c.App.Writer, "Annotations:") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we just include here the format [x_min, y_min, x_max, y_max] |
||
printf(c.c.App.Writer, "Bounding Box Format: [x_min, y_min, x_max, y_max]") | ||
if resp.Annotations != nil { | ||
for _, bbox := range resp.Annotations.Bboxes { | ||
printf(c.c.App.Writer, " Bounding Box ID: %s, Label: %s", | ||
bbox.Id, bbox.Label) | ||
printf(c.c.App.Writer, " Coordinates: [%f, %f, %f, %f]", | ||
bbox.XMinNormalized, bbox.YMinNormalized, bbox.XMaxNormalized, bbox.YMaxNormalized) | ||
if bbox.Confidence != nil { | ||
printf(c.c.App.Writer, " Confidence: %.4f", *bbox.Confidence) | ||
} | ||
} | ||
for _, classification := range resp.Annotations.Classifications { | ||
printf(c.c.App.Writer, " Classification Label: %s", classification.Label) | ||
if classification.Confidence != nil { | ||
printf(c.c.App.Writer, " Confidence: %.4f", *classification.Confidence) | ||
} | ||
} | ||
} else { | ||
printf(c.c.App.Writer, " No annotations.") | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You would know better than I would but is this the kind of format that you would want? Meaning, normally from a CLI I would expect JSON or something configurable that I can do programmatically. But I also know absolutely nothing about ML and this could be the way ML people want the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, this format isn't very good for most usecases, but it's readable. I think the primary use case of this command will be to debug / easily check if the service is functioning.