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

您好我正在build立一个image processing的分类器,这个代码是一个API预测整个代码正在运行的图像的图像类除了这一行(pred = model.predict_classes(test_image))这个api是在Django框架和m使用python2.7

here is a point if I am running this code like normally ( without making an api)its 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) 

One Solution collect form web for “ValueError at / image / Tensor张量(“activation_5 / Softmax:0”,shape =(?,4),dtype = float32)不是该图的元素”

您的test_image和tensorflow模型的input不匹配。

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

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

  • Keras,如何获得每一层的输出?
  • 如何在TensorFlow上进行Xavier初始化
  • 在CPU上运行Tensorflow
  • 如何将张量转换为TensorFlow中的一个numpy数组?
  • Tensorflow一个热门编码器?
  • 如何在张量stream中有效地应用梯度裁剪?
  • 如何构build和使用Google TensorFlow C ++ api
  • 如何在Tensorflow中只使用Python来自定义激活函数?
  • tf.app.run()如何工作?
  • tf.placeholder和tf.Variable有什么区别?
  • 用TensorFlow模型进行预测