-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlbp_model.py
72 lines (65 loc) · 2.77 KB
/
lbp_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time : 2023/4/18 23:18
# @Author : achieve_dream
# @File : lbp_model.py
# @Software: Pycharm
import numpy as np
from skimage.feature import local_binary_pattern
from model import Model
class LBPModel(Model):
"""
机器学习, 图像分类模型
"""
def __init__(self, img_nums: int = 300, n_points: int = 12, radius: int = 5):
"""
初始化对象
:param n_points: lbp采样点数
:param radius: lbp的采样半径
:param img_nums: 加载数据集图片数量
"""
super().__init__(img_nums)
self.__n_points = n_points
self.__radius = radius
def compute(self):
"""
计算所有数据集的lbp值, 然后算出后面9列对第一列的距离, 并用sigmoid进行归一化
:return: None
"""
# 存储第一列的lbp, 方便后面的数据集和它进行比较
lbp1_list = [local_binary_pattern(self.read_img(col_1), self.__n_points, self.__radius, method='ror') for col_1
in self.dataset[:, 0]]
predicts = []
target_labels = []
# 预测正确的个数
positive_nums = 0
# 计算后9列到第1列的距离, 30 * 9 * 30
row_index = 0
# 图像有多少分类
class_nums = self.img_nums // 10
for cols in self.dataset[:, 1:]: # class_nums 行
# 9 * 30, 真实标签[ 1, 0, 0, 0 ..., 0] * 9 (9列属于同一个类)
target_labels_rows = [*[0] * row_index, 1, *[0] * (class_nums - 1 - row_index)] * 9
# 展开为一行
target_labels.extend(target_labels_rows)
for col in cols: # 9 列
lbp2 = local_binary_pattern(self.read_img(col), self.__n_points, self.__radius, method='ror')
# 先计算lbp的欧式距离, 然后通过softmax预测出这10个类别的概率
predict_scores = self.softmax(np.array([np.linalg.norm(lbp - lbp2) for lbp in lbp1_list]))
# 概率最大值的下标即分类标签
if predict_scores.argmax() == row_index:
positive_nums += 1
# 9 * 30, 预测标签
predicts.extend(predict_scores)
row_index += 1
return np.array(target_labels), np.array(predicts), positive_nums / (class_nums * 9)
def run(self):
targets, predicts, accuracy = self.compute()
print(f"LBP准确率: {round(accuracy, 2)}")
np.save("target_labels", targets)
np.save("lbp_predicts", predicts)
self.plot(targets, predicts, "lbp_roc")
if __name__ == '__main__':
model = LBPModel(img_nums=300, n_points=12, radius=5)
# model.plot(np.load("target_labels.npy"), np.load("lbp_predicts.npy"))
model.run()