首页 > 其他分享 >floor_plan_meshproject增加角度正则损失

floor_plan_meshproject增加角度正则损失

时间:2025-01-15 23:03:04浏览次数:1  
标签:decoded angle room floor torch vertex vertices meshproject plan

数据结构

data.x  #(128, 16)  16 = triangles(9) + confidence(7)
"""
每个元素
[x1, y1, z1, x2, y2, z2, x3, y3, z3, 
c_v1, c_v2, c_v3, c_e1, c_e2, c_e3, c_f]
"""
data.y  #(128)


encoded_x #(128, 576)
encoded_x_conv # (2, 96, 576)
decoded_x_conv # (2, 96, 4)
decoded_x # (128, 4)

Angle_loss 计算

\[L_{\text{angle}} = p_{\text{merge}}^{\alpha} \times \min_{t \in \{90, 180, 270, 360\}} | \text{Angle}_{\text{merge}} - t | \]

\[Loss= w_1 L_{\text{classification}} + w_2 L_{\text{angle}} \]

1、朴素实现

data.x  #(num_triangles, 16) , 16 = triangles(9) + confidence(7)
# 每个元素
[x1, y1, z1, x2, y2, z2, x3, y3, z3, 
c_v1, c_v2, c_v3, c_e1, c_e2, c_e3, c_f]

data.y  #(128)

image-20250115223828926

# 每个 triangle 的信息(添加了角)
data.x_with_angle
{
    face_idx: i,
    vertices: [(x1, y1, z1), (x2, y2, z2), (x3, y3, z3)],
    angles: [α1, α2, α3]
}

# 获取所有 room triangles

#取它们的顶点(去重)

(x1, y1, z1): {
    vertices: (x1, y1, z1),
    faces_idx: [1, 2, 3, ...], # 包含该顶点的room面idx
    angle: [α1, α2, α3, ...], # 该顶点在该面的angle
    decoded_x: [[p_11, p_12, p_13, p_14], [p_21, p_22, p_23, p_24], [p_31, p_32, p_33, p_34], ...]
}

# 计算angle_merge, p_merge

# loss 计算

朴素实现
def angle_loss_torch(self, data, decoded_x):
    """
    doc: 计算角度正则化损失
    return: total_angle_loss: 角度正则化损失
    """

    # 1. 选出所有最大分类概率为room的三角形(及其索引)
    softmax_decoded_x = torch.nn.functional.softmax(decoded_x, dim=1)  # 使用softmax得到不同分类的概率
    max_probs, max_indices = torch.max(softmax_decoded_x, dim=1)
    room_indices = torch.where(max_indices == 2)[0]

    # 获取每个面的三个角 data.x_with_angle
    vertices = data.x[:, :9].reshape(-1, 3, 3)  # 提取顶点坐标(num_triangle, 3, 3)
    angles = angle_func(vertices)  # 计算内角 (num_triangle, 3)
    data.x_with_angle = [
        {
            'triangle_idx': i,
            'vertices': [tuple(v) for v in vertices[i]],
            'angles': angles[i].tolist()
        }
        for i in range(vertices.shape[0])
    ]

    # 2. 取它们的顶点(去重)
    vertex_info = {}
    for i in room_indices:
        triangle = data.x_with_angle[i]
        face_idx = triangle['triangle_idx']
        vertices = triangle['vertices']
        angles = triangle['angles']
        
        for vertex, angle in zip(vertices, angles):
            vertex_key = tuple(vertex)
            if vertex_key not in vertex_info:
                vertex_info[vertex_key] = {
                    'vertices': vertex,
                    'faces_idx': [],
                    'angles': [],
                    'decoded_x': []
                }
            
            vertex_info[vertex_key]['faces_idx'].append(face_idx)
            vertex_info[vertex_key]['angles'].append(angle)
            vertex_info[vertex_key]['decoded_x'].append(softmax_decoded_x[face_idx])

    # 3. 计算每个顶点的角度总和和融合概率特征
    for vertex_key in vertex_info:
        vertex_info[vertex_key]['angle_merge'] = sum(vertex_info[vertex_key]['angles'])
        vertex_info[vertex_key]['p_merge'] = torch.mean(torch.stack(vertex_info[vertex_key]['decoded_x']), dim=0)

    # 4. angle_loss = p^alpha * min|θ - t|
    t_values = torch.tensor([np.pi/2, np.pi, 3*np.pi/2, 2*np.pi], dtype=torch.float32)
    alpha = self.config.angle_loss_alpha
    total_angle_loss = 0

    for vertex_key, info in vertex_info.items():
        angle_sum = info['angle_merge']
        p = info['p_merge'][2]
        abs_diffs = torch.abs(angle_sum - t_values)
        min_abs_diff = torch.min(abs_diffs)
        angle_loss = p**alpha * min_abs_diff
        total_angle_loss += angle_loss

    return total_angle_loss

for循环太多,考虑能不能优化

2、优化循环

算法优化

# room 顶点
room_vertices = vertices[room_indices].reshape(-1, 3)  

# room angle
room_angles = angles[room_indices].reshape(-1)  # (num_room_triangles * 3)

# 从 softmax_decoded_x 中提取出属于“room”类别的三角形的概率分布,并将每个三角形的概率分布重复 3 次,以对应每个三角形的 3 个顶点
room_decoded_x = softmax_decoded_x[room_indices].repeat_interleave(3, dim=0)  # (num_room_triangles * 3, num_cls)


# 获取去重顶点
unique_vertices
# room_vertices 中相应元素在 unique_vertices 中的索引
inverse_indeces

# angle_merge、p_merge
angle_merge.scatter_add_(0, inverse_indices, room_angles)
优化循环
def angle_loss(self, data, decoded_x):
    """
    doc: 计算角度正则化损失
    return: total_angle_loss: 角度正则化损失
    """

    # 1. 选出所有最大分类概率为room的三角形(及其索引)
    softmax_decoded_x = torch.nn.functional.softmax(decoded_x, dim=1)  # 使用softmax得到不同分类的概率
    max_probs, max_indices = torch.max(softmax_decoded_x, dim=1)
    room_indices = torch.where(max_indices == 2)[0]

    # 获取每个面的三个角 data.x_with_angle
    vertices = data.x[:, :9].reshape(-1, 3, 3)  # 提取顶点坐标(num_triangle, 3, 3)
    angles = angle_func(vertices)  # 计算内角 (num_triangle, 3)


    # 2. 取它们的顶点(去重)
    room_vertices = vertices[room_indices].reshape(-1, 3)  # (num_room_triangles * 3, 3)
    room_angles = angles[room_indices].reshape(-1)  # (num_room_triangles * 3)
    room_decoded_x = softmax_decoded_x[room_indices].repeat_interleave(3, dim=0)  # (num_room_triangles * 3, num_cls)

    # 3. 计算每个顶点的角度总和和融合概率特征
    unique_vertices, inverse_indices = torch.unique(room_vertices, dim=0, return_inverse=True)
    angle_merge = torch.zeros(unique_vertices.shape[0], dtype=torch.float32, device=room_vertices.device)
    p_merge = torch.zeros(unique_vertices.shape[0], softmax_decoded_x.shape[1], dtype=torch.float32, device=room_vertices.device)

    angle_merge.scatter_add_(0, inverse_indices, room_angles)
    p_merge.scatter_add_(0, inverse_indices.unsqueeze(1).expand(-1, softmax_decoded_x.shape[1]), room_decoded_x)
    p_merge = p_merge / torch.bincount(inverse_indices).unsqueeze(1).float()

    # 4. angle_loss = p^alpha * min|θ - t|
    t_values = torch.tensor([np.pi/2, np.pi, 3*np.pi/2, 2*np.pi], dtype=torch.float32, device=angle_merge.device)
    alpha = self.config.angle_loss_alpha
    total_angle_loss = 0

    for angle_sum, p in zip(angle_merge, p_merge):
        abs_diffs = torch.abs(angle_sum - t_values)
        min_abs_diff = torch.min(abs_diffs)
        angle_loss = p[2]**alpha * min_abs_diff
        total_angle_loss += angle_loss

    return total_angle_loss

标签:decoded,angle,room,floor,torch,vertex,vertices,meshproject,plan
From: https://www.cnblogs.com/sherioc/p/18673858

相关文章

  • 【PCL】Segmentation 模块—— 平面模型分割(Plane model segmentation)
    1、简介PCL(PointCloudLibrary)中的平面模型分割(PlaneModelSegmentation)是一种从点云数据中提取平面结构的方法。它通过识别点云中符合平面模型的点集,将场景中的平面区域分割出来。1.1主要步骤选择模型:选择平面模型作为分割目标。采样点:随机选取点云中的点用于模型拟......
  • 如何在Eplan里面添加插件
    运行MicrosoftVisualStudio并新建一个C#类库项目,如下图所示http://s4/middle/9a1ad43agc08e09b3c0f3&690API开发之入门篇"TITLE="EPLAN API开发之入门篇"/>新建项目窗口上部选择.NET版本,并设置好项目名称和路径。重命名C#源文件名为“AddInModule.cs”,类名改名为“AddInM......
  • EPLAN P8 学习笔记 配图 20250114
    组织结构、细节会生疏。Pageproperties-Fullpagename、Pagetype、PagedescriptionFullpagename-StructureidentifiersMainProjecttree-IdentifierStructurePageTypeNameDescriptionPagesObject元素structure结构identifier.excalidrawProjectData-S......
  • 用于决策的世界模型 -- 论文 World Models (2018) & PlaNet (2019) 讲解
    参考资料:[2411.14499]UnderstandingWorldorPredictingFuture?AComprehensiveSurveyofWorldModels[1803.10122]WorldModelsLearningLatentDynamicsforPlanningfromPixelsKaixhin/PlaNet:DeepPlanningNetwork:Controlfrompixelsbylatentplanning......
  • PlantUml使用向导
    作为一名开发人员,你是否只关注代码,从未绘制过UML?从现在开始,你应该开始绘制它了!作为绘制UML的工具,你选择的是什么?Visio?Rose?还是亿图?又或者PowerDesign?从现在开始,你有一个新的选择了.只要有文本编辑器,你就可以编辑和修改UML.只要有浏览器,你就可以打开和查看用......
  • 【保姆级】2025最新Midjourney Plan订阅指南,一键操作轻松订阅!
    1.Midjourney介绍Midjourney是一款AI制图工具,只要关键字,就能通过AI算法生成相对应的图片,只需要不到一分钟。可以选择不同画家的艺术风格,例如安迪华荷、达芬奇,达利和毕加索等,还能识别特定镜头或摄影术语。有别于谷歌的Imagen和DALL.E,Midjourney是第一个快速生成AI制图并......
  • [ABC311D] Grid Ice Floor
    前言:题解看不懂,太高深了(我太蒻了),于是自己写了一篇。思路:bfs大法,记录新的单次滑倒的中点(撞石头),并记录经过的点,总之还是很简单的。代码:#include<bits/stdc++.h>usingnamespacestd;constintN=210;intn,m;intvis[N][N],cnt[N][N];intdx[4]={0,0,-1,1};intdy[4......
  • netplan apply报错No module named ‘netifaces‘
    Ubuntu20.04.5LTS\n\l,ctrl+alt+f2切换字符登录f1切换图形处理办法:root登录执行root@node37:/disk1/Qwen2.5-72B-Instruct-GPTQ-Int4#cat/etc/netplan/01-network-manager-all.yaml #LetNetworkManagermanagealldevicesonthissystemnetwork: version:2......
  • 【c# Unity-Shader版本贪吃蛇教程】一张Plane渲染的Shader贪吃蛇
    前言    开局一张plane,其余靠shader编。本游戏为shader绘制贪吃蛇,没有3D模型,想了解3D版本的开发,可以跳转到【c#Unity贪吃蛇教程】    已经是第五期C#不同平台制作贪吃蛇了,前三期分别是【c#控制台贪吃蛇教程】、【c#winform贪吃蛇教程】、【c#WPF贪吃蛇教......
  • [20250109]dbms_xplan.display_cursor+peeked_binds无法查看绑定变量值.txt
    [20250109]dbms_xplan.display_cursor+peeked_binds无法查看绑定变量值.txt--//在我使用自己写的dpc.sql脚本中我会加入peeked_binds参数查看绑定变量值,但是有时候会遇到无法查看的情况。--//以前自己很少关注这个细节,应该有别的途径获取绑定变量值,最近在优化一条sql语句正好遇到,......