当前位置: 首页 > news >正文

【机器人】复现 GrainGrasp 精细指导的灵巧手抓取

GrainGrasp为每个手指提供细粒度的接触指导,为灵巧手生成精细抓取策略

通过单独调整每个手指的接触来实现更稳定的抓取,从而提供了更接近人类能力的抓取指导。

论文地址:GrainGrasp: Dexterous Grasp Generation with Fine-grained Contact Guidance

代码地址:https://github.com/wmtlab/GrainGrasp

看一下抓取效果:

看一物体CAD的抓取效果

不同视角观察:

1、创建Conda环境

首先创建一个Conda环境,名字为GrainGrasp,python版本为3.9

然后进入GrainGrasp环境

conda create -n GrainGrasp python=3.9
conda activate GrainGrasp

2、安装PyTorch

我们使用的版本是pytorch==2.0.1+cu118

执行下面命令进行安装:

conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.8 -c pytorch -c nvidia

3、安装GrainGrasp依赖库

下载GrainGrasp代码,然后进行GrainGrasp-main目录中

代码地址:https://github.com/wmtlab/GrainGrasp

创建一个文件requirements.txt,编写内容:

numpy==1.23.0
open3d==0.17.0
trimesh==4.1.7
attrdict==2.0.1
mano
chumpy==0.70

开始安装啦

pip install -r requirements.txt  -i https://pypi.tuna.tsinghua.edu.cn/simple

等待安装完成~

4、安装pytorch3d

访问 PyTorch3D 官方下载页面,找到与我们的环境匹配的 PyTorch3D 0.7.5 安装包

名称:pytorch3d-0.7.5-py39_cu118_pyt201.tar.bz2

然后下载并安装:

conda install pytorch3d-0.7.5-py39_cu118_pyt201.tar.bz2

5、模型推理

首先从MANO网站下载MANO模型文件(MANO_LEFT.pkl、MANO_RIGHT.pkl),到mano/models/目录中

手部支持的自由度:

这个配置文件整合了多个模块的路径和参数设置,用于手部和物体的 3D 重建和姿态估计任务。

每个模块的配置都包含了必要的路径和训练参数,以便在实际运行时能够正确加载数据和模型,并进行有效的训练和测试。

运行示例1:

python run_complete.py -i=8 -s=1234

看一下抓取效果:

看一物体CAD的抓取效果:

run_complete.py 源代码:

import os
import time
import torch
import trimesh
import argparse
import numpy as np
import open3d as o3d
from utils import annotate
from utils import vis
from utils import tools
from config import cfgs
from GrainGrasp import GrainGrasp


if __name__ == "__main__":
    # 设置设备为 GPU 或 CPU
    device = "cuda" if torch.cuda.is_available() else "cpu"
    
    # 创建命令行参数解析器
    parge = argparse.ArgumentParser(description="GrainGrasp")
    parge.add_argument("--idx", "-i", type=int, default=2, help="物体的索引")
    parge.add_argument("--epochs", "-e", type=int, default=300, help="优化的轮数")
    parge.add_argument("--threshold", "-t", type=float, default=0.0, help="阈值")
    parge.add_argument("--select", "-s", type=str, default="12345", help="选择的手指索引,最多5根手指:12345")
    parge.add_argument("--vis_pc", "-vp", type=bool, default=True, help="是否可视化点云")
    parge.add_argument("--vis_mesh", "-vm", type=bool, default=True, help="是否可视化网格")
    parge.add_argument("--vis_process", "-vprocess", type=bool, default=True, help="是否可视化优化过程")
    args = parge.parse_args()
    
    # 将选择的手指索引从字符串转换为整数列表
    select_finger_idx = list(map(lambda x: int(x), args.select))
    
    # 从配置文件中获取采样点数量
    sample_points_num = cfgs.obman_config.sample_points_num
    
    # 加载物体的三维网格模型
    obj_path = os.path.join("sample", str(args.idx), "obj_mesh.obj")
    obj_mesh = trimesh.load_mesh(obj_path)
    
    # 从网格中采样点云数据(备用方法)
    # obj_pc = tools.pc_sample(obj_mesh, sample_points_num)
    
    # 从文件加载点云数据
    obj_pc_path = os.path.join("sample", str(args.idx), "obj_pc.npy")  # 点云数据形状为 [3, 3000]
    obj_pc = np.load(obj_pc_path).T  # 转置为 [3000, 3]
    obj_pc = torch.Tensor(obj_pc)  # 转换为 PyTorch 张量
    
    # 初始化 GrainGrasp 模型
    grain_grasp = GrainGrasp(cfgs.dcog_config, cfgs.cvae_model_path, device)
    
    # 开始计时
    time_start = time.time()
    
    # 执行推理过程,获取抓取姿态优化结果
    result = grain_grasp.inference_complete(
        obj_pc,
        epochs=args.epochs,
        select_finger_idx=select_finger_idx,
        threshold=args.threshold,
    )
    
    print("运行时间是 {:.2f} 秒".format(time.time() - time_start))
    print("能量值为 ", result.E_pen)
    print("最小能量索引为 ", result.min_idx)
    
    hand_pc_final = result.min_idx_hand_pc # 获取优化后的手部点云数据
    hand_face = grain_grasp.dcog_model.rh_faces[0].cpu() # 获取手部网格的面数据
    hand_color = annotate.get_finger_colors(hand_pc_final) # 为手部点云数据分配颜色
    
    # 创建手部网格的 Open3D对象
    hand_mesh_o3d = vis.get_o3d_mesh(hand_pc_final, hand_face, [0, 0.8, 1], hand_color)
    obj_colors_true = annotate.get_obj_colors(result.obj_cls.cpu()) # 为物体点云数据分配颜色
    obj_pcd = vis.get_o3d_pcd(obj_pc.cpu().detach(), obj_colors_true)
    obj_mesh_o3d = vis.trimesh2o3d(obj_mesh)
    
    # 如果设置为可视化点云,则显示手部和物体点云
    if args.vis_pc:
        vis.vis_HandObject([hand_mesh_o3d], [obj_pcd])
    
    # 如果设置为可视化网格,则显示手部和物体网格
    if args.vis_mesh:
        vis.vis_HandObject([hand_mesh_o3d], [obj_mesh_o3d])
    
    # 如果设置为可视化优化过程,则显示优化过程
    if args.vis_process:
        record_hand_pc = result.min_idx_record_hand_pc
        record_handmesh_o3d = vis.get_o3d_mesh(record_hand_pc[0], hand_face, [0, 0.8, 1], hand_color)
        vis.vis_GraspProcess(record_handmesh_o3d, record_hand_pc[1:], obj_mesh_o3d)

思路流程

  1. 设备设置

    • 检查是否有可用的 GPU 设备,如果有则使用 GPU,否则使用 CPU。

  2. 参数解析

    • 使用 argparse 解析命令行参数,设置物体索引、优化轮数、阈值、手指选择、可视化选项等。

  3. 数据加载

    • 加载物体的三维网格模型(obj_mesh.obj)。

    • 从文件加载物体点云数据(obj_pc.npy),并将其转换为 PyTorch 张量。

  4. 模型初始化

    • 初始化 GrainGrasp 模型,加载预训练模型路径(cfgs.cvae_model_path)。

  5. 推理过程

    • 调用模型的 inference_complete 方法进行抓取姿态优化。

    • 传入物体点云数据、优化轮数、手指索引和阈值等参数。

    • 计算运行时间并输出优化结果(能量值和最小能量索引)。

  6. 结果可视化

    • 将优化后的手部点云数据和物体点云/网格模型进行可视化。

    • 如果设置为可视化点云,则显示手部网格和物体点云。

    • 如果设置为可视化网格,则显示手部网格和物体网格。

    • 如果设置为可视化优化过程,则显示优化过程中的手部姿态变化。

运行示例2:

python run_only_opt.py -i=4 -s=134

看一下抓取效果

看一物体CAD的抓取效果:

run_only_opt.py 源代码:

import os
import time
import torch
import trimesh
import argparse
import numpy as np
import open3d as o3d
from utils import annotate
from utils import vis
from utils import tools
from utils import Load_obman
from config import cfgs
from GrainGrasp import GrainGrasp


if __name__ == "__main__":
    # 设置设备为 GPU 或 CPU
    device = "cuda" if torch.cuda.is_available() else "cpu"
    
    # 初始化命令行参数解析器
    parge = argparse.ArgumentParser(description="GrainGrasp")
    parge.add_argument("--idx", "-i", type=int, default=2, help="物体的索引")
    parge.add_argument("--epochs", "-e", type=int, default=300, help="优化的轮数")
    parge.add_argument("--K", "-k", type=int, default=50, help="优化过程中的参数 K")
    parge.add_argument("--threshold", "-t", type=float, default=0.0, help="优化过程中的阈值")
    parge.add_argument("--select", "-s", type=str, default="12345", help="选择的手指索引,最多5根手指:12345")
    parge.add_argument("--vis_pc", "-vp", type=bool, default=True, help="是否可视化点云")
    parge.add_argument("--vis_mesh", "-vm", type=bool, default=True, help="是否可视化网格")
    parge.add_argument("--vis_process", "-vprocess", type=bool, default=True, help="是否可视化优化过程")
    args = parge.parse_args()

    # 将选择的手指索引从字符串转换为整数列表
    select_finger_idx = list(map(lambda x: int(x), args.select))
    
    # 从配置文件中获取采样点数量
    sample_points_num = cfgs.obman_config.sample_points_num

    # 构造物体网格模型路径
    obj_path = os.path.join("sample", str(args.idx), "obj_mesh.obj")
    # 加载物体网格模型
    obj_mesh = trimesh.load_mesh(obj_path)
    # 从网格中采样物体点云数据
    obj_pc = tools.pc_sample(obj_mesh, sample_points_num)
    # 构造手部点云数据路径
    hand_pc_path = os.path.join("sample", str(args.idx), "hand_pc.npy")  # [3,3000]

    # 加载手部点云数据
    hand_pc = np.load(hand_pc_path)
    obj_pc = torch.Tensor(obj_pc)
    hand_pc = torch.Tensor(hand_pc)

    # 初始化 GrainGrasp 模型
    grain_grasp = GrainGrasp(cfgs.dcog_config, None, device)
    # 开始计时
    time_start = time.time()
    # 执行优化过程,获取抓取姿态优化结果
    result = grain_grasp.inference_only_opt(
        obj_pc,
        hand_pc=hand_pc,
        K=args.K,
        epochs=args.epochs,
        select_finger_idx=select_finger_idx,
        threshold=args.threshold,
    )

    print("运行时间是 {:.2f} 秒".format(time.time() - time_start))
    print("能量值为 ", result.E_pen)
    print("最小能量索引为 ", result.min_idx)

    # 获取优化后的手部点云数据
    hand_pc_final = result.min_idx_hand_pc
    # 获取手部网格的面数据
    hand_face = grain_grasp.dcog_model.rh_faces[0].cpu()
    # 为手部点云数据分配颜色
    hand_color = annotate.get_finger_colors(hand_pc_final)
    # 创建手部网格的 Open3D 对象
    hand_mesh_o3d = vis.get_o3d_mesh(hand_pc_final, hand_face, [0, 0.8, 1], hand_color)
    # 为物体点云数据分配颜色
    obj_colors_true = annotate.get_obj_colors(result.obj_cls.cpu())
    # 创建物体点云的 Open3D 对象
    obj_pcd = vis.get_o3d_pcd(obj_pc.cpu().detach(), obj_colors_true)
    # 将 Trimesh 格式的物体网格转换为 Open3D 格式
    obj_mesh_o3d = vis.trimesh2o3d(obj_mesh)


    # 如果设置为可视化点云,则显示手部和物体点云
    if args.vis_pc:
        vis.vis_HandObject([hand_mesh_o3d], [obj_pcd])

    # 如果设置为可视化网格,则显示手部和物体网格
    if args.vis_mesh:
        vis.vis_HandObject([hand_mesh_o3d], [obj_mesh_o3d])

    # 如果设置为可视化优化过程,则显示优化过程
    if args.vis_process:
        record_hand_pc = result.min_idx_record_hand_pc
        record_handmesh_o3d = vis.get_o3d_mesh(record_hand_pc[0], hand_face, [0, 0.8, 1], hand_color)
        vis.vis_GraspProcess(record_handmesh_o3d, record_hand_pc[1:], obj_mesh_o3d)

思路流程

  1. 设备设置与参数解析

    • 检查是否有可用的 GPU 设备,如果有则使用 GPU,否则使用 CPU。

    • 使用 argparse 解析命令行参数,设置物体索引、优化轮数、参数 K、阈值、手指选择、可视化选项等。

  2. 数据加载

    • 加载物体的三维网格模型(obj_mesh.obj)。

    • 从物体网格模型中采样点云数据(tools.pc_sample)。

    • 从文件加载手部点云数据(hand_pc.npy),并将其转换为 PyTorch 张量。

  3. 模型初始化

    • 初始化 GrainGrasp 模型,不加载预训练模型路径(None)。

  4. 优化过程

    • 调用模型的 inference_only_opt 方法进行抓取姿态优化。

    • 传入物体点云数据、手部点云数据、参数 K、优化轮数、手指索引和阈值等参数。

    • 计算运行时间并输出优化结果(能量值和最小能量索引)。

  5. 结果可视化

    • 将优化后的手部点云数据和物体点云/网格模型进行可视化。

    • 如果设置为可视化点云,则显示手部网格和物体点云。

    • 如果设置为可视化网格,则显示手部网格和物体网格。

    • 如果设置为可视化优化过程,则显示优化过程中的手部姿态变化。

分享完成~

相关文章:

  • Android开发检查是否是各大厂商手机的工具类
  • Redis 服务搭建
  • Spring MVC的请求和响应
  • JavaScript 函数基础
  • IoTDB看门狗配置后不生效
  • 算法 | 小龙虾优化算法原理,引言,公式,算法改进综述,应用场景及matlab完整代码
  • ISIS-2 邻居建立关系
  • 4.1 C#获取目录的3个方法的区别
  • 【考研政治】2026考研政治马原部分关键词带背 导论
  • Mysql基本查询(上)
  • 情绪分析和深度强化学习确实能够在一定程度上增强股市预测
  • 实战-MySQL5.7升级8.0遇到的四个问题
  • 解决安卓so库异常无法打印堆栈的问题
  • 网络拓扑图
  • 区块链驱动金融第九章——解锁比特币 “平台” 的多元潜力
  • vue中ts引入组件,无法找到模块xxx的声明文件。xxx隐式拥有 “any“ 类型。
  • HTTP长连接与短连接的前世今生
  • 【商城实战(63)】配送区域与运费设置全解析
  • Python字典
  • Flume-试题
  • 单人飞行器首次实现海拔5000米载人试飞
  • 焦点访谈丨售假手段又翻新,警惕化肥“忽悠团”的坑农套路
  • 多所院校高规格召开考研动员会,有学院考研报名率达84%
  • 体坛联播|热刺追平单赛季输球纪录,世俱杯或创收20亿美元
  • 龚正会见哥伦比亚总统佩特罗
  • 泽连斯基抵达安卡拉,称乌将派出最高级别代表团参与谈判