大规模优化问题,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 = params
    return (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 interp1d


def 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] = 0
    offs[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 xy



def 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 grid

def 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:
            continue
        links_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_cons3
        
        
        loss = (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_coords

def 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的idx
    links = {} 
    
    for r in link_gpd.itertuples(): 
        #按照固定点类别区分模式
        mode = (0 if (r.snode_type in [250]) else
                1 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 = e
        e += len(xy) 
        #当前link对应的pts点序从b到e-1
        links[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]).T
            l[:] = [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 gpd
    out_gpd(link_gpd,node_gpd,links,optimized_nodes,nodes)

if __name__ == '__main__': 
    main() 

优化后的结果见(蓝色)

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-22 00:00:02       168 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-22 00:00:02       185 阅读
  3. 在Django里面运行非项目文件

    2024-07-22 00:00:02       155 阅读
  4. Python语言-面向对象

    2024-07-22 00:00:02       169 阅读

热门阅读

  1. 嵌入式软件工作能力

    2024-07-22 00:00:02       31 阅读
  2. C#各种锁知识点

    2024-07-22 00:00:02       32 阅读
  3. Python之后端Django(四)

    2024-07-22 00:00:02       34 阅读
  4. n4.Nginx 核心模块

    2024-07-22 00:00:02       33 阅读
  5. android audio 相机按键音:(二)加载与修改

    2024-07-22 00:00:02       42 阅读
  6. 数字转换(树形DP)

    2024-07-22 00:00:02       37 阅读
  7. python 爬虫技术 第03节 基础复习 控制结构

    2024-07-22 00:00:02       32 阅读