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

Tensorflow.js and Tensorflow(keras_model.h5) give different results. #108

Open
hermes7308 opened this issue May 7, 2020 · 0 comments
Open
Assignees

Comments

@hermes7308
Copy link

hermes7308 commented May 7, 2020

I am trying to use for image filtering.

Tensorflow.js gives the correct result but Tensorflow(keras_model.h5) gives the incorrect result.

I export the same training project, why this gives me different results.

I just change webcam to file using Tensorflow.js

Tensorflow.js

    // run the webcam image through the image model
    async function predict() {
        // predict can take in an image, video or canvas html element

        const image = document.getElementById("face-image");
        if (image == null) {
            alert("Please select image.");
            return;
        }

        labelContainer.innerHTML = "";

        const prediction = await model.predict(image, false);
        for (let i = 0; i < maxPredictions; i++) {
           const classPrediction =
                prediction[i].className + ": " + prediction[i].probability.toFixed(2);
            labelContainer.childNodes[i].innerHTML = classPrediction;
        }
    }

return: [[1.0 0]] (correct)

Tensorflow(keras_model.h5)

    # Create the array of the right shape to feed into the keras model
    # The 'length' or number of images you can put into the array is
    # determined by the first position in the shape tuple, in this case 1.
    data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)

    # Replace this with the path to your image
    image = Image.open(image_file_path)

    # resize the image to a 224x224 with the same strategy as in TM2:
    # resizing the image to be at least 224x224 and then cropping from the center
    size = (224, 224)
    image = ImageOps.fit(image, size, Image.ANTIALIAS)

    # turn the image into a numpy array
    image_array = np.asarray(image)

    # display the resized image
    image.show()

    # Normalize the image
    normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1

    # Load the image into the array
    data[0] = normalized_image_array

    # run the inference
    prediction = model.predict(data)

return: [[0.32318377 0.6768162]]

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