python - 使用 SVM 作为图像分类器时,精度/F 分数预计为 10% 吗?

标签 python machine-learning scikit-learn svm

作为我的机器学习学习项目,我尝试使用 SVM(支持向量机)对多米诺骨牌的不同图像进行分类。我这个项目很大程度上基于这个项目 https://scikit-learn.org/stable/auto_examples/applications/plot_face_recognition.html#sphx-glr-auto-examples-applications-plot-face-recognition-py我重新创建并理解了它,并获得了大约 70% 的精度/F1(如果我没记错的话)。我在我的项目中使用了大部分相同的代码。

在我的项目中,我有 28 个不同的文件夹,每个文件夹中有 100 个不同的 100x100px 多米诺骨牌图像(即 2800 个图像)。多米诺骨牌是用不同的背景、不同的变焦和不同的旋转来拍摄的。这些图像可以在这里找到:https://www.kaggle.com/wallcloud/photographs-of-28-different-domino-tiles

我已经测试过:

  • 在SVC上对C、gamma、kernel进行各种组合,找到最优组合
  • 具有不同数量组件的 PCA(500 似乎是最佳数量)
  • 使用 LabelEncoder(没有区别)
  • 不同的测试大小(0.1 似乎是最佳)
  • 裁剪图像(提高分数)、在图像上使用滤镜(降低分数)以及将其设为黑白(降低分数)。

尽管如此,我的分数仍然无法超过 10%,这与 Scikit-Learn 项目在面部上取得的成绩相差甚远。

根据我从经验丰富的机器学习工程师那里收到的反馈,数据应该足以对多米诺骨牌进行分类。我怀疑 SVM:s 是否真的适合作为图像分类器,但随着 Scikit-Learn 项目使用它,我认为它也应该有效。我确信 CNN 可以很好地解决这个问题,但这不是我的问题。

当我输出多米诺骨牌的“特征脸”时,它们显示为“运动模糊”,这似乎与旋转的多米诺骨牌有关。这可能是一个潜在的原因(Scikit-Learn 的面部图像未旋转)。然而,我希望模型能更好地捕捉多米诺骨牌上的点,但这种假设可能是错误的。

我现在的问题是:

问:考虑到数据的数量和类型以及使用 SVM 作为分类器,我的 10% 分数是否符合预期?还是我错过了一些关键的东西?

我的Python代码

import time
import matplotlib.pyplot as plt
from sklearn import svm, metrics
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
#from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing 
from sklearn.decomposition import PCA
import numpy as np
import os # Working with files and folders
from PIL import Image # Image processing
from PIL import ImageFilter

### 
### Data can be downloaded from https://www.dropbox.com/sh/s5f38k4l2on5mba/AACNQgXuw1edwEb6oO1w3CfOa?dl=0
### 


start = time.time()
rootdir = os.getcwd()

image_file = 'images.npy'
key_file = 'keys.npy'

def predict_me(image_file_name, scaler, pca):
  pm = Image.open(image_file_name)
  pm = pm.resize([66,66])
  a = np.array(pm.convert('L')).reshape(1,-1)
  #a = np.array(pm.resize([66,66]).convert('L')).reshape(1,-1)) # array 66x66
  a = scaler.transform(a)
  a = pca.transform(a)
  return classifier.predict(a)

def crop_image(im, sq_size):
  new_width = sq_size
  new_height = sq_size
  width, height = im.size   # Get dimensions 
  left = (width - new_width)/2
  top = (height - new_height)/2
  right = (width + new_width)/2
  bottom = (height + new_height)/2
  imc = im.crop((left, top, right, bottom))
  return imc 

#def filter_image(im):
  # All filter makes it worse
  #imf = im.filter(ImageFilter.EMBOSS)
  #return imf

def provide_altered_images(im):
  im_list = [im]
  im_list.append(im.rotate(90))
  im_list.append(im.rotate(180))
  im_list.append(im.rotate(270))
  return im_list

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,66*66), "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):  
            use_im = crop_image(im,66) # Most images are shot from too far away. This removes portions of it.
            #use_im = filter_image(use_im) # Filters image, but does no good at all
            im_list = provide_altered_images(use_im) # Create extra data with 3 rotated images of every image
            for alt_im in im_list:
              key_array.append(dir_name)  # Each image here is still the same as directory name
              numpied_image = np.array(alt_im.convert('L')).reshape(1,-1) # Converts to grayscale
              #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=0.001, C=10, kernel='rbf', class_weight='balanced') # gamma and C from tests
#le = preprocessing.LabelEncoder()
#le.fit(key)
#transformed_key = le.transform(key)
transformed_key = key


X_train, X_test, y_train, y_test = train_test_split(pixel_arr, transformed_key, test_size=0.1,random_state=7)

#scaler = preprocessing.StandardScaler()

pca = PCA(n_components=500, svd_solver='randomized', whiten=True)
# Fit on training set only.
#scaler.fit(X_train)
pca.fit(X_train)

# Apply transform to both the training set and the test set.
#X_train = scaler.transform(X_train)
#X_test = scaler.transform(X_test)

X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)


print ("Fit classifier")
classifier = classifier.fit(X_train_pca, y_train)
print ("Score = " + str(classifier.score(X_test_pca, y_test)))

# Now predict the value of the domino on the test data:
expected = y_test

print ("Predicting")
predicted = classifier.predict(X_test_pca)

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, labels  =list(set(key))))
end = time.time()
print(end - start)

输出(最后一个是以秒为单位的时间)

Score = 0.09830205540661305
Predicting
Classification report for classifier SVC(C=10, cache_size=200, class_weight='balanced', coef0=0.0, decision_function_shape='ovr', degree=3, gamma=0.001, kernel='rbf', max_iter=-1, probability=False, random_state=None, shrinking=True, tol=0.001, verbose=False):
          precision    recall  f1-score   support

  b'0x0'       0.22      0.44      0.30        27
  b'1x0'       0.24      0.23      0.24        43
  b'1x1'       0.15      0.12      0.13        49
  b'2x0'       0.13      0.15      0.14        34
  b'2x1'       0.16      0.16      0.16        44
  b'2x2'       0.02      0.03      0.03        36
  b'3x0'       0.05      0.06      0.05        36
  b'3x1'       0.05      0.05      0.05        42
  b'3x2'       0.08      0.09      0.08        46
  b'3x3'       0.15      0.16      0.15        50
  b'4x0'       0.15      0.15      0.15        40
  b'4x1'       0.07      0.05      0.06        42
  b'4x2'       0.02      0.02      0.02        41
  b'4x3'       0.09      0.08      0.09        49
  b'4x4'       0.11      0.10      0.11        39
  b'5x0'       0.18      0.12      0.14        42
  b'5x1'       0.00      0.00      0.00        38
  b'5x2'       0.02      0.02      0.02        43
  b'5x3'       0.07      0.08      0.07        36
  b'5x4'       0.07      0.04      0.05        51
  b'5x5'       0.11      0.14      0.12        42
  b'6x0'       0.03      0.03      0.03        37
  b'6x1'       0.07      0.10      0.08        31
  b'6x2'       0.03      0.03      0.03        33
  b'6x3'       0.09      0.07      0.08        45
  b'6x4'       0.02      0.03      0.03        30
  b'6x5'       0.16      0.19      0.17        37
  b'6x6'       0.10      0.08      0.09        36

   micro avg       0.10      0.10      0.10      1119
   macro avg       0.09      0.10      0.10      1119
   weighted avg       0.10      0.10      0.10      1119


115.74487614631653

最佳答案

我认为原因之一是你不应该直接将原始图像作为 SVM 分类器的输入,即使你应用了 PCA。您应该计算描述图像的形状、对比度和颜色的特征并将其放入分类器中,还是使用CNN。 CNN 是用来对图像进行分类的,并且其结构会自动计算图像的特征。

关于python - 使用 SVM 作为图像分类器时,精度/F 分数预计为 10% 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918681/

相关文章:

python - docker中的tensorflow镜像是否使用GPU?

python - 通过另一个数据框中的匹配索引划分数据框列

python - 如何修复属性错误: 'str' object has no attribute '_radius' ?

r - 错误 : *** line 1 of `undefined.cases' : bad value of . .. 属性

Python sklearn kaggle/titanic 教程在最后一个特征尺度上失败

python - 是否可以在 python 中的函数上使用模式匹配?就像一个可以响应多个调用的函数

python - Tensorflow:tf.name_scope不带 'with'

python - 在 Scikit-Learn 特征提取中合并 CountVectorizer

machine-learning - 回到 XOR 问题的基础 - 从根本上感到困惑

Python:float() 参数必须是字符串或数字,而不是 'pandas._libs.interval.Interval'