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

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

TensorFlow sequential model is not recognising the full shape of the data

问题

  1. import tensorflow as tf
  2. from traffic import IMG_WIDTH, IMG_HEIGHT
  3. import cv2
  4. import os
  5. import numpy as np
  6. model = tf.keras.models.load_model("neural-network")
  7. image = cv2.imread("gtsrb\\1\\00000_00000.ppm")
  8. image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
  9. print(image.shape)
  10. 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

  1. <details>
  2. <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)

  1. The image.shape doesn&#39;t agree with the shape that the model reads in the model.predict() line
  2. I run this code, and the image.shape is (30,30,3)
  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;
  4. Any help greatly appreciated as I have no clue why this is happening
  5. </details>
  6. # 答案1
  7. **得分**: 1
  8. ```python
  9. # 你需要使用来自tensorflow的expand_dims函数:
  10. import tensorflow as tf
  11. from traffic import IMG_WIDTH, IMG_HEIGHT
  12. import cv2
  13. import os
  14. import numpy as np
  15. model = tf.keras.models.load_model("neural-network")
  16. image = cv2.imread("gtsrb\\
    The image.shape doesn&#39;t agree with the shape that the model reads in the model.predict() line
  17. I run this code, and the image.shape is (30,30,3)
  18. 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;
  19. Any help greatly appreciated as I have no clue why this is happening 
  20. </details>
  21. # 答案1
  22. **得分**: 1
  23. ```python
  24. # 你需要使用来自tensorflow的expand_dims函数:
  25. import tensorflow as tf
  26. from traffic import IMG_WIDTH, IMG_HEIGHT
  27. import cv2
  28. import os
  29. import numpy as np
  30. model = tf.keras.models.load_model("neural-network")
  31. image = cv2.imread("gtsrb\\1\\00000_00000.ppm")
  32. image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
  33. image = tf.convert_to_tensor(image, dtype=tf.float32)
  34. image = tf.expand_dims(image, 0)
  35. print(image.shape)
  36. model.predict(image)
  37. # 如果图像不是RGB格式,请在将其转换为张量之前使用以下代码将其转换为RGB格式:
  38. image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
  39. 000_00000.ppm")
  40. image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
  41. image = tf.convert_to_tensor(image, dtype=tf.float32)
  42. image = tf.expand_dims(image, 0)
  43. print(image.shape)
  44. model.predict(image)
  45. # 如果图像不是RGB格式,请在将其转换为张量之前使用以下代码将其转换为RGB格式:
  46. image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
英文:

You need to use expand_dims function from tensorflow:

  1. import tensorflow as tf
  2. from traffic import IMG_WIDTH, IMG_HEIGHT
  3. import cv2
  4. import os
  5. import numpy as np
  6. model = tf.keras.models.load_model(&quot;neural-network&quot;)
  7. image = cv2.imread(&quot;gtsrb\\
    import tensorflow as tf
  8. from traffic import IMG_WIDTH, IMG_HEIGHT
  9. import cv2
  10. import os
  11. import numpy as np
  12. model = tf.keras.models.load_model(&quot;neural-network&quot;)
  13. image = cv2.imread(&quot;gtsrb\\1\\00000_00000.ppm&quot;)
  14. image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
  15. image = tf.convert_to_tensor(image, dtype=tf.float32)
  16. image = tf.expand_dims(image , 0)
  17. print(image.shape)
  18. model.predict(image)
  19. 000_00000.ppm&quot;)
  20. image = cv2.resize(image, (IMG_WIDTH, IMG_HEIGHT))
  21. image = tf.convert_to_tensor(image, dtype=tf.float32)
  22. image = tf.expand_dims(image , 0)
  23. print(image.shape)
  24. model.predict(image)

Also use this to convert it to RGB if not already in RGB before converting it to tensor:

  1. 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:

确定