How To Fix Datatype Mismatch To Predict Images Using My Trained Model?
I trained a CNN, but I'm unable to use it to make predictions. All of my images are in a folder
model = tf.keras.models.load_model("C:\Sid\CNNs\MoonRocks.h5") import os filepath = "C:\Sid\Projects\LunarMoonRocks\DataSet\Test Images" for img in os.listdir(filepath): img_path = os.path.join(filepath, img) img_array=cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) new_array=cv2.resize(img_array, (480, 480)) img_tbp = new_array.reshape(-1, 480, 480, 1) prediction = model.predict([img_tbp])
This code shows the error
TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64
I tried to change the image type to float32 using
image = tf.image.decode_jpeg(img_tbp) image = tf.cast(image, tf.float32)
But that showed the error
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
How do I get my model to predict the images in the folder?
decode_jpeg is used to decode the contents of image file which is read in binary format. You have already read your image file using OpenCV and OpenCV reads the file in NumPy format. Had you made use of
read_file, then you should have used
Now coming to your problem, you could have converted your
uint8 image to floating type either by just using
tf.cast operation as you had done it, but you have most likely forgotten to normalize your image from the range of [0, 255] in
uint8 to [0, 1] in float. So you could have directly converted your image to float and bring the number's value in range of [0, 1] using:
image = image / 255.0
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module