[Machine Learning] sklearn's integrated learning is used for image classification from 0 to 1, attention points and pit points


foreword

After the rise of deep learning, it seems that machine learning has declined, but it is still very useful in fixed scenarios. The following is the identification task of the exhibition hall project. As a rule, the basic knowledge of integrated learning is less, or not, because there are already many such articles. Mainly scenario-based business problem solving.


1. Demand Analysis

1.1 Scenarios

insert image description here
The small figures in the picture frame will move to the left near the water and call the police. Target detection such as yolov is achievable, but now I want to learn about sklearn's integrated learning. So adopted this method.

challenge, change of light
insert image description here

1.2 Solutions

The camera is not moving, so opencv is used to cut this part out for classification. There are two types of people and no one. 0 means there is someone, and 1 means no one.
insert image description here

2. Code

2.1 Feature extraction

There are many ways to extract image features, here are two haar and lbp

# 提取Haar特征
def extract_haar_features(images):
    features = []

    for image in images:
        # a = 1
        # feature = cv2.HOGDescriptor().compute(image)
        feature = cv2.HOGDescriptor().compute(image).flatten()
        features.append(feature)

    return np.array(features)

# 提取lbp特征
def extract_lbp_features(images):
    features = []

    for image in images:
        # a = 1
        feature = local_binary_pattern(image, 8, 1, method='uniform').flatten()
        features.append(feature)

    return np.array(features)

2.2 Building a classifier

I used decision tree, random forest, KNN, and SVM four classifiers here, because they are packaged by sklearn, and they are very simple to call.

# 构建决策树分类器
def build_decision_tree_classifier(X_train, y_train):
    clf = DecisionTreeClassifier(max_depth=5)
    clf.fit(X_train, y_train)
    return clf

# 构建随机森林分类器
def build_random_forest_classifier(X_train, y_train):
    clf = RandomForestClassifier(n_estimators=50, max_depth=3)
    clf.fit(X_train, y_train)
    return clf

# 构建KNN分类器
def build_knn_classifier(X_train, y_train):
    clf = KNeighborsClassifier(n_neighbors=10,algorithm='kd_tree')
    clf.fit(X_train, y_train)
    return clf

# 构建SVM分类器
def build_svm_classifier(X_train, y_train):
    clf = SVC(kernel='linear', C=0.025)
    clf.fit(X_train, y_train)
    return clf

2.4 Integration Model

Build the ensemble model and save the weights, the final prediction of clf is formed by voting of several classifiers.

# 构建集成模型
def build_ensemble_model(X_train, y_train):
    clfs = []

    clf1 = build_decision_tree_classifier(X_train, y_train)
    clfs.append(('dt', clf1))

    clf2 = build_random_forest_classifier(X_train, y_train)
    clfs.append(('rf', clf2))

    clf3 = build_knn_classifier(X_train, y_train)
    clfs.append(('knn', clf3))

    clf4 = build_svm_classifier(X_train, y_train)
    clfs.append(('svm', clf4))

    eclf = VotingClassifier(estimators=clfs, voting='hard')
    
    # 输出训练过程
    for clf in [clf1,clf2,clf3,clf4, eclf]:
        clf.fit(X_train, y_train)
        y_pred = clf.predict(X_test)
        acc = accuracy_score(y_test, y_pred)
        print(f'{
      
      clf.__class__.__name__} Accuracy: {
      
      acc}')

    eclf.fit(X_train, y_train)

    # 保存模型权重
    joblib.dump(eclf, target_weight_path)
    


    return eclf

2.5 Total training code

import cv2
import numpy as np
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, VotingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import joblib
from util_help import read_images
import os
import pickle
from skimage.feature import local_binary_pattern

model_name = "people_drown"
weight_name = model_name + ".joblib"
save_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"weight",model_name)
os.makedirs(save_dir,exist_ok=True)
target_weight_path = os.path.join(save_dir,weight_name)
# 提取Haar特征
def extract_haar_features(images):
    features = []

    for image in images:
        # a = 1
        # feature = cv2.HOGDescriptor().compute(image)
        feature = cv2.HOGDescriptor().compute(image).flatten()
        features.append(feature)

    return np.array(features)

# 提取Haar特征
def extract_lbp_features(images):
    features = []

    for image in images:
        # a = 1
        feature = local_binary_pattern(image, 8, 1, method='uniform').flatten()
        features.append(feature)

    return np.array(features)

# 构建决策树分类器
def build_decision_tree_classifier(X_train, y_train):
    clf = DecisionTreeClassifier(max_depth=5)
    clf.fit(X_train, y_train)
    return clf

# 构建随机森林分类器
def build_random_forest_classifier(X_train, y_train):
    clf = RandomForestClassifier(n_estimators=50, max_depth=3)
    clf.fit(X_train, y_train)
    return clf

# 构建KNN分类器
def build_knn_classifier(X_train, y_train):
    clf = KNeighborsClassifier(n_neighbors=10,algorithm='kd_tree')
    clf.fit(X_train, y_train)
    return clf

# 构建SVM分类器
def build_svm_classifier(X_train, y_train):
    clf = SVC(kernel='linear', C=0.025)
    clf.fit(X_train, y_train)
    return clf

# 构建集成模型
def build_ensemble_model(X_train, y_train):
    clfs = []

    clf1 = build_decision_tree_classifier(X_train, y_train)
    clfs.append(('dt', clf1))

    clf2 = build_random_forest_classifier(X_train, y_train)
    clfs.append(('rf', clf2))

    clf3 = build_knn_classifier(X_train, y_train)
    clfs.append(('knn', clf3))

    clf4 = build_svm_classifier(X_train, y_train)
    clfs.append(('svm', clf4))

    eclf = VotingClassifier(estimators=clfs, voting='hard')
    
    # 输出训练过程
    for clf in [clf1,clf2,clf3,clf4, eclf]:
        clf.fit(X_train, y_train)
        y_pred = clf.predict(X_test)
        acc = accuracy_score(y_test, y_pred)
        print(f'{
      
      clf.__class__.__name__} Accuracy: {
      
      acc}')

    eclf.fit(X_train, y_train)

    # 保存模型权重
    joblib.dump(eclf, target_weight_path)
    


    return eclf

# 对测试集进行预测
def predict(model, test_features):
    preds = model.predict(test_features)
    return preds

# 评估模型准确率
def evaluate(y_true, y_pred):
    acc = accuracy_score(y_true, y_pred)
    print(f'Accuracy: {
      
      acc}')

# 加载模型权重

clf = joblib.load(target_weight_path) if os.path.exists(target_weight_path)  else None

image_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),"data","handle",model_name)
# 读取数据集
images, labels = read_images(image_dir)
print(len(images))
# 提取特征
# features = extract_haar_features(images)
features = extract_lbp_features(images)

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.25, random_state=42)

# 构建或加载集成模型
if clf is None:
    clf = build_ensemble_model(X_train, y_train)

# 对测试集进行预测
y_pred = predict(clf, X_test)

# 评估模型性能
evaluate(y_test, y_pred)

3. fast api encapsulation

To be continued, under development.

4. Summary

For light effects, does converting to grayscale make a difference - this is a big topic? to be continued.
In addition, when using Haar to extract features, the weight file will reach a terrifying 12G, and the memory will explode during training. This problem delayed me for a long time, and the reason is unknown.
Later, the method of changing to lbp is very small and the speed is very fast.

Guess you like

Origin blog.csdn.net/weixin_40293999/article/details/130523651
Recommended