Skip to content

Commit 77bdc8f

Browse files
Added subections for object detection/pixel classification
1 parent 17cf1c3 commit 77bdc8f

File tree

1 file changed

+95
-25
lines changed

1 file changed

+95
-25
lines changed

guide/14-deep-learning/model_extension_guide.ipynb

Lines changed: 95 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Add a new model using Model Extension"
8+
]
9+
},
310
{
411
"cell_type": "markdown",
512
"metadata": {},
@@ -11,9 +18,9 @@
1118
"cell_type": "markdown",
1219
"metadata": {},
1320
"source": [
14-
"With `arcgis.learn`, there are a multitude of machine learning models available for different tasks. There are models for object detection, pixel classification, image translation, natural language processing, point-cloud data, etc. and the list keeps on growing. But, what if you came across a deep learning model that is not yet a part of the `learn` module and you want to use it from its library or its open-source code on github? What if you created your own deep learning model for a specific task you are working on? What if you want to use these new models with all the capabilities of the ArcGIS ecosystem?\n",
21+
"With `arcgis.learn`, there are a multitude of machine learning models available for different tasks. There are models for object detection, pixel classification, image translation, natural language processing, point-cloud data, etc. and the list keeps on growing. But what if you come across a deep learning model that is not yet a part of the `learn` module and you want to use it from its library or its open-source code on GitHub? What if you created your own deep learning model for a specific task you are working on? What if you want to use these new models with all the capabilities of the ArcGIS ecosystem?\n",
1522
"\n",
16-
"There is a solution - **Model Extension**, a general purpose wrapper for any **object detection** and **pixel classification** model on top of our existing framework. It wraps all the details of our stack of Pytorch, Fastai, and the learn module and provides an easy to implement structure for the integration of a third-party deep learning model."
23+
"There is a solution - **Model Extension**, a general-purpose wrapper for any **object detection** and **pixel classification** model on top of our existing framework. It wraps all the details of our stack of PyTorch, Fastai, and the learn module and provides an easy to implement structure for the integration of a third-party deep learning model."
1724
]
1825
},
1926
{
@@ -84,22 +91,43 @@
8491
"cell_type": "markdown",
8592
"metadata": {},
8693
"source": [
87-
"1. `on_batch_begin`: This function is required to transform the input data and the target (the ground truth) used for training the model. The transformation of inputs is in accordance to the model input requirements. This function is equivalent to the fastai on_batch_begin function, but is called in succession of it. Therefore, transformation of inputs is needed only if the format required by the model is different from what fastai transforms it into.\n",
94+
"1. `on_batch_begin`: This function is required to transform the input data and the target (the ground truth) used for training the model. The transformation of inputs is in accordance to the model input requirements. This function is equivalent to the fastai on_batch_begin function, but is called in succession of it. Therefore, transformation of inputs is needed only if the format required by the model is different from what fastai transforms it into."
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"The function receives the following arguments:\n",
102+
"+ `learn` - a fastai learner object\n",
88103
"\n",
89-
" The function receives the following arguments:\n",
90-
" + `learn` - a fastai learner object\n",
91-
" + `model_input_batch` - fastai transformed batch of input images: tensor of shape [N,C,H,W] where\n",
104+
"+ `model_input_batch` - fastai transformed batch of input images: tensor of shape [N, C, H, W] with values in the range -1 and 1, where\n",
105+
" N - batch size\n",
106+
" C - number of channels (bands) in the image\n",
107+
" H - height of the image\n",
108+
" W - width of the image \n",
109+
"\n",
110+
"+ `model_target_batch` - fastai transformed batch of targets. The targets will be of different type and shape for object detection and pixel classification.\n",
111+
" \n",
112+
" **Object Detection**\n",
113+
" \n",
114+
" list of tensors [bboxes, classes]\n",
115+
" \n",
116+
" bboxes: tensor of shape [N, B, 4], where \n",
92117
" N - batch size\n",
93-
" C - number of channels (bands) in the image\n",
94-
" H - height of the image\n",
95-
" W - width of the image\n",
118+
" B - the maximum number of boxes present in any image of the batch\n",
119+
" 4 - the bounding box coordinates in the order y1, x1, y2, x2 and values in the range -1 to 1\n",
120+
" \n",
121+
" classes: tensor of shape [N, B] representing class of each bounding box\n",
122+
"\n",
123+
" **Pixel Classification**\n",
124+
"\n",
125+
" tensor of shape [N, K, H, W] representing a binary raster, where\n",
96126
"\n",
97-
" + `model_target_batch` - fastai transformed batch of targets: list of tensors [bboxes, classes]\n",
98-
" bboxes is a tensor of shape [N, B, 4] where \n",
99-
" N is the batch size\n",
100-
" B is the max number of boxes present in any image of the batch\n",
101-
" 4 is for the bounding box coordinates in the order y1, x1, y2, x2 with values in range -1 to 1\n",
102-
" classes is a tensor of shape [N, B] representing class of each bounding box"
127+
" N - batch size\n",
128+
" K - number of classes in the dataset\n",
129+
" H - height of the image\n",
130+
" W - width of the image"
103131
]
104132
},
105133
{
@@ -126,11 +154,11 @@
126154
"2. `transform_input`: This function is required to transform the input images during inferencing.\n",
127155
" \n",
128156
" The function receives the following arguments:\n",
129-
" + `xb` - fastai transformed batch of input images: tensor of shape [N,C,H,W] where\n",
130-
" N - batch size\n",
131-
" C - number of channels (bands) in the image\n",
132-
" H - height of the image\n",
133-
" W - width of the image"
157+
" + `xb` - fastai transformed batch of input images: tensor of shape [N, C, H, W], where\n",
158+
" N - batch size\n",
159+
" C - number of channels (bands) in the image\n",
160+
" H - height of the image\n",
161+
" W - width of the image"
134162
]
135163
},
136164
{
@@ -252,12 +280,19 @@
252280
"cell_type": "markdown",
253281
"metadata": {},
254282
"source": [
255-
"The raw output of the model is usually not in the best descernible form. It needs to be post-processed to be understood by the user. The `post_process` function is used to transform the raw outputs of the model to a specific format for the final results and visualization pipeline to ingest.\n",
283+
"The raw output of the model is usually not in the best descernible form. It needs to be post-processed to be understood by the user. The `post_process` function is used to transform the raw outputs of the model to a specific format for the final results and visualization pipeline to ingest."
284+
]
285+
},
286+
{
287+
"cell_type": "markdown",
288+
"metadata": {},
289+
"source": [
290+
"**Object Detection**\n",
256291
"\n",
257292
"The function receives the following arguments:\n",
258-
"+ `pred` - Raw output of the model for a batch if images\n",
293+
"+ `pred` - Raw output of the model for a batch of images\n",
259294
"+ `nms_overlap` - Non-maxima suppression value used to select from overlapping bounding boxes\n",
260-
"+ `thresh` - Confidence threshold to be used to filter the predictions\n",
295+
"+ `thres` - Confidence threshold to be used to filter the predictions\n",
261296
"+ `chip_size` - Size of the image chips on which predictions are made\n",
262297
"+ `device` - Device (CPU or GPU) on which the output needs to be put after post-processing\n",
263298
"\n",
@@ -291,6 +326,41 @@
291326
" return post_processed_pred"
292327
]
293328
},
329+
{
330+
"cell_type": "markdown",
331+
"metadata": {},
332+
"source": [
333+
"**Pixel Classification**\n",
334+
"\n",
335+
"The function receives the following arguments:\n",
336+
"+ `pred` - Raw output of the model for a batch of images\n",
337+
"+ `thres` - Confidence threshold to be used to filter the predictions\n",
338+
"\n",
339+
"Returns:\n",
340+
"+ `post_processed_pred`: tensor of shape [N, 1, H, W] or a List/Tuple of N tensors of shape [1, H, W], where\n",
341+
" N - batch size\n",
342+
" H - height of the image\n",
343+
" W - width of the image\n",
344+
" \n",
345+
" The values (type: LongTensor) of the tensor denote the predicted class of each pixel.\n",
346+
" "
347+
]
348+
},
349+
{
350+
"cell_type": "code",
351+
"execution_count": 2,
352+
"metadata": {},
353+
"outputs": [],
354+
"source": [
355+
"def post_process(self, pred, thres):\n",
356+
" \"\"\"\n",
357+
" Fuction to post process the output of the model in validation/infrencing mode.\n",
358+
" \"\"\"\n",
359+
" post_processed_pred = ...\n",
360+
" \n",
361+
" return post_processed_pred"
362+
]
363+
},
294364
{
295365
"cell_type": "markdown",
296366
"metadata": {},
@@ -532,7 +602,7 @@
532602
"cell_type": "markdown",
533603
"metadata": {},
534604
"source": [
535-
"From here onwards, we can continue with the usual workflow of using an `arcgis.learn` deep learning model. Checkout [Detecting Swimming Pools using Deep Learning sample](https://developers.arcgis.com/python/sample-notebooks/detecting-swimming-pools-using-satellite-image-and-deep-learning/) sample notebook to see the workflow for an **object detection** model and the [Land Conver Classification using Satellite Imagery and Deep Learning](https://developers.arcgis.com/python/sample-notebooks/land-cover-classification-using-unet/) sample notebook for the workflow for a **pixel classification** model."
605+
"From here onwards, we can continue with the usual workflow of using an `arcgis.learn` deep learning model. Refer to [Detecting Swimming Pools using Deep Learning sample](https://developers.arcgis.com/python/sample-notebooks/detecting-swimming-pools-using-satellite-image-and-deep-learning/) sample notebook to see the workflow for an **object detection** model and the [Land Conver Classification using Satellite Imagery and Deep Learning](https://developers.arcgis.com/python/sample-notebooks/land-cover-classification-using-unet/) sample notebook for the workflow for a **pixel classification** model."
536606
]
537607
},
538608
{
@@ -559,7 +629,7 @@
559629
"name": "python",
560630
"nbconvert_exporter": "python",
561631
"pygments_lexer": "ipython3",
562-
"version": "3.6.12"
632+
"version": "3.7.11"
563633
}
564634
},
565635
"nbformat": 4,

0 commit comments

Comments
 (0)