- 🔬 Data Science
- 🥠 Deep Learning
When ground data is not available, crater detection and counting play a vital role in predicting the moons surface age. Further, crater detection is necessary for identifying viable landing sites for lunar landers, as well as for establishing landmarks for navigation on the moon. Traditionally, craters have manually been digitized using visual interpretation, a very time-consuming and inefficient method. However, with the proliferation of deep learning, the process of crater detection is now able to be bolstered by automated detection. This notebook will demonstrate how the ArcGIS API for Python can be used to train a deep learning crater detection model using a Digital Elevation Model (DEM), which can then be deployed in ArcGIS Pro or ArcGIS Enterprise.
import os from pathlib import Path from arcgis import GIS from arcgis.learn import MaskRCNN, prepare_data
Connect to your GIS
gis = GIS('home')
Export training data
The DEM of the moon will be used as
input_raster for the training data and has a spatial resolution of 118m. It can be downloaded from the USGS website.
lunar_dem = gis.content.get('beae2f7947704c938eb957c6deb2fa2b') lunar_dem
The crater feature layer will be used as the
Input Feature Class for the training data.
Feature layer of craters 5-20 km will be used to export training data at 500 and 5000 cell size.
craters_5_20km = gis.content.get('af240e45a3ca445b88d6ec19209d3bb5') craters_5_20km
Feature layer of craters more than 20 km will be used to export training data at 10000 cell size.
craters_more_than_20km = gis.content.get('36272e93ec4547abba495c797a4fb921') craters_more_than_20km
The data will be exported in the “RCNN Masks” metadata format, which is available in the Export Training Data For Deep Learning tool. This tool is available in both ArcGIS Pro and ArcGIS Image Server. The various inputs required by the tool are described below:
Input Raster: Lunar DEM
Input Feature Class Or Classified Raster Or Table: The craters feature layer
Tile Size X & Tile Size Y: 256
Stride X & Stride Y: 128
Meta Data Format: 'RCNN Masks', as we are training a MaskRCNN model.
Cell Size: The data is exported to 3 different cell sizes so that it can learn to detect craters of different diameters. The data is exported at 500, 5000, and 10000.
Processing Extent: Default
Note: The training data must be exported in the same output folder for all three cell sizes.
Inside the exported data folder, the 'images' folder contains all of the chips of the DEM, and the 'labels' folder contains the masks of the craters.
Alternatively, we have provided a subset of training data containing a few samples that follow the same directory structure mentioned above, as well as the raster and crater feature layers used for exporting the training dataset. The provided data can be used directly to run the experiments.
training_data = gis.content.get('0a4e2d4ad7bf41f6973c7e3434faf7d4') training_data
filepath = training_data.download(file_name=training_data.name)
#Extract the data from the zipped image collection import zipfile with zipfile.ZipFile(filepath, 'r') as zip_ref: zip_ref.extractall(Path(filepath).parent)
output_path = Path(os.path.join(os.path.splitext(filepath)))
data = prepare_data(output_path, batch_size=8)
Visualize a few samples from your training data
To get a sense of what the training data looks like, the
arcgis.learn.show_batch() method will randomly select a few training chips and visualize them.
Load model architecture
arcgis.learn module provides the
MaskRCNN model for instance segmentation tasks.
MaskRCNN is based on a pretrained convolutional neural network, like
ResNet, that acts as the 'backbone'. More details about
MaskRCNN can be found here.
model = MaskRCNN(data)
Tuning for optimal learning rate
Learning rate is one of the most important hyperparameters in model training. ArcGIS API for Python provides a learning rate finder that automatically chooses the optimal learning rate for you.
lr = model.lr_find() lr
Fit the model
Next, the model is trained for a few epochs, with
early_stopping=True and the learning rate recommended above.
model.fit(100, lr, early_stopping=True)
Epoch 56: early stopping
Here, the model training automatically stopped at the 56th epoch. We can see reasonable results, as both the training and validation losses decreased considerably, indicating that the model is learning to translate between different domains of imagery.
Save the model
Here, we will save the trained model as a 'Deep Learning Package' ('.dlpk'). The Deep Learning Package format is the standard format used when deploying deep learning models on the ArcGIS platform.
Published DLPK Item Id: 81e635734e1b4d558cd51e2cee44833a
Visualize results in validation set
It is a good practice to view the results of the model viz-a-viz the ground truth. The
model.show_results() method can be used to display the detected craters. Each detection is visualized as a mask by default.
For the accuracy assessment, we can compute the average precision score for the trained model. Average precision will compute the average precision on the validation set for each class. We can compute the average precision score by calling
model.average_precision_score. It takes the following parameters:
detect_thresh: The probability above which a detection will be considered for computing the average precision.
iou_thresh: The intersection over union threshold with the ground truth labels, above which a predicted bounding box will be considered a true positive.
mean: If set to
False, will return the class-wise average precision. Otherwise will return the mean average precision.
arcpy.ia.DetectObjectsUsingDeepLearning(in_raster="dem_for_inference", out_detected_objects=r"\\detected_craters", in_model_definition=r"\\models\moon_mrcnn_2\moon_mrcnn_2.dlpk", model_arguments ="padding 56;batch_size 4;threshold 0.6;return_bboxes False", run_nms="NMS", confidence_score_field="Confidence", class_value_field="Class", max_overlap_ratio=0, processing_mode="PROCESS_AS_MOSAICKED_IMAGE")
The inferencing above was done on the following cell sizes: 500m (small craters), 5000m (medium-sized craters), and 10,000m (large craters). All of the inferenced outputs were then merged together to get the final feature layer, which consists of all the detected craters.
Finally, after we have used ArcGIS Pro to detect craters in two different areas, we will publish the results to our portal and visualize them as maps.
from arcgis.mapping import WebMap ## Area1 Craters LunarArea1 = gis.content.get('1a76ed548cfc4e159d860ae253e5ecc3') map1 = WebMap(LunarArea1) map1
## Area2 Craters LunarArea2 = gis.content.get('2ce5ad91fab649c7982dde750cce3390') map2 = WebMap(LunarArea2) map2
In the maps above, we can see that the trained
MaskRCNN model was able to accurately detect craters of varying sizes. While the training data that was provided to the model contained craters digitized as circle features, the model was even able to learn to detect more accurate, non-circular boundaries of the craters.
This notebook showcased how instance segmentation models, like
MaskRCNN, can be used to automatically detect lunar craters using a DEM. We also demonstrated how custom transformations based on the data, irrespective of already present standard transformations, can be added during data preparation to achieve better performance.