# Cattle Weight Estimation Using Fully and Weakly Supervised Segmentation from 2D Images
## 2 Materials and Methods
### Segmentation Methods
#### 2.4.2 Wss  
For WSS, to obtain an activation map for the segmentation mask, the Xception model was used with two classes of data, an image with an animal, and an image with only a background; an activation map was obtained using the Grad-CAM method [16]. Because the desired mask quality could not be obtained by using Grad-CAM alone, it was obtained using the Puzzle-CAM method [17]. Grad-CAM is a learning visualization technique for interpreting processes in CNNs and has been studied to identify the features that models learn [18]. Grad-CAM uses the feature map \(A^{k}\) of the trained model and gradient map \(\alpha_{k}^{c}\), which is a gradient value for updating \(A^{k}\) then, \(\alpha_{k}^{c}\) and \(A^{k}\) are matched element-wise. Map \(\alpha_{k}^{c}\) can be expressed as follows:  
\[\alpha_{k}^{c}=\frac{1}{Z}\sum_{i}\sum_{j}\frac{\partial y_{c}}{\partial A_{i, j}^{k}}. \tag{1}\]  
where \(i\) and \(j\) are the width and height, respectively, and \(Z\) is the number of pixels in the map. Moreover, \(y_{c}\) is the predicted value of the model. Therefore, Equation (1) represents the global average pooling processed values of the gradient map that occurs when the classification model classifies class \(c\) and the weights are updated. Next, \(L_{\text{Grad-CAM}}^{c}\) is obtained by multiplying Equation (1) by \(A^{k}\), and then passing it through the ReLU function to discard values less than zero. This process can be expressed as follows:  
\[L_{\text{Grad-CAM}}^{c}=\text{ReLU}(\sum_{k}\alpha_{k}^{c}A^{k}). \tag{2}\]  
Because Grad-CAM is based on a classification model, it is unsuitable for segmentation tasks. In Figure 8, which shows the Grad-CAM results for our data, it can be seen that the activation map is generated only for a specific part of the cattle. This is because the classification model was able to distinguish the presence or absence of cattle using only the head and buttocks. This result cannot be expected in the case of segmentation, which is the purpose of WSS. To solve this problem, three types of Puzzle-CAM losses were used.  
Puzzle-CAM defines three types of loss, as shown in Figure 9. The first is the classification loss of the original input image, the second is the classification loss of four image patches, and the third is the difference between the CAM of the original input image and the CAM of the four-patch image. Because the difference between the CAM of the original input image and the CAM of the four-patch image is used as a loss, it is possible to prevent only a specific part of the object from being visualized. To leverage the advantages of  
Figure 8: Grad-CAM results.  
Figure 7: Image segmentation result of the FSS method.  
Puzzle-CAM and Grad-CAM simultaneously, an activation map is generated using the Grad-CAM method, and the new loss of Puzzle-CAM (i.e., loss1 + loss2 + loss3) is used to update the Xception model. When training the Xception model, a Grad-CAM activation map is generated every time the weights are updated, and a new loss is calculated so that the Xception model learns by considering both the classification ability and the activation map generation ability. The results are depicted in Figure 10. As can be seen, the results of Grad-CAM, which only used a part of the animal as a region of interest, are improved.