本机windows搭建达摩院与高德联合出品的地理地址自然语言处理模型MGeo实战

本文详细介绍了如何在Windows环境下搭建达摩院与高德合作的MGeo地理地址自然语言处理模型,包括环境配置、ModelScope安装、深度学习库选择及安装、测试代码执行和耗时分析。文章还提及了GeoGLUE数据集和模型训练的选项。
摘要由CSDN通过智能技术生成

本机windows搭建达摩院与高德联合出品的地理地址自然语言处理模型MGeo实战

简介

多模态地理文本预训练模型MGeo

MGeo提炼了常用的地址处理任务并建立了地理语义理解能力评测基准GeoGLUE,使用MGeo底座在GeoGLUE中提供的任务数据集上进行了训练。
地址信息处理是对地址相关文本的自动化挖掘、理解与关联。这项技术广泛地应用在社会生活的各个场景之中。例如我们常用的地图软件中就用到了大量的地址信息处理技术来构建POI库,实现POI搜索与推荐;在外卖物流行业中,对于地址的解析、定位准确率的提升则直接带来运力成本的大量降低;目前在诸多挪车、外呼、报警等场景下也用上了地址自动化处理技术,大大节省了接线员定位事故发生地的时间;在零售行业中,地址也是会员体系管理的核心要素。
地址信息处理涵盖了多种的NLP任务,从输入的形式来看可以分为:单句任务:输入是一条地址;句子对任务:输入是两条地址;多模态任务:输入是地址以及地图;针对这三种输入形态设计了三种预训练任务:注意力对抗预训练(ASA)、句子对预训练(MaSTS)、多模态预训练。并用多任务预训练(MOMETAS)技术将这三种预训练任务进行动态组合,训练得到适合于多类地址任务的预训练底座。

GeoGLUE

地理语义理解能力评测基准 GeoGLUE(GeoGraphic Language Understanding Evaluation)是由阿里巴巴达摩院自然语言处理组与高德联合发起提供的数据集,旨在推动地理相关文本处理技术和社区的发展。地理文本即描述地理实体、位置的自然文本,具有表达方式丰富、蕴含空间推理、知识强依赖等特点。此外,地理文本与现实地理世界的关联性也带来了诸多挑战。地理文本信息的自动化处理是许多地理语义应用的核心技术,本榜单提炼了其中多个典型场景:地图搜索、电商物流、政府登记、金融交通,并设计了六个核心任务:门址地址要素解析、地理实体对齐、Query-POI库召回、Query-POI相关性排序、地址Query成分分析、WhereWhat切分。为了避免地理信息安全问题,涉及POI地址库部分我们基于全开源GIS系统OpenStreetMap标注,并人工编写了数十万Query。

环境安装

IDE:VSCode
CPU:9代i7
GPU:GTX1650
MEM:32G
OS:windows11

感觉GPU拿不出手,用CPU跑一下试试。

ModelScope相关安装

conda虚拟环境

# CPU版本
conda create -n py37testmaas python=3.7

openssl-1.1.1w | 5.5 MB | ######################################################################################## | 100%
setuptools-65.6.3 | 1.1 MB | ######################################################################################## | 100%
python-3.7.16 | 17.2 MB | ######################################################################################## | 100%
wincertstore-0.2 | 15 KB | ######################################################################################## | 100%
sqlite-3.41.2 | 894 KB | ######################################################################################## | 100%
ca-certificates-2023 | 127 KB | ######################################################################################## | 100%
pip-22.3.1 | 2.7 MB | ######################################################################################## | 100%
wheel-0.38.4 | 82 KB | ######################################################################################## | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

深度学习基础库

  • tensorflow默认版本报错
pip install cryptography==3.4.8  tensorflow==1.15.5  torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0

ERROR: Could not find a version that satisfies the requirement tensorflow1.15.5 (from versions: 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.6.0rc0, 2.6.0rc1, 2.6.0rc2, 2.6.0, 2.6.1, 2.6.2, 2.6.3, 2.6.4, 2.6.5, 2.7.0rc0, 2.7.0rc1, 2.7.0, 2.7.1, 2.7.2, 2.7.3, 2.7.4, 2.8.0rc0, 2.8.0rc1, 2.8.0, 2.8.1, 2.8.2, 2.8.3, 2.8.4, 2.9.0rc0, 2.9.0rc1, 2.9.0rc2, 2.9.0, 2.9.1, 2.9.2, 2.9.3, 2.10.0rc0, 2.10.0rc1, 2.10.0rc2, 2.10.0rc3, 2.10.0, 2.10.1, 2.11.0rc0, 2.11.0rc1, 2.11.0rc2, 2.11.0, 2.11.1, 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.12.1, 2.13.0rc0, 2.13.0rc1, 2.13.0rc2, 2.13.0, 2.13.1, 2.14.0rc0, 2.14.0rc1, 2.14.0, 2.14.1, 2.15.0rc0, 2.15.0rc1, 2.15.0)
ERROR: No matching distribution found for tensorflow
1.15.5

  • 换成tensorflow==2.5.0
pip install tensorflow==2.5.0  torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0

漫长的等待过程…

Defaulting to user installation because normal site-packages is not writeable
Collecting tensorflow==2.5.0
Downloading tensorflow-2.5.0-cp39-cp39-win_amd64.whl (422.6 MB)
|████████████████████████████████| 422.6 MB 136 kB/s

  • torch也出错

Collecting torch==1.11.0
Downloading torch-1.11.0-cp39-cp39-win_amd64.whl (157.9 MB)
|███████▌ | 37.5 MB 81 kB/s eta 0:24:31ERROR: Exception:
Traceback (most recent call last):

安装到一半报错了。
觉得安装时间太长,出去吃了个饭,回来就发现报错。分析是电脑设置了睡眠,导致安装连接超时,所以报错。取消电脑休眠设置,重新安装。

pip install torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0

成功。
Collecting torch1.11.0
Downloading torch-1.11.0-cp39-cp39-win_amd64.whl (157.9 MB)
|████████████████████████████████| 157.9 MB 12 kB/s
Collecting torchvision
0.12.0
Downloading torchvision-0.12.0-cp39-cp39-win_amd64.whl (1.0 MB)
|████████████████████████████████| 1.0 MB 656 kB/s
Collecting torchaudio==0.11.0
Downloading torchaudio-0.11.0-cp39-cp39-win_amd64.whl (372 kB)
|████████████████████████████████| 372 kB 595 kB/s

Requirement already satisfied: typing-extensions in c:\programdata\anaconda3\lib\site-packages (from torch1.11.0) (4.1.1)
Requirement already satisfied: requests in c:\programdata\anaconda3\lib\site-packages (from torchvision
0.12.0) (2.27.1)
Requirement already satisfied: numpy in c:\programdata\anaconda3\lib\site-packages (from torchvision0.12.0) (1.21.5)
Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\programdata\anaconda3\lib\site-packages (from torchvision
0.12.0) (9.0.1)
Requirement already satisfied: charset-normalizer~=2.0.0 in c:\programdata\anaconda3\lib\site-packages (from requests->torchvision0.12.0) (2.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\programdata\anaconda3\lib\site-packages (from requests->torchvision
0.12.0) (1.26.9)
Requirement already satisfied: idna<4,>=2.5 in c:\programdata\anaconda3\lib\site-packages (from requests->torchvision0.12.0) (3.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\programdata\anaconda3\lib\site-packages (from requests->torchvision
0.12.0) (2021.10.8)
Installing collected packages: torch, torchvision, torchaudio
WARNING: The scripts convert-caffe2-to-onnx.exe, convert-onnx-to-caffe2.exe and torchrun.exe are installed in ‘C:\Users\NieNVIDIA\AppData\Roaming\Python\Python39\Scripts’ which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed torch-1.11.0 torchaudio-0.11.0 torchvision-0.12.0

安装modelscope模型

pip install "modelscope[nlp]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

测试代码

代码


from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import pandas as pd
def get_pcdt(inputs):
    task = Tasks.token_classification
    model = 'damo/mgeo_geographic_elements_tagging_chinese_base'
    pipeline_ins = pipeline(task=task, model=model)
    res = pipeline_ins(input=inputs)
    pcdt = {'prov': '', 'city': '', 'district': '', 'town': ''}
    for r in res['output']:
        if r['type'] in pcdt:
            pcdt[r['type']] = r['span']
    return pcdt
df = pd.read_excel('test.xlsx')
total_pcdt = {'prov': [], 'city': [], 'district': [], 'town': []}
for line in df['address']:
    res = get_pcdt(line)
    for k in res:
        total_pcdt[k].append(res[k])
for k in total_pcdt:
    df[k] = total_pcdt[k]
df.to_excel('test_out.xlsx', index=False, header=True)

输入xlsx文件

在这里插入图片描述

运行这段测试代码

报错

Traceback (most recent call last):
File “d:\workspace_python\AI_EAS_Python\MGeoExample-main\MGeoExample-main\地址相似度判断\process.py”, line 18, in
df = pd.read_excel(“test.xlsx”)
File “C:\ProgramData\Anaconda3\lib\site-packages\pandas\util_decorators.py”, line 311, in wrapper
return func(*args, **kwargs)
File “C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel_base.py”, line 457, in read_excel
io = ExcelFile(io, storage_options=storage_options, engine=engine)
File “C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel_base.py”, line 1376, in init
ext = inspect_excel_format(
File “C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\excel_base.py”, line 1250, in inspect_excel_format
with get_handle(
File “C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\common.py”, line 798, in get_handle
handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: ‘test.xlsx’

将输入、输出的xlsx文件路径换成绝对路径

df = pd.read_excel("D:/workspace_python/AI_EAS_Python/MGeoExample-main/MGeoExample-main/地址相似度判断/test.xlsx")

df.to_excel("D:/workspace_python/AI_EAS_Python/MGeoExample-main/MGeoExample-main/地址相似度判断/test_out.xlsx", index=False, header=True)

重新运行,成功

2024-02-21 12:37:52,205 - modelscope - INFO - Use user-specified model
revision: v1.2.0 Downloading:
100%|█████████████████████████████████████████████████████████████████████████████|
1.57k/1.57k [00:00<00:00, 231kB/s] Downloading: 100%|█████████████████████████████████████████████████████████████████████████████|
2.21k/2.21k [00:00<00:00, 368kB/s] Downloading: 100%|██████████████████████████████████████████████████████████████████████████████|
390M/390M [00:41<00:00, 9.87MB/s] Downloading:
100%|████████████████████████████████████████████████████████████████████████████████████|
9.92k/9.92k [00:00<?, ?B/s] Downloading: 100%|██████████████████████████████████████████████████████████████████████████████|
107k/107k [00:00<00:00, 2.00MB/s]

输出xlsx

在这里插入图片描述

耗时

在CPU环境下,不包括环境安装,仅执行推理过程,耗时共4秒。如下:

开始时间:2024-02-21 12:37:46,509
完成时间:2024-02-21 12:41:21,134

如果有自定义样本可以下载数据集进行训练

运行上述实战不需要执行下列GeoGLUE模型操作。但是如有自定义样本可以下载数据集进行训练。

git clone https://www.modelscope.cn/datasets/damo/GeoGLUE.git
  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

North_D

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值