Hello,
i'm a student in data science and i'm having trouble with a Cloud Python function I developed to perform inference with a TensorFlow (.h5) model.
Problem Description:
My function is triggered by adding an image to a Google Cloud Storage bucket. It should download the image, preprocess it, and perform a prediction with my TensorFlow model. However, I'm seeing the following issue:
what I have already done.
the log messages that I receive
I tensorflow/core/util/port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered"timestamp: "2024-12-05T22:11:34.974800Z"
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
E external/local_xla/xla/stream_executor/cuda/cuda_driver.cc:152] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model
Google cloud function code
import os
from google.cloud import storage
import tensorflow as tf
from PIL import Image
import numpy as np
from cloudevents.http import CloudEvent
import functions_framework
# Set logging level to INFO
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
# Initialize Google Cloud Storage client
storage_client = storage.Client()
# Define bucket and model file names
bucket = storage_client.bucket('tensorflow-mlops') # Replace 'tensorflow-mlops' with your bucket name
model_file = 'mlops_project_model.h5'
local_model_path = f'/tmp/{model_file}'
# Download the model file from GCS
blob = bucket.blob(model_file)
blob.download_to_filename(local_model_path)
# Load the TensorFlow model
modele = tf.keras.models.load_model(local_model_path)
@functions_framework.cloud_event
def analyser_image(cloud_event: CloudEvent):
"""Cloud Function triggered by adding an image to a GCS bucket."""
storage_client = storage.Client()
# Get information about the uploaded image from the event
file = cloud_event.data
bucket_name = file['bucket']
file_name = file['name']
# Download the image from GCS
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(file_name)
image_path = f'/tmp/{file_name}'
blob.download_to_filename(image_path)
# Preprocess the image
img = Image.open(image_path)
img = img.resize((64, 64))
img_array = np.array(img) / 255.0 # Normalize pixel values
img_array = np.expand_dims(img_array, axis=0) # Add a batch dimension
# --- End of test section ---
try:
# Perform prediction using the loaded model
prediction = modele.predict(img_array)
# Convert prediction to string and save it to a file
prediction_str = str(prediction)
output_file_name = "prediction.txt"
local_output_path = f"/tmp/{output_file_name}"
with open(local_output_path, "w") as f:
f.write(prediction_str)
output_bucket_name = "output-predictions"
output_bucket = storage_client.bucket(output_bucket_name)
output_blob = output_bucket.blob(output_file_name)
output_blob.upload_from_filename(local_output_path)
# Return success message and status code
return "Prediction completed successfully.", 200
except Exception as e:
# --- This section writes "Test3" to a file and uploads it to GCS (for debugging/testing) ---
prediction_str = "Test3"
output_file_name = "test3.txt"
local_output_path = f"/tmp/{output_file_name}"
with open(local_output_path, "w") as f:
f.write(prediction_str)
output_bucket_name = "output-predictions"
output_bucket = storage_client.bucket(output_bucket_name)
output_blob = output_bucket.blob(output_file_name)
output_blob.upload_from_filename(local_output_path)
# --- End of test section ---
# Return error message and status code
return f"Error during prediction: {e}", 500
Thanks to everyone who helps me !