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

Add image annotate example #1687

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions examples/image_annotate/definitions/all_imagesets.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
config {
type: "operations",
hasOutput: true
}

-- Create the object table for the images
CREATE EXTERNAL TABLE IF NOT EXISTS
${self()}
WITH CONNECTION `us.gcs` OPTIONS( object_metadata = 'SIMPLE',
uris = ['${constants.IMAGE_BUCKET}'] );
25 changes: 25 additions & 0 deletions examples/image_annotate/definitions/annotated_imagesets.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
config {
type: "incremental"
}

SELECT
*
FROM
${ref("object_table")}
WHERE
ml_annotate_image_status NOT LIKE "${constants.RETRYABLE_ERROR}"
${
when(incremental(), `AND
${constants.MERGE_COLUMN} NOT IN (
SELECT
${constants.MERGE_COLUMN}
FROM
${self()})
OR updated > (
Copy link
Contributor

Choose a reason for hiding this comment

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

Where is field coming from? I don't see it listed in the output of ML.ANNOTATE_IMAGE

https://cloud.google.com/bigquery/docs/reference/standard-sql/bigqueryml-syntax-annotate-image

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, why do you need that bit given the above?

SELECT
MAX(updated)
FROM
${self()})`)
}
LIMIT
${constants.BATCH_SIZE}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
config {
type: "assertion"
}

SELECT
*
FROM
${ref("object_table")}
WHERE
ml_annotate_image_status LIKE "${constants.RETRYABLE_ERROR}"
10 changes: 10 additions & 0 deletions examples/image_annotate/definitions/object_table.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
config {
type: "table",
}

SELECT
*
FROM
ML.ANNOTATE_IMAGE( MODEL ${ref("vision_model")},
TABLE ${ref("unannotated_imagesets")},
STRUCT(['LABEL_DETECTION'] AS vision_features))
19 changes: 19 additions & 0 deletions examples/image_annotate/definitions/unannotated_imagesets.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
config {
type: "view"
}

js {
// Ref is avoided here to avoid the circular dependency chain.
const ANNOTATED_IMAGESETS = `${ctx.schema()}` + ".annotated_imagesets"
}

SELECT
*
FROM
${ref("all_imagesets")}
LEFT JOIN
Copy link
Contributor

Choose a reason for hiding this comment

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

WHERE uri NOT IN (SELECT uri FROM ${ANNOTATED_IMAGESETS}) is probably easier to read. Not sure if more or less efficient though

`${ANNOTATED_IMAGESETS}`
ON
${ref("all_imagesets")}.uri = `${ANNOTATED_IMAGESETS}`.uri
WHERE
`${ANNOTATED_IMAGESETS}`.uri IS NULL
9 changes: 9 additions & 0 deletions examples/image_annotate/definitions/vision_model.sqlx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
config {
type: "operations",
hasOutput: true
}

-- Model for connecting to the Vision API
CREATE MODEL IF NOT EXISTS
${self()} REMOTE
WITH CONNECTION `us.gcs` OPTIONS (remote_service_type = 'cloud_ai_vision_v1');
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions examples/image_annotate/includes/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
BATCH_SIZE: 100,
MERGE_COLUMN: "uri",
RETRYABLE_ERROR: "A retryable error occurred:",
IMAGE_BUCKET: "gs://example-bucket/imagesets/*"
};
9 changes: 9 additions & 0 deletions examples/image_annotate/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Image Annotate

Use BigQuery ML to annotate images from a GCS bucket.

Images where annotation failed are filtered out, and retried in subsequent runs.

![Image Annotation DAG](./image-annotation-dag.png?raw=true "Image Annotation DAG")

<!-- TODO(ekrekr): add more info on setting up BigQuery connections. -->
4 changes: 4 additions & 0 deletions examples/image_annotate/workflow_settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defaultProject: dataform-demos
defaultLocation: US
defaultDataset: sample
defaultAssertionDataset: sample_assertions
Loading