使用SVM进行性别识别

我这几周一直在做一个性别识别项目(使用Python),起初使用Fisherfaces作为特征提取方法,并使用基于欧几里得距离的1-NN分类器,但后来我觉得这种方法不够可靠(这是我个人的看法),所以我打算使用SVM。然而,当我试图创建和训练一个模型以应用于我的图像数据集时,我感到迷茫了。我在http://scikit-learn.org上找不到我需要的命令。我尝试了以下代码,但它不起作用,我不知道为什么执行时会出现这个错误:

  File "prueba.py", line 46, in main    clf.fit(R, r)  File "/Users/Raul/anaconda/lib/python2.7/site-packages/sklearn/svm/base.py", line 139, in fit     X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')  File "/Users/Raul/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.py", line 350, in check_array    array.ndim)ValueError: Found array with dim 3. Expected <= 2

这是我的代码:

import os, sysimport numpy as npimport PIL.Image as Imageimport cv2from sklearn import svmdef read_images(path, id, sz=None):    c = id    X,y = [], []    for dirname, dirnames, filenames in os.walk(path):        for subdirname in dirnames:            subject_path = os.path.join(dirname, subdirname)            for filename in os.listdir(subject_path):                try:                    im = Image.open(os.path.join(subject_path, filename))                    im = im.convert("L")                    # resize to given size (if given)                    if (sz is not None):                        im = im.resize(sz, Image.ANTIALIAS)                    X.append(np.asarray(im, dtype=np.uint8))                    y.append(c)                except IOError as e:                    print "I/O error({0}): {1}".format(e.errno, e.strerror)                except:                    print "Unexpected error:", sys.exc_info()[0]                    raise                        #c = c+1    return [X,y]def main():    # check arguments    if len(sys.argv) != 3:        print "USAGE: example.py </path/to/images/males> </path/to/images/females>"        sys.exit()    # read images and put them into Vectors and id's    [X,x] = read_images(sys.argv[1], 1)    [Y, y] = read_images(sys.argv[2], 0)    # R all images and r all id's    [R, r] = [X+Y, x+y]    clf = svm.SVC()    clf.fit(R, r)if __name__ == '__main__':    main()

我非常感谢任何关于如何使用SVM进行性别识别的帮助,谢谢阅读


回答:

X.append(np.asarray(im, dtype=np.uint8))

我猜这是添加了一个二维数组。你可能需要在添加之前将其展平,这样每个实例看起来像这样:

array([255, 255, 255, ..., 255, 255, 255], dtype=uint8)

而不是这样:

array([   [255, 255, 255, ..., 255, 255, 255],   [255, 255, 255, ..., 255, 255, 255],   [255,   0,   0, ...,   0,   0,   0],   ...,    [255,   0,   0, ...,   0,   0,   0],   [255, 255, 255, ..., 255, 255, 255],   [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

试试这个:

X.append(np.asarray(im, dtype=np.uint8).ravel())

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注