【问题标题】:How to test accuracy from images that were not in the dataset如何测试不在数据集中的图像的准确性
【发布时间】:2020-12-10 23:05:20
【问题描述】:

我正在使用 train_test_split 来训练和测试我的数据,这是一个将数据划分为训练和测试的有趣概念,但是如果我想加载一些不在测试数据中的数据怎么办?

我的问题是 train_test_split 只随机处理数据,我想看看外部图像属于什么标签。

目前,我正在从图像中提取 22 个特征并使用这些特征来训练线性 SVC 进行识别,现在根据 train_test_split 我在测试集上得到 94%,这没关系,我想做的只是测试它在不在数据集中的图像上。 train_test_split 从之前加载的数据集中接收数据用于训练和测试,但我想加载图像并直接测试它们。

可重现的示例:(3 张图像,10 个特征)

import sklearn
from sklearn.model_selection import train_test_split
from sklearn import metrics

y_target = [1]*1 + [2]*1 + [3]*1 # number of images per person
data = np.asarray([[152., 236., 228., 168., 236., 224.,  70., 223., 175., 195.],
       [140., 233., 226., 161., 234., 220.,  67., 220., 159., 194.],
       [135., 233., 225., 157., 234., 221.,  65., 220., 159., 193.]])

svc_ = SVC(kernel='linear', C=0.00005)

A_train, A_test, b_train, b_test = train_test_split(
        data, y_target, test_size=0.25, random_state=0)

def train(clf, A_train, A_test, b_train, b_test):
    
    clf.fit(A_train, b_train)
    print ("Accuracy on training set:")
    print (clf.score(A_train, b_train))

train(svc_, A_train, A_test, b_train, b_test)

例如,我将如何测试以下图片的功能?

([[126., 232., 225., 149., 231., 222.,  60., 218., 152., 191.]])

所以,我正在做的是选择一个特定的图像,对其进行一些编辑,然后我想看看我的分类器在测试这张经过编辑、未经训练且不在数据集中的图像时的表现,例如,如果我从互联网上挑选了一张图片,我将如何测试它??

【问题讨论】:

    标签: python machine-learning scikit-learn svm train-test-split


    【解决方案1】:

    如果您知道如何从图像中获取您感兴趣的特征,只需加载图像,收集特征,然后根据正确的值进行预测和测试。例如

    y_test = [[1], [2], [3]]
    images = # fill in however you are getting your images into memory here
    clf.score(images, y_test)
    
    # or get the predictions by hand and do your own metric
    predictions = clf.predict(images)
    mse = np.mean(np.square(y_test - predictions))
    

    在执行此操作之前,您应该已经训练了分类器。

    【讨论】:

    • 我不明白你的解决方案,我将如何针对我没有加载到数据集中的图像测试我的训练?你和我做的一样
    • 您不必将图像加载到数据集中。例如定义你的序列 test_image = ([[126., 232., 225., 149., 231., 222., 60., 218., 152., 191.]]) 然后调用 predictions = clf.predict( test_image) 就是你所需要的
    猜你喜欢
    • 2017-05-03
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2020-03-23
    • 2019-12-04
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    相关资源
    最近更新 更多