Ten classic machine learning algorithms: Naive Bayesian image pixel segmentation combat-Nemo fish image segmentation (python code + detailed comments)

Preface


  This article is the first major assignment of the author's pattern recognition class-using Naive Bayes to segment nemo fish image regions. It is a simple practice of using Bayesian to do binary classification . It is suitable for getting started with Bayesian algorithm. Now I put the brief theory and the code written by the author here for your reference. I don't know if any friends have any questions. It is obviously image area segmentation. How can it be related to binary classification. In fact, pixel-by-pixel image segmentation is doing classification. Of course, the segmentation here does not refer to complex semantic segmentation, but simply segmentation based on gray or color distribution.

  Bayesian Theory: Top Ten Classical Machine Learning Algorithms: Talking about Bayesian Decision Making

  EM and Bayes: Top Ten Classical Machine Learning Algorithms: Another Path EM Algorithm + Gaussian Mixture Model Actual Combat

  Code demonstration PPT and data: download


Tasks and data


  • data

  The image fish.bmpand the mask mask.mat, and the mask is multiplied by the image to obtain the ROI of the region to be segmented. There are mainly two types of areas in the small fish area. The following is to use Naive Bayes to separate these two parts-using different colors to represent different areas.

Insert picture description here
  Training data sample.mat, it is a two-dimensional matlab array, the first column is the gray value, the second column is the RGB value, the fifth column is the current gray value or the category label corresponding to the RGB value (1, -1) . It contains the distribution of gray values ​​or RGB values ​​of the two types of regions, according to which the parameters of the class probability density functions of the two types of regions are estimated.

Insert picture description here

  • task

  Task 1: Use maximum likelihood on the training data to estimate the probability density function of the gray value of the two types of regions, and use the minimum error Bayes to fish.bmpsegment the ROI gray image.
  Task 2: Use maximum likelihood on the training data to estimate the probability density function of the RGB values ​​of the two types of regions, and use the minimum error Bayes to fish.bmpsegment the ROI color image.


Theory and Realization

  The training process of the statistical model is the estimation process of the parameters of the class conditional probability density function . Through the parameter estimation method, the parameters are estimated from the training samples, and the training is completed. The test or application process is the process of statistical decision-making or classification using the estimated class probability density function and the prior probability representing the proportion of the class . Maximum likelihood is a commonly used parameter estimation method, used in Gaussian Mixture Model (GMM).

  • Maximum likelihood estimation

  Suppose we are facing a Gaussian Mixture Model (GMM) , that is, the gray value or RGB value of each category training sample obeys a normal distribution, and the normal distribution contains two parameters, the mean variance or the mean covariance.

  We know that the purpose of maximum likelihood is to find the parameters that are most likely to produce the probability density function of the sample sequence . The probability of generating the sample sequence under a certain probability density function parameter is as in formula (1), which is the likelihood function.

Insert picture description here
  When each sample is independent and identically distributed ( iid ), the corresponding Bayesian decision is Naive Bayes . Then formula (1) can be transformed into the form of probability multiplication , as in formula (2), Insert picture description here
  after taking the natural logarithm on both sides, the derivative tool can be used to find its maximum point, that is, the maximum likelihood. In the case of the Gaussian mixture model , the analytical solution of the extreme points of the likelihood function of a sample sequence is:

  One-dimensional situation: corresponding to the gray value.

Insert picture description here

Insert picture description here

  Multi-dimensional situation: The corresponding RGB value in three dimensions
Insert picture description here
Insert picture description here
  can be seen from the above 4 formulas. The formulas for finding the mean and variance that we are accustomed to are actually the result of maximum likelihood estimation under the premise of defaulting the central limit theorem.

copyright © Yi Shu: https://blog.csdn.net/sinat_35907936/article/details/109167111

  • Minimum error Bayesian decision

  From the top ten classic algorithms of machine learning: talk about Bayesian decision-making in a simple way , we can know that when the category status is equalized, the minimum error rate or the maximum posterior probability Bayesian decision can be used. Only by comparing the numerator, that is, the following formula, you can get the decision-choose the larger result of the two categories as the decision result.

Insert picture description here

  • Algorithm flow

  The algorithm flow is shown in the figure below. The first half is the training process of the statistical model, that is, the maximum likelihood (MLE) is used for parameter estimation. The test process is the process of decision-making. The picture shows the process of making grayscale or color decision or segmentation pixel by pixel for Nemo fish. PDF is a normal distribution PDF function included in the scipy package. You only need to give its parameters and input to get the output value.
Insert picture description here

  • Algorithm implementation

  Data loading and analysis. Python can read matlab files through the scipy module. After the matlab file is read, it is displayed in the form of a dictionary.

from scipy import io
from scipy.stats import norm
import numpy as np
import PIL.Image as Image
import matplotlib.pyplot as plt
from scipy.stats import multivariate_normal

# 数据加载与解析
mask = io.loadmat('mask.mat')['Mask']                              # 数据为一个字典,根据key提取数据
sample = io.loadmat('sample.mat')['array_sample']
src_image = Image.open('fish.bmp')
RGB_img = np.array(src_image)
Gray_img = np.array(src_image.convert('L'))

# 根据Mask,获取ROI区域
Gray_ROI = (Gray_img * mask)/255
RGB_mask = np.array([mask, mask, mask]).transpose(1, 2, 0)
RGB_ROI = (RGB_img * RGB_mask)/255

# 根据标签拆分数据
gray1 = []
gray2 = []
RGB1 = []
RGB2 = []

for i in range(len(sample)):
    if(sample[i][4]) == 1.:                                           # 数据第5位为标签
        gray1.append(sample[i][0])
        RGB1.append(sample[i][1:4])
    else:
        gray2.append(sample[i][0])
        RGB2.append(sample[i][1:4])

RGB1 = np.array(RGB1)
RGB2 = np.array(RGB2)

  Calculate the prior probability. The prior probability can be obtained by the proportion of each category in the sample. It can also come from the proportion of each category in the world.

# 计算两类在数据中的占比,即先验概率
P_pre1 = len(gray1)/len(sample)
P_pre2 = 1-P_pre1

  One-dimensional Bayesian decision-making, which calculates the Bayesian posterior probability of one-dimensional input.

# 一维时,贝叶斯
# ------------------------------------------------------------------------------------#
# 数据为一维时(灰度图像),用最大似然估计两个类别条件概率pdf的参数——标准差与均值
gray1_m = np.mean(gray1)
gray1_s = np.std(gray1)
gray2_m = np.mean(gray2)
gray2_s = np.std(gray2)
# print(gray1_s, gray2_s)

# 绘制最大似然估计出的类条件pdf
x = np.arange(0, 1, 1/1000)
gray1_pdf = norm.pdf(x, gray1_m, gray1_s)
gray2_pdf = norm.pdf(x, gray2_m, gray2_s)

plt.figure(0)
ax = plt.subplot(2, 1, 1)
ax.set_title('p(x|w)')

ax.plot(x, gray1_pdf, 'r', x, gray2_pdf, 'b')
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
ax1 = plt.subplot(2, 1, 2)
ax1.plot(x, P_pre1*gray1_pdf, 'r', x, P_pre2*gray2_pdf, 'b')
ax1.set_title('p(w)*p(x|w)')
ax1.set_xlabel('x')
ax1.set_ylabel('f(x)')

# 用最大后验贝叶斯对灰度图像进行分割
gray_out = np.zeros_like(Gray_img)
for i in range(len(Gray_ROI)):
    for j in range(len(Gray_ROI[0])):
        if Gray_ROI[i][j] == 0:
            continue
        elif P_pre1*norm.pdf(Gray_ROI[i][j], gray1_m, gray1_s) > P_pre2*norm.pdf(Gray_ROI[i][j], gray2_m, gray2_s):   # 贝叶斯公式分子比较
            gray_out[i][j] = 100
        else:
            gray_out[i][j] = 255

# plt.imshow(RGB_ROI)
plt.figure(1)
bx = plt.subplot(1, 1, 1)
bx.set_title('gray ROI')
bx.imshow(Gray_ROI, cmap='gray')
plt.figure(2)
bx1 = plt.subplot(1, 1, 1)
bx1.set_title('gray segment result')
bx1.imshow(gray_out, cmap='gray')

  Segmentation result:

Insert picture description here

copyright © Yi Shu: https://blog.csdn.net/sinat_35907936/article/details/109167111

  Three-dimensional Bayesian decision-making, to calculate the Bayesian posterior probability of three-dimensional input.

# 三维时,贝叶斯
# ------------------------------------------------------------------------------------#
# 数据为三维时(彩色图像),用最大似然估计两个类别条件概率pdf的参数——协方差与均值
RGB1_m = np.mean(RGB1, axis=0)
RGB2_m = np.mean(RGB2, axis=0)

cov_sum1 = np.zeros((3, 3))
cov_sum2 = np.zeros((3, 3))

for i in range(len(RGB1)):
    # print((RGB1[i]-RGB1_m).reshape(3, 1))
    cov_sum1 = cov_sum1 + np.dot((RGB1[i]-RGB1_m).reshape(3, 1), (RGB1[i]-RGB1_m).reshape(1, 3))

for i in range(len(RGB2)):
    cov_sum2 = cov_sum2 + np.dot((RGB2[i]-RGB2_m).reshape(3, 1), (RGB2[i]-RGB2_m).reshape(1, 3))

RGB1_cov = cov_sum1/(len(RGB1)-1)                      # 无偏估计除以N-1
RGB2_cov = cov_sum2/(len(RGB2)-1)

xx = np.array([x, x, x])
# print(P_pre1*multivariate_normal.pdf(RGB1, RGB1_m, RGB1_cov))

# 用最大后验贝叶斯对彩色图像进行分割
RGB_out = np.zeros_like(RGB_ROI)
for i in range(len(RGB_ROI)):
    for j in range(len(RGB_ROI[0])):
        if np.sum(RGB_ROI[i][j]) == 0:
            continue
        elif P_pre1*multivariate_normal.pdf(RGB_ROI[i][j], RGB1_m, RGB1_cov) > P_pre2*multivariate_normal.pdf(RGB_ROI[i][j], RGB2_m, RGB2_cov): # 贝叶斯公式分子比较
            RGB_out[i][j] = [255, 0, 0]
        else:
            RGB_out[i][j] = [0, 255, 0]
# print(RGB_ROI.shape)

# 显示RGB ROI,与彩色分割结果
plt.figure(3)
cx = plt.subplot(1, 1, 1)
cx.set_title('RGB ROI')
cx.imshow(RGB_ROI)
plt.figure(4)
cx1 = plt.subplot(1, 1, 1)
cx1.set_title('RGB segment result')
cx1.imshow(RGB_out)
plt.show()

  Segmentation result:
Insert picture description here

copyright © Yi Shu: https://blog.csdn.net/sinat_35907936/article/details/109167111


reference


  Zhang Xuegong. Pattern Recognition (Third Edition) . M. Tsinghua University Press. 2010.

Guess you like

Origin blog.csdn.net/sinat_35907936/article/details/109167111