【问题标题】:ValueError: Shapes (1, 1, 512, 4) and (2, 512, 1, 1) are incompatible how to fix this error?ValueError:形状 (1, 1, 512, 4) 和 (2, 512, 1, 1) 不兼容如何解决此错误?
【发布时间】:2020-04-09 19:32:03
【问题描述】:

我不太会英文,所以可能写得很笨拙。 我的自定义代码在识别时出错。我试图改变分析方法和文件位置。但没有任何帮助。我不是超级程序员,所以我不太了解代码深处发生了什么。我也不太明白输出图像应该保存在哪里以及它将被称为什么。 程序代码:

from imageai.Prediction.Custom import CustomImagePrediction
import os

execution_path = os.getcwd()

prediction = CustomImagePrediction()
prediction.setModelTypeAsSqueezeNet()
prediction.setModelPath(os.path.join(execution_path, "model_ex-065_acc-0.875000.h5"))
prediction.setJsonPath(os.path.join(execution_path, "model_class.json"))
prediction.loadModel(num_objects=4)

predictions, probabilities = prediction.predictImage(os.path.join(execution_path, "ScreenHack.jpg"), result_count=5)

for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction , " : " , eachProbability)

错误文本:

ValueError: Shapes (1, 1, 512, 4) and (2, 512, 1, 1) are incompatible

【问题讨论】:

  • 必须在重塑的两侧具有相同数量的元素。你正试图将 2048 个元素塞进 1024 个元素中。我们无能为力,因为你忽略了给我们预期的 MRE
  • 您的图像“ScreenHack.jpg”的尺寸与您的模型所需的尺寸不匹配,很可能。您应该会在该行中看到错误。

标签: python tensorflow


【解决方案1】:

将所有值相乘可以获得的实际元素数量,首先它会下注 2048,其次只有 1024。这就是它无法重塑的原因。

【讨论】:

  • 感谢您如此迅速地回复。你能更详细地描述我的问题吗?训练文件、输入图像或其他错误?哪些元素有问题?
  • 我更改了文件大小(这对我来说不是很好),错误消失了,但又出现了一个:OSError: Unable to open file (unable to open file: name = 'C:\ Users\Doctor\PycharmProjects\FAKE44Type\venv\model_ex-065_acc-0.875000.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0) 我还是不知道怎么做输出图像被保存。但是不行...更改h5文件所在目录后,出现同样的错误:ValueError: Shapes (1, 1, 512, 4) and (2, 512, 1, 1) is incompatible
  • 很难理解它为什么不同。可能是图像大小不适合该模型。或者例如模型等待灰度图像,然后您尝试传递 rgb 图像
猜你喜欢
  • 2021-02-23
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-05
  • 2021-02-14
  • 2020-08-19
相关资源
最近更新 更多