首页 > 其他分享 >使用 Unity Sentis 和 Compute Shader,2d106det.onnx 进行高效人脸网格标记

使用 Unity Sentis 和 Compute Shader,2d106det.onnx 进行高效人脸网格标记

时间:2024-05-31 17:31:18浏览次数:23  
标签:Sentis 2d106det onnx 192 using RenderTexture new 106 public

前言

前篇:使用 Unity Sentis 和 Compute Shader,det_10g.onnx 进行高效人脸五官定位-CSDN博客

在计算机视觉领域,人脸网格标记是一项重要的任务,用于识别人脸关键点和特征。本文将介绍如何使用 Unity Sentis 和 Compute Shader,结合 2d106det.onnx 模型,实现高效的人脸网格标记。我将提供完整的代码示例。

模型分析

输入值:

模型的输入是1x3x192x192的一张图片;

输出值:

输出值为(1,212)的张量,存储的是106个特征点的位置,可以将它变形为(106,2)方便计算;

代码示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Sentis;

public class Landmark2D : MonoBehaviour
{
    public ModelAsset modelAsset;
    public Model model; 
    private IWorker worker;
    private GPUComputeBackend gpu;
    
    //处理图片
    public Texture2D t2d;
    public Material landMarkMat;


    private RenderTexture fc1RT;
    public ComputeShader outputCS;
    

    private void Start()
    {

        fc1RT = new RenderTexture(106, 1, 0,RenderTextureFormat.RGFloat);
        model = ModelLoader.Load(modelAsset);  
      
        gpu = new GPUComputeBackend();
        
        var model2 = Functional.Compile(input =>
        {
            var outputs = model.Forward(input);
            var pred = outputs[0].Reshape(new []{106,2});
            pred = pred[.., 0..2] + 1;
            pred = pred[.., 0..2] * (192 / 2);
            return pred;

        }, InputDef.FromModel(model)[0]
        );
        worker = WorkerFactory.CreateWorker(BackendType.GPUCompute, model2);
        Get(t2d);
    }

   
    public RenderTexture rt1;
    public RenderTexture markTexture;
    public void Get(Texture2D source )
    {
        rt1 = RenderTexture.GetTemporary(192,192,0);
        landMarkMat.SetTexture("_MainTex",source);
        Graphics.Blit(source,rt1,landMarkMat,0);   
        
        using (var input = TextureConverter.ToTensor(rt1, 192, 192, 3))
        {
            TensorFloat _255 = new TensorFloat(new TensorShape(1), new float[] {255});
            TensorFloat newInput = TensorFloat.AllocNoData(new TensorShape(1, 3, 192,192)); 
            gpu.Mul(input,_255,newInput);
            worker.Execute(newInput);
        }
   
        
        using var fc1 = worker.PeekOutput("output_0") as TensorFloat;  
        fc1.Reshape(new TensorShape(1,106,1,2));
         
        using  TensorFloat tagetT = TensorFloat.AllocNoData(new TensorShape(1, 2, 1,106)); 
        gpu.Transpose(fc1,tagetT,new int[] {0, 3, 1, 2});
        
        fc1RT = TextureConverter.ToTexture(tagetT, 106, 1, 2);  
        
        
        markTexture = new RenderTexture(192, 192, 0);
        markTexture.enableRandomWrite = true;  // 允许随机写入
        markTexture.Create();  // 创建纹理
        
        outputCS.SetTexture(0,"fcTex",fc1RT);
        outputCS.SetTexture(0,"outTex",markTexture);
        outputCS.Dispatch (0, (106 + 15)/16, 1, 1); 
        
 
        landMarkMat.SetTexture("_MarkTex",markTexture);
        
        /*fc1.CompleteOperationsAndDownload();
        for (int i = 0; i < 212; i++)
        {
            print(fc1[i]);
        }*/
    
    }

    private void OnDestroy()
    {
        worker.Dispose();
        gpu.Dispose();
        RenderTexture.ReleaseTemporary(rt1);
    }
}

标签:Sentis,2d106det,onnx,192,using,RenderTexture,new,106,public
From: https://blog.csdn.net/m0_55632444/article/details/139357795

相关文章

  • 解决labelme中AI Model Ai Mask Ai Polygon选项下载onnx权重慢或者无法下载问题
    新版的labelme中已经内置了AIModel功能,能够通过模型智能识别图像中想要标注的区域,能够显著减少工作量。但是第一次使用这个功能的时候会要下载模型权重,此时一般速度会非常慢,或者出现报错无法下载,下面提出一种解决方法。如图,有5种模型,每个模型需要分别下载encoder和decoder两......
  • Ubuntu下的onnxruntime(c++)编译 转载文章 非原创
    仓库下载gitclone--depth=1--branchv1.12.1https://github.com.cnpmjs.org/microsoft/onnxruntime.git注意:需要更换国内镜像源编译GPU./build.sh--skip_tests--use_cuda--configRelease--build_shared_lib--parallel--cuda_home/usr/local/cuda-11.3--cudnn_home/u......
  • Windows下使用ONNXRuntime推理YOLOv8
    一、准备工作将训练好的pt文件转为onnx格式。yoloexportmodel=best.ptformat=onnxdevice=0opset=13dynamic#如果是动态Shape的话,命令行参数dydynamic一定要加上,不然就是static的模型二、下载与安装ONNXRuntime注意:下载安装onnxruntime-gpu时需要保证其与cuda的兼容......
  • Windows下使用ONNXRuntime的GPU进行推理时提示cudnn64_8.dll异常
    一、问题复现将模型放到GPU上推理时时发生的异常。OrtSessionOptionsAppendExecutionProvider_CUDA(session_options,0);...ort_outputs=session_.Run(Ort::RunOptions{nullptr},inputNames.data(),&input_tensor_,1,outNames.data(),outNames.size());二、解......
  • paddlepaddle自定义网络模型及onnx模型转换与推理
    前面介绍过了使用Paddleseg套件训练后,使用export.py文件导出模型为推理部署模型。具体可以参考之前的:https://www.cnblogs.com/wancy/p/18028138本文介绍使用paddle自定义简单二分类CNN模型到训练数据集再到转换onnx模型推理。1. 数据集划分我这里将数据划分为tr......
  • window10安装insightface、onnxruntime-gpu、视频换脸
    1.https://onnxruntime.ai/docs/execution-providers/CUDA-ExecutionProvider.html#requirements2.下载cuda_12.2.0_536.25_windows3.下载cudnncudnn-windows-x86_64-8.5.0.96_cuda11-archive4.condacreate-nonnx_testpython=3.105.根据官方文档说明cuda12.x的版本安装o......
  • C# 中图像和 OnnxRuntime.Tensor 的互转
    因毕设需要,尝试了将PyTorch模型转为ONNX然后用C#做推理,记录一下经验。总的来说,C#对深度学习的支持远不如Python,缺少很多必要的库,不少代码需要自己编写。思路毕设做的是image-to-image的low-level视觉任务,因此需要3个主要步骤:从硬盘读取图片并转成张量(imageto......
  • 【已解决】onnx转TensorRT遇到Assertion failed: axis >= 0 && axis < nbDims的问题
    最近在jetsonnano上部署YOLOv8的时候遇到了许多问题(参考jetsonnano部署YOLOv8),大部分比较好解决,其中在将模型部署到jetsonnano的阶段遇到了如下两个问题:静态onnx转TensorRT遇到Assertionfailed:axis>=0&&axis<nbDims问题动态onnx转tensorrt报错:Attributenotfound......
  • ONNX 的简介及应用
    文章目录一、ONNX是什么二、ONNX具备哪些功能三、ONNX的相关概念四、ONNX的应用领域一、ONNX是什么ONNX(OpenNeuralNetworkExchange)是一种开放的深度学习模型交换格式,类似于JSON格式。它允许将模型从一个深度学习框架转换到另一个框架,以便在不同的平台和设备......
  • YOLOv8图像分割:使用ONNX模型进行推理
    基于COCO数据集的YOLOv8目标分割onnx模型推理在本博客中,我们将探讨如何使用YOLOv8目标分割模型进行推理,包括图片,视频文件,摄像头实时分割,特别是ONNX在不同大小(YOLOv8n,YOLOv8s,YOLOv8m,YOLOv8l,YOLOv8x)的模型上进行的实验。我们还将讨论所需的环境配置,代码实现,以及如何......