Я делаю проект детектора бананов с SVM classifier
. У меня есть 358
образцы изображений для обучения и проведения тренингов-сплит с test-size=0.2
, random_state=42
.
Вот так выглядит мой набор данных:
Я пометил каждое изображение 0
или 1
в качестве имени файла postfix
. Но classification_report(...)
всегда возвращает:
Accuracy: 0.7352941176470589
UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples.
'precision', 'predicted', average, warn_for)
precision recall f1-score support
0 0.74 1.00 0.85 50
1 0.00 0.00 0.00 18
accuracy 0.74 68
macro avg 0.37 0.50 0.42 68
weighted avg 0.54 0.74 0.62 68
Класс 1
всегда имеет 0.00
в сводке таблицы.
Мой полный исходный код:
import os
import zipfile
import numpy as np
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score
from sklearn.externals import joblib
import cv2
zip_ref = zipfile.ZipFile("dataset.zip", "r")
zip_ref.extractall()
zip_ref.close()
path = "bananas_dataset"
img_files = [(os.path.join(root, name))
for root, dirs, files in os.walk(path)
for name in files if name.endswith((".jpg"))]
winSize = (32, 32)
blockSize = (16, 16)
blockStride = (8, 8)
cellSize = (8, 8)
nbins = 9
derivAperture = 1
winSigma = -1.
histogramNormType = 0
L2HysThreshold = 0.2
gammaCorrection = 1
nlevels = 64
useSignedGradients = True
hog = cv2.HOGDescriptor(winSize, blockSize, blockStride,
cellSize, nbins, derivAperture, winSigma, histogramNormType,
L2HysThreshold, gammaCorrection, nlevels, useSignedGradients)
features = np.zeros((1, 324), np.float32)
labels = np.zeros(1, np.int64)
for i in img_files:
img = cv2.imread(i)
resized_img = cv2.resize(img, winSize)
descriptor = np.transpose(hog.compute(resized_img))
features = np.vstack((features, descriptor))
labels = np.vstack((labels, int(i[-5])))
features = np.delete(features, (0), axis=0)
labels = np.delete(labels, (0), axis=0).ravel()
X_train, X_test, y_train, y_test = train_test_split(features,
labels,
test_size=0.2,
random_state=42)
print("X_train: {}, y_train: {}".format(X_train.shape, y_train.shape))
print("X_test: {}, y_test: {}".format(X_test.shape, y_test.shape))
clf = svm.SVC()
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
print("Accuracy: {}".format(accuracy_score(y_test, y_pred)))
print("Classification report:")
print(classification_report(y_test, y_pred))
joblib.dump(clf, "banana_hog_svm_clf.pkl")
Это привело к тому, что мой процесс предсказания всегда возвращал класс 0
в качестве результата. Почему это случилось?