大规模优化问题,Scipy?Ceres?PyTorch!

背景:

优化问题一般通过scipy.optimize或者Ceres Solver优化器求解。但在参数量较大的优化问题上,scipy提供的BFGSL-BFGS-BCGSLSQP等梯度优化算法其复杂度和存储需求指数级上升,无法满足计算效率;而Ceres需要额外的语言来支持,当前算法工程师一般以python为主,学习成本较大。且项目环境需要多语言混编,配置与部署难度较大。

本文使用PyTorch/TensorFlow等机器学习框架,通过自动求导高效计算梯度,避免手动计算梯度复杂性,利用CPU/CPU来加速求解优化问题。

PyTorch优化四部曲:

# 优化迭代次数,设为1000次
for _ in range(1000):# 1. 清零梯度optimizer.zero_grad()# 通过调用 zero_grad(),我们清零了所有被优化参数的梯度。这是因为在 PyTorch 中,# 每次进行反向传播时,梯度默认是累积的(即不会自动清零)。# 如果不清理梯度,可能会导致累积误差,从而影响下一步的梯度计算。# 2. 计算损失函数loss = objective_function(params)# 这里我们计算当前参数下的目标函数值(损失函数)。# 损失函数的值将作为后续反向传播计算梯度的基础。# 根据参数(params)的当前值,求出损失值(即预测和实际的误差)。# 3. 反向传播计算梯度loss.backward()# 调用 loss.backward() 会进行反向传播,计算损失函数对所有优化参数的梯度。# 这些梯度将存储在各个参数的 .grad 属性中,以便在优化步骤中使用。# 4. 更新参数optimizer.step()# 调用 optimizer.step() 会应用优化器来更新参数。# 优化器会利用刚才计算的梯度(存储在 .grad 属性中),按照指定优化算法(如 Adam)# 的规则更新参数。这样一来,参数会朝着能最小化损失函数的方向一步步调整。

开胃小菜:

在三维空间中,给定初始点(0,0,0),求解优化使其距离点(2,3,4)最近。

import torch# 定义目标函数,取任意数量优化变量
def objective_function(params):x, y, z = paramsreturn (x - 2)**2 + (y - 3)**2 + (z - 4)**2# 初始化优化变量
params = [torch.tensor([0.0], requires_grad=True),torch.tensor([0.0], requires_grad=True),torch.tensor([0.0], requires_grad=True)]# 优化器选择 Adam ,并传入列表参数
optimizer = torch.optim.Adam(params, lr=0.1)# 迭代优化
for _ in range(1000):optimizer.zero_grad()loss = objective_function(params)loss.backward()optimizer.step()# 输出优化结果
print(f"Optimized x: {params[0].item()}")
print(f"Optimized y: {params[1].item()}")
print(f"Optimized z: {params[2].item()}")

正餐:

假设场景:现有一张二维平面的游戏地图,并附带一份初始路网,请优化路网形状,使其平滑、美化、且与各个传送点保持挂接。

游戏地图如下,白色区域代表道路,黑色区域为地图外场景:

初始路网可视化见:

初始路网以link和node两张表表示:

node表

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "nodeid": "90cbaa72-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 586.0, 795.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbaa9a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 576.0, 788.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbaaf4-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 621.0, 780.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbaa18-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 592.0, 810.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba9f0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 557.0, 822.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbacf2-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 769.0, 687.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb22e-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1002.0 }, "geometry": { "type": "Point", "coordinates": [ 1070.0, 343.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbaf4a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 812.0, 472.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb2b0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1058.0, 325.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb27e-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1002.0 }, "geometry": { "type": "Point", "coordinates": [ 1097.0, 327.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb256-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1086.0, 336.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba626-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 372.0, 940.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba6a8-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 420.0, 901.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba5fe-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 390.0, 961.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba6da-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 396.0, 893.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba702-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 537.0, 847.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba5ae-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 370.0, 979.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba892-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1002.0 }, "geometry": { "type": "Point", "coordinates": [ 552.0, 917.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba93c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 563.0, 887.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba554-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 412.0, 1042.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba96e-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 576.0, 878.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba5d6-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 503.0, 978.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb5e4-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 1149.0, 391.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb67a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1200.0, 366.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb4e0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1161.0, 413.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb5b2-3e8e-11ef-a98f-a13c99648c9a", "node_type": 250.0 }, "geometry": { "type": "Point", "coordinates": [ 1413.0, 392.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb1fc-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1117.0, 380.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb1d4-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1110.0, 426.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbadf6-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 899.0, 567.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbae78-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 936.0, 550.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbae50-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 892.0, 555.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb1a2-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1101.0, 436.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbadce-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 872.0, 568.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbaa40-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 588.0, 799.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba72a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 546.0, 835.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb652-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1138.0, 370.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbad9c-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 902.0, 570.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbad74-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 911.0, 587.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbad4c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 886.0, 603.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba658-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 367.0, 935.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba680-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 357.0, 920.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba75c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 340.0, 807.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba324-3e8e-11ef-a98f-a13c99648c9a", "node_type": 250.0 }, "geometry": { "type": "Point", "coordinates": [ 270.0, 1191.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb620-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 1145.0, 385.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba996-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 581.0, 875.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb3dc-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1187.0, 556.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb04e-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 975.0, 767.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbad1a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 862.0, 607.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbaef0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 671.0, 527.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba57c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 343.0, 985.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cba8ec-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 572.0, 902.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb0f8-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 956.0, 708.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbb404-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1333.0, 551.0 ] } },
{ "type": "Feature", "properties": { "nodeid": "90cbac48-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 861.0, 841.0 ] } }
]
}

link表

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "linkid": "7cba8b52-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cbaa72-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbaa9a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 586.0, 795.0 ], [ 576.0, 788.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bfa830-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaa9a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba9f0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 576.0, 788.0 ], [ 557.0, 822.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "918092ca-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaaf4-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbaa9a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 621.0, 780.0 ], [ 605.0, 774.0 ], [ 578.0, 784.0 ], [ 576.0, 788.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bfa088-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaa18-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba9f0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 592.0, 810.0 ], [ 557.0, 822.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7e44c-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba9f0-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba72a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 557.0, 822.0 ], [ 546.0, 835.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7d84e-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaaf4-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbaa18-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 621.0, 780.0 ], [ 607.0, 805.0 ], [ 592.0, 810.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7b4a4-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbacf2-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbaaf4-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 769.0, 687.0 ], [ 729.0, 703.0 ], [ 621.0, 780.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cbb79a4-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1002.0, "enode_type": null, "snodeid": "90cbb22e-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb2b0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1070.0, 343.0 ], [ 1058.0, 325.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "91800daa-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaf4a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb2b0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 812.0, 472.0 ], [ 862.0, 441.0 ], [ 867.0, 444.0 ], [ 1058.0, 325.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "9180a2ce-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb2b0-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb256-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1058.0, 325.0 ], [ 1072.0, 316.0 ], [ 1086.0, 336.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb77728-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaf4a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbadce-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 812.0, 472.0 ], [ 872.0, 568.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "9180199e-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbaf4a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbaef0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 812.0, 472.0 ], [ 702.0, 540.0 ], [ 681.0, 545.0 ], [ 671.0, 527.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc38e5a-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1002.0, "enode_type": null, "snodeid": "90cbb27e-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb256-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1097.0, 327.0 ], [ 1086.0, 336.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90beb5e2-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb256-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb1fc-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1086.0, 336.0 ], [ 1117.0, 380.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cbcdf4c-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cba626-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba5fe-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 372.0, 940.0 ], [ 390.0, 961.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb801ca-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba6a8-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba5fe-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 420.0, 901.0 ], [ 404.0, 947.0 ], [ 390.0, 961.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bff07e-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba5fe-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba5ae-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 390.0, 961.0 ], [ 370.0, 979.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7f6ee-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba6da-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba6a8-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 396.0, 893.0 ], [ 420.0, 901.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7f176-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba702-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba6a8-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 537.0, 847.0 ], [ 420.0, 901.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb80f3a-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba5ae-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba57c-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 370.0, 979.0 ], [ 343.0, 985.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb812aa-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba5ae-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba554-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 370.0, 979.0 ], [ 391.0, 1000.0 ], [ 412.0, 1042.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cbdd14a-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1002.0, "enode_type": null, "snodeid": "90cba892-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba8ec-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 552.0, 917.0 ], [ 572.0, 902.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bfd47c-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba93c-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba8ec-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 563.0, 887.0 ], [ 572.0, 902.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "918066b0-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba554-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba8ec-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 412.0, 1042.0 ], [ 427.0, 1085.0 ], [ 593.0, 982.0 ], [ 585.0, 957.0 ], [ 591.0, 939.0 ], [ 579.0, 913.0 ], [ 572.0, 902.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc29266-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cba96e-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba93c-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 576.0, 878.0 ], [ 563.0, 887.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7f4d2-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba702-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba93c-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 537.0, 847.0 ], [ 563.0, 887.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb80d00-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba5d6-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba554-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 503.0, 978.0 ], [ 412.0, 1042.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cbebd44-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cbb5e4-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb4e0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1149.0, 391.0 ], [ 1161.0, 413.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb74f5a-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb67a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb4e0-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1200.0, 366.0 ], [ 1180.0, 402.0 ], [ 1161.0, 413.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90becfe6-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb4e0-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb1d4-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1161.0, 413.0 ], [ 1150.0, 419.0 ], [ 1110.0, 426.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb74456-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb67a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb652-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1200.0, 366.0 ], [ 1172.0, 353.0 ], [ 1138.0, 370.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "9180030a-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske_solid", "snode_type": 250.0, "enode_type": null, "snodeid": "90cbb5b2-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb67a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1413.0, 392.0 ], [ 1410.0, 408.0 ], [ 1371.0, 401.0 ], [ 1370.0, 397.0 ], [ 1314.0, 375.0 ], [ 1200.0, 366.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb751f8-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb1fc-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb1d4-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1117.0, 380.0 ], [ 1110.0, 426.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb75cd4-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb1d4-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb1a2-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1110.0, 426.0 ], [ 1101.0, 436.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc01324-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cbadf6-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbae50-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 899.0, 567.0 ], [ 892.0, 555.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb78cc2-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbae78-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbae50-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 936.0, 550.0 ], [ 901.0, 548.0 ], [ 892.0, 555.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bf31b6-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbae50-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbadce-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 892.0, 555.0 ], [ 872.0, 568.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7903c-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbae78-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbad74-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 936.0, 550.0 ], [ 928.0, 575.0 ], [ 911.0, 587.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7629c-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb1a2-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbae78-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1101.0, 436.0 ], [ 936.0, 550.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb797da-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbadce-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbad1a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 872.0, 568.0 ], [ 862.0, 607.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc1046e-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cbaa40-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbaa18-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 588.0, 799.0 ], [ 592.0, 810.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7e604-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba72a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba702-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 546.0, 835.0 ], [ 537.0, 847.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bec2e4-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb652-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb1fc-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1138.0, 370.0 ], [ 1117.0, 380.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc47f36-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cbad9c-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbad74-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 902.0, 570.0 ], [ 911.0, 587.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bf39b8-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbad74-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbad4c-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 911.0, 587.0 ], [ 886.0, 603.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb799ec-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbad4c-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbad1a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 886.0, 603.0 ], [ 862.0, 607.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb79e38-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbad4c-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb0f8-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 886.0, 603.0 ], [ 956.0, 708.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc572b0-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cba658-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba680-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 367.0, 935.0 ], [ 357.0, 920.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7fbc6-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba6da-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba680-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 396.0, 893.0 ], [ 357.0, 920.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "90bfe46c-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba680-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba57c-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 357.0, 920.0 ], [ 340.0, 932.0 ], [ 334.0, 967.0 ], [ 343.0, 985.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7e276-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cba75c-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba6da-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 340.0, 807.0 ], [ 396.0, 893.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "918079fc-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske_solid", "snode_type": 250.0, "enode_type": null, "snodeid": "90cba324-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba57c-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 270.0, 1191.0 ], [ 269.0, 1188.0 ], [ 278.0, 1153.0 ], [ 305.0, 1135.0 ], [ 343.0, 985.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cc693ca-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cbb620-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb652-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1145.0, 385.0 ], [ 1138.0, 370.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "917f7e30-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske_solid", "snode_type": 1001.0, "enode_type": null, "snodeid": "90cba996-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cba72a-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 581.0, 875.0 ], [ 569.0, 852.0 ], [ 546.0, 835.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7674c-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb1a2-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb3dc-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1101.0, 436.0 ], [ 1187.0, 556.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "918025ce-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb3dc-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb404-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1187.0, 556.0 ], [ 1290.0, 495.0 ], [ 1335.0, 549.0 ], [ 1333.0, 551.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "918089e2-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb3dc-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb0f8-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 1187.0, 556.0 ], [ 1092.0, 623.0 ], [ 1078.0, 652.0 ], [ 1043.0, 654.0 ], [ 956.0, 708.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "91803c76-3e8e-11ef-a98f-a13c99648c9a", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbb04e-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbb0f8-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 975.0, 767.0 ], [ 973.0, 761.0 ], [ 959.0, 754.0 ], [ 944.0, 722.0 ], [ 956.0, 708.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7a2b6-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbad1a-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbacf2-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 862.0, 607.0 ], [ 785.0, 658.0 ], [ 769.0, 687.0 ] ] } },
{ "type": "Feature", "properties": { "linkid": "7cb7bada-3e8e-11ef-bae9-7993d366a9e1", "kind": "ske", "snode_type": null, "enode_type": null, "snodeid": "90cbacf2-3e8e-11ef-a98f-a13c99648c9a", "enodeid": "90cbac48-3e8e-11ef-a98f-a13c99648c9a" }, "geometry": { "type": "LineString", "coordinates": [ [ 769.0, 687.0 ], [ 801.0, 719.0 ], [ 861.0, 841.0 ] ] } }
]
}
#自建link and node
import numpy as np
import cv2
from skimage.morphology import skeletonize
import torch
import torch.optim as optim
import torch.nn.functional as F
import geopandas as gpd 
import shapely.geometry as G 
from scipy.interpolate import interp1ddef read_binary_image(path):"""读取二值图像"""img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)_, binary_img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)return binary_img #向量V单位化
def normed(v):return v/np.maximum(np.linalg.norm(v, axis=-1, keepdims=True), 1e-9)#插值
def resample(xy, interval = 8.0, tol = 0.1):xy = np.asarray(G.LineString(xy).simplify(tol, True).coords, np.double)norms = np.linalg.norm(xy[1:] - xy[:-1], axis=1)offs = np.empty(len(xy), xy.dtype)offs[0] = 0offs[1:] = np.add.accumulate(norms)n = max(int(np.ceil(offs[-1]/interval)), 1)kwargs = dict(copy=False, fill_value='extrapolate', assume_sorted=True)xy = interp1d(offs, xy, axis=0, **kwargs)(np.linspace(0, offs[-1], n + 1, dtype=xy.dtype))return xydef generate_distance_transform(road_mask):"""生成距离变换图(反映每个像素到最近非路面像素的距离);为了保证修型后的线段不压盖边界,设计损失函数,当其里边界越近,损失越大;压盖边界时,损失极大"""grid = cv2.distanceTransform(255 - road_mask, cv2.DIST_L2, 5)# cv2.imwrite("grid.jpg",grid)grid2 = cv2.distanceTransform(road_mask, cv2.DIST_L2, 5)# cv2.imwrite("grid2.jpg",grid2)grid = np.where(grid2 > 0, -grid2, grid)grid = np.round(np.sqrt(np.tanh(grid/3) + 1)*45000).astype(np.int32)# cv2.imwrite("tmp2.jpg",grid.astype(np.uint8))# print(grid)# print(np.max(grid),np.min(grid))return griddef smooth_skeleton(nodes, links_dict, nodes_dict, dist_transform, fi, follow,lambda1=1.0, lambda2=1.0, lambda3=1.0, lambda4=1.0, lambda5=1.0, lambda6=1.0, max_iterations=50):"""使用 PyTorch 优化骨架节点,使骨架尽量平滑且不与非路面相交"""device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')nodes_tensor = torch.tensor(nodes, dtype=torch.float32, requires_grad=True, device=device)dist_transform_tensor = torch.tensor(dist_transform, dtype=torch.float32, device=device).unsqueeze(0).unsqueeze(0)print(nodes_tensor.shape)optimizer = optim.Adam([nodes_tensor], lr=0.03)H, W = dist_transform.shape#同一link相邻两个点links_cons1 = [] for d in links_dict.values(): (b, e) = d['rng']links_cons1.extend([(fi(i), fi(i + 1))for i in range(b, e - 1)])links_cons1 = np.array(links_cons1) initial_diffs = nodes[links_cons1[:, 0]] - nodes[links_cons1[:, 1]]initial_distances = np.linalg.norm(initial_diffs, axis=1)initial_distances = np.maximum(initial_distances, 1)#同一link相邻三个点links_cons2 = [] for d in links_dict.values(): (b, e) = d['rng']if e-b <= 2:continuelinks_cons2.extend([(fi(i), fi(i + 1), fi(i + 2))for i in range(b, e - 2)])links_cons2 = np.array(links_cons2) #固定点 nods_cons1 = [] for d in links_dict.values(): mode = d["mode"] (b, e) = d['rng'] if mode < 2 : nods_cons1.append([fi(b)]) nods_cons1 = np.array(nods_cons1) initial_solid = nodes[nods_cons1[:, 0]]  #固定点射出方向vecs_cons1 = [] for d in links_dict.values(): mode = d["mode"] (b, e) = d['rng'] if mode == 1: vecs_cons1.append([fi(b), fi(b+1)]) vecs_cons1 = np.array(vecs_cons1) initial_vecs1 = nodes[vecs_cons1[:, 0]] - nodes[vecs_cons1[:, 1]] #多link挂接时,角度均匀vecs_cons2 = [] #多link挂接时,近似180度的两个射出角保持直线vecs_cons3 = [] for d in nodes_dict.values(): node_couple_list = d["vecs"] if len(node_couple_list) >= 2: for idx in range(len(node_couple_list)):(s,e) = node_couple_list[idx] if len(node_couple_list) == idx+1:(ss,ee) = node_couple_list[0] else:(ss,ee) = node_couple_list[idx+1] vecs_cons2.append([fi(s), fi(e), fi(ss), fi(ee)]) if abs(np.arctan2(np.cross(nodes[fi(s)] - nodes[fi(e)] , nodes[fi(ss)] - nodes[fi(ee)]), (nodes[fi(s)] - nodes[fi(e)] )@(nodes[fi(ss)] - nodes[fi(ee)])))>2.8:vecs_cons3.append([fi(e), fi(s), fi(ee)])vecs_cons2 = np.array(vecs_cons2) vecs_cons3 = np.array(vecs_cons3) links_cons2 = np.concatenate([links_cons2,vecs_cons3],axis=0)for _ in range(max_iterations):optimizer.zero_grad()# 平滑项# smoothness_term = torch.sum(torch.norm(nodes_tensor[links[:, 0]] - nodes_tensor[links[:, 1]], dim=1))# 保持与初始距离的比例,且最小长度为1个像素length__start_points = nodes_tensor[links_cons1[:, 0]]length__end_points = nodes_tensor[links_cons1[:, 1]]length__distances = torch.norm(length__start_points - length__end_points, dim=1)smoothness_term = torch.sum((length__distances - torch.tensor(initial_distances, device=device))**2)  # 平滑项,角度均匀first_points = nodes_tensor[links_cons2[:, 0]]second_points = nodes_tensor[links_cons2[:, 1]]third_points = nodes_tensor[links_cons2[:, 2]]smoothness_term2 = (1 - F.cosine_similarity((second_points-first_points).unsqueeze(0), (third_points-second_points).unsqueeze(0), dim=1)).sum()# print("debug",smoothness_term2)#固定点solid_points = nodes_tensor[nods_cons1[:, 0]]solid_term = torch.sum(torch.norm(solid_points - torch.tensor(initial_solid, device=device), dim=1))#固定射出角度solid_first_points = nodes_tensor[vecs_cons1[:, 0]]solid_second_points = nodes_tensor[vecs_cons1[:, 1]]solid_vecs_term = (1 - F.cosine_similarity((solid_first_points-solid_second_points).unsqueeze(0), torch.tensor(initial_vecs1, device=device).unsqueeze(0), dim=1)).sum()# 距离项# 将节点坐标转换为插值中的标准化坐标并取样grid = nodes_tensor.unsqueeze(0).unsqueeze(0)  # [1, 1, N, 2]grid = torch.cat((grid[:, :, :, 0:1] * 2 / (W - 1) - 1, grid[:, :, :, 1:2] * 2 / (H - 1) - 1), dim=3)sampled_dist = F.grid_sample(dist_transform_tensor, grid, mode='bilinear', padding_mode='border', align_corners=True)distance_term = sampled_dist.view(-1).sum() #多度点均匀挂接 one_vec = nodes_tensor[vecs_cons2[:, 0]] - nodes_tensor[vecs_cons2[:, 1]] the_other_vec = nodes_tensor[vecs_cons2[:, 2]] - nodes_tensor[vecs_cons2[:, 3]] # 计算点积 dot_products = torch.sum(one_vec * the_other_vec, dim=1)cross_products = one_vec[:, 0] * the_other_vec[:, 1] - one_vec[:, 1] * the_other_vec[:, 0]# 计算夹角 nodes_angles = torch.atan2(cross_products, dot_products)# 将结果张量的形状调整为 (100, 1)nodes_angles = nodes_angles.view(-1, 1) nodes_vecs_term = torch.sum(torch.max(- nodes_angles + torch.full_like(nodes_angles, torch.tensor(torch.pi / 2)),torch.full_like(nodes_angles, torch.tensor(0))))# #近似180度挂接的保持直线# vecs_cons3loss = (lambda1 * smoothness_term +  #保持原有长度损失lambda2 * distance_term +  #grid损失lambda3 * smoothness_term2 +  #角度平滑损失lambda4 * solid_term +  #固定点不动约束lambda5 * solid_vecs_term +#固定点射出角约束lambda6 * nodes_vecs_term #多度挂接角度均匀 )loss.backward() optimizer.step()print(loss)optimized_coords = nodes_tensor.cpu().detach().numpy()for (i, j) in follow.items():optimized_coords[i] = optimized_coords[j]return optimized_coordsdef visualize_skeleton(binary_img, nodes, links, optimized_nodes,fi):"""可视化优化前后的骨架"""img = cv2.cvtColor(binary_img, cv2.COLOR_GRAY2BGR) view_links = []for d in links.values(): (b, e) = d['rng']view_links.extend([(fi(i), fi(i + 1)) for i in range(b, e - 1)]) links = np.array(view_links) # 绘制原始骨架(红色)for x, y in nodes: cv2.circle(img, (x, y), 2, (0, 0, 255), -1)for i, j in links: cv2.line(img, tuple(nodes[i]), tuple(nodes[j]), (0, 0, 255), 1)# # 绘制优化后的骨架(蓝色)# for x, y in optimized_nodes:#     cv2.circle(img, (int(x), int(y)), 2, (255, 0, 0), -1)# for i, j in links:#     cv2.line(img, tuple(optimized_nodes[i].astype(int)), tuple(optimized_nodes[j].astype(int)), (255, 0, 0), 1)cv2.imwrite("res0.jpg",img)def out_gpd(links_gpdf,nodes_gpdf,links,pts,nodes):links_gpdf = links_gpdf.set_geometry([G.LineString(pts[b:e]).simplify(0.5, True)for (b, e) in [links[k]['rng'] for k in links_gpdf['linkid']]])nodes_gpdf = nodes_gpdf.set_geometry([G.Point(pts[nodes[k]['vecs'][0][0]])for k in nodes_gpdf['nodeid']])gpd.GeoDataFrame(links_gpdf).to_file('./link2_out.geojson')gpd.GeoDataFrame(nodes_gpdf).to_file('./node2_out.geojson')return def main():"""主函数"""# 读取二值图像binary_img = read_binary_image('./1.jpg')link_path = "./link.geojson"node_path = "./node.geojson"link_gpd = gpd.read_file(link_path) node_gpd = gpd.read_file(node_path) # 提取节点和连边# 初始化节点字典,保存节点及其连出的向量信息nodes = {}for r in node_gpd.itertuples():fixed = (not np.isnan(r.node_type))nodes[r.nodeid] = {'vecs': []}# 初始化点集列表和线段字典pts = []e = 0 #记录pts的idxlinks = {} for r in link_gpd.itertuples(): #按照固定点类别区分模式mode = (0 if (r.snode_type in [250]) else1 if (r.snode_type in [1001, 1002, 1003]) else 2)xy = np.array(r.geometry.coords, np.double)if mode == 1:if len(xy) == 2:#如果固定点挂接的link是两个形点,那么添加一个靠近重点的四等分点,组成三个形点的link。xy = np.array([xy[0], (xy[0] + xy[1]*3)/4, xy[1]])#在插值的时候保留第一个点xy = np.concatenate([xy[:1], resample(xy[1:])])else:xy = resample(xy)pts.append(xy)b = ee += len(xy) #当前link对应的pts点序从b到e-1links[r.linkid] = {'rng': (b, e), 'mode': mode}nodes[r.snodeid]['vecs'].append((b + 0, b + 1))nodes[r.enodeid]['vecs'].append((e - 1, e - 2))#三维展开成二维,即将所有的line坐标平铺,取消line维度  pts = np.concatenate(pts)# 用于记录同一个点的信息,对于每个节点,如果它连出多条边,将这些边的起点合并成一个。follow = {} for n in nodes.values():l = n['vecs']if len(l) > 2:#计算node节点连的所有边的角度,并按照顺/逆时针调整顺序v = np.array([pts[i2] - pts[i1] for (i1, i2) in l]).Tl[:] = [l[i] for i in np.argsort(np.arctan2(v[1], v[0]))]#follow 字典,代表同一个点if len(l) > 1:for (i, _) in l[1:]:follow[i] = l[0][0]def fi(i): return follow.get(i, i)print(pts.shape)print(links["7cba8b52-3e8e-11ef-bae9-7993d366a9e1"])# 生成距离变换图dist_transform = generate_distance_transform(binary_img)# lambda1  #保持原有长度损失
# lambda2  #grid损失
# lambda3  #角度平滑损失
# lambda4 #固定点不动约束
# lambda5 #固定点射出角约束
# lambda6  #多度挂接角度均匀 # 优化整个骨架optimized_nodes = smooth_skeleton(pts, links, nodes, dist_transform, fi, follow, lambda1=1, lambda2=0.01, lambda3 = 2000000, lambda4 = 10000000,lambda5 = 10000000,lambda6=1400000, max_iterations=5000)# 可视化优化前后的骨架 visualize_skeleton(binary_img, pts.astype(np.int32), links, optimized_nodes,fi)# out gpdout_gpd(link_gpd,node_gpd,links,optimized_nodes,nodes)if __name__ == '__main__': main() 

优化后的结果见(蓝色)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/47153.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

VTK----3D picking的原理、类型及实现

目录 3D picking概述 3D射线投射原理 VTK picking框架 vtkPicker(选Actor) vtkPointPicker(选点) vtkCellPicker(选单元) vtkAreaPicker(框选) 3D picking概述 3D picking 是一种在三维场景中确定用户点击或指向的对象的技术。这在3D应用程序和游戏中非常常见,…

React Native 自定义 Hook 获取组件位置和大小

在 React Native 中自定义 Hook useLayout 获取 View、Pressable 等组件的位置和大小的信息 import {useState, useCallback} from react import {LayoutChangeEvent, LayoutRectangle} from react-nativeexport function useLayout() {const [layout, setLayout] useState&l…

JavaScript Math 函数举例

https://andi.cn/page/621577.html

Mongodb数组字段索引之多键索引

学习mongodb&#xff0c;体会mongodb的每一个使用细节&#xff0c;欢迎阅读威赞的文章。这是威赞发布的第92篇mongodb技术文章&#xff0c;欢迎浏览本专栏威赞发布的其他文章。如果您认为我的文章对您有帮助或者解决您的问题&#xff0c;欢迎在文章下面点个赞&#xff0c;或者关…

【中项第三版】系统集成项目管理工程师 | 第 5 章 软件工程② | 5.4 - 5.8

前言 第 5 章对应的内容选择题和案例分析都会进行考查&#xff0c;这一章节属于技术的内容&#xff0c;学习要以教材为准。 目录 5.4 软件实现 5.4.1 软件配置管理 5.4.2 软件编码 5.4.3 软件测试 5.5 部署交付 5.5.1 软件部署 5.5.2 软件交付 5.5.3 持续交付 5.5.4…

Java语言程序设计——篇五

数组 概述数组定义实例展示实战演练 二维数组定义数组元素的使用数组初始化器实战演练&#xff1a;矩阵计算 &#x1f4ab;不规则二维数组实战演练&#xff1a;杨辉三角形 概述 ⚡️数组是相同数据类型的元素集合。各元素是有先后顺序的&#xff0c;它们在内存中按照这个先后顺…

论文分享|AAAI2024‘北航|软标签监督实现通用密集检索——图文检索中的跨模态和单模态软标签对齐

论文题目&#xff1a;Cross-Modal and Uni-Modal Soft-Label Alignment for Image-Text Retrieval 来源&#xff1a;AAAI2024/实验室师兄/北航 方向&#xff1a;跨模态检索 开源地址&#xff1a;https://github.com/lerogo/aaai24_itr_cusa 摘要 近年来&#xff0c;目前的…

保障低压设备安全!中国星坤连接器精密工艺解析!

在现代电子设备中&#xff0c;连接器扮演着至关重要的角色&#xff0c;它们是电子系统之间沟通的桥梁。随着技术的发展&#xff0c;对连接器的需求也在不断提升&#xff0c;特别是在低电压应用领域。中国星坤最新推出的低压连接器&#xff0c;以其精密性和安全性&#xff0c;为…

Kafka Producer发送消息流程之分区器和数据收集器

文章目录 1. Partitioner分区器2. 自定义分区器3. RecordAccumulator数据收集器 1. Partitioner分区器 clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java&#xff0c;中doSend方法&#xff0c;记录了生产者将消息发送的流程&#xff0c;其中有一步…

书生浦语-大模型平台学习-环境搭建01

任务&#xff1a;完成SSH连接与端口映射并运行hello_world.py 详细步骤详见&#xff1a;https://github.com/InternLM/Tutorial/blob/camp3/docs/L0/Linux/readme.md 1、InternStudio介绍 InternStudio 是大模型时代下的云端算力平台。基于 InternLM 组织下的诸多算法库支持…

CentOS快速安装Docker(腾讯镜像源)

这里是引用"> 1、卸载旧版本的 Docker yum list installed | grep docker yum -y remove docker-ce-cli.x86_64 yum -y remove docker-ce.x86_64 yum -y remove containerd.io2、安装相关依赖 yum install -y yum-utils device-mapper-persistent-data lvm23、添加 …

嵌入式人工智能(9-基于树莓派4B的PWM-LED呼吸灯)

1、PWM简介 (1)、什么是PWM 脉冲宽度调制(PWM)&#xff0c;是英文“Pulse Width Modulation”的缩写&#xff0c;简称脉宽调制&#xff0c;是在具有惯性的系统中利用微处理器的数字输出来对模拟电路进行控制的一种非常有效的技术&#xff0c;广泛应用在从测量、通信到功率控制…

学习大数据DAY17 PLSQL基础语法6和Git的基本操作

目录 包 存储过程调试功能 作业 阶段复习作业 Git课程目录 什么是版本控制 没有版本控制的缺点 常见的版本工具 版本控制分类 1. 本地版本控制 2. 集中版本控制 3. 分布式版本控制 Git与SVN主要区别 Git软件安装及配置 Windows系统安装Git 安装Tortoise Git(乌龟…

降Compose十八掌之『震惊百里』| Animations

公众号「稀有猿诉」 原文链接 降Compose十八掌之『震惊百里』| Animations 动画对于UI来说无疑是最重要的核心功能&#xff0c;它能够让UI变得生动有吸引力。适当的使用动画可以提升UI的流畅性&#xff0c;让UI体验更为顺滑。在Jetpack Compose中有丰富的函数可以用来实…

六西格玛设计:以客户为中心,驱动企业持续创新

在当今竞争激烈的市场环境中&#xff0c;企业要想脱颖而出&#xff0c;就必须在产品质量、服务效率和客户满意度上不断追求卓越。六西格玛设计&#xff08;Six Sigma Design&#xff09;作为一种高度规范化的管理方法&#xff0c;正逐步成为众多企业实现这一目标的重要工具。张…

NSSCTF中24网安培训day2中web题目【下】

[NISACTF 2022]easyssrf 这道题目考察的是php伪协议的知识点 首先利用file协议进行flag查找 file:///flag.php 接着我们用file协议继续查找fl4g file:///fl4g 接着我们访问此文件&#xff0c;得到php代码如下 这里存在着stristr的函数&#x…

Linux中的环境变量

一、基本概念 环境变量(environment variables)一般是指在操作系统中用来指定操作系统运行环境的一些参数。 如&#xff1a;我们在编写C/C代码的时候&#xff0c;在链接的时候&#xff0c;从来不知道我们的所链接的动态静态库在哪里&#xff0c;但是照样可以链接成功&#xff…

Cesium能做啥,加载哪些数据源,开源免费用商用吗?这里告诉你。

很多小伙伴对Cesium是什么&#xff0c;一知半解&#xff0c;本文是基础知识的扫盲&#xff0c;为大家分享cesium是什么、能做什么、默认数据是什么&#xff0c;为什么首先要进行数据加载&#xff0c;要加载哪些数据&#xff0c;希望通过这些带你入个门&#xff0c;欢迎点赞评论…

vue仿甘特图开发工程施工进度表

前言 本文是根据项目实际开发中一个需求开发的demo&#xff0c;仅用了elementUI&#xff0c;可当作独立组件使用&#xff0c;C V即用。 当然没考虑其他的扩展性和一些数据的校验&#xff0c;主要是提供一个处理思路&#xff0c;有需要的小伙伴可以直接复制&#xff1b;本demo的…

高职院校人工智能人才培养成果导向系统构建、实施要点与评量方法

一、引言 近年来&#xff0c;人工智能技术在全球范围内迅速发展&#xff0c;对各行各业产生了深远的影响。高职院校作为培养高技能人才的重要基地&#xff0c;肩负着培养人工智能领域专业人才的重任。为了适应社会对人工智能人才的需求&#xff0c;高职院校需要构建一套科学、…