guojin-yan / OpenVINO-CSharp-API

OpenVINO wrapper for .NET.
Apache License 2.0
137 stars 35 forks source link

关于Model read_model( string model_path, Tensor weights ) 从Tensor对象载入模型权重的疑问。 #16

Closed xhndg closed 7 months ago

xhndg commented 8 months ago

查阅到方法public Model read_model(string model_path,Tensor weights)。 该方法看起来像是能从内存中的Tensor对象载入模型权重。

如果我 使用 BinaryReader reader = new BinaryReader(File.Open(bin_path, FileMode.Open)); byte[] bytes = reader.ReadBytes((int)reader.BaseStream.Length); 读取权重文件。再使用bytes 数组构建Tensor传入read_model(string model_path,Tensor weights)。是不是能实现效果?

但是从数组构建权重的方法Tensor(element.Type type, Shape shape, byte[] mat);需要获取权重的element.Type,这个已知,即ElementType.F32。而shape感觉是整个模型的数据结构?如果我要获取,目前我只能通过https://netron.app/查看模型结构来慢慢构建。请教一下,获取模型的shape并构建Tensor是否有更高效的方法?

guojin-yan commented 8 months ago
public byte[] content_from_file(string file)
{
    FileStream fs = new FileStream(get_model_bin_file_name(), FileMode.Open, FileAccess.Read);

    long len = fs.Seek(0, SeekOrigin.End);

    fs.Seek(0, SeekOrigin.Begin);

    byte[] data = new byte[len + 1];

    fs.Read(data, 0, (int)len);
    return data;
}

使用方式如下,可以参考使用:

byte[] data = content_from_file(“*.bin”);
Shape shape = new Shape(new List<long> { 1, data.Length });
Tensor tensor = new Tensor(new element.Type(element.Type_t.u8), shape, data);
Core core = new Core();
Model model = core.read_model(“*.xml”, tensor);

不过该接口是最近进行测试修改的,之前发布的版本使用还有问题,如果想使用的话请参考:
https://github.com/guojin-yan/OpenVINO-CSharp-API/blob/12a9ceef778136b8fcc022a782100b7e9eb3a251/src/CSharpAPI/core/core.cs#L175

xhndg commented 8 months ago

似乎Tensor的shape有问题

xhndg commented 8 months ago

// 读取文件中的内容

        byte[] bytes = content_from_file(bin_path);

        Tensor tensor = new Tensor(new OvType(ElementType.U8), new Shape(new long[] { 1, bytes.Length}), bytes);
        Model model = core.read_model(model_path, tensor);

报错 OpenVinoSharp.OVException:“Exception from src\inference\src\core.cpp:103: Exception from src\inference\src\model_reader.cpp:178: Unable to read the model. Please check if the model format is supported and model is correct.

guojin-yan commented 8 months ago

请问你使用的是csharp3.1的最新源码吗?目前发布的程序包还是之前的版本,这个接口使用还存在问题。你可以直接下载源码进行使用。

xhndg commented 8 months ago
public byte[] content_from_file(string file)
{
    FileStream fs = new FileStream(get_model_bin_file_name(), FileMode.Open, FileAccess.Read);

    long len = fs.Seek(0, SeekOrigin.End);

    fs.Seek(0, SeekOrigin.Begin);

    byte[] data = new byte[len + 1];

    fs.Read(data, 0, (int)len);
    return data;
}

使用方式如下,可以参考使用:

byte[] data = content_from_file(“*.bin”);
Shape shape = new Shape(new List<long> { 1, data.Length });
Tensor tensor = new Tensor(new element.Type(element.Type_t.u8), shape, data);
Core core = new Core();
Model model = core.read_model(“*.xml”, tensor);

不过该接口是最近进行测试修改的,之前发布的版本使用还有问题,如果想使用的话请参考:

https://github.com/guojin-yan/OpenVINO-CSharp-API/blob/12a9ceef778136b8fcc022a782100b7e9eb3a251/src/CSharpAPI/core/core.cs#L175 引用了该方法

xhndg commented 8 months ago

我在vs2017 上用的是 OpenVINO.CSharp.Windows 2023.2.0.3

xhndg commented 8 months ago

框架是 framework4.8

guojin-yan commented 8 months ago

目前这个程序包这个接口还存在问题,如果你想使用这个接口的话,可以先通过克隆源码,然后将这个 https://github.com/guojin-yan/OpenVINO-CSharp-API/blob/csharp3.1/src/CSharpAPI/CSharpAPI.csproj 项目添加到你的项目中即可。 在使用时只需要安装这个程序包:OpenVINO.runtime.win