英文:
TensorFlow sequential model is not recognising the full shape of the data
问题
import tensorflow as tf
from traffic import IMG_WIDTH, IMG_HEIGHT
import cv2
import os
import numpy as np
model = tf.keras.models.load_model("neural-network")
image = cv2.imread("gtsrb\\1\\00000_00000.ppm")
image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
print(image.shape)
model.predict(image)
The image.shape doesn't agree with the shape that the model reads in the model.predict() line
I run this code, and the image.shape is (30,30,3)
but when I predict the image, it raises an error, which states "ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 30, 30, 3), found shape=(None, 30, 3)"
Any help greatly appreciated as I have no clue why this is happening
<details>
<summary>英文:</summary>
import tensorflow as tf
from traffic import IMG_WIDTH, IMG_HEIGHT
import cv2
import os
import numpy as np
model = tf.keras.models.load_model("neural-network")
image = cv2.imread("gtsrb\1\00000_00000.ppm")
image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
print(image.shape)
model.predict(image)
The image.shape doesn't agree with the shape that the model reads in the model.predict() line
I run this code, and the image.shape is (30,30,3)
but when I predict the image, it raises an error, which states "ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 30, 30, 3), found shape=(None, 30, 3)"
Any help greatly appreciated as I have no clue why this is happening
</details>
# 答案1
**得分**: 1
```python
# 你需要使用来自tensorflow的expand_dims函数:
import tensorflow as tf
from traffic import IMG_WIDTH, IMG_HEIGHT
import cv2
import os
import numpy as np
model = tf.keras.models.load_model("neural-network")
image = cv2.imread("gtsrb\\The image.shape doesn't agree with the shape that the model reads in the model.predict() line
I run this code, and the image.shape is (30,30,3)
but when I predict the image, it raises an error, which states "ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 30, 30, 3), found shape=(None, 30, 3)"
Any help greatly appreciated as I have no clue why this is happening
</details>
# 答案1
**得分**: 1
```python
# 你需要使用来自tensorflow的expand_dims函数:
import tensorflow as tf
from traffic import IMG_WIDTH, IMG_HEIGHT
import cv2
import os
import numpy as np
model = tf.keras.models.load_model("neural-network")
image = cv2.imread("gtsrb\\1\\00000_00000.ppm")
image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
image = tf.convert_to_tensor(image, dtype=tf.float32)
image = tf.expand_dims(image, 0)
print(image.shape)
model.predict(image)
# 如果图像不是RGB格式,请在将其转换为张量之前使用以下代码将其转换为RGB格式:
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
000_00000.ppm")
image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
image = tf.convert_to_tensor(image, dtype=tf.float32)
image = tf.expand_dims(image, 0)
print(image.shape)
model.predict(image)
# 如果图像不是RGB格式,请在将其转换为张量之前使用以下代码将其转换为RGB格式:
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
英文:
You need to use expand_dims function from tensorflow:
import tensorflow as tf
from traffic import IMG_WIDTH, IMG_HEIGHT
import cv2
import os
import numpy as np
model = tf.keras.models.load_model("neural-network")
image = cv2.imread("gtsrb\\import tensorflow as tf
from traffic import IMG_WIDTH, IMG_HEIGHT
import cv2
import os
import numpy as np
model = tf.keras.models.load_model("neural-network")
image = cv2.imread("gtsrb\\1\\00000_00000.ppm")
image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
image = tf.convert_to_tensor(image, dtype=tf.float32)
image = tf.expand_dims(image , 0)
print(image.shape)
model.predict(image)
000_00000.ppm")
image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
image = tf.convert_to_tensor(image, dtype=tf.float32)
image = tf.expand_dims(image , 0)
print(image.shape)
model.predict(image)
Also use this to convert it to RGB if not already in RGB before converting it to tensor:
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论