简体   繁体   English

如何在 CNN 中使用我自己的图像进行预测?

[英]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 pillowpython3 -m pip install pillow安装它,以便您确定您使用的是正确的 python 版本。

There are two concepts to take into consideration when predicting on your own image, and using grayscale images.在预测您自己的图像和使用灰度图像时,需要考虑两个概念。

  1. By default, the grayscale mode in OpenCV or PIL, considers a gray image without the last axis(color axis).默认情况下,OpenCV 或 PIL 中的灰度模式,考虑没有最后一个轴(颜色轴)的灰度图像。 That means, you have to use 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)
  2. You use again, 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.这是必要的,因为默认情况下,在 Keras/TensorFlow 中,您只能预测批次。 In your case, a photo practically means a batch_size of 1.在您的情况下,照片实际上意味着 batch_size 为 1。

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.

相关问题 我正在尝试使用 CNN 进行股价预测,但我的代码似乎不起作用,我需要更改或添加什么? - I am trying to use CNN for stock price prediction but my code does not seem to work, what do I need to change or add? 如何在 TensorFlow 号码识别中使用自己的手绘图像 - How do I use my own Hand-Drawn Image in TensorFlow Number Recognition 如何在TensorFlow中使用我自己的图像? - How do i use my own images in TensorFlow? 我想将我的 CNN 模型的特定预测更改为概率 - I want to change the specific prediction of my CNN-model to a probability 如何将我自己的循环与 pyhook 而不是 pumpMessages() 一起使用? - How do I use my own loop with pyhook instead of pumpMessages()? 如何将 CNN 预测保存为 float32 数据类型的图像? - How to save CNN prediction as image with float32 data type? 如何以图像形式保存CNN模型的输出(预测)? - How to save output (prediction) of the CNN model in the form of an image? 如何以图像格式保存CNN model的预测结果? - How to save prediction result of the CNN model in the image format? 在 MNIST 数据集上训练后,如何在 keras 中使用 cnn 预测我自己的图像 - how to predict my own image using cnn in keras after training on MNIST dataset 如何在Tensorflow中为CNN设置图像数据集? - How do I set up a image dataset in tensorflow for a CNN?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM