<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Image embeddings in BigQuery for image similarity and clustering tasks
This notebook shows how to do use a pre-trained embedding as a vector representation of an image in Google Cloud Storage.
Given this embedding, we can load it as a BQ-ML model and then carry out document similarity or clustering.
This notebook accompanies the following Medium blog post
Step1:  Embedding model for images
We're going to use the EfficientNets model trained on ImageNet. It is compact and trained on a large variety of real-world images.
Step2:  The model on TensorFlow Hub expects images of a certain size, and provided as normalized arrays. 
So, we'll define a serving function that carries out the necessary reading and preprocessing of the images.
Step3:  Loading model into BigQuery
Since we saved the model in SavedModel format into GCS it is straightforward to load it into BigQuery
Let's load the model into a BigQuery dataset named advdata (create it if necessary)
Step4:  From the BigQuery web console, click on "schema" tab for the newly loaded model. You will see that the input is a string called filename and the output is called output_0.  The model is computationally expensive.
<ASSISTANT_TASK:>
Python Code:
BUCKET='ai-analytics-solutions-kfpdemo'  # CHANGE to a bucket you own
Explanation: Image embeddings in BigQuery for image similarity and clustering tasks
This notebook shows how to do use a pre-trained embedding as a vector representation of an image in Google Cloud Storage.
Given this embedding, we can load it as a BQ-ML model and then carry out document similarity or clustering.
This notebook accompanies the following Medium blog post:
End of explanation
import tensorflow as tf
import tensorflow_hub as tfhub
import os
model = tf.keras.Sequential()
model.add(tf.keras.Input(shape=[None,None,3]))
model.add(tfhub.KerasLayer("https://tfhub.dev/google/efficientnet/b4/feature-vector/1", name='image_embeddings'))
model.summary()
Explanation: Embedding model for images
We're going to use the EfficientNets model trained on ImageNet. It is compact and trained on a large variety of real-world images.
End of explanation
@tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
def serve(filename):
    img = tf.io.read_file(filename[0])
    img = tf.io.decode_image(img, channels=3)
    img = tf.cast(img, tf.float32) / 255.0
    #img = tf.image.resize(img, [380, 380])
    return model(img)
path='gs://{}/effnet_image_embedding'.format(BUCKET)
tf.saved_model.save(model, path, signatures={'serving_default': serve})
!saved_model_cli show --all --dir gs://$BUCKET/effnet_image_embedding
Explanation: The model on TensorFlow Hub expects images of a certain size, and provided as normalized arrays. 
So, we'll define a serving function that carries out the necessary reading and preprocessing of the images.
End of explanation
%%bigquery
CREATE OR REPLACE MODEL advdata.effnet_image_embed
OPTIONS(model_type='tensorflow', model_path='gs://ai-analytics-solutions-kfpdemo/effnet_image_embedding/*')
Explanation: Loading model into BigQuery
Since we saved the model in SavedModel format into GCS it is straightforward to load it into BigQuery
Let's load the model into a BigQuery dataset named advdata (create it if necessary)
End of explanation
%%bigquery
SELECT output_0 FROM
ML.PREDICT(MODEL advdata.effnet_image_embed,(
SELECT 'gs://gcs-public-data--met/634108/0.jpg' AS filename))
Explanation: From the BigQuery web console, click on "schema" tab for the newly loaded model. You will see that the input is a string called filename and the output is called output_0.  The model is computationally expensive.
End of explanation
<END_TASK>