## 3 Materials and Methods
### Applications and Performance  
#### 3.2.1 Application in Heterogeneous Environments  
The Ethoflow was run on a machine with Intel i7-9750H CPU 2.60 GHz \(\times\) 12, 8 GB RAM and GPU NVIDIA(r) GeForce(r) GTX 1660 (6 GB) Ti Max-Q. To apply Ethoflow in a heterogeneous environment experiment, we trained the IS model to detect the bee _Melipona quadrificaita_ through the 1325 images in various heterogeneous backgrounds (Figure 4). In addition to these image data, the inputs with bounding box positions, classes, and masks (pixel-wise positions of the animals) are required to train the IS model [9]. The manual generation of these inputs is a laborious task. Then, we developed a heuristic to automatically generate these inputs based on several random backgrounds and a video in homogeneous conditions to detect objects using manual segmentation or [PERSON]'s method. Frames are randomly sampled in the video and pass through the algorithm's preprocessing and object detection stages (Figure 5A). Then, the animals are \"copied,\" and the contours are \"pasted\" into random backgrounds (Figure 5B). Concomitantly, the bounding box, class, and mask of each animals are saved in a dictionary with the following structure: Dictionary {image: {object}: {box: {center: {x,y}, width, height}; class:{target}; mask:{all points (x,y)}}}]]]].  
Of all the data generated with the heuristic, 976 (74%) were used for training, 249 (19%) for validation, and 100 (7%) to evaluate the classification using the average precision (_AP_) [36]. To obtain _AP_, we calculated the intersection over union (_IoU_) of the predicted  
Figure 3: Convolutional neural network architecture defined after hyperparameter optimization (Appendix B) to recognize animal complex behavior on Ethoflow. This model was configured with stochastic gradient descent (learning rate at 0.0001 and momentum at 0.9) as an optimizer and binary cross-entropy as a loss function. Batch normalization was applied before max-pooling layers. Dropout was also applied after dense inner layers. In the inner layers (convolutional or dense), the function activation was Elu. The dimensions (width \(\times\) height \(\times\) depth) of feature map are given in each layer; the output dimensions of a layer are the same as the input dimensions of the next layer. In the flatten process, the data are transformed into a vector to enter the dense layers. In the last dense layer, a sigmoid function is applied, which gives the binary output.  
bounding boxes (i.e., the \(x\), \(y\) coordinates in the upper-left corner and width and height of the rectangular box around the object of interest) and target bounding boxes. Based on the _loU,_ the precision (Equation (23)) and recall (Equation (24)) can be calculated using the true positives (\(TP\)), false positives (\(FP\)), and false negatives (\(FN\)) for the detected objects (\(DO\)) in a determined threshold (\(x\)) (Equation (25)).  
\[\text{precision}= \frac{TP}{TP+FP}. \tag{23}\] \[\text{recall}= \frac{TP}{TP+FN}.\] (24) \[\left\{\begin{array}{c}\text{if }IoU\ \geq\ \text{x},\ DO=TP\\ \text{if }IoU\ <\ \text{x},\ DO=FP\\ \text{if the model fails to detect a target object, }\ DO=FN\end{array}\right\} \tag{25}\]  
There is a tradeoff between the precision and recall, wherein the higher the recall, the more the model tends to find all the target objects, i.e., a low \(FN\) value. However, an increase in the recall tends to decrease the precision, as it increases \(FP\). Considering equally spaced recall levels \(n=(0,\ 0.1,\cdots,\ 1.0)\), interpolation is performed using the highest precision value for a given recall. Then, the \(AP\) is obtained from the interpolated values of the precision \(\left(P_{interp}(r)\right)\):  
\[AP=\frac{1}{n}\sum_{i=0}^{n}P_{interp}(r_{i}). \tag{26}\]  
Figure 4: Examples of some images that were generated automatically to train the model for tracking the stingless bee _Melipona quadrificata_ in different conditions of a heterogeneous background. The white arrows indicate some bee contours pasted in the backgrounds.