[英]How do I use my own image for a prediction in CNN?
I'm trying to use my own 28x28 pixel image in my CNN but I keep getting this error when I'm trying to reshape it:我试图在我的 CNN 中使用我自己的 28x28 像素图像,但是当我尝试重塑它时,我不断收到此错误:
x = x.reshape(1,28,28,1)
ValueError: cannot reshape array of size 2352 into shape (1,28,28,1)
My image is 28x28, but I can't change it into a single greyscale channel.我的图像是 28x28,但我无法将其更改为单个灰度通道。 I've been using spyder via anaconda and I keep getting an "UnsatisfiableError" whenever I am trying to install opencv onto my root environment.
我一直在通过 anaconda 使用 spyder,每当我尝试将 opencv 安装到我的根环境时,我都会收到“UnsatisfiableError”。 Pip install doesn't work either.
Pip 安装也不起作用。 I was wondering if anyone had anyway to reshape my images without using an external library and just using tensorflow.
我想知道是否有人不得不在不使用外部库而仅使用 tensorflow 的情况下重塑我的图像。
I have searched SO for the answer and tried solutions suggested but unfortunately none of them have worked.我已经搜索了答案并尝试了建议的解决方案,但不幸的是,它们都没有奏效。
Thanks in advance.提前致谢。
In reply to AKX:回复 AKX:
def Predict(imgPath):
x = plt.imread(imgPath)
x = x.reshape(1,28,28,1)
with graph.as_default():
out = model.predict(x)
return out
In reply to Skander HR:回复 Skander HR:
(28,28,3)
In reply to Matias Valdenegro:回复 Matias Valdenegro:
My problem is that openCV isn't working.我的问题是 openCV 不起作用。 It's installed but I've tried to check if there were any proxy servers, installed python3.dll and tried installing the microsoft mediafeaturepack to get rid of the error but none of those worked.
它已安装,但我尝试检查是否有任何代理服务器,安装了 python3.dll 并尝试安装 microsoft mediafeaturepack 以消除错误,但没有一个起作用。 When I write a program like this:
当我编写这样的程序时:
import cv2 as cv
print("done")
I get the error message:我收到错误消息:
import cv2 as cv
ImportError: DLL load failed: The specified module could not be found.
you are mostly using an RGB image of size 28*28 so your size is 28*28*3=2352.you need to convert it into grayscale image to match your tensor shape.您主要使用大小为 28*28 的 RGB 图像,因此您的大小为 28*28*3=2352。您需要将其转换为灰度图像以匹配您的张量形状。
you can convert using opencv using below command:-您可以使用以下命令使用 opencv 进行转换:-
gray_image = cv2.imread(path_to_image, cv2.IMREAD_GRAYSCALE)
Just use Pillow
to resize your image:只需使用
Pillow
调整图像大小:
from PIL import Image
import numpy as np
...
img = Image.fromarray(x)
img = img.resize((28,28))
x = np.asarray(img)
x = x.reshape(1,28,28,1)
...
install it using pip pip install pillow
or python3 -m pip install pillow
so that you're sure you're using the correct python version.使用 pip
pip install pillow
或python3 -m pip install pillow
安装它,以便您确定您使用的是正确的 python 版本。
There are two concepts to take into consideration when predicting on your own image, and using grayscale images.在预测您自己的图像和使用灰度图像时,需要考虑两个概念。
np.expand_dims(image,axis=2)
or image = image[..., np.newaxis]
.np.expand_dims(image,axis=2)
或image = image[..., np.newaxis]
。 After this step, your image will be of size (width,height,1)
.(width,height,1)
。np.expand_dims(image, axis=0)
, to add the batch_index size.np.expand_dims(image, axis=0)
来添加 batch_index 大小。 This is necessary, since by default in Keras/TensorFlow you only predict on batches. If you bear these two in mind you will successfully and easily use model.predict()
to get your prediction.如果您牢记这两点,您将成功且轻松地使用
model.predict()
进行预测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.