# Research on Image Feature Extraction and Environment Inference Based on Invariant Learning
## 3 Image Feature Extraction Method Based on Invariant Learning
### Irm Resnet18 Network Training and Feature Extraction  
The input data are initially divided by environment using the IRM-ResNet18 network before being classified. Additionally, the model's loss function is optimized using invariant learning. Figure 5 depicts the IRM-ResNet18 network's structure. By inputting the training set into the ERM-based ResNet18 network model for training and using Grad-CAM to generate heatmaps for the test set images, CAM is overlaid onto the original images to produce visualized images. On the one hand, relevant spurious environmental features are extracted by setting a grayscale threshold value (5000). On the other hand, after extracting the relevant spurious environmental features, the Grad-CAM algorithm fully learns the invariant features causally related to the classification target for subsequent analysis. Finally, the features extracted by the IRM-ResNet18 are used as input \begin{table}
\begin{tabular}{c c c c c} \hline \hline  & \multicolumn{2}{c}{**Training Set**} & \multicolumn{2}{c}{**Test Set**} \\  & **Coyotes** & **Raccoons** & **Coyotes** & **Raccoons** \\ \hline Location 1 & 808 & 800 & / & / \\ Location 2 & 512 & 420 & / & / \\ Location 3 & / & / & 239 & 1134 \\ \hline \hline \end{tabular}
\end{table} 
Table 1: The allocation of samples between the training and test sets. Figure 5: Network architecture of IRM ResNet18. variables for training support vector machine and random forest models to detect their prediction accuracy on the test set. Figure 6 depicts the exact training procedure. Figure 7 displays the accuracy curves of the models both before and after invariant learning was added to the training and test sets, and Table 2 illustrates the comparison of prediction accuracy among different models on the training and test sets. The experimental results indicate that the ERM-based ResNet18 network has a higher prediction accuracy on the training set compared to the IRM-ResNet18 and the results of models like V-REx and CoCo proposed in reference [12]. However, the Res-Net18 model shows a prediction accuracy of 73.8% and exhibits significant performance variability on the test dataset. In contrast, the IRM-ResNet18 achieves a prediction accuracy of 88.6% on the test set, significantly surpassing ResNet18 in both accuracy and stability. Furthermore, the prediction accuracy of the IRM-ResNet18 gradually converges between the training and test sets, while ResNet18's performance differs noticeably between the two. In terms of prediction accuracy, the performance gap of the ERM-based ResNet18 network between training and testing is relatively small. These results demonstrate that the IRM-ResNet18 model can accurately learn the in-variant features in images, effectively ignoring spurious environmental features, thereby achieving consistent and stable high-accuracy predictions across different environments. To visually display the image areas that the network focuses on, Grad-CAM was used to generate heatmaps for the test set images. CAM was overlaid onto the original images to produce visualized images. In the heatmaps, the darker the color of an area, the more attention the network pays to that region, as shown in Figure 8. In the original  
Figure 6: Flow chart of experiment. Figure 7: Prediction accuracy of network on training and test set with iteration steps. images, the areas marked by red circles indicate where the animals are located. In the heatmaps produced by the IRM-ResNet18 network, the animal areas are shown in dark red, indicating that the network has thoroughly learned the animal features. Conversely, in the heatmaps from the ResNet18 network, the animal areas are lighter in color, indicating that the network has predominantly learned spurious environmental features and largely ignored the animal features. The heatmap results demonstrate that the IRM-ResNet18 algorithm can fully learn the invariant features causally related to the classification target.