scikit-learn - 直方图的长度因情况而异
问题描述
我正在运行LBP
算法来images
按它们的纹理特征进行分类。分类方法LinearSVC
在sklearn.svm
包中。
得到直方图和拟合已经完成,SVM
但有时length
取决于histogram
.image
示例如下:
from skimage import feature
from scipy.stats import itemfreq
from sklearn.svm import LinearSVC
import numpy as np
import cv2
import cvutils
import csv
import os
def __get_hist(image, radius):
NumPoint = radius*8
lbp = feature.local_binary_pattern(image, NumPoint, radius, method="uniform")
x = itemfreq(lbp.ravel())
hist = x[:,1]/sum(x[:,1])
return hist
def get_trainHist_list(train_txt):
train_dic = {}
with open(train_txt, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter = ' ')
for row in reader:
train_dic[row[0]] = int(row[1])
hist_list=[]
key_list=[]
label_list=[]
for key, label in train_dic.items():
img = cv2.imread("D:/Python36/images/texture/%s" %key, cv2.IMREAD_GRAYSCALE)
key_list.append(key)
label_list.append(label)
hist_list.append(__get_hist(img,3))
bundle = [np.array(key_list), np.array(label_list), np.array(hist_list)]
return bundle
train_txt = 'D:/Python36/images/class_train.txt'
train_hist = get_trainHist_list(train_txt)
model = LinearSVC(C=100.0, random_state=42)
model.fit(train_hist[2], train_hist[1])
for i in train_hist[2]:
print(len(i))
test_img = cv2.imread("D:/Python36/images/texture_test/flat-3.png", cv2.IMREAD_GRAYSCALE)
hist= np.array(__get_hist(test_img, 3))
print(len(hist))
prediction = model.predict([hist])
print(prediction)
结果
26
26
26
26
26
26
25
Traceback (most recent call last):
File "D:\Python36\texture.py", line 44, in <module>
prediction = model.predict([hist])
File "D:\Python36\lib\site-packages\sklearn\linear_model\base.py", line 324, in predict
scores = self.decision_function(X)
File "D:\Python36\lib\site-packages\sklearn\linear_model\base.py", line 305, in decision_function
% (X.shape[1], n_features))
ValueError: X has 25 features per sample; expecting 26
如您所见,length
of histogram
fortraining images
全部为 26,但test_img
's 为 25。因此,predict
inSVM
不起作用。
我猜test_img
在 中有空的部分histogram
,空的部分可能已经跳过了。(我不知道)
有人有想法修复它吗?
解决方案
8 个点的邻域有 59 种不同的统一 LBP。这应该是您的特征向量的维度,但这不是因为您曾经itemfreq
计算直方图(作为旁注,itemfreq
已弃用)。通过获得的直方图的长度itemfreq
是图像中不同的均匀 LBP 的数量。如果图像中不存在一些统一的 LBP,则生成的直方图的 bin 数量将低于 59。这个问题可以通过使用bincount
以下玩具示例轻松解决:
import numpy as np
from skimage import feature
from scipy.stats import itemfreq
lbp = np.array([[0, 0, 0, 0],
[1, 1, 1, 1],
[8, 8, 9, 9]])
hi = itemfreq(lbp.ravel())[:, 1] # wrong approach
hb = np.bincount(lbp.ravel(), minlength=59) # proposed method
输出如下所示:
In [815]: hi
Out[815]: array([4, 4, 2, 2], dtype=int64)
In [816]: hb
Out[816]:
array([4, 4, 0, 0, 0, 0, 0, 0, 2, 2, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0], dtype=int64)
推荐阅读
- amazon-web-services - 使用 AWS Cognito 保护托管在 EC2 上的 Web 应用程序,而无需修改 Web 应用程序
- sql - 查询 PostgreSQL 的 Open-High-Low-Close (OHLC)
- python - 数据框中的条件连接
- c - 我应该在此代码中进行哪些更改以使其打印某些内容?insert 函数是否需要是 struct 类型才能插入节点?
- tensorflow - 为什么 batch_normalization 在训练 = True 时产生全零输出,但在训练 = False 时产生非零输出?
- node.js - 在graphql中为用户类型中的字段编写字段级解析器
- postgresql - 当我们对应用程序进行 docker 化时,docker 的卷是从哪里获取的
- authentication - 无法通过我的身份验证器应用程序登录 Heroku
- ruby-on-rails - 在 Rails 中,如果枚举有空格,有没有办法访问它的查询快捷方式?
- javascript - 将 Javascript 函数与 Ajax Post 合并