# Research on Image Feature Extraction and Environment Inference Based on Invariant Learning
## 4 Image Environment Partitioning System Based on Invariant Learning  
To address the issue of background environment partitioning in mixed image datasets, this paper proposes an image environment partitioning method that combines invariant learning and Grad-CAM theory. In the absence of environment labels, the K-means clustering algorithm is initially applied to roughly divide the images using the full image data and assign environment labels. Then, the IRM-ResNet18 model described in the previous section is constructed, and Grad-CAM theory is used to extract the N pixels with the lowest contribution to the model's classification task as the environment features (i.e., spurious features for the classification task). The value of N can be adjusted according to the corresponding characteristics of different image datasets. After obtaining the environment features, K-means clustering is then employed once more to more precisely divide the images and assign environment labels. This process can be appropriately iterated based on the effectiveness of the environment partitioning until the desired state is reached. The flowchart of this process is shown in Figure 9.  
In this section, the iWildCam 2019 dataset is again used as an example for environment partitioning experiments. For simplicity, the initial coarse partitioning and invariant model training processes are omitted, and the classification model trained in the previous section is directly used for more detailed environment partitioning of photos taken by the same camera. Based on the environment partitioning method proposed in this paper, images taken by a single camera are categorized into three groups based on brightness levels: high, medium, and low. The distinction between high and medium brightness is based on whether there is direct sunlight, while low brightness indicates a dark environment, as shown in Figure 10.  
\begin{table}
\begin{tabular}{c c c c} \hline \hline
**Model** & **Feature Dataset** & **Training Set** & **Test Set** \\ \hline \multirow{3}{*}{SVC} & IRM-ResNet18 & 61.6\% & 61.3\% \\  & ResNet18 & 61.9\% & 52.3\% \\ \cline{1-1}  & IRM-ResNet18 & 100\% & 37.7\% \\ \cline{1-1}  & ResNet18 & 100\% & 36.0\% \\ \hline \hline \end{tabular}
\end{table}
Table 3: Prediction accuracy of two feature datasets on different models.  
For K-means clustering, the number of categories is specified as 3, the amount of iterations is adjusted to 1000, and the pixel count \(N\) is adjusted to 50,000. The images clustered into the three groups are labeled as Environment 1, 2, and 3, with each one standing for a setting with high, medium, and low brightness, accordingly. Table 4 displays the partitioning outcomes for the ResNet18 and IRM-ResNet18 models.  
According to Table 4, both models achieve 100% partitioning accuracy in dark environments. The notable distinction between dark surroundings and various brightness levels accounts for this great accuracy, allowing for high-precision partitioning even without specifically extracting environmental features. However, for the partitioning accuracy in the other two environments, the environment partitioning method based on invariant learning significantly outperforms the ERM method, validating the suggested method's efficiency in extracting environmental features and its advantages in partitioning mixed image environments.  
\begin{table}
\begin{tabular}{c c c} \hline \hline
**Title 1** & **IRM-ResNet18** & **ResNet18** \\ \hline Environment 1 & 83.3\% & 77.8\% \\ Environment 2 & 84.7\% & 75.0\% \\ Environment 3 & 100.0\% & 100.0\% \\ Total & 88.2\% & 82.2\% \\ \hline \hline \end{tabular}
\end{table}
Table 4: Accuracy of environment division of two models.  
Figure 10: Environment division diagram: (**a**) High brightness, (**b**) relatively high brightness, (**c**) low brightness.  
Figure 9: Image environment inference process based on invariant learning.