/ image / Tensor Tensor(“ activation_5 / Softmax:0”,shape =(?, 4),dtype = float32)处的ValueError不是此图的元素

发布于 2021-01-29 19:23:27

我正在构建图像处理分类器,并且此代码是用于预测整个代码正在运行的图像的图像类的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)
关注者
0
被浏览
139
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您的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)
    

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看