model_path = "model"

!mkdir {model_path}
!cp -r tmp/Format-Serving/* {model_path}/import tensorflow as tf

model = tf.keras.models.load_model(model_path)test_image_path = tf.keras.utils.get_file(
    "test-image.png", "https://i.ibb.co/F58NjRq/test-image.png"
)

test_label_path = tf.keras.utils.get_file(
    "gt-image.png", "https://i.ibb.co/BjPbS6c/gt-image.png"
)import numpy as np
from tensorflow.keras.applications import mobilenet_v2

with open(test_image_path, "rb") as f:
    test_image = f.read()

with open(test_label_path, "rb") as f:
    gt_image = f.read()

test_image = tf.io.decode_png(test_image, channels=3)
test_image = tf.image.resize(test_image, size=(128, 128))
test_image = mobilenet_v2.preprocess_input(test_image)
test_gt = tf.io.decode_png(gt_image, channels=1)

input_numpy = np.array(test_image)
input_tensor = tf.convert_to_tensor(input_numpy)
input_tensor = input_tensor[tf.newaxis, ...]prediction = model.predict(input_tensor)
print(prediction.shape)

seg_mask = tf.math.argmax(prediction, -1)
seg_mask = tf.squeeze(seg_mask)
seg_maskimport matplotlib.pyplot as plt

f, axs = plt.subplots(1, 2)
f.set_figheight(10)
f.set_figwidth(20)

axs[0].set_title("Prediction", {"fontsize": 40})
axs[0].imshow(seg_mask)
axs[0].axis("off")
axs[1].set_title("Ground truth", {"fontsize": 40})
axs[1].imshow(tf.squeeze(test_gt, axis=2))
axs[1].axis("off")

plt.show()import google.auth # pip install -U google-auth
from google.auth.transport.requests import AuthorizedSession

import tensorflow as tf 
import base64
import jsonPROJECT_ID = "gcp-ml-172005"
REGION = "us-central1"
ENDPOINT_ID = "2220630858061053952"credentials, _ = google.auth.default()
service_endpoint = f"https://{REGION}-aiplatform.googleapis.com"
authed_session = AuthorizedSession(credentials)url = "{}/v1/projects/{}/locations/{}/endpoints/{}:predict".format(
    service_endpoint, PROJECT_ID, REGION, ENDPOINT_ID
)
print("Endpoint: ", url)serving_input = list(
    model.signatures["serving_default"].structured_input_signature[1].keys()
)[0]
print("Serving function input:", serving_input)with open(test_image_path, "rb") as f:
    image = f.read()

b64str = base64.urlsafe_b64encode(image).decode('utf-8')single_instance_request_body = {
    "instances": [
        {serving_input: b64str}
    ]
}

two_instances_request_body = {
    "instances": [
        {serving_input: b64str},
        {serving_input: b64str},
    ]
}response = authed_session.post(url, data=json.dumps(single_instance_request_body))
print(response)
print(response.content)import ast

response = response.content.decode('utf-8')
response = ast.literal_eval(response)

seg_mask_ve = tf.convert_to_tensor(response['predictions'])
seg_mask_ve = tf.squeeze(seg_mask_ve)
seg_mask_veimport matplotlib.pyplot as plt

f, axs = plt.subplots(1, 3)
f.set_figheight(10)
f.set_figwidth(30)

axs[0].set_title("Prediction(local)", {"fontsize": 40})
axs[0].imshow(seg_mask)
axs[0].axis("off")
axs[1].set_title("Prediction(vertex endpoint)", {"fontsize": 40})
axs[1].imshow(seg_mask_ve)
axs[1].axis("off")
axs[2].set_title("Ground truth", {"fontsize": 40})
axs[2].imshow(tf.squeeze(test_gt, axis=2))
axs[2].axis("off")

plt.show()import numpy as np
np.testing.assert_allclose(seg_mask, seg_mask_ve)response = authed_session.post(url, data=json.dumps(two_instances_request_body))
print(response)
print(response.content)response = response.content.decode('utf-8')
response = ast.literal_eval(response)

seg_mask = tf.convert_to_tensor(response['predictions'])
seg_mask = tf.squeeze(seg_mask)
seg_mask