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

打印 上一主题 下一主题

主题 523|帖子 523|积分 1569

背景:
优化题目一般通过scipy.optimize或者Ceres Solver优化器求解。但在参数量较大的优化题目上,scipy提供的BFGS、L-BFGS-B、CG、SLSQP等梯度优化算法其复杂度和存储需求指数级上升,无法满足计算效率;而Ceres需要额外的语言来支持,当前算法工程师一般以python为主,学习本钱较大。且项目环境需要多语言混编,配置与摆设难度较大。
本文使用PyTorch/TensorFlow等呆板学习框架,通过主动求导高效计算梯度,避免手动计算梯度复杂性,使用CPU/CPU来加速求解优化题目。

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

开胃小菜:
在三维空间中,给定初始点(0,0,0),求解优化使其距离点(2,3,4)近来。
  1. import torch
  2. # 定义目标函数,取任意数量优化变量
  3. def objective_function(params):
  4.     x, y, z = params
  5.     return (x - 2)**2 + (y - 3)**2 + (z - 4)**2
  6. # 初始化优化变量
  7. params = [torch.tensor([0.0], requires_grad=True),
  8.           torch.tensor([0.0], requires_grad=True),
  9.           torch.tensor([0.0], requires_grad=True)]
  10. # 优化器选择 Adam ,并传入列表参数
  11. optimizer = torch.optim.Adam(params, lr=0.1)
  12. # 迭代优化
  13. for _ in range(1000):
  14.     optimizer.zero_grad()
  15.     loss = objective_function(params)
  16.     loss.backward()
  17.     optimizer.step()
  18. # 输出优化结果
  19. print(f"Optimized x: {params[0].item()}")
  20. print(f"Optimized y: {params[1].item()}")
  21. print(f"Optimized z: {params[2].item()}")
复制代码

正餐:
假设场景:现有一张二维平面的游戏地图,并附带一份初始路网,请优化路网形状,使其平滑、美化、且与各个传送点保持挂接。
游戏地图如下,白色区域代表道路,黑色区域为地图外场景:

初始路网可视化见:

初始路网以link和node两张表表示:
node表
  1. {
  2. "type": "FeatureCollection",
  3. "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  4. "features": [
  5. { "type": "Feature", "properties": { "nodeid": "90cbaa72-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 586.0, 795.0 ] } },
  6. { "type": "Feature", "properties": { "nodeid": "90cbaa9a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 576.0, 788.0 ] } },
  7. { "type": "Feature", "properties": { "nodeid": "90cbaaf4-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 621.0, 780.0 ] } },
  8. { "type": "Feature", "properties": { "nodeid": "90cbaa18-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 592.0, 810.0 ] } },
  9. { "type": "Feature", "properties": { "nodeid": "90cba9f0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 557.0, 822.0 ] } },
  10. { "type": "Feature", "properties": { "nodeid": "90cbacf2-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 769.0, 687.0 ] } },
  11. { "type": "Feature", "properties": { "nodeid": "90cbb22e-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1002.0 }, "geometry": { "type": "Point", "coordinates": [ 1070.0, 343.0 ] } },
  12. { "type": "Feature", "properties": { "nodeid": "90cbaf4a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 812.0, 472.0 ] } },
  13. { "type": "Feature", "properties": { "nodeid": "90cbb2b0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1058.0, 325.0 ] } },
  14. { "type": "Feature", "properties": { "nodeid": "90cbb27e-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1002.0 }, "geometry": { "type": "Point", "coordinates": [ 1097.0, 327.0 ] } },
  15. { "type": "Feature", "properties": { "nodeid": "90cbb256-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1086.0, 336.0 ] } },
  16. { "type": "Feature", "properties": { "nodeid": "90cba626-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 372.0, 940.0 ] } },
  17. { "type": "Feature", "properties": { "nodeid": "90cba6a8-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 420.0, 901.0 ] } },
  18. { "type": "Feature", "properties": { "nodeid": "90cba5fe-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 390.0, 961.0 ] } },
  19. { "type": "Feature", "properties": { "nodeid": "90cba6da-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 396.0, 893.0 ] } },
  20. { "type": "Feature", "properties": { "nodeid": "90cba702-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 537.0, 847.0 ] } },
  21. { "type": "Feature", "properties": { "nodeid": "90cba5ae-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 370.0, 979.0 ] } },
  22. { "type": "Feature", "properties": { "nodeid": "90cba892-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1002.0 }, "geometry": { "type": "Point", "coordinates": [ 552.0, 917.0 ] } },
  23. { "type": "Feature", "properties": { "nodeid": "90cba93c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 563.0, 887.0 ] } },
  24. { "type": "Feature", "properties": { "nodeid": "90cba554-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 412.0, 1042.0 ] } },
  25. { "type": "Feature", "properties": { "nodeid": "90cba96e-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 576.0, 878.0 ] } },
  26. { "type": "Feature", "properties": { "nodeid": "90cba5d6-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 503.0, 978.0 ] } },
  27. { "type": "Feature", "properties": { "nodeid": "90cbb5e4-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 1149.0, 391.0 ] } },
  28. { "type": "Feature", "properties": { "nodeid": "90cbb67a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1200.0, 366.0 ] } },
  29. { "type": "Feature", "properties": { "nodeid": "90cbb4e0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1161.0, 413.0 ] } },
  30. { "type": "Feature", "properties": { "nodeid": "90cbb5b2-3e8e-11ef-a98f-a13c99648c9a", "node_type": 250.0 }, "geometry": { "type": "Point", "coordinates": [ 1413.0, 392.0 ] } },
  31. { "type": "Feature", "properties": { "nodeid": "90cbb1fc-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1117.0, 380.0 ] } },
  32. { "type": "Feature", "properties": { "nodeid": "90cbb1d4-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1110.0, 426.0 ] } },
  33. { "type": "Feature", "properties": { "nodeid": "90cbadf6-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 899.0, 567.0 ] } },
  34. { "type": "Feature", "properties": { "nodeid": "90cbae78-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 936.0, 550.0 ] } },
  35. { "type": "Feature", "properties": { "nodeid": "90cbae50-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 892.0, 555.0 ] } },
  36. { "type": "Feature", "properties": { "nodeid": "90cbb1a2-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1101.0, 436.0 ] } },
  37. { "type": "Feature", "properties": { "nodeid": "90cbadce-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 872.0, 568.0 ] } },
  38. { "type": "Feature", "properties": { "nodeid": "90cbaa40-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 588.0, 799.0 ] } },
  39. { "type": "Feature", "properties": { "nodeid": "90cba72a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 546.0, 835.0 ] } },
  40. { "type": "Feature", "properties": { "nodeid": "90cbb652-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1138.0, 370.0 ] } },
  41. { "type": "Feature", "properties": { "nodeid": "90cbad9c-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 902.0, 570.0 ] } },
  42. { "type": "Feature", "properties": { "nodeid": "90cbad74-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 911.0, 587.0 ] } },
  43. { "type": "Feature", "properties": { "nodeid": "90cbad4c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 886.0, 603.0 ] } },
  44. { "type": "Feature", "properties": { "nodeid": "90cba658-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 367.0, 935.0 ] } },
  45. { "type": "Feature", "properties": { "nodeid": "90cba680-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 357.0, 920.0 ] } },
  46. { "type": "Feature", "properties": { "nodeid": "90cba75c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 340.0, 807.0 ] } },
  47. { "type": "Feature", "properties": { "nodeid": "90cba324-3e8e-11ef-a98f-a13c99648c9a", "node_type": 250.0 }, "geometry": { "type": "Point", "coordinates": [ 270.0, 1191.0 ] } },
  48. { "type": "Feature", "properties": { "nodeid": "90cbb620-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 1145.0, 385.0 ] } },
  49. { "type": "Feature", "properties": { "nodeid": "90cba996-3e8e-11ef-a98f-a13c99648c9a", "node_type": 1001.0 }, "geometry": { "type": "Point", "coordinates": [ 581.0, 875.0 ] } },
  50. { "type": "Feature", "properties": { "nodeid": "90cbb3dc-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1187.0, 556.0 ] } },
  51. { "type": "Feature", "properties": { "nodeid": "90cbb04e-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 975.0, 767.0 ] } },
  52. { "type": "Feature", "properties": { "nodeid": "90cbad1a-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 862.0, 607.0 ] } },
  53. { "type": "Feature", "properties": { "nodeid": "90cbaef0-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 671.0, 527.0 ] } },
  54. { "type": "Feature", "properties": { "nodeid": "90cba57c-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 343.0, 985.0 ] } },
  55. { "type": "Feature", "properties": { "nodeid": "90cba8ec-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 572.0, 902.0 ] } },
  56. { "type": "Feature", "properties": { "nodeid": "90cbb0f8-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 956.0, 708.0 ] } },
  57. { "type": "Feature", "properties": { "nodeid": "90cbb404-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 1333.0, 551.0 ] } },
  58. { "type": "Feature", "properties": { "nodeid": "90cbac48-3e8e-11ef-a98f-a13c99648c9a", "node_type": null }, "geometry": { "type": "Point", "coordinates": [ 861.0, 841.0 ] } }
  59. ]
  60. }
复制代码
link表
  1. {
  2. "type": "FeatureCollection",
  3. "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  4. "features": [
  5. { "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 ] ] } },
  6. { "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 ] ] } },
  7. { "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 ] ] } },
  8. { "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 ] ] } },
  9. { "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 ] ] } },
  10. { "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 ] ] } },
  11. { "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 ] ] } },
  12. { "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 ] ] } },
  13. { "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 ] ] } },
  14. { "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 ] ] } },
  15. { "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 ] ] } },
  16. { "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 ] ] } },
  17. { "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 ] ] } },
  18. { "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 ] ] } },
  19. { "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 ] ] } },
  20. { "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 ] ] } },
  21. { "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 ] ] } },
  22. { "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 ] ] } },
  23. { "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 ] ] } },
  24. { "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 ] ] } },
  25. { "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 ] ] } },
  26. { "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 ] ] } },
  27. { "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 ] ] } },
  28. { "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 ] ] } },
  29. { "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 ] ] } },
  30. { "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 ] ] } },
  31. { "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 ] ] } },
  32. { "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 ] ] } },
  33. { "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 ] ] } },
  34. { "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 ] ] } },
  35. { "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 ] ] } },
  36. { "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 ] ] } },
  37. { "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 ] ] } },
  38. { "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 ] ] } },
  39. { "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 ] ] } },
  40. { "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 ] ] } },
  41. { "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 ] ] } },
  42. { "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 ] ] } },
  43. { "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 ] ] } },
  44. { "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 ] ] } },
  45. { "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 ] ] } },
  46. { "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 ] ] } },
  47. { "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 ] ] } },
  48. { "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 ] ] } },
  49. { "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 ] ] } },
  50. { "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 ] ] } },
  51. { "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 ] ] } },
  52. { "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 ] ] } },
  53. { "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 ] ] } },
  54. { "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 ] ] } },
  55. { "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 ] ] } },
  56. { "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 ] ] } },
  57. { "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 ] ] } },
  58. { "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 ] ] } },
  59. { "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 ] ] } },
  60. { "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 ] ] } },
  61. { "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 ] ] } },
  62. { "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 ] ] } },
  63. { "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 ] ] } },
  64. { "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 ] ] } }
  65. ]
  66. }
复制代码
  1. #自建link and node
  2. import numpy as np
  3. import cv2
  4. from skimage.morphology import skeletonize
  5. import torch
  6. import torch.optim as optim
  7. import torch.nn.functional as F
  8. import geopandas as gpd
  9. import shapely.geometry as G
  10. from scipy.interpolate import interp1d
  11. def read_binary_image(path):
  12.     """读取二值图像"""
  13.     img = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
  14.     _, binary_img = cv2.threshold(img, 200, 255, cv2.THRESH_BINARY)
  15.     return binary_img
  16. #向量V单位化
  17. def normed(v):
  18.     return v/np.maximum(np.linalg.norm(v, axis=-1, keepdims=True), 1e-9)
  19. #插值
  20. def resample(xy, interval = 8.0, tol = 0.1):
  21.     xy = np.asarray(G.LineString(xy).simplify(tol, True).coords, np.double)
  22.     norms = np.linalg.norm(xy[1:] - xy[:-1], axis=1)
  23.     offs = np.empty(len(xy), xy.dtype)
  24.     offs[0] = 0
  25.     offs[1:] = np.add.accumulate(norms)
  26.     n = max(int(np.ceil(offs[-1]/interval)), 1)
  27.     kwargs = dict(copy=False, fill_value='extrapolate', assume_sorted=True)
  28.     xy = interp1d(offs, xy, axis=0, **kwargs)(
  29.       np.linspace(0, offs[-1], n + 1, dtype=xy.dtype))
  30.     return xy
  31. def generate_distance_transform(road_mask):
  32.     """生成距离变换图(反映每个像素到最近非路面像素的距离);
  33.     为了保证修型后的线段不压盖边界,设计损失函数,当其里边界越近,损失越大;压盖边界时,损失极大"""
  34.     grid = cv2.distanceTransform(255 - road_mask, cv2.DIST_L2, 5)
  35.     # cv2.imwrite("grid.jpg",grid)
  36.     grid2 = cv2.distanceTransform(road_mask, cv2.DIST_L2, 5)
  37.     # cv2.imwrite("grid2.jpg",grid2)
  38.     grid = np.where(grid2 > 0, -grid2, grid)
  39.     grid = np.round(np.sqrt(np.tanh(grid/3) + 1)*45000).astype(np.int32)
  40.     # cv2.imwrite("tmp2.jpg",grid.astype(np.uint8))
  41.     # print(grid)
  42.     # print(np.max(grid),np.min(grid))
  43.     return grid
  44. 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):
  45.     """使用 PyTorch 优化骨架节点,使骨架尽量平滑且不与非路面相交"""
  46.     device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  47.     nodes_tensor = torch.tensor(nodes, dtype=torch.float32, requires_grad=True, device=device)
  48.     dist_transform_tensor = torch.tensor(dist_transform, dtype=torch.float32, device=device).unsqueeze(0).unsqueeze(0)
  49.     print(nodes_tensor.shape)
  50.     optimizer = optim.Adam([nodes_tensor], lr=0.03)
  51.     H, W = dist_transform.shape
  52.     #同一link相邻两个点
  53.     links_cons1 = []
  54.     for d in links_dict.values():
  55.         (b, e) = d['rng']
  56.         links_cons1.extend([(fi(i), fi(i + 1))
  57.                          for i in range(b, e - 1)])
  58.     links_cons1 = np.array(links_cons1)
  59.     initial_diffs = nodes[links_cons1[:, 0]] - nodes[links_cons1[:, 1]]
  60.     initial_distances = np.linalg.norm(initial_diffs, axis=1)
  61.     initial_distances = np.maximum(initial_distances, 1)
  62.     #同一link相邻三个点
  63.     links_cons2 = []
  64.     for d in links_dict.values():
  65.         (b, e) = d['rng']
  66.         if e-b <= 2:
  67.             continue
  68.         links_cons2.extend([(fi(i), fi(i + 1), fi(i + 2))
  69.                          for i in range(b, e - 2)])
  70.     links_cons2 = np.array(links_cons2)
  71.     #固定点
  72.     nods_cons1 = []
  73.     for d in links_dict.values():
  74.         mode = d["mode"]
  75.         (b, e) = d['rng']
  76.         if mode < 2 :
  77.             nods_cons1.append([fi(b)])
  78.     nods_cons1 = np.array(nods_cons1)
  79.     initial_solid = nodes[nods_cons1[:, 0]]  
  80.     #固定点射出方向
  81.     vecs_cons1 = []
  82.     for d in links_dict.values():
  83.         mode = d["mode"]
  84.         (b, e) = d['rng']
  85.         if mode == 1:
  86.             vecs_cons1.append([fi(b), fi(b+1)])
  87.     vecs_cons1 = np.array(vecs_cons1)
  88.     initial_vecs1 = nodes[vecs_cons1[:, 0]] - nodes[vecs_cons1[:, 1]]
  89.     #多link挂接时,角度均匀
  90.     vecs_cons2 = []
  91.     #多link挂接时,近似180度的两个射出角保持直线
  92.     vecs_cons3 = []
  93.     for d in nodes_dict.values():
  94.         node_couple_list = d["vecs"]
  95.         if len(node_couple_list) >= 2:
  96.             for idx in range(len(node_couple_list)):
  97.                 (s,e) = node_couple_list[idx]
  98.                 if len(node_couple_list) == idx+1:
  99.                     (ss,ee) = node_couple_list[0]
  100.                 else:
  101.                     (ss,ee) = node_couple_list[idx+1]
  102.                 vecs_cons2.append([fi(s), fi(e), fi(ss), fi(ee)])
  103.                 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:
  104.                     vecs_cons3.append([fi(e), fi(s), fi(ee)])
  105.     vecs_cons2 = np.array(vecs_cons2)
  106.     vecs_cons3 = np.array(vecs_cons3)
  107.     links_cons2 = np.concatenate([links_cons2,vecs_cons3],axis=0)
  108.    
  109.     for _ in range(max_iterations):
  110.         optimizer.zero_grad()
  111.         # 平滑项
  112.         # smoothness_term = torch.sum(torch.norm(nodes_tensor[links[:, 0]] - nodes_tensor[links[:, 1]], dim=1))
  113.         # 保持与初始距离的比例,且最小长度为1个像素
  114.         length__start_points = nodes_tensor[links_cons1[:, 0]]
  115.         length__end_points = nodes_tensor[links_cons1[:, 1]]
  116.         length__distances = torch.norm(length__start_points - length__end_points, dim=1)
  117.         smoothness_term = torch.sum((length__distances - torch.tensor(initial_distances, device=device))**2)  
  118.         
  119.         # 平滑项,角度均匀
  120.         first_points = nodes_tensor[links_cons2[:, 0]]
  121.         second_points = nodes_tensor[links_cons2[:, 1]]
  122.         third_points = nodes_tensor[links_cons2[:, 2]]
  123.         smoothness_term2 = (1 - F.cosine_similarity((second_points-first_points).unsqueeze(0), (third_points-second_points).unsqueeze(0), dim=1)).sum()
  124.         # print("debug",smoothness_term2)
  125.         #固定点
  126.         solid_points = nodes_tensor[nods_cons1[:, 0]]
  127.         solid_term = torch.sum(torch.norm(solid_points - torch.tensor(initial_solid, device=device), dim=1))
  128.         #固定射出角度
  129.         solid_first_points = nodes_tensor[vecs_cons1[:, 0]]
  130.         solid_second_points = nodes_tensor[vecs_cons1[:, 1]]
  131.         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()
  132.         # 距离项
  133.         # 将节点坐标转换为插值中的标准化坐标并取样
  134.         grid = nodes_tensor.unsqueeze(0).unsqueeze(0)  # [1, 1, N, 2]
  135.         grid = torch.cat((grid[:, :, :, 0:1] * 2 / (W - 1) - 1, grid[:, :, :, 1:2] * 2 / (H - 1) - 1), dim=3)
  136.         sampled_dist = F.grid_sample(dist_transform_tensor, grid, mode='bilinear', padding_mode='border', align_corners=True)
  137.         distance_term = sampled_dist.view(-1).sum()
  138.         #多度点均匀挂接
  139.         one_vec = nodes_tensor[vecs_cons2[:, 0]] - nodes_tensor[vecs_cons2[:, 1]]
  140.         the_other_vec = nodes_tensor[vecs_cons2[:, 2]] - nodes_tensor[vecs_cons2[:, 3]]
  141.         # 计算点积
  142.         dot_products = torch.sum(one_vec * the_other_vec, dim=1)
  143.         cross_products = one_vec[:, 0] * the_other_vec[:, 1] - one_vec[:, 1] * the_other_vec[:, 0]
  144.         # 计算夹角
  145.         nodes_angles = torch.atan2(cross_products, dot_products)
  146.         # 将结果张量的形状调整为 (100, 1)
  147.         nodes_angles = nodes_angles.view(-1, 1)
  148.         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))))
  149.         # #近似180度挂接的保持直线
  150.         # vecs_cons3
  151.         
  152.         
  153.         loss = (lambda1 * smoothness_term +  #保持原有长度损失
  154.                lambda2 * distance_term +  #grid损失
  155.                lambda3 * smoothness_term2 +  #角度平滑损失
  156.                lambda4 * solid_term +  #固定点不动约束
  157.                lambda5 * solid_vecs_term +#固定点射出角约束
  158.                lambda6 * nodes_vecs_term #多度挂接角度均匀
  159.                )
  160.         loss.backward()
  161.         
  162.         optimizer.step()
  163.         print(loss)
  164.     optimized_coords = nodes_tensor.cpu().detach().numpy()
  165.     for (i, j) in follow.items():
  166.         optimized_coords[i] = optimized_coords[j]
  167.     return optimized_coords
  168. def visualize_skeleton(binary_img, nodes, links, optimized_nodes,fi):
  169.     """可视化优化前后的骨架"""
  170.     img = cv2.cvtColor(binary_img, cv2.COLOR_GRAY2BGR)
  171.     view_links = []
  172.     for d in links.values():
  173.         (b, e) = d['rng']
  174.         view_links.extend([(fi(i), fi(i + 1))
  175.                          for i in range(b, e - 1)])
  176.     links = np.array(view_links)
  177.     # 绘制原始骨架(红色)
  178.     for x, y in nodes:
  179.         cv2.circle(img, (x, y), 2, (0, 0, 255), -1)
  180.     for i, j in links:
  181.         cv2.line(img, tuple(nodes[i]), tuple(nodes[j]), (0, 0, 255), 1)
  182.     # # 绘制优化后的骨架(蓝色)
  183.     # for x, y in optimized_nodes:
  184.     #     cv2.circle(img, (int(x), int(y)), 2, (255, 0, 0), -1)
  185.     # for i, j in links:
  186.     #     cv2.line(img, tuple(optimized_nodes[i].astype(int)), tuple(optimized_nodes[j].astype(int)), (255, 0, 0), 1)
  187.     cv2.imwrite("res0.jpg",img)
  188. def out_gpd(links_gpdf,nodes_gpdf,links,pts,nodes):
  189.     links_gpdf = links_gpdf.set_geometry([
  190.           G.LineString(pts[b:e]).simplify(0.5, True)
  191.           for (b, e) in [links[k]['rng'] for k in links_gpdf['linkid']]])
  192.     nodes_gpdf = nodes_gpdf.set_geometry([
  193.           G.Point(pts[nodes[k]['vecs'][0][0]])
  194.           for k in nodes_gpdf['nodeid']])
  195.     gpd.GeoDataFrame(links_gpdf).to_file('./link2_out.geojson')
  196.     gpd.GeoDataFrame(nodes_gpdf).to_file('./node2_out.geojson')
  197.     return
  198. def main():
  199.     """主函数"""
  200.     # 读取二值图像
  201.     binary_img = read_binary_image('./1.jpg')
  202.     link_path = "./link.geojson"
  203.     node_path = "./node.geojson"
  204.    
  205.     link_gpd = gpd.read_file(link_path)
  206.     node_gpd = gpd.read_file(node_path)
  207.    
  208.     # 提取节点和连边
  209.     # 初始化节点字典,保存节点及其连出的向量信息
  210.     nodes = {}
  211.     for r in node_gpd.itertuples():
  212.         fixed = (not np.isnan(r.node_type))
  213.         nodes[r.nodeid] = {'vecs': []}
  214.     # 初始化点集列表和线段字典
  215.     pts = []
  216.     e = 0 #记录pts的idx
  217.     links = {}
  218.    
  219.     for r in link_gpd.itertuples():
  220.         #按照固定点类别区分模式
  221.         mode = (0 if (r.snode_type in [250]) else
  222.                 1 if (r.snode_type in [1001, 1002, 1003]) else 2)
  223.         xy = np.array(r.geometry.coords, np.double)
  224.         if mode == 1:
  225.             if len(xy) == 2:
  226.                 #如果固定点挂接的link是两个形点,那么添加一个靠近重点的四等分点,组成三个形点的link。
  227.                 xy = np.array([xy[0], (xy[0] + xy[1]*3)/4, xy[1]])
  228.                 #在插值的时候保留第一个点
  229.             xy = np.concatenate([xy[:1], resample(xy[1:])])
  230.         else:
  231.             xy = resample(xy)
  232.         pts.append(xy)
  233.         b = e
  234.         e += len(xy)
  235.         #当前link对应的pts点序从b到e-1
  236.         links[r.linkid] = {'rng': (b, e), 'mode': mode}
  237.         nodes[r.snodeid]['vecs'].append((b + 0, b + 1))
  238.         nodes[r.enodeid]['vecs'].append((e - 1, e - 2))
  239.     #三维展开成二维,即将所有的line坐标平铺,取消line维度  
  240.     pts = np.concatenate(pts)
  241.     # 用于记录同一个点的信息,对于每个节点,如果它连出多条边,将这些边的起点合并成一个。
  242.     follow = {}
  243.     for n in nodes.values():
  244.         l = n['vecs']
  245.         if len(l) > 2:
  246.             #计算node节点连的所有边的角度,并按照顺/逆时针调整顺序
  247.             v = np.array([pts[i2] - pts[i1] for (i1, i2) in l]).T
  248.             l[:] = [l[i] for i in np.argsort(np.arctan2(v[1], v[0]))]
  249.         #follow 字典,代表同一个点
  250.         if len(l) > 1:
  251.             for (i, _) in l[1:]:
  252.                 follow[i] = l[0][0]
  253.     def fi(i):
  254.         return follow.get(i, i)
  255.     print(pts.shape)
  256.     print(links["7cba8b52-3e8e-11ef-bae9-7993d366a9e1"])
  257.    
  258.     # 生成距离变换图
  259.     dist_transform = generate_distance_transform(binary_img)
  260. # lambda1  #保持原有长度损失
  261. # lambda2  #grid损失
  262. # lambda3  #角度平滑损失
  263. # lambda4 #固定点不动约束
  264. # lambda5 #固定点射出角约束
  265. # lambda6  #多度挂接角度均匀
  266.    
  267.     # 优化整个骨架
  268.     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)
  269.     # 可视化优化前后的骨架
  270.     visualize_skeleton(binary_img, pts.astype(np.int32), links, optimized_nodes,fi)
  271.     # out gpd
  272.     out_gpd(link_gpd,node_gpd,links,optimized_nodes,nodes)
  273. if __name__ == '__main__':
  274.     main()
复制代码
优化后的结果见(蓝色)


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

tsx81429

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表