Lunar Craters Detection using Deep Learning

  • 🔬 Data Science
  • 🥠 Deep Learning

Introduction

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.

Necessary imports

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
dem_training_raster
dem_training_rasterImagery Layer by demos_deldev
Last Modified: January 14, 2022
0 comments, 0 views

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
craters_diameter_5_20km
The purpose of this shapefile is to provide a catalog of craters greater to or equal to 5 km (kilometers) and less that or equal to 20 km in diameter centered between 60S to 60N latitude and 90E to 180E longitude.Feature Layer Collection by demos_deldev
Last Modified: November 26, 2021
0 comments, 3 views

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
craters_diameter_more_than_20km
craters_diameter_more_than_20kmFeature Layer Collection by demos_deldev
Last Modified: November 26, 2021
0 comments, 4 views

Methodology

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.

Environments:

  • 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.

Model training

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
lunar_craters_detection_from_digital_elevation_models_using_deep_learning_
lunar_craters_detection_from_digital_elevation_models_using_deep_learningImage Collection by api_data_owner
Last Modified: January 14, 2022
0 comments, 6 views
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)

Prepare data

output_path = Path(os.path.join(os.path.splitext(filepath)[0]))
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.

data.show_batch(rows=2)
<Figure size 1080x720 with 6 Axes>

Load model architecture

The 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
<Figure size 432x288 with 1 Axes>
3.630780547701014e-05

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)
56.00% [56/100 1:09:59<54:59]
epochtrain_lossvalid_losstime
06.9443995.09536301:00
13.0833382.41094401:04
22.2646182.28742501:08
32.1029382.24994501:09
42.0324362.22955701:15
52.0099232.24045201:15
61.9824282.19159901:12
71.9608922.27753601:17
81.9910662.21776101:17
91.9381402.13226701:17
101.8686052.11323601:19
111.7770412.07541801:17
121.7245451.98025701:17
131.6883911.94077201:17
141.6449721.87294801:16
151.6113571.86407201:14
161.5330231.81066701:17
171.5398681.77985001:18
181.5118171.77686001:19
191.4895221.79649901:18
201.4789981.68959501:17
211.4823211.74763801:16
221.4592651.68937001:15
231.3950781.69254701:13
241.4192131.63743901:15
251.4076911.67585301:13
261.3843651.62308501:13
271.3545621.62051601:21
281.3459681.63175801:13
291.3683541.60537801:13
301.3508301.61650801:13
311.3246331.61894601:12
321.2979691.59198201:12
331.2631831.64976801:13
341.2884921.61224001:13
351.2991361.59992501:12
361.2760541.60423701:12
371.2617391.57272601:12
381.2705141.53442301:13
391.2656351.53122101:15
401.2787501.58369701:15
411.2574611.53030801:13
421.2546751.58519701:13
431.2510331.52102901:13
441.2406561.53733601:13
451.2572851.52809101:13
461.2280901.56142601:22
471.1970091.54050301:15
481.2496791.50799601:17
491.2143841.49852701:14
501.2082891.45011801:15
511.2003281.50924001:13
521.1848231.46443601:19
531.1769641.49830701:15
541.2036551.48085601:14
551.1566131.52159401:14

100.00% [10/10 00:06<00:00]
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.

model.save("moon_mrcnn_2", publish=True)
Published DLPK Item Id: 81e635734e1b4d558cd51e2cee44833a
WindowsPath('C:/Users/shi10484/AppData/Local/Temp/lunar_craters_detection_from_digital_elevation_models_using_deep_learning/models/moon_mrcnn_2')

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.

model.show_results(rows=2, mask_threshold=0.7)

Accuracy assessment

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.
model.average_precision_score()
100.00% [10/10 00:09<00:00]
{'1': 0.4536149187175708}

Model inferencing

After training the MaskRCNN model and saving the weights of the detected crater masks, we can use the Detect Objects Using Deep Learning tool available in ArcGIS Pro and ArcGIS Image Server.

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.

Results visualization

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.

Conclusion

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.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.