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

Dee*_*kar 6 python django machine-learning keras tensorflow

我正在构建一个图像处理分类器,这段代码是一个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)
Run Code Online (Sandbox Code Playgroud)

Vu *_*ong 3

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

# 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)
Run Code Online (Sandbox Code Playgroud)

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