File size: 8,960 Bytes
20ed5d7
ffd6b68
 
20ed5d7
 
 
 
ffd6b68
 
 
20ed5d7
 
ffd6b68
20ed5d7
ffd6b68
20ed5d7
ffd6b68
20ed5d7
 
 
ffd6b68
20ed5d7
ffd6b68
20ed5d7
 
ffd6b68
 
20ed5d7
 
 
 
 
ffd6b68
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
20ed5d7
 
 
ffd6b68
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
 
 
 
 
20ed5d7
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
20ed5d7
 
 
 
 
 
 
 
 
 
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
20ed5d7
 
 
 
 
 
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
20ed5d7
 
 
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
20ed5d7
 
 
 
 
 
 
 
 
 
 
 
 
ffd6b68
 
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20ed5d7
ffd6b68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

# Interpretable-SONAR-Image-Classifier

Explainable AI for Underwater SONAR Image Classifier to enhance model interpretability with techniques like LIME, SHAP, and Grad-CAM. It employs transfer learning with popular deep learning models (e.g., VGG16, ResNet50, DenseNet121) to classify images, while providing insights into model predictions, making it suitable for use cases requiring transparency and reliability in SONAR image analysis.

The following guide details the requirements, usage, and examples for running the scripts within the project, along with how to generate explanations for model predictions.


## Prerequisites

- **Python 3.9**
- **Conda**: You can create the required environment using `environment.yaml`.

### Setting Up the Environment

1. **Conda Environment**: To ensure all dependencies are correctly installed, create a new environment using the provided `environment.yaml` file.

   ```bash

   conda env create -f environment.yaml

   ```

2. **Activate the Environment**:

   ```bash

   conda activate Interpretable-SONAR-Image-Classifier

   ```

This setup ensures compatibility and includes all necessary packages and versions as defined in the `environment.yaml` file.

### Running the Scripts

The following sections describe each script and its usage. Run these scripts directly from the command line or within a Python script.

#### 1. `data_loader.py`



This script loads, processes, splits datasets (train, val, test), and performs optional data augmentation.



**Command Line Usage:**



```sh

python data_loader.py --path <path_to_data> --target_folder <path_to_target_folder> --dim <dimension> --batch_size <batch_size> --num_workers <num_workers> [--augment_data]
```



**Arguments:**



- `--path`: Path to the raw data.

- `--target_folder`: Directory for processed data.

- `--dim`: Image resize dimension (e.g., 224 for 224x224).

- `--batch_size`: Batch size for data loading.

- `--num_workers`: Number of workers for data loading.

- `--augment_data` (optional): Enables data augmentation.



**Example:**



```sh

python data_loader.py --path "./dataset" --target_folder "./processed_data" --dim 224 --batch_size 32 --num_workers 4 --augment_data

```

**Dataset Structure:**

```plaintext

├── Dataset (Raw)

   ├── class_name_1

   │   └── *.jpg

   ├── class_name_2

   │   └── *.jpg

   ├── class_name_3

   │   └── *.jpg

   └── class_name_4

       └── *.jpg

```

#### 2. `train.py`

This script trains models with options for transfer learning and custom base models.

**Command Line Usage:**

```sh

python train.py --base_models <model_names> --shape <shape> --data_path <data_path> --log_dir <log_dir> --model_dir <model_dir> --epochs <epochs> --optimizer <optimizer> --learning_rate <learning_rate> --batch_size <batch_size>

```

**Arguments:**

- `--base_models`: Comma-separated list of base models (e.g., "VGG16,DenseNet121").
- `--shape`: Image shape, e.g., `224 224 3`.
- `--data_path`: Path to processed data.
- `--log_dir`: Directory for logs.
- `--model_dir`: Directory to save models.
- `--epochs`: Number of epochs.
- `--optimizer`: Optimizer type (`adam` or `sgd`).
- `--learning_rate`: Learning rate.
- `--batch_size`: Training batch size.
- `--patience`: Early stopping patience.

**Example:**

```sh

python train.py --base_models "VGG16,DenseNet121" --shape 224 224 3 --data_path "./processed_data" --log_dir "./logs" --model_dir "./models" --epochs 100 --optimizer "adam" --learning_rate 0.0001 --batch_size 32

```

#### 3. `test.py`

Tests the trained models and logs the results.

**Command Line Usage:**

```sh

python test.py --data_path <data_path> --base_model_name <base_model_name> --model_path <model_path> --models_folder_path <models_folder_path> --log_dir <log_dir>

```

**Arguments:**

- `--models_dir` (optional): Path to the models directory.
- `--model_path`: Specific model path (.h5 file).
- `--img_path`: Image file path for testing.
- `--test_dir`: Test dataset directory.
- `--train_dir`: Directory for training data.
- `--log_dir`: Directory for logs.

**Example:**

```sh

python test.py --model_path "./models/vgg16_model.h5" --test_dir "./test_data" --train_dir "./data/train" --log_dir "./logs"

```

#### 4. `predict.py`

Makes predictions on new images.

**Command Line Usage:**

```sh

python predict.py --model_path <model_path> --img_path <img_path> --train_dir <train_dir>

```

**Arguments:**

- `--model_path`: Model file path.
- `--img_path`: Image file path.
- `--train_dir`: Directory for label decoding.

**Example:**

```sh

python predict.py --model_path "./models/vgg16_model.h5" --img_path "./images/test_image.jpg" --train_dir "./data/train"

```

#### 5. `classify_image_and_explain.py`



Makes predictions and generates explanations using LIME, SHAP, or Grad-CAM.



**Command Line Usage:**



```sh

python classify_image_and_explain.py --image_path <image_path> --model_path <model_path> --train_directory <train_directory> --num_samples <num_samples> --num_features <num_features> --segmentation_alg <segmentation_alg> --kernel_size <kernel_size> --max_dist <max_dist> --ratio <ratio> --max_evals <max_evals> --batch_size <batch_size> --explainer_types <explainer_types> --output_folder <output_folder>
```



**Arguments:**



- `--image_path`: Path to the image file.

- `--model_path`: Model file path.

- `--train_directory`: Directory of training images for label decoding.

- `--num_samples`: Sample count for LIME.

- `--num_features`: Feature count for LIME.

- `--segmentation_alg`: Segmentation algorithm for LIME.

- `--kernel_size`: Kernel size for segmentation.

- `--max_dist`: Max distance for segmentation.

- `--ratio`: Ratio for segmentation.

- `--max_evals`: Max evaluations for SHAP.

- `--batch_size`: Batch size for SHAP.

- `--explainer_types`: Comma-separated list of explainers (`lime`, `shap`, `gradcam`).

- `--output_folder`: Directory to save explanations.



**Example:**



```sh

python classify_image_and_explain.py --image_path "./images/test_image.jpg" --model_path "./models/model.h5" --train_directory "./data/train" --num_samples 300 --num_features 100 --segmentation_alg "quickshift" --kernel_size 4 --max_dist 200 --ratio 0.2 --max_evals 400 --batch_size 50 --explainer_types "lime,gradcam" --output_folder "./explanations"

```

### Supported Base Models

The following base models are supported for training:
- VGG16
- VGG19
- ResNet50
- ResNet101
- InceptionV3
- DenseNet121
- DenseNet201
- MobileNetV2
- Xception
- InceptionResNetV2
- NASNetLarge
- NASNetMobile
- EfficientNetB0
- EfficientNetB7

### Running Scripts in Jupyter Notebook

You can also run these scripts programmatically using Python's `subprocess` module. Here is an example of how to do this for each script:

```python

import subprocess



# Run data_loader.py

subprocess.run([

    "python", "data_loader.py",

    "--path", "./data",

    "--target_folder", "./processed_data",

    "--dim", "224",

    "--batch_size", "32",

    "--num_workers", "4",

    "--augment_data"

])



# Run train.py

subprocess.run([

    "python", "train.py",

    "--base_models", "VGG16,ResNet50",

    "--shape", "224, 224, 3",

    "--data_path", "./data",

    "--log_dir", "./logs",

    "--model_dir", "./models",

    "--epochs", "100",

    "--optimizer", "adam",

    "--learning_rate", "0.001",

    "--batch_size", "32",

    "--patience", "10"

])



# Run test.py

subprocess.run([

    "python", "test.py",

    "--models_dir", "./models",

    "--img



_path", "./images/test_image.jpg",

    "--train_dir", "./data/train",

    "--log_dir", "./logs"

])



# Run classify_image_and_explain.py

subprocess.run([

    "python", "classify_image_and_explain.py",

    "--image_path", "./images/test_image.jpg",

    "--model_path", "./models/model.h5",

    "--train_directory", "./data/train",

    "--num_samples", "300",

    "--num_features", "100",

    "--segmentation_alg", "quickshift",

    "--kernel_size", "4",

    "--max_dist", "200",

    "--ratio", "0.2",

    "--max_evals", "400",

    "--batch_size", "50",

    "--explainer_types", "lime,gradcam",

    "--output_folder", "./explanations"

])

```

## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

## Citing the part of the project: Under water sonar image classifier with XAI LIME

If you use our SONAR classifier or the explainer in your research, please use the following BibTeX entry.

```

@article{natarajan2024underwater,

  title={Underwater SONAR Image Classification and Analysis using LIME-based Explainable Artificial Intelligence},

  author={Natarajan, Purushothaman and Nambiar, Athira},

  journal={arXiv preprint arXiv:2408.12837},

  year={2024}

}

```