.wts文件

The .wts content format

The .wts is plain text file.

For example the lenet5.wts, part content are shown below.

The first line is a number, indicate how many lines it has, excluding itself.

And then each line is

[weight name] [value count = N] [value1] [value2], ..., [valueN]

The value is in HEX format.

10
conv1.weight 150 be40ee1b bd20bab8 bdc4bc53 .......
conv1.bias 6 bd327058 .......
conv2.weight 2400 3c6f2220 3c693090 ......
conv2.bias 16 bd183967 bcb1ac8a .......
fc1.weight 48000 3c162c20 bd25196a ......
fc1.bias 120 3d3c3d49 bc64b948 ......
fc2.weight 10080 bce095a4 3d33b9dc ......
fc2.bias 84 bc71eaa0 3d9b276c ....... 
fc3.weight 840 3c252870 3d855351 .......
fc3.bias 10 bdbe4bb8 3b119ee0 ......

使用notebook打开.wts文件
在这里插入图片描述
加载wts文件的权重如下所示。

// TensorRT weight files have a simple space delimited format:
// [type] [size] <data x size in hex>
std::map<std::string, Weights> loadWeights(const std::string file) {
    std::cout << "Loading weights: " << file << std::endl;
    std::map<std::string, Weights> weightMap;

    // Open weights file
    std::ifstream input(file);
    assert(input.is_open() && "Unable to load weight file. please check if the .wts file path is right!!!!!!");

    // Read number of weight blobs
    int32_t count;
    input >> count; // 读取出来的值是363,表示wts文件总共有363行
    assert(count > 0 && "Invalid weight map file.");

    while (count--)   // 开始遍历每一行进行读取
    {
        Weights wt{ DataType::kFLOAT, nullptr, 0 };
        uint32_t size;  // size表示每一行的数据数,第一行读取出来是3456个数据

        // Read name and type of blob
        std::string name; // name就是每一层的名称,如name = model.0.conv.conv.weights ;
        input >> name >> std::dec >> size;   // 读取name与size参数,格式是十进制
        wt.type = DataType::kFLOAT;

        // Load blob--使用reinterpret_cast强制将void*转换为uint32_t*类型
        //C++不允许直接将一个类型的指针赋值给另一个类型,除非进行显式类型转换。
        uint32_t* val = reinterpret_cast<uint32_t*>(malloc(sizeof(val) * size)); // 分配空间
        for (uint32_t x = 0, y = size; x < y; ++x)
        {
            input >> std::hex >> val[x];  // 循环遍历读取权重值,数据格式是十六进制--hex
        }
        wt.values = val; // 保存权重值

        wt.count = size;
        weightMap[name] = wt;
    }

    return weightMap;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值