CSV Inference Example
What you'll need
Inference code
import json
from zipfile import ZipFile
import pandas as pd
import tensorflow as tf
# Extract and load the model from Valohai inputs
with ZipFile("/valohai/inputs/model/model.zip", "r") as f:
f.extractall()
model = tf.keras.models.load_model("model")
# Load CSV data from Valohai inputs
csv = pd.read_csv("/valohai/inputs/data/data.csv")
labels = csv.pop("target")
data = tf.data.Dataset.from_tensor_slices((dict(csv), labels))
batch_data = data.batch(batch_size=32)
# Run predictions
results = model.predict(batch_data)
# Build results dictionary: {"1": 0.375, "2": 0.76}
flattened_results = results.flatten()
indexed_results = enumerate(flattened_results, start=1)
metadata = dict(indexed_results)
# Print each result as Valohai metadata for tracking
for value in metadata.values():
print(json.dumps({"result": str(value)}))
# Save results to Valohai outputs
with open("/valohai/outputs/results.json", "w") as f:
# NumPy float32 values need stringification for JSON
json.dump(metadata, f, default=lambda v: str(v))Define the step
Run the inference
Check your results
Last updated
Was this helpful?
