python - 在 tensorflow 中预测上传的图像时如何解释 expand_dims

标签 python numpy tensorflow conv-neural-network

这些天我开始熟悉 CNN 和 tensorflow(通过猫/狗分类问题),在编写预测函数时,我意识到代码使用了 expand_dims,我无法解释 [1]。
当我能够获得所需的 (150, 150, 3) 上传图像数组时,为什么不能将其提供给预测函数?我训练/验证的图像也具有相同的大小(150、150、3)。添加这个额外的 (1, 150, 150, 3) 到底有什么帮助???
提前谢谢了,
[1]

import numpy as np

from google.colab import files
from keras.preprocessing import image

uploaded=files.upload()

for fn in uploaded.keys():
 
  # predicting images
  path='/content/' + fn
  img=image.load_img(path, target_size=(150, 150))
  
  x=image.img_to_array(img)
  x=np.expand_dims(x, axis=0)
  images = np.vstack([x])
  
  classes = model.predict(images, batch_size=10)
  
  print(classes[0])
  
  if classes[0]>0.5:
    print(fn + " is a dog")
    
  else:
    print(fn + " is a cat")

最佳答案

图像开头的额外维度通常用于批量大小。批量大小表示您一次捆绑多少张图像以提供给 CNN。批次也可以是单个图像,但是它仍然需要额外的维度 1 来显示批次的大小。例如,如果批量大小为 5,则您的输入数组的形状为 (5, 150, 150, 3)

关于python - 在 tensorflow 中预测上传的图像时如何解释 expand_dims,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62671394/

相关文章:

python - Python 是否有一个 toString() 等价物,我可以将一个类转换为字符串吗?

python - 如何在 numpy 数组中找到元组的索引?

python - 将 Python 警告/错误跟踪到 numpy 和 scipy 中的行号

python - 计算numpy中两个矩阵的行之间的夹角

python - 为 CNN(音频识别)转换 MFCC 频谱图的输入

python - tensorflow 错误KeyError : "The name ' import/Mul' refers to an Operation not in the graph.“

python - 我怎样才能使 python 图的点随着时间的推移出现?

python - 使用python将xgboost模型保存到hdfs

python - 使用 uWSGI 部署 Pylons

python - 如何在没有互联网连接的情况下安装tensorflow?