【教程3/6】1小时学会 Segment Anything Model (SAM) 遥感影像分割

本节是1小时学会 Segment Anything Model (SAM) 遥感影像分割系列教程的第三节

代码(源码下载在最下方):

a180771f92cea4e9d3e652e5cf7b8b7b.jpeg

Install dependencies

Uncomment and run the following cell to install the required dependencies.

import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE" #OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized
os.environ['PROJ_LIB'] =r"F:\Anaconda3\envs\samgeo\Lib\site-packages\pyproj\proj_dir\share\proj"
import leafmap
from samgeo import tms_to_geotiff
from samgeo.text_sam import LangSAM

Create an interactive map

m = leafmap.Map(center=[-22.17615, -51.253043], zoom=18, height="800px")
m.add_basemap("Esri.WorldImagery")
m
Map(center=[-22.17615, -51.253043], controls=(ZoomControl(options=['position', 'zoom_in_text', 'zoom_in_title'…

Download a sample image

Pan and zoom the map to select the area of interest. Use the draw tools to draw a polygon or rectangle on the map

bbox = m.user_roi_bounds()
if bbox is None:
    bbox = [-51.2565, -22.1777, -51.2512, -22.175]
image = "Image.tif"
# tms_to_geotiff(output=image, bbox=bbox, zoom=19, source="Satellite", overwrite=True)

You can also use your own image. Uncomment and run the following cell to use your own image.

Display the downloaded image on the map.

m.layers[-1].visible = False
m.add_raster(image, layer_name="Image")
m
Map(bottom=18898354.0, center=[-22.17615, -51.253043], controls=(ZoomControl(options=['position', 'zoom_in_tex…

Initialize LangSAM class

The initialization of the LangSAM class might take a few minutes. The initialization downloads the model weights and sets up the model for inference.

# import samgeo
# samgeo.update_package()
sam = LangSAM()
final text_encoder_type: bert-base-uncased


Some weights of the model checkpoint at bert-base-uncased were not used when initializing BertModel: ['cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.bias', 'cls.predictions.transform.dense.weight', 'cls.seq_relationship.bias', 'cls.seq_relationship.weight', 'cls.predictions.transform.dense.bias']
- This IS expected if you are initializing BertModel from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing BertModel from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).

Specify text prompts

text_prompt = "tree"

Segment the image

Part of the model prediction includes setting appropriate thresholds for object detection and text association with the detected objects. These threshold values range from 0 to 1 and are set while calling the predict method of the LangSAM class.

box_threshold: This value is used for object detection in the image. A higher value makes the model more selective, identifying only the most confident object instances, leading to fewer overall detections. A lower value, conversely, makes the model more tolerant, leading to increased detections, including potentially less confident ones.

text_threshold: This value is used to associate the detected objects with the provided text prompt. A higher value requires a stronger association between the object and the text prompt, leading to more precise but potentially fewer associations. A lower value allows for looser associations, which could increase the number of associations but also introduce less precise matches.

Remember to test different threshold values on your specific data. The optimal threshold can vary depending on the quality and nature of your images, as well as the specificity of your text prompts. Make sure to choose a balance that suits your requirements, whether that's precision or recall.

sam.predict(image, text_prompt, box_threshold=0.24, text_threshold=0.24)

Visualize the results

Show the result with bounding boxes on the map.

sam.show_anns(
    cmap='Greens',
    box_color='red',
    title='Automatic Segmentation of Trees',
    blend=True,
)

Show the result without bounding boxes on the map.

sam.show_anns(
    cmap='Greens',
    add_boxes=False,
    alpha=0.5,
    title='Automatic Segmentation of Trees',
)
sam.show_anns(
    cmap='Greys_r',
    add_boxes=False,
    alpha=1,
    title='Automatic Segmentation of Trees',
    blend=False,
    output='trees.tif',
)

c4aacf4defedc2f369757e846bd8cfb4.png

png

Convert the result to a vector format.

sam.raster_to_vector("trees.tif", "trees.shp")

Show the results on the interactive map.

m.add_raster("trees.tif", layer_name="Trees", palette="Greens", opacity=0.5, nodata=0)
style = {
    "color": "#3388ff",
    "weight": 2,
    "fillColor": "#7c4185",
    "fillOpacity": 0.5,
}
m.add_vector("trees.shp", layer_name="Vector", style=style)
m
Map(bottom=1209461600.0, center=[-22.176349999999996, -51.25385], controls=(ZoomControl(options=['position', '…
Interactive segmentation
# sam.show_map()

ANOTHER 2023-07-11 11:25

from samgeo import SamGeo
sam1 = SamGeo()
sam1.clear_cuda_cache()
sam.predict(image, "roads", box_threshold=0.24, text_threshold=0.24)
sam.show_anns(
    cmap='Reds',
    add_boxes=False,
    alpha=0.5,
    title='Automatic Segmentation of roads',
)
sam.show_anns(
    cmap='Greys_r',
    add_boxes=False,
    alpha=1,
    title='Automatic Segmentation of roads',
    blend=False,
    output='roads.tif',
)
sam.raster_to_vector("roads.tif", "roads.shp")
m.add_raster("trees.tif", layer_name="Trees", palette="Greens", opacity=0.5, nodata=0)
style = {
    "color": "#3388ff",
    "weight": 2,
    "fillColor": "#7c4185",
    "fillOpacity": 0.5,
}
m.add_vector("roads.shp", layer_name="Vector", style=style)
m

下载地址(点击下方小程序):

  • 14
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GIS 数据栈

谢谢打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值