/image/Tensor Tensor("activation_5/Softmax:0&",shape =(?, 4),dtype = float32)处的ValueError不是此图的元素 [英] ValueError at /image/ Tensor Tensor("activation_5/Softmax:0", shape=(?, 4), dtype=float32) is not an element of this graph

查看:218
本文介绍了/image/Tensor Tensor("activation_5/Softmax:0&",shape =(?, 4),dtype = float32)处的ValueError不是此图的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an image processing classifier and this code is an API to predict the image class of the image the whole code is running except this line (pred = model.predict_classes(test_image)) this API is made in Django framework and am using python 2.7

如果我像往常一样运行此代码(无需创建API),这一点就可以很好地运行

here is a point if I am running this code like normally ( without making an API) it's running perfectly

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)

推荐答案

您的test_image和张量流模型的输入不匹配.

Your test_image and input of tensorflow model is not match.

# 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)

以上仅为假设.如果要调试,我想您应该打印图像大小并与模型定义的第一个布局进行比较.并检查尺寸(宽度,高度,深度)是否匹配

The above is just assumption. If you want to debug, i guess you should print your image size and compare with first layout of your model definition. And check whe the size (width, height, depth) is match

这篇关于/image/Tensor Tensor("activation_5/Softmax:0&",shape =(?, 4),dtype = float32)处的ValueError不是此图的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆