【问题标题】:ValueError at /image/ Tensor Tensor("activation_5/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graphValueError at /image/ Tensor Tensor("activation_5/Softmax:0", shape=(?, 4), dtype=float32) 不是该图的元素
【发布时间】:2018-04-27 22:43:55
【问题描述】:

我正在构建一个图像处理分类器,此代码是一个 API,用于预测整个代码正在运行的图像的图像类,除了这一行 (pred = model.predict_classes(test_image)) 此 API 是在 Django 框架中制作的,并且我正在使用 python 2.7

如果我像往常一样运行此代码(不创建 API),那么它运行良好

def classify_image(request):
if request.method == 'POST' and request.FILES['test_image']:

    fs = FileSystemStorage()
    fs.save(request.FILES['test_image'].name, request.FILES['test_image'])


    test_image = cv2.imread('media/'+request.FILES['test_image'].name)

    if test_image is not None:
        test_image = cv2.resize(test_image, (128, 128))
        test_image = np.array(test_image)
        test_image = test_image.astype('float32')
        test_image /= 255
        print(test_image.shape)
    else:
        print('image didnt load')

    test_image = np.expand_dims(test_image, axis=0)
    print(test_image)
    print(test_image.shape)

    pred = model.predict_classes(test_image)
    print(pred)

return JsonResponse(pred, safe=False)

【问题讨论】:

标签: python django machine-learning tensorflow keras


【解决方案1】:

你的test_image和tensorflow模型的输入不匹配。

# Your image shape is (, , 3)
test_image = cv2.imread('media/'+request.FILES['test_image'].name)

if test_image is not None:
    test_image = cv2.resize(test_image, (128, 128))
    test_image = np.array(test_image)
    test_image = test_image.astype('float32')
    test_image /= 255
    print(test_image.shape)
else:
    print('image didnt load')

# Your image shape is (, , 4)
test_image = np.expand_dims(test_image, axis=0)
print(test_image)
print(test_image.shape)

pred = model.predict_classes(test_image)

以上只是假设。如果你想调试,我想你应该打印你的图像大小并与你的模型定义的第一个布局进行比较。并检查大小(宽、高、深)是否匹配

【讨论】:

  • 当我运行这段代码而不是像正常情况下的 django 时,它运行得很好,但是在 api 中给出了这个错误,我不知道为什么
  • 能否把classify_image类的所有日志贴出来。
  • 我的意思是,api执行结果(打印命令的输出)。英语不好,抱歉
  • ValueError at /image/ Tensor Tensor(“activation_5/Softmax:0”, shape=(?, 4), dtype=float32) 在 model.predict_classes 之后不是该图的元素
  • 嘿,问题是您的分类器无法与您的网络(wsgi)很好地连接。有人qiita.com/itisyuu/items/7c9d7ff43b3936704918 也有和你一样的问题。都是日文,但代码可用。希望对您有所帮助
猜你喜欢
  • 2020-06-09
  • 2019-04-22
  • 2019-09-17
  • 2019-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-07-18
  • 2019-09-24
  • 2018-04-25
相关资源
最近更新 更多