# Water Quality Monitoring Method Based on TLD 3D Fish Tracking and XGBoost

Authors: Shuhong Cheng; Shijun Zhang; Leihua Li; Dianfan Zhang
Journal: Mathematical Problems in Engineering
 (2018)

---

## Abstract

Aiming at the problem of water quality monitoring, this paper presents a method of biological water quality monitoring based on TLD (Tracking-Learning-Detection) framework and XGBoost (eXtreme Gradient Boosting). Firstly, under the framework of TLD, an independent tracking system is designed; TLD captures 3D coordinate information of fish based on video and calculates the behavior of fish movement parameters which can reflect the change of water quality via processing the coordinate information of the fish body. The data of coordinate information will be more prominent via the data processing. The integration of all built XGBoost water quality monitoring model which is based on characteristic parameters; the model was used to analyze and evaluate fish behavior parameters under unknown water quality to achieve the purpose of water quality monitoring.


---

## Body

## 1. Introduction

In the era of Industrial 4.0, highly automated and intelligent manufacturing technology will gradually occupy the field of human society in the field of industrial development. In the field of water quality pollution, an efficient, convenient, and intelligent monitoring method becomes more urgent and necessary. In order to prevent and deal with the current situation of water quality pollution, the existing water quality monitoring technologies are physical and chemical analysis technology, automatic detection technology, and biological monitoring technology [1, 2]. The physical and chemical analysis technology and automatic detection technology for long-term real-time monitoring cost are relatively large; at the same time, these two technologies can not achieve the comprehensive evaluation of water pollution degree. The biological monitoring technique has been adopted. Biological monitoring technology is comprehensive, rich, and continuous; in recent years it plays an increasingly important role and has a broad application prospect in the environmental assessment. In the monitoring of biological water quality, fish, as an important indicator organism, with its movement characteristics, physiological characteristics, and other information directly reflect the changes in the water environment and the current situation of environmental pollution. Many scholars such as Kim et al. obtain the parameters of motion behavior of fish by computer vision technology [3–8]; only a few scholars put forward the method of water quality anomaly monitoring according to these characteristic parameters. Serra-Toro et al., Lai and Chiu, and Zhangzan et al., respectively, analyze the fish swimming behavior to get the relationship between exercise behavior parameters and water quality via recursive algorithm, fuzzy reasoning method, and data evaluation, so as to achieve the purpose of monitoring the abnormal water quality [9–11].Although these methods can be used for monitoring, there are a lot of defects in the selection of feature parameters, the length of operation, the accuracy of monitoring, and the processing of individual differences. The choice of parameters has great influence on the model; the model cannot choose effective parameters to monitor; it can only be passively chosen by human choice; because the monitoring process of these methods was divided into a plurality of independent links, from data acquisition to the final monitoring, the whole operation was too long to show the results in time and effectively; data acquisition and processing process was too complex; each link increases the error component data, resulting in low accuracy of monitoring results. It only selects features unilaterally; the sample data can not reflect the difference, the sample can not be comprehensively taken into account, and the monitoring results also are one-sided.This paper presents the water quality monitoring methods under the framework of TLD and XGBoost; based on 3D fish tracking, we use TLD method to get the fish body coordinate in different water conditions and determine the characteristic parameters based on the calculation results of fish movement parameters. XGBoost is adopted to analyze and evaluate the feature parameter, establishing semantic mapping, and the movement of fish behavior characteristic parameters model, and finally it realized the abnormal water quality monitoring.


## 2. Extraction of Fish Characteristic Parameters

### 2.1. TLD Framework Introduction

TLD was a long-term single objective tracking algorithm proposed by Dr. Kalal et al. [12, 13]. The TLD framework mainly includes three parts: tracking part, learning part, and detection part. The relationship of the three parts and the tracking process is shown in Figure 1, for tracking part, mainly using the Forward-Backward Error method, using Lucas-Kanade optical flow tracking, the tracking results using Forward-Backward Error as feedback, the Euclidean distance for FB error compared with the original position, the distance which the longer will be abandon, this tracking method of using FB error to discard bad values was called Median Flow, which discards those larger than 50% of the European distance set; detection of TLD has three parts: the variance classifier module, set classifier module, and nearest neighbor classifier module; these three classifiers are cascaded. Each scan window of the current frame sequentially passes through the above three classifiers, both are considered to contain foreground objects; the learning part is the same as in other methods of target detection; detection module in TLD may also have errors, and the error was nothing more than the two cases being negative sample error and positive sample error. The learning module is used according to the results of the tracking module to evaluate the two kinds of errors of detection module, based on the results of the assessment to generate the target training samples and update the detection module; meanwhile, the tracking module “key points” were updated, in order to avoid similar mistakes.Figure 1
TLD frame structure.


### 2.2. Fish Tracking Framework

We put forward a more specific framework for fish tracking based on the framework of TLD. Imitating TLD settings, the process of track is also divided into three parts, each part of the connection is as shown in Figure2; we collected video by frame rate interception and input it to the TLD tracking module.Figure 2
Fish tracking frame structure.At any time, the tracked target could be represented by its state property. The state property can be a tracking box that represents the location and size of the target, or a marker that identifies whether the tracked target was visible. The two-track box in the space domain similarity was used to measure the overlap; the calculation method is that the two track frame intersections have joint segmentation. The shape of the object was represented by image patch P, and each fish body image was sampled from the tracking frame and normalized to the same size. In the tracking module, NCC is said to be the normalized correlation coefficient; BF (brute force selection mechanism) is said to be the forced choice mechanism, tracking each pixel in a first frame in the whole fish body image sequence; FB (Forward-Backward Error) was used to estimate trajectory. Finally, through the interaction of NCC and BF, we can get the stable tracking point by taking the two intersections.The fish body images were not only input to the tracking module, but also used in the detection module. The test needs to use the positive and negative training sample information for training in advance, so that the detection module has certain detection ability. The detection module includes three filters, respectively, Variance filter, Fern variance filter (Random-Fern), and NN filter; through the three filters the errors in image are removed and the best target fish image is obtained. The tracking module and the detection module obtain the sample information to be fused and input to the learning module. Learning module used P-N learning. The main idea was that the detector error can be identified by two types of constraints. The function of P-expert was to find the new appearance (deformation) of the target and to increase the number of positive samples. The role of N-expert was to generate negative training samples. The premise of N-expert was that the tracked object may only appear in a position in the video frame, so if the position of the foreground target is determined, all around it must be a negative sample. The learning module was used to correct the tracking module and the detection module, and the Fern structure would be updated, and the weight is adjusted to make the next result more reliable.The image of the fish captured by the video was processed by the above steps, and finally the coordinate information of the fish in the three-dimensional space was obtained. Because the TLD tracking result was the two-dimensional plane coordinate information, the camera position needs to be arranged in order to collect three-dimensional coordinates. The camera was placed in the vertical direction and the horizontal direction of the fish tank, as shown in Figures3(a) and 3(b); the two pictures show our camera positioning; in order to facilitate viewing, vertical plane and horizontal plane with a fish tank were drawn out; the blue side is said to be the vertical direction to the information acquisition of X-Y; the green part is said to be the horizontal direction to the information acquisition of X-Z.Figure 3
Camera position and fish body space coordinates.
(a) (b)The position of some fish in the space is as shown in Figure4. Figures 4(a) and 4(c) show that the fish coordinate point projected onto a plane and get the scatterplot. In (b), (d) the blue oval line in the graph represents different points in different plane; elliptical is said to be in a different point in different plane. So you can see clearly the distribution of fish in the three-dimensional space. The x-axis of the two cameras overlap, and the x-axis overlap information is removed to get the 3D information of the x-y-z surface.Figure 4
Camera position and fish body space coordinates.
(a) (b) (c) (d)


### 2.3. Parameter Calculationa

The trajectory of fish can be expressed as a series of discrete discontinuous points; that is to say, the trajectory ofn video sequence images of a fish can be described as(1)T=xt,yt,zt. Getting fish behavior characteristic parameters which can reflect the status of the water ecological environment by using trajectory: average moving distance, speed, acceleration, X direction discrete distance, Y direction discrete distance, and distribution area, the definitions are as follows.Average moving distance:(2)d=x1-x22+y1-y22+z1-z22.(x1,y1,z1) and (x2,y2,z2) are used to represent the coordinates of the starting point and the end point of the fish target tracking trajectory in unit time. Speed V would be defined as(3)v=dt. Accelerationa would be defined as(4)a=v1-v2t.v1,v2, respectively, expressed the speed of t1,t2,Δt=t2-t1,(t2>t1).The distribution of fish in a single plane has great difference between normal and abnormal water quality; we use measured dispersion to describe the relationship between the single fish and fish. The dispersion formula would be defined as(5)LS2=∑i=1Nxi-x¯2∑i=1Nyi-y¯2∑i=1Nzi-z¯2.The characteristics of the dispersion of the fish body coordinates were concerned with the position of the center of gravity and the center of gravity of each individual, which integrates the characteristics of the whole and the individual. Similarly, in the X-Y plane, the distribution area of fish also has great features. Shoal area calculation formula is(6)S=S1=Δx×ΔyS1<S2,S2=πr2S1>S2.Δx represents the maximum distance between X coordinates of all individuals in a fish at the same time, Δy is said to be the maximum distance between Y coordinate; r is said to be the fish gathered together to form the minimum circumscribed circle radius. Finally, we use TLD and especially placed cameras to collect the coordinates; the characteristic parameters are obtained through calculation to establish a large enough data set.


## 2.1. TLD Framework Introduction

TLD was a long-term single objective tracking algorithm proposed by Dr. Kalal et al. [12, 13]. The TLD framework mainly includes three parts: tracking part, learning part, and detection part. The relationship of the three parts and the tracking process is shown in Figure 1, for tracking part, mainly using the Forward-Backward Error method, using Lucas-Kanade optical flow tracking, the tracking results using Forward-Backward Error as feedback, the Euclidean distance for FB error compared with the original position, the distance which the longer will be abandon, this tracking method of using FB error to discard bad values was called Median Flow, which discards those larger than 50% of the European distance set; detection of TLD has three parts: the variance classifier module, set classifier module, and nearest neighbor classifier module; these three classifiers are cascaded. Each scan window of the current frame sequentially passes through the above three classifiers, both are considered to contain foreground objects; the learning part is the same as in other methods of target detection; detection module in TLD may also have errors, and the error was nothing more than the two cases being negative sample error and positive sample error. The learning module is used according to the results of the tracking module to evaluate the two kinds of errors of detection module, based on the results of the assessment to generate the target training samples and update the detection module; meanwhile, the tracking module “key points” were updated, in order to avoid similar mistakes.Figure 1
TLD frame structure.


## 2.2. Fish Tracking Framework

We put forward a more specific framework for fish tracking based on the framework of TLD. Imitating TLD settings, the process of track is also divided into three parts, each part of the connection is as shown in Figure2; we collected video by frame rate interception and input it to the TLD tracking module.Figure 2
Fish tracking frame structure.At any time, the tracked target could be represented by its state property. The state property can be a tracking box that represents the location and size of the target, or a marker that identifies whether the tracked target was visible. The two-track box in the space domain similarity was used to measure the overlap; the calculation method is that the two track frame intersections have joint segmentation. The shape of the object was represented by image patch P, and each fish body image was sampled from the tracking frame and normalized to the same size. In the tracking module, NCC is said to be the normalized correlation coefficient; BF (brute force selection mechanism) is said to be the forced choice mechanism, tracking each pixel in a first frame in the whole fish body image sequence; FB (Forward-Backward Error) was used to estimate trajectory. Finally, through the interaction of NCC and BF, we can get the stable tracking point by taking the two intersections.The fish body images were not only input to the tracking module, but also used in the detection module. The test needs to use the positive and negative training sample information for training in advance, so that the detection module has certain detection ability. The detection module includes three filters, respectively, Variance filter, Fern variance filter (Random-Fern), and NN filter; through the three filters the errors in image are removed and the best target fish image is obtained. The tracking module and the detection module obtain the sample information to be fused and input to the learning module. Learning module used P-N learning. The main idea was that the detector error can be identified by two types of constraints. The function of P-expert was to find the new appearance (deformation) of the target and to increase the number of positive samples. The role of N-expert was to generate negative training samples. The premise of N-expert was that the tracked object may only appear in a position in the video frame, so if the position of the foreground target is determined, all around it must be a negative sample. The learning module was used to correct the tracking module and the detection module, and the Fern structure would be updated, and the weight is adjusted to make the next result more reliable.The image of the fish captured by the video was processed by the above steps, and finally the coordinate information of the fish in the three-dimensional space was obtained. Because the TLD tracking result was the two-dimensional plane coordinate information, the camera position needs to be arranged in order to collect three-dimensional coordinates. The camera was placed in the vertical direction and the horizontal direction of the fish tank, as shown in Figures3(a) and 3(b); the two pictures show our camera positioning; in order to facilitate viewing, vertical plane and horizontal plane with a fish tank were drawn out; the blue side is said to be the vertical direction to the information acquisition of X-Y; the green part is said to be the horizontal direction to the information acquisition of X-Z.Figure 3
Camera position and fish body space coordinates.
(a) (b)The position of some fish in the space is as shown in Figure4. Figures 4(a) and 4(c) show that the fish coordinate point projected onto a plane and get the scatterplot. In (b), (d) the blue oval line in the graph represents different points in different plane; elliptical is said to be in a different point in different plane. So you can see clearly the distribution of fish in the three-dimensional space. The x-axis of the two cameras overlap, and the x-axis overlap information is removed to get the 3D information of the x-y-z surface.Figure 4
Camera position and fish body space coordinates.
(a) (b) (c) (d)


## 2.3. Parameter Calculationa

The trajectory of fish can be expressed as a series of discrete discontinuous points; that is to say, the trajectory ofn video sequence images of a fish can be described as(1)T=xt,yt,zt. Getting fish behavior characteristic parameters which can reflect the status of the water ecological environment by using trajectory: average moving distance, speed, acceleration, X direction discrete distance, Y direction discrete distance, and distribution area, the definitions are as follows.Average moving distance:(2)d=x1-x22+y1-y22+z1-z22.(x1,y1,z1) and (x2,y2,z2) are used to represent the coordinates of the starting point and the end point of the fish target tracking trajectory in unit time. Speed V would be defined as(3)v=dt. Accelerationa would be defined as(4)a=v1-v2t.v1,v2, respectively, expressed the speed of t1,t2,Δt=t2-t1,(t2>t1).The distribution of fish in a single plane has great difference between normal and abnormal water quality; we use measured dispersion to describe the relationship between the single fish and fish. The dispersion formula would be defined as(5)LS2=∑i=1Nxi-x¯2∑i=1Nyi-y¯2∑i=1Nzi-z¯2.The characteristics of the dispersion of the fish body coordinates were concerned with the position of the center of gravity and the center of gravity of each individual, which integrates the characteristics of the whole and the individual. Similarly, in the X-Y plane, the distribution area of fish also has great features. Shoal area calculation formula is(6)S=S1=Δx×ΔyS1<S2,S2=πr2S1>S2.Δx represents the maximum distance between X coordinates of all individuals in a fish at the same time, Δy is said to be the maximum distance between Y coordinate; r is said to be the fish gathered together to form the minimum circumscribed circle radius. Finally, we use TLD and especially placed cameras to collect the coordinates; the characteristic parameters are obtained through calculation to establish a large enough data set.


## 3. Establishment of Water Quality Monitoring Model

XGBoost (eXtreme Gradient Boosting) [14, 15] is designed by Dr. Chen Tianqi in Gradient Machine (GBDT) and the GBDT has been improved in boosting. On the basis of GBDT, XGBoost modified the objective function and loss function.


### 3.1. Objective Function, Obj

First of all, the objective function of GBDT is as follows:(7)Objective:∑i=1nlyi,y^i+∑kΩfk,fk∈F, where l is the loss function, Ω(fk) is a regular term, and fk is tree complexity. After series of processing, we get a new objective function which is easier to use:(8)Objt:∑i=1nlyi,y^it-1+ftxi+Ωft+constant. XGBoost Taylor expansion for ft(xi) makes it more similar to our previous goals. The process is as follows:(9)fx+Δx≃fx+f′xΔx+12f′′xΔx2. By applying Taylor expansion to the three terms, we can make it clear that the final objective function depends on the first derivative and the second derivative of the error function of each data point.Definition:(10)gi=δy^t-1lyi,y^t-1,hi=δy^t-12lyi,y^t-1. Bring (9) and (10) back to (8):(11)Objt≃∑i=1nlyi,y^it-1+giftxi+12hift2xi+Ωft+constant. Taking into account the square loss, can also continue to (10):(12)gi=δy^t-1y^t-1-yi2=2y^t-1-yi,hi=δy^t-12yi-y^t-12=2. In this way, we can remove the constant term and get the new objective function:(13)∑i=1ngiftxi+12htft2xi+Ωft. In the equation, gi=δy^(t-1)l(yi,y^(t-1)),hi=δy^(t-1)2l(yi,y^(t-1)).Then define the complexity of the tree:(14)ftx=wqx,w∈RT,q:Rd⟶1,2,3,…,T, where w is a leaf vector, q is a tree structure, which defines the complexity of the number of nodes in a tree, and L2 is square of the output of each tree node. Under this new definition, we can rewrite the objective function as follows: I is defined as a set of samples on each leaf; Ij={i∣q(xi)=j}.(15)Objt≃∑i=1ngiftxi+12hift2xi+ΩftObjt=∑i=1ngiwqxi+12hiwqxi2+γT+λ12∑j=1Twj2Objt=∑j=1T∑i∈Ijgiwj+12∑i∈Ijhj+λwj2+γT.To further simplify the formula, we define(16)Gj=∑i∈Ijgi,Hj=∑i∈Ijhi,(17)Objt≃∑j=1T∑i∈Ijgiwj+12∑i∈Ijhj+λwj2+γT,(18)Objt=∑j=1TGjwj+12Hj+λwj2+λT. Based on the derivation of it being equal to 0, the following can be obtained:(19)wj∗=-GjHj+λ. Bring the optimal solution wj∗ back to (18), and the final objective function is obtained:(20)Obj=-12∑j=1TGj2Hj+λ+γT. The above objective function, Obj, represents the maximum amount of reduction in the target when the structure of a tree is specified. You can call it a structural score. The smaller the Obj, the better the structure of the tree.


### 3.2. Add Scoring Function Gain

(21) G a i n = 1 2 G L 2 H L + λ + G R 2 H R + λ + G L + G R 2 H L + H R + λ - γ . G L 2 / H L + λ represents the left subtree fraction, GR2/HR+λ represents the right subtree fraction, (GL+GR)2/HL+HR+λ represents an undivided score, and γ represents the complexity cost of adding new leaf nodes. For each expansion, or to efficiently enumerate all possible segmentation schemes, suppose you want to enumerate all the conditions of x<a, for a particular splita to calculatea’s left and right derivative, as shown in Figure 5.Figure 5
Sketch map of segmentation.It would be found that for all ‘a’, you can give all the gradients and GL and GR as long as a scan is done from left to right. And then the above formula can be used to calculate the score of each partition.


### 3.3. XGBoost Biological Water Quality Monitoring Model

XGBoost joined the regularization; regularized boosting was a great help in reducing overfitting. Compared with GBDT the XGBoost can achieve parallel processing; the speed has been greatly improved; comparing SVM classification with XGBoost, XGBoost allows the user to define custom optimization objectives and evaluation criteria; it will add a new dimension in the model, so it is not subject to any restrictions on the data processing; in the process of data collection, because various artificial or experimental defects exist, it will inevitably lead to the phenomenon of data loss; while the XGBoost was built to deal with the missing value rules, the user needs to provide a different values sample and then take it as a parameter input; so this value is taken as missing values; XGBoost has different treatment methods in different nodes encountered with missing values and will learn when encountering the missing value in the future how to deal with it. The method provides a good solution for the deletion of the fish characteristic parameters, and the XGBoost allows the use of cross validation in each round of boosting iteration. Therefore, the optimal number of boosting iterations can be easily obtained; XGBoost can continue to train on the results of the previous round. This feature was a great advantage in the application of the classification of fish characteristic parameters and can realize the continuity of the model.Therefore, we try to use XGBoost to replace the previous classifier to establish water quality classification model. Prior to the establishment of the model, we need to preprocess the collected feature data to improve the training speed and accuracy of the model. Firstly, Smoothing was performed to create a feature sample set. XGBoost was an excellent decision tree classifier, which can use the objective function and scoring function as the model’s performance.XGBoost water quality classification model:(22)Obj=-12∑j=1TGj2Hj+λ+γT,Gain=12GL2HL+λ+GR2HR+λ+GL+GR2HL+HR+λ-γ. In this paper, the steps of establishing water quality monitoring model were as follows:( 1 ) We use the TLD technology to preprocess the fish motion parameters and set up a set of characteristic parameters.( 2 ) The feature parameter set was used as the input of XGBoost for training.( 3 ) Cross validation method was used to obtain the optimal number of boosting iterations.( 4 ) The tree structure and the characteristic score were used to analyze and evaluate the model.The algorithm flow was shown in Algorithm1.Algorithm 1:TLD + XGBoost for water quality.
Input: Part.1 Image patches (positive and negative image patches),Ii; Target(x,y,z), Targetn; Output: Tracking results (xi,yi,zi), at time t; (1) t⇐1⋯T (2) ifT=1then (3)  Marker the target (4) else (5)  Stage 1: using NCC and BF get the stable points. (6)  Stage 2: Ii through Variance filter, Fern filter, NN filter get the best patches and staable point. (7)  Compare stage 1 with stage 2 get the best points. (8)  Update the Random Fren and positive, negative sample set. (9) end if Input: Part.2 Fish characteristic parameter:Fi as data, Fi; Model parameter:Pi include (base-score = 0.5, colsample-bylevel = 1, colsample-bytree = 1, gamma = 0, learning-rate = 0.1, max-delta-step = 0, max- depth = 3, min-child-weight = 1, missing = None,n-estimators = 100, nthread = −1, objective = binary: logistic, reg-alpha = 0, reg-lambda = 1, scale-pos-weight = 1, seed = 0, silent = True, subsample = 1) Output: Water Quality degree:Qi; model; Classification Accuracy (10) Load data (11) Split data into train and test sets by train-test-split( ). (12) Load XGBClassifier and model.predict. (13) Calculating Classification Accuracy


## 3.1. Objective Function, Obj

First of all, the objective function of GBDT is as follows:(7)Objective:∑i=1nlyi,y^i+∑kΩfk,fk∈F, where l is the loss function, Ω(fk) is a regular term, and fk is tree complexity. After series of processing, we get a new objective function which is easier to use:(8)Objt:∑i=1nlyi,y^it-1+ftxi+Ωft+constant. XGBoost Taylor expansion for ft(xi) makes it more similar to our previous goals. The process is as follows:(9)fx+Δx≃fx+f′xΔx+12f′′xΔx2. By applying Taylor expansion to the three terms, we can make it clear that the final objective function depends on the first derivative and the second derivative of the error function of each data point.Definition:(10)gi=δy^t-1lyi,y^t-1,hi=δy^t-12lyi,y^t-1. Bring (9) and (10) back to (8):(11)Objt≃∑i=1nlyi,y^it-1+giftxi+12hift2xi+Ωft+constant. Taking into account the square loss, can also continue to (10):(12)gi=δy^t-1y^t-1-yi2=2y^t-1-yi,hi=δy^t-12yi-y^t-12=2. In this way, we can remove the constant term and get the new objective function:(13)∑i=1ngiftxi+12htft2xi+Ωft. In the equation, gi=δy^(t-1)l(yi,y^(t-1)),hi=δy^(t-1)2l(yi,y^(t-1)).Then define the complexity of the tree:(14)ftx=wqx,w∈RT,q:Rd⟶1,2,3,…,T, where w is a leaf vector, q is a tree structure, which defines the complexity of the number of nodes in a tree, and L2 is square of the output of each tree node. Under this new definition, we can rewrite the objective function as follows: I is defined as a set of samples on each leaf; Ij={i∣q(xi)=j}.(15)Objt≃∑i=1ngiftxi+12hift2xi+ΩftObjt=∑i=1ngiwqxi+12hiwqxi2+γT+λ12∑j=1Twj2Objt=∑j=1T∑i∈Ijgiwj+12∑i∈Ijhj+λwj2+γT.To further simplify the formula, we define(16)Gj=∑i∈Ijgi,Hj=∑i∈Ijhi,(17)Objt≃∑j=1T∑i∈Ijgiwj+12∑i∈Ijhj+λwj2+γT,(18)Objt=∑j=1TGjwj+12Hj+λwj2+λT. Based on the derivation of it being equal to 0, the following can be obtained:(19)wj∗=-GjHj+λ. Bring the optimal solution wj∗ back to (18), and the final objective function is obtained:(20)Obj=-12∑j=1TGj2Hj+λ+γT. The above objective function, Obj, represents the maximum amount of reduction in the target when the structure of a tree is specified. You can call it a structural score. The smaller the Obj, the better the structure of the tree.


## 3.2. Add Scoring Function Gain

(21) G a i n = 1 2 G L 2 H L + λ + G R 2 H R + λ + G L + G R 2 H L + H R + λ - γ . G L 2 / H L + λ represents the left subtree fraction, GR2/HR+λ represents the right subtree fraction, (GL+GR)2/HL+HR+λ represents an undivided score, and γ represents the complexity cost of adding new leaf nodes. For each expansion, or to efficiently enumerate all possible segmentation schemes, suppose you want to enumerate all the conditions of x<a, for a particular splita to calculatea’s left and right derivative, as shown in Figure 5.Figure 5
Sketch map of segmentation.It would be found that for all ‘a’, you can give all the gradients and GL and GR as long as a scan is done from left to right. And then the above formula can be used to calculate the score of each partition.


## 3.3. XGBoost Biological Water Quality Monitoring Model

XGBoost joined the regularization; regularized boosting was a great help in reducing overfitting. Compared with GBDT the XGBoost can achieve parallel processing; the speed has been greatly improved; comparing SVM classification with XGBoost, XGBoost allows the user to define custom optimization objectives and evaluation criteria; it will add a new dimension in the model, so it is not subject to any restrictions on the data processing; in the process of data collection, because various artificial or experimental defects exist, it will inevitably lead to the phenomenon of data loss; while the XGBoost was built to deal with the missing value rules, the user needs to provide a different values sample and then take it as a parameter input; so this value is taken as missing values; XGBoost has different treatment methods in different nodes encountered with missing values and will learn when encountering the missing value in the future how to deal with it. The method provides a good solution for the deletion of the fish characteristic parameters, and the XGBoost allows the use of cross validation in each round of boosting iteration. Therefore, the optimal number of boosting iterations can be easily obtained; XGBoost can continue to train on the results of the previous round. This feature was a great advantage in the application of the classification of fish characteristic parameters and can realize the continuity of the model.Therefore, we try to use XGBoost to replace the previous classifier to establish water quality classification model. Prior to the establishment of the model, we need to preprocess the collected feature data to improve the training speed and accuracy of the model. Firstly, Smoothing was performed to create a feature sample set. XGBoost was an excellent decision tree classifier, which can use the objective function and scoring function as the model’s performance.XGBoost water quality classification model:(22)Obj=-12∑j=1TGj2Hj+λ+γT,Gain=12GL2HL+λ+GR2HR+λ+GL+GR2HL+HR+λ-γ. In this paper, the steps of establishing water quality monitoring model were as follows:( 1 ) We use the TLD technology to preprocess the fish motion parameters and set up a set of characteristic parameters.( 2 ) The feature parameter set was used as the input of XGBoost for training.( 3 ) Cross validation method was used to obtain the optimal number of boosting iterations.( 4 ) The tree structure and the characteristic score were used to analyze and evaluate the model.The algorithm flow was shown in Algorithm1.Algorithm 1:TLD + XGBoost for water quality.
Input: Part.1 Image patches (positive and negative image patches),Ii; Target(x,y,z), Targetn; Output: Tracking results (xi,yi,zi), at time t; (1) t⇐1⋯T (2) ifT=1then (3)  Marker the target (4) else (5)  Stage 1: using NCC and BF get the stable points. (6)  Stage 2: Ii through Variance filter, Fern filter, NN filter get the best patches and staable point. (7)  Compare stage 1 with stage 2 get the best points. (8)  Update the Random Fren and positive, negative sample set. (9) end if Input: Part.2 Fish characteristic parameter:Fi as data, Fi; Model parameter:Pi include (base-score = 0.5, colsample-bylevel = 1, colsample-bytree = 1, gamma = 0, learning-rate = 0.1, max-delta-step = 0, max- depth = 3, min-child-weight = 1, missing = None,n-estimators = 100, nthread = −1, objective = binary: logistic, reg-alpha = 0, reg-lambda = 1, scale-pos-weight = 1, seed = 0, silent = True, subsample = 1) Output: Water Quality degree:Qi; model; Classification Accuracy (10) Load data (11) Split data into train and test sets by train-test-split( ). (12) Load XGBClassifier and model.predict. (13) Calculating Classification Accuracy


## 4. Experimental Results and Analysis

In the fish red carp, body length was about 3 cm; the camera is BNT shadow (HD720) and using tank; normal water quality is common water; abnormal water is chemical reagent which is added to copper or chromium.In order to verify the validity and feasibility of the method in this paper, the following experiments are designed. Get the fish motion video images of normal and abnormal water quality by using the digital camera in the course of the experiment; each frame size was480∗640, the frame of 25 f/s.


### 4.1. Select Characteristic Parameters

In order to improve the speed of data processing and the classification accuracy of XGBoost classifier, we set the parameters selection and choose normal water quality data set; the abnormal water quality data was intercepted according to certain sampling frequency. The characteristic parameters of the six groups were selected as follows: near distance,d, speed, V, acceleration, a, X, dispersion direction, and Y, dispersion direction and distribution area S, as shown in Figure 6.Figure 6
Schematic diagram of characteristic parameters.In Figure6 the color changes from blue to red, showing the water quality change from normal water quality to abnormal water quality. Left with blue is said to be the healthy state; right with red is said to be the unhealthy water quality. It can be observed from Figure 6 that characteristic parameters have obvious trend; in normal water, acceleration a, speed V, near distance d, X dispersion direction, and Y dispersion direction in the direction of the image on the left were less active; in each image the number of difference changes was low; and the right was risk quality, comparing characteristic parameter changes with the left; there was a significant difference.


### 4.2. Feature Parameter Preprocessing and Sample Set

The preprocessing of the characteristic parameters was to remove the gross errors in the data by Rajda (3σ) criterion.Due to the normalization of XGBoost own data normalization, here were no longer repeat operation.The concrete steps to build a sample set were as follows:( 1 ) The original data of fish motion parameters were obtained by randomly selecting 2000 frames of images under normal and abnormal water quality.( 2 ) Set up the original data sample set, including training samples 2000 × 6 (normal and abnormal water quality of each 1000 × 6) and test samples 2000 × 6 (normal and abnormal water quality of each 1000 × 6).( 3 ) Preprocess the original data.


### 4.3. Experimental Result

A total of 4 sets of samples were established in this experiment; we use these data for testing; test results were shown in Table1. As can be seen from the table, the classification accuracy and time all have an excellent performance. Because the data were too much, here we choose the last set of data: original data 4 for detailed instructions. Using XGBoost the output of the classification model of original data 4 is obtained; the model of the code in the name of the characteristics of the parameters is shown in Table 2. The score of each characteristic parameter in the model was shown in Figure 7.Table 1
XGBoost classification performance.
Data types Accuracy% Time/s Original data 1 98.66 0.0620 Original data 2 99.40 0.0590 Original data 3 98.81 0.0659 Original data 4 97.92 0.0650 Original data 5 98.56 0.0630 Original data 6 99.32 0.0645Table 2
Characteristic parameters.
Characteristic parameter Code X dispersion direction–X-LS f0 Y dispersion direction–Y-LS f1 Distribution area–S f2 Speed–V f3 Acceleration–a f4 Near distance–d f5Figure 7
Schematic diagram of the model.The specific weight of each characteristic parameter for the overall sample set and their respective classification accuracy were shown in Table3. It can be seen from the chart during the classification processing, that the model selects the most obvious characteristic parameters; for those weak characteristic parameters, model was used to adjust the weight of its classification, especially particularly bad data directly discarded. The model iterates over each feature parameter and can be used to segment the training set by calculating the eigenvalues of each feature parameter and then using tree structured Figure 8 to view the decision-making process (3rd boosted tree). In Figure 8, the feature and the feature values for each split were shown as well as the output leaf nodes. The split decisions with each node and the different colors for left and right splits (blue and red) were also shown.Table 3
Accuracy of XGBoost classification.
Feature Thresh Feature Accuracy% X-LS 0.274 f0 93.33 S 0.250 f2 96.97 Y-LS 0.215 f1 98.18 V 0.130 f3 97.88 a 0.130 f4 97.88 d 0.000 f5 98.94Figure 8
Tree structure.


### 4.4. Model Evaluation and Optimization

#### 4.4.1.k-Fold Cross Validation

This time the cross validation process is to repeat the experiment forK times, each time from the K sections to choose a different part as test data (ensure that the K part of the data was tested separately) and the rest of the K-1 test data as training data for experiment. Finally, the obtained K experimental results are average. The verification results were shown in Table 4. It includes both the mean (Accuracy%) and standard deviation (Error%) classification accuracy. As can be seen from the table, XGBoost as a classifier can still maintain a high accuracy after multiple cross validation, and the time used for each classification was very small.Table 4
K-fold cross validation results.
Number Time/s Accuracy% Error% ( 2 ) 0.1040 91.75 1.55 ( 3 ) 0.1749 92.55 5.39 ( 4 ) 0.2650 93.85 4.41 ( 5 ) 0.3439 92.60 5.36 ( 6 ) 0.4140 94.04 6.99 ( 7 ) 0.4830 95.15 5.52 ( 8 ) 0.5660 94.05 7.94 ( 9 ) 0.6390 94.06 6.85 ( 10 ) 0.7380 93.70 8.23


#### 4.4.2. Loss Function and Classification Error

Using XGBoost as a decision tree classifier, we can also use the loss function and classification error to evaluate our water quality classification model, as shown in Figure9. X-axis abscissa represents the number of data.Figure 9
Loss function and classification error.The Log Loss function is very common as the evaluation criteria; in Figure9, you can clearly see the model loss decreases and reaching the ideal state. The classification error is divided into two kinds: one is the training error (Train), another is test error (Test); the training error stabilized at around 0.010 and the test error finally remains at a very low value and can fully meet the application of our water quality classification model.


### 4.5. Optimization

We optimize the processing speed of the model and use the computer multicore processor (CPU) to improve the speed of the model. From a cost point of view, the price of a single core CPU was cheaper, but the speed of computing compared with multicore CPU comparison has great gap. From Figure10 we can see that when the number of processors in the core model selection gradually increased, the processing speed of the model has great changes too, because the test data from the scale is still relatively small, not enough to fully display the processing speed of the model, but the advantages of multicore processor in speed optimization still can be seen.Figure 10
Multicore computing speed optimization.


### 4.6. Comparison between XGBoost and SVM

SVM is one of the current mainstream classifiers that has always been a very good performance in the classification problem; in order to demonstrate the performance of the XGBoost classifier, we made a comparison between SVM and XGBoost. Because the kernel function of SVM will affect the classification results, in order to allow the SVM classifier to achieve the best working condition, we adopted the previous laboratory work [16]; using RBF kernel parameters, the penalty factor C is given in Table 5. XGBoost uses the data set for original data 4; in order to play the best working condition, SVM needs to be further normalized on the basis of original data 4.Table 5
Comparison of XGBoost and SVM classification.
Types f 0 f 1 f 2 f 3 f 4 f 5 SVM-time/s 10.016 13.330 15.241 15.730 16.052 16.412 XGBoost-time/s 0.060 0.066 0.073 0.075 0.089 0.094 SVM-accuracy% 93.65 94.50 65.60 66.90 50.00 50.00 XGBoost-accuracy% 93.45 96.88 98.07 98.07 97.92 97.92 SVM-σ 0.12 0.058 0.058 0.058 0.012 0.012 SVM-C 7.46 3.73 7.46 7.46 7.46 7.46The final SVM classification effect is shown in Figure11.Figure 11
SVM classification effect.As shown in Figure12, compared with the accuracy of the classification and the time used in the classification, XGBoost has a huge advantage, especially the advantages of classification time; XGBoost is faster than SVM to speed up to two orders of magnitude. Details are shown in Table 5.Figure 12
Classification speed and accuracy of XGBoost and SVM.


## 4.1. Select Characteristic Parameters

In order to improve the speed of data processing and the classification accuracy of XGBoost classifier, we set the parameters selection and choose normal water quality data set; the abnormal water quality data was intercepted according to certain sampling frequency. The characteristic parameters of the six groups were selected as follows: near distance,d, speed, V, acceleration, a, X, dispersion direction, and Y, dispersion direction and distribution area S, as shown in Figure 6.Figure 6
Schematic diagram of characteristic parameters.In Figure6 the color changes from blue to red, showing the water quality change from normal water quality to abnormal water quality. Left with blue is said to be the healthy state; right with red is said to be the unhealthy water quality. It can be observed from Figure 6 that characteristic parameters have obvious trend; in normal water, acceleration a, speed V, near distance d, X dispersion direction, and Y dispersion direction in the direction of the image on the left were less active; in each image the number of difference changes was low; and the right was risk quality, comparing characteristic parameter changes with the left; there was a significant difference.


## 4.2. Feature Parameter Preprocessing and Sample Set

The preprocessing of the characteristic parameters was to remove the gross errors in the data by Rajda (3σ) criterion.Due to the normalization of XGBoost own data normalization, here were no longer repeat operation.The concrete steps to build a sample set were as follows:( 1 ) The original data of fish motion parameters were obtained by randomly selecting 2000 frames of images under normal and abnormal water quality.( 2 ) Set up the original data sample set, including training samples 2000 × 6 (normal and abnormal water quality of each 1000 × 6) and test samples 2000 × 6 (normal and abnormal water quality of each 1000 × 6).( 3 ) Preprocess the original data.


## 4.3. Experimental Result

A total of 4 sets of samples were established in this experiment; we use these data for testing; test results were shown in Table1. As can be seen from the table, the classification accuracy and time all have an excellent performance. Because the data were too much, here we choose the last set of data: original data 4 for detailed instructions. Using XGBoost the output of the classification model of original data 4 is obtained; the model of the code in the name of the characteristics of the parameters is shown in Table 2. The score of each characteristic parameter in the model was shown in Figure 7.Table 1
XGBoost classification performance.
Data types Accuracy% Time/s Original data 1 98.66 0.0620 Original data 2 99.40 0.0590 Original data 3 98.81 0.0659 Original data 4 97.92 0.0650 Original data 5 98.56 0.0630 Original data 6 99.32 0.0645Table 2
Characteristic parameters.
Characteristic parameter Code X dispersion direction–X-LS f0 Y dispersion direction–Y-LS f1 Distribution area–S f2 Speed–V f3 Acceleration–a f4 Near distance–d f5Figure 7
Schematic diagram of the model.The specific weight of each characteristic parameter for the overall sample set and their respective classification accuracy were shown in Table3. It can be seen from the chart during the classification processing, that the model selects the most obvious characteristic parameters; for those weak characteristic parameters, model was used to adjust the weight of its classification, especially particularly bad data directly discarded. The model iterates over each feature parameter and can be used to segment the training set by calculating the eigenvalues of each feature parameter and then using tree structured Figure 8 to view the decision-making process (3rd boosted tree). In Figure 8, the feature and the feature values for each split were shown as well as the output leaf nodes. The split decisions with each node and the different colors for left and right splits (blue and red) were also shown.Table 3
Accuracy of XGBoost classification.
Feature Thresh Feature Accuracy% X-LS 0.274 f0 93.33 S 0.250 f2 96.97 Y-LS 0.215 f1 98.18 V 0.130 f3 97.88 a 0.130 f4 97.88 d 0.000 f5 98.94Figure 8
Tree structure.


## 4.4. Model Evaluation and Optimization

### 4.4.1.k-Fold Cross Validation

This time the cross validation process is to repeat the experiment forK times, each time from the K sections to choose a different part as test data (ensure that the K part of the data was tested separately) and the rest of the K-1 test data as training data for experiment. Finally, the obtained K experimental results are average. The verification results were shown in Table 4. It includes both the mean (Accuracy%) and standard deviation (Error%) classification accuracy. As can be seen from the table, XGBoost as a classifier can still maintain a high accuracy after multiple cross validation, and the time used for each classification was very small.Table 4
K-fold cross validation results.
Number Time/s Accuracy% Error% ( 2 ) 0.1040 91.75 1.55 ( 3 ) 0.1749 92.55 5.39 ( 4 ) 0.2650 93.85 4.41 ( 5 ) 0.3439 92.60 5.36 ( 6 ) 0.4140 94.04 6.99 ( 7 ) 0.4830 95.15 5.52 ( 8 ) 0.5660 94.05 7.94 ( 9 ) 0.6390 94.06 6.85 ( 10 ) 0.7380 93.70 8.23


### 4.4.2. Loss Function and Classification Error

Using XGBoost as a decision tree classifier, we can also use the loss function and classification error to evaluate our water quality classification model, as shown in Figure9. X-axis abscissa represents the number of data.Figure 9
Loss function and classification error.The Log Loss function is very common as the evaluation criteria; in Figure9, you can clearly see the model loss decreases and reaching the ideal state. The classification error is divided into two kinds: one is the training error (Train), another is test error (Test); the training error stabilized at around 0.010 and the test error finally remains at a very low value and can fully meet the application of our water quality classification model.


## 4.4.1.k-Fold Cross Validation

This time the cross validation process is to repeat the experiment forK times, each time from the K sections to choose a different part as test data (ensure that the K part of the data was tested separately) and the rest of the K-1 test data as training data for experiment. Finally, the obtained K experimental results are average. The verification results were shown in Table 4. It includes both the mean (Accuracy%) and standard deviation (Error%) classification accuracy. As can be seen from the table, XGBoost as a classifier can still maintain a high accuracy after multiple cross validation, and the time used for each classification was very small.Table 4
K-fold cross validation results.
Number Time/s Accuracy% Error% ( 2 ) 0.1040 91.75 1.55 ( 3 ) 0.1749 92.55 5.39 ( 4 ) 0.2650 93.85 4.41 ( 5 ) 0.3439 92.60 5.36 ( 6 ) 0.4140 94.04 6.99 ( 7 ) 0.4830 95.15 5.52 ( 8 ) 0.5660 94.05 7.94 ( 9 ) 0.6390 94.06 6.85 ( 10 ) 0.7380 93.70 8.23


## 4.4.2. Loss Function and Classification Error

Using XGBoost as a decision tree classifier, we can also use the loss function and classification error to evaluate our water quality classification model, as shown in Figure9. X-axis abscissa represents the number of data.Figure 9
Loss function and classification error.The Log Loss function is very common as the evaluation criteria; in Figure9, you can clearly see the model loss decreases and reaching the ideal state. The classification error is divided into two kinds: one is the training error (Train), another is test error (Test); the training error stabilized at around 0.010 and the test error finally remains at a very low value and can fully meet the application of our water quality classification model.


## 4.5. Optimization

We optimize the processing speed of the model and use the computer multicore processor (CPU) to improve the speed of the model. From a cost point of view, the price of a single core CPU was cheaper, but the speed of computing compared with multicore CPU comparison has great gap. From Figure10 we can see that when the number of processors in the core model selection gradually increased, the processing speed of the model has great changes too, because the test data from the scale is still relatively small, not enough to fully display the processing speed of the model, but the advantages of multicore processor in speed optimization still can be seen.Figure 10
Multicore computing speed optimization.


## 4.6. Comparison between XGBoost and SVM

SVM is one of the current mainstream classifiers that has always been a very good performance in the classification problem; in order to demonstrate the performance of the XGBoost classifier, we made a comparison between SVM and XGBoost. Because the kernel function of SVM will affect the classification results, in order to allow the SVM classifier to achieve the best working condition, we adopted the previous laboratory work [16]; using RBF kernel parameters, the penalty factor C is given in Table 5. XGBoost uses the data set for original data 4; in order to play the best working condition, SVM needs to be further normalized on the basis of original data 4.Table 5
Comparison of XGBoost and SVM classification.
Types f 0 f 1 f 2 f 3 f 4 f 5 SVM-time/s 10.016 13.330 15.241 15.730 16.052 16.412 XGBoost-time/s 0.060 0.066 0.073 0.075 0.089 0.094 SVM-accuracy% 93.65 94.50 65.60 66.90 50.00 50.00 XGBoost-accuracy% 93.45 96.88 98.07 98.07 97.92 97.92 SVM-σ 0.12 0.058 0.058 0.058 0.012 0.012 SVM-C 7.46 3.73 7.46 7.46 7.46 7.46The final SVM classification effect is shown in Figure11.Figure 11
SVM classification effect.As shown in Figure12, compared with the accuracy of the classification and the time used in the classification, XGBoost has a huge advantage, especially the advantages of classification time; XGBoost is faster than SVM to speed up to two orders of magnitude. Details are shown in Table 5.Figure 12
Classification speed and accuracy of XGBoost and SVM.


## 5. Conclusion

We obtain 3D coordinates by tracking fish in the water. A series of motion characteristic parameters which can be used to represent the water quality were calculated; in the processing of parameters, we found that some features can be used to distinguish between normal and abnormal water quality. According to this idea, water quality monitoring model was set up based on XGBoost classifiers. After a large number of experiments, this model can realize the water quality classification quickly, accurately, and conveniently. Compared with the previous classifier, XGBoost was more outstanding. However, the whole process of water quality monitoring can not achieve closure, so we must rely on line training and human error processing; the whole system can not achieve real-time monitoring. We will further strengthen the model’s ability to distinguish. In the analysis of the feature image, the processing of the intermediate state can be transformed into the frequency domain by the way of signal processing, removing interference information, and improving the accuracy of early warning. And as a follow-up we can deteriorate the experimental environment and enhance the robustness of the system.



---

*Source: 5604740-2018-03-18.xml*