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

ML Engine example: bad data preprocessing results in bad predictions #101

Open
fdb opened this issue May 27, 2020 · 1 comment
Open

ML Engine example: bad data preprocessing results in bad predictions #101

fdb opened this issue May 27, 2020 · 1 comment

Comments

@fdb
Copy link

fdb commented May 27, 2020

The image-to-json.py conversion script used in chapter 13 does not preprocess the data, instead leaving all inputs as values between 0-255. If used with the model from chapter 3, the predictions will be more or less random.

An easy solution is to convert the value to floating-point numbers. Here's the script I use:

import json
import numpy as np
import tensorflow as tf
from PIL import Image

model = tf.keras.models.load_model('cats_vs_dogs_01')
# The layer name of the input needs to be present in the JSON file.
input_layer_name = model.layers[0].name

img = Image.open('data/cats-vs-dogs/cat.9827.jpg')
# Because the values are floating-point, don't make the image to large
img.thumbnail((128, 128), Image.ANTIALIAS)
# Preprocess the data
data = np.asarray(img) / 255.
with open('cat_image.json', 'w') as fp:
    json.dump({input_layer_name: data.tolist()}, fp)

This script can now be used as such:

export AI_MODEL='your_model_name'
export AI_JSON_FILE='cat_image.json'
export AI_REGION='europe-west4'
gcloud ai-platform predict \
    --model $AI_MODEL \
    --json-instances $AI_JSON_FILE \
    --region $AI_REGION
@sidgan
Copy link
Collaborator

sidgan commented Mar 20, 2023

Thanks @fdb , do you want to modify the scipt and send a PR for it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants