SVM 到性别识别
SVM to gender recognition
我这几周一直在做一个性别识别项目(在 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, sys
import numpy as np
import PIL.Image as Image
import cv2
from sklearn import svm
def 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))
我想这是在追加一个二维数组。您可能希望在附加之前 flatten 它,以便每个实例看起来像这样:
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())
我这几周一直在做一个性别识别项目(在 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, sys
import numpy as np
import PIL.Image as Image
import cv2
from sklearn import svm
def 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))
我想这是在追加一个二维数组。您可能希望在附加之前 flatten 它,以便每个实例看起来像这样:
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())