TensorFlow的序列模型无法识别数据的完整形状。

huangapple go评论65阅读模式
英文:

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&#39;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 &quot;ValueError: Input 0 of layer &quot;sequential&quot; is incompatible with the layer: expected shape=(None, 30, 30, 3), found shape=(None, 30, 3)&quot;
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&#39;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 &quot;ValueError: Input 0 of layer &quot;sequential&quot; is incompatible with the layer: expected shape=(None, 30, 30, 3), found shape=(None, 30, 3)&quot;
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(&quot;neural-network&quot;)
image = cv2.imread(&quot;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(&quot;neural-network&quot;)
image = cv2.imread(&quot;gtsrb\\1\\00000_00000.ppm&quot;)
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&quot;) 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)

huangapple
  • 本文由 发表于 2023年5月26日 17:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339315.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定