|
1 | 1 | # tree-risk-ai |
2 | 2 | Artificial Intelligence for Tree Failure Identification and Risk Quantification |
3 | 3 |
|
| 4 | +## Summary |
| 5 | +The code and models in this repository implement convolutional neural network (CNN) models to predict tree likelihood of failure categories from a given input image. The categories are: |
| 6 | +- Improbable: failure unlikely either during normal or extreme weather conditions |
| 7 | +- Possible: failure expected under extreme weather conditions; but unlikely during normal weather conditions |
| 8 | +- Probable: failure expected under normal weather conditions within a given time frame |
| 9 | +Original input images are 3024 x 4032 pixels. We assess the performance of an optimized CNN using 64-pixel, 128-pixel and 224-pixel inputs (after data augmentation expands samples from 525 images to 2525 images). |
| 10 | +We also evaluate performance under four classification scenarios (investigating how various category groupings impact classifier performance): |
| 11 | +Pr_Im: {Probable, Improbable}; 2 classes |
| 12 | +PrPo_Im: {Probable + Possible, Improbable}; 2 classes |
| 13 | +Pr_PoIm: {Probable, Possible + Improbable}; 2 classes |
| 14 | +Pr_Po_Im: {Probable, Possible, Improbable}; 3 classes |
| 15 | + |
4 | 16 | ## Step 1: Label input data |
5 | | -Inputs are images (currently 3024 x 4032 pixels). These are currently saved locally and not accessible on the remote. Email the collaborators for data access. To perform labeling, run `process-image-files`. The current framework assumes theraw images are housed in `data/raw/Pictures for AI`. |
| 17 | +Inputs are images (currently 3024 x 4032 pixels). These are currently saved locally and not accessible on the remote. Email the collaborators for data access. To perform labeling, run `label-image-files.py`. The user must specify the path to the raw images (`RAW_IMAGE_DIR`). The current framework assumes the raw images are housed in `data/raw/Pictures for AI`. |
6 | 18 |
|
7 | 19 |
|
8 | 20 | ## Step 2: Preprocess images |
9 | | -In this step, we perform image resizing and data augmentation (random cropping, horizontal flipping - probability of 50%). The user can specify the expansion factor for the original set of images. For instances, if there are 500 images in the original set and an expansion factor of 5 is specified for the preprocessing function, then the final augmented set will contain 2500 images. Finally, image training sets are generated for 4 classification scenarios and for user-specified resolutions, e.g. 64 x 64 px, 128 x 128 px, etc. One-hot-vector encoding is also performed. Each set of images and labels are saved as an array of tuples in a binary `.npy` file. |
| 21 | +In this step (`preprocess-images.py`), we perform image resizing and data augmentation (random cropping, horizontal flipping - probability of 50%). The user can specify the expansion factor for the original set of images. For instance, there are 525 images in the original dataset. if an expansion factor of 5 is specified for the preprocessing function, then the final augmented set will contain 2525 images. Finally, image training sets are generated for 4 classification scenarios and for user-specified resolutions, e.g. 64 x 64 px, 128 x 128 px, etc. One-hot-vector encoding is also performed. Each set of images and labels are saved as an array of tuples in a binary `.npy` file. The `preprocess-images.py` script also includes a `plotProcessedImages()` function that generates a specified number of randomly chosen input images for each scenario. |
| 22 | + |
| 23 | +The user can also plot selected processed images using the functions in the `plot-processed-images.py` script. To explore all the processed images in a matrix plot, use `exploreProcessedImages()`. Figure 2 in the manuscript was generated using the `plotSelectedProcessedImages()` function. |
10 | 24 |
|
11 | 25 | ## Step 3: CNN hyperparameter optimization |
12 | | -We use the `HyperModel` module from `keras.tuner` to optimize the following parameters in our convolutional neural network: |
| 26 | +We use the `Hyperband` function from `keras.tuner` to optimize the following parameters in our convolutional neural network: kernel size of first convolutional layer, units in the 2 dense layers, their respective dropout rates and activation functions. The routine is carried out in `cnn-hyperparameter-optimization.py`. The search is performed for 12 cases (3 resolutions and 4 classification scenarios). |
| 27 | +- The results are tabulated via `tabulate-optimal-hyperparameters.py` (which generates the CSV files used to create Table 4 in the manuscript). |
| 28 | + |
| 29 | +## Step 4: Sensitivity tests |
| 30 | +In `resolution-scenario-sensitivity.py`, the function `testResolutionScenarioPerformance()` conducts CNN model fitting for each combination of resolution and scenario as specified by the user in `RESOLUTION_LIST` and `SCENARIO_LIST` respectively. This is done via k-fold cross-validation. Validation metrics of macro-average precision, recall and $F_1$ are also implemented. Model histories are saved for each trial. |
| 31 | + |
| 32 | +Tabulation and visualization summaries of the results are implemented in `senstivity-analysis.ipynb`. |
| 33 | +- Figure 4 in the manuscript is generated using `plotMeanAccuracyLoss()`. |
| 34 | +- Figure 5 is generated using `plotSummaryValidationMetrics()` |
| 35 | + |
| 36 | +Furthermore, we aggregate performance statistics in `senstivity-analysis.ipynb` and performance Welch's tests to determine if there are significant differences in outcomes. |
| 37 | +- The function `getScenarioResolutionMeanPerformance()` generates Table 6. |
| 38 | +- The function `resolutionPerformanceComparisonStats()` generates Table 7. |
| 39 | +- The function `scenarioPerformanceComparisonStats()` generates Table 8. |
| 40 | + |
| 41 | +## Step 5: Detailed CNN performance analysis |
| 42 | +In `cnn-performance.py`, we define the function `trainModelWithDetailedMetrics()` which implements CNN model-fitting, along with sklearn classification metrics, including a confusion matrix, for a given resolution/scenario instance. The loss and performance results are visualized in the `plot-cnn-performance.ipynb` notebook, using the function `plotCNNPerformanceMetrics()`. |
| 43 | +- Figure 6 in the manuscript is generated via `plotCNNPerformanceMetrics()`. |
| 44 | +- Figure 7 is based on the confusion matrices saved from running `getScenarioModelPerformance()`, which in turns runs `trainModelWithDetailedMetrics()`. |
| 45 | +The trained model is saved to `results/models/`. |
| 46 | + |
| 47 | +## Step 6: CNN Visualization and Inference (in progress) |
| 48 | +We implement GradCAM and saliency maps to understand how the CNN classifies an image. This is done using `plotGradCAM()` and `plotSaliency()` in `cnn-visualization.ipynb`. A prior trained model is loaded (e.g. `m = models.load_model('../../results/models/opt-cnn-Pr_Im-128-px/model')`) and used as an input to either of the functions mentioned. |
13 | 49 |
|
14 | 50 |
|
| 51 | +Please note: Function and classes that are used in two or more scripts are housed in `helpers.py` |
0 commit comments