回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我正在努力使用支持向量机分类器来根据多米诺骨牌的类别对图像进行分类,例如1x3。你知道吗</p>
<p>我有28种不同类型的多米诺骨牌的2000多张图片(可以下载<a href="https://www.dropbox.com/sh/s5f38k4l2on5mba/AACNQgXuw1edwEb6oO1w3CfOa?dl=0" rel="nofollow noreferrer">here</a>)。你知道吗</p>
<p>我使用scikit learn和SVM作为算法运行以下脚本:</p>
<pre><code>import matplotlib.pyplot as plt
from sklearn import svm, metrics
from sklearn.model_selection import train_test_split
import numpy as np
import os # Working with files and folders
from PIL import Image # Image processing
rootdir = os.getcwd()
image_file = 'images.npy'
key_file = 'keys.npy'
if (os.path.exists(image_file) and os.path.exists(key_file)):
print "Loading existing numpy's"
pixel_arr = np.load(image_file)
key = np.load(key_file)
else:
print "Creating new numpy's"
key_array = []
pixel_arr = np.empty((0,10000), "uint8")
for subdir, dirs, files in os.walk('data'):
dir_name = subdir.split("/")[-1]
if "x" in dir_name:
for file in files:
if ".DS_Store" not in file:
im = Image.open(os.path.join(subdir, file))
if im.size == (100,100):
key_array.append(dir_name)
numpied_image = np.array(im.convert('L')).reshape(1,-1)
#Image.fromarray(np.reshape(numpied_image,(-1,100)), 'L').show()
pixel_arr = np.append(pixel_arr, numpied_image, axis=0)
im.close()
key = np.array(key_array)
np.save(image_file, pixel_arr)
np.save(key_file, key)
# Create a classifier: a support vector classifier
classifier = svm.SVC(gamma='auto')
X_train, X_test, y_train, y_test = train_test_split(pixel_arr, key, test_size=0.1,random_state=33)
# We learn the digits on the first half of the digits
print "Fitting classifier"
classifier.fit(X_train, y_train)
# Now predict the value of the digit on the second half:
expected = y_test
print "Predicting"
predicted = classifier.predict(X_test)
print("Classification report for classifier %s:\n%s\n"
% (classifier, metrics.classification_report(expected, predicted)))
print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
</code></pre>
<p>其结果如下:</p>
<pre><code>Classification report for classifier SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False):
precision recall f1-score support
0x0 0.00 0.00 0.00 9
1x0 0.00 0.00 0.00 9
1x1 0.00 0.00 0.00 12
2x0 0.00 0.00 0.00 12
2x1 0.00 0.00 0.00 10
2x2 0.00 0.00 0.00 7
3x0 0.00 0.00 0.00 7
3x1 0.00 0.00 0.00 8
3x2 0.00 0.00 0.00 8
3x3 0.01 1.00 0.02 3
4x0 0.00 0.00 0.00 11
4x1 0.00 0.00 0.00 10
4x2 0.00 0.00 0.00 8
4x3 0.00 0.00 0.00 15
4x4 0.00 0.00 0.00 8
5x0 0.00 0.00 0.00 12
5x1 0.00 0.00 0.00 7
5x2 0.00 0.00 0.00 11
5x3 0.00 0.00 0.00 7
5x4 0.00 0.00 0.00 9
5x5 0.00 0.00 0.00 14
6x0 0.00 0.00 0.00 11
6x1 0.00 0.00 0.00 12
6x2 0.00 0.00 0.00 11
6x3 0.00 0.00 0.00 9
6x4 0.00 0.00 0.00 9
6x5 0.00 0.00 0.00 18
6x6 0.00 0.00 0.00 13
avg / total 0.00 0.01 0.00 280
>>> print("Confusion matrix:\n%s" % metrics.confusion_matrix(expected, predicted))
Confusion matrix:
[[ 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0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]
[ 0 0 0 0 0 0 0 0 0 11 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0]]
</code></pre>
<p>显然有些事情很不对劲。即使分类器可以自由地猜测,它也会获得更好的精度!我的怀疑是,我还不能确认我创建key/y标签的方式是不正确的。不过,脚本运行时没有错误,但无法预测任何内容。你知道吗</p>
<p>有一件事让我觉得密钥有问题,那就是混淆矩阵没有任何标签。你知道吗</p>
<p>当你得到这样的结果时,什么可能是一个错误?你知道吗</p>
<p>编辑:我尝试在<code>key</code>上使用LabelEncoder,但是结果是一样的。你知道吗</p>
<p>Edit2:我还尝试了不同的lambda,并手动将lambda设置为0.00001,结果显示分类器得分为0.05(与上面的结果相比,这是一个改进)。我不期望分类器在这些数据上是完美的,但我至少期望在60-70%的范围内,而不是5%。你知道吗</p>