Skip To Content

示例:使用预训练模型进行推断

深度学习可用于自动化从影像中手动提取要素的任务。 使用 ArcGIS 预训练模型,您可以减少对大型数据集、强大算力以及深度学习知识或专业知识的需求。 这些模型已经针对不同的地理数据进行了训练,并且在各个地区都表现良好。 ArcGIS 预训练模型可通过 ArcGIS Living Atlas of the World 供所有具有 ArcGIS 账户的用户使用。 在以下工作流中,Car Detection-USA 模型用于识别高分辨率无人机和航空影像中的汽车。

要求

执行此工作流需要满足以下要求:

  • 用于运行模型的航空影像。
    注:

    您可以从 OpenAerialMap 下载包含汽车的航空影像。

  • 深度学习的运算量非常大,建议您使用强大的 GPU 来处理大型数据集。

使用预训练模型进行推断

以下部分介绍了使用 ArcGIS 预训练模型进行推断所涉及的过程。

Python 库导入

导入以下 Python 库:

from arcgis.gis import GIS
from arcgis.learn import detect_objects
from arcgis.raster.analytics import copy_raster
 
gis = GIS("home")

添加数据和模型进行分析

您可以使用两种方法将数据集添加到笔记本。

第一种方法是将航空影像上传到笔记本工作空间,将其添加到代码单元格,然后运行 copy_raster() 函数来提取数据。

dataset = "/arcgis/home/Aerial imagery.tif"
imagery = copy_raster(input_raster=dataset, output_name="Aerial_imagery", raster_type_name="Raster Dataset")

第二种方法是将航空影像作为托管影像图层添加到您的门户,然后使用其 itemID 将影像图层添加到您的笔记本。

imagery = gis.content.get("<imagery-layer-item-id>")

添加深度学习包

您可以从 ArcGIS Living Atlas 下载预训练的 Car Detection - USA 模型。 下载模型后,将其作为项目添加到您的门户,并使用其 itemID 在笔记本中访问它。

model = gis.content.get("<deep-learning-package-item-id>")

运行模型

要在笔记本中运行预训练模型,请使用 arcgis.learn 中的 detect_objects 模块。 该模块将把您的影像作为输入,并检测和计算其中存在的汽车数量。

注:

您可以通过使用不同的值增强模型的参数来潜在提高模型的性能,直到达到所需的输出:

model_arguments = {
	"padding": "100",	# The number of pixels at the border of image tiles from which predictions are blended for adjacent tiles. To smooth the output while reducing artifacts, increase the value
	"batch_size": "16",	# Change batch size as per GPU specifications
	"threshold": "0.5",	# Get detections greater than 50% confidence
	"tile_size": "224",	# The width and height of image tiles into which the imagery is split for prediction
        }

注:

如果配置了 GPU,则可以在 detect_object() 方法内部设置上下文,将其设置为 GPU 环境:

context = { "processorType": "GPU" }

num_cars = {}
 
detected_cars = detect_objects(
        input_raster=imagery,
        model = model,
        output_name = "detected_cars",
        )
 
num_cars[imagery.name] = len(detected_cars.query(as_df=True))

模型结果

最后,运行模型后,您就可以显示 num_cars 的输出:

print(f"Number of cars is {num_cars}.")