【问题标题】:Cannot import Image to test Scikit learn application无法导入图像来测试 Scikit learn 应用程序
【发布时间】:2020-08-13 02:14:39
【问题描述】:

我是一名学习 Scikit 的新手程序员,所以我的问题是一个基本问题。我使用草图数据集创建了我的第一个机器学习代码程序,用于在苹果和香蕉草图之间进行对象识别,它在训练和测试方面工作得很好。

import cv2 as cv
import numpy as np
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split as tts
from sklearn.metrics import accuracy_score

#loading datasets
apples_Full = np.load('dataset/apple.npy')
bananas_Full = np.load('dataset/banana.npy')

N_Samples = 1000
test_Number = 0.2
APPLE = 0
BANANA = 1

def normalize(data):
    return np.interp(data , [0 , 255] , [-1 , 1])

apples = apples_Full[:N_Samples]
bananas = bananas_Full[:N_Samples]
dataset = np.concatenate((apples , bananas))
dataset = normalize(dataset)
labels = [APPLE] * N_Samples + [BANANA] * N_Samples
#spliting data
x_train , x_test , y_train , y_test = tts(dataset , labels ,test_size = test_Number)

alg = SVC()
alg.fit(x_train , y_train)
preds = alg.predict(x_test)
Result = accuracy_score(y_test , preds)
print(Result)

现在我想输入一个草图图像,以便将其用作对象识别应用程序。我尝试导入图像并将其转换为 .npy 文件并将其用作数据集,就像测试步骤一样,但出现错误: X.shape[1] = 151875应该等于784,训练时的特征个数

testfile = "My_test.jpg"
Image = cv.imread(testfile)
TEST = np.array(Image , dtype = 'uint8')
np.save('My_test' + '.npy' , TEST)
Sketch = np.load('My_test.npy')
Sketch = np.reshape(Sketch, (1 , -1))
Testdata = normalizer(Sketch)
finaltest = alg.predict(Testdata)
print(finaltest)

我该怎么办?

【问题讨论】:

  • 以下解决方案有效吗?

标签: python machine-learning scikit-learn computer-vision


【解决方案1】:

由于错误,我认为您在推理过程中输入的形状与您在模型训练期间使用的图像形状不同。您在训练过程中使用了大小为28*28 的图像,在推理过程中输入了不同大小的图像,您只需要resize 您的测试图像,如下所示:

testfile = "My_test.jpg"
Image = cv.imread(testfile)
Image = cv.resize(Image,(28,28))  # will convert your image to 28*28
TEST = np.array(Image , dtype = 'uint8')

另外,如果你想标准化你的数据,你可以将整个图像除以255,而不是使用np.interp,如下所示:

Image = Image/255.0

希望这会有所帮助!

【讨论】:

  • 感谢您的回复!我在我的程序中使用了你的代码,但是当我想在它上面实现我的算法时: alg.predict (TEST) 。我得到了这个值错误:找到暗淡的数组 3. Estimator expected
  • 我认为这可能是因为我的数据库是由没有颜色维度的草图组成的。虽然我的测试图像是一个草图,但它不是 2d 并且它有第三个暗淡。我想知道怎么做。
  • 您可以使用np.squeeze(TEST) 删除多余的第三维。
  • 试过了还是一样的错误:(
猜你喜欢
  • 2012-07-12
  • 2015-12-09
  • 2021-01-17
  • 1970-01-01
  • 2017-01-09
  • 2016-04-09
  • 2014-05-19
  • 1970-01-01
  • 2017-09-12
相关资源
最近更新 更多