libtorch 哪些函数比较常用?
如何打印模型?
// print register_module
// auto Tiny_Net = std::make_shared<VGG9>();
// print_modules(Tiny_Net)
void print_modules(const std::shared_ptr<torch::nn::Module> &module, size_t level = 0) {
auto tabs = [&](size_t num) {
for (size_t i = 0; i < num; i++) {
std::cout << "\t";
}
};
std::cout << module->name() << " (\n";
for (const auto& parameter : module->named_parameters()) {
tabs(level + 1);
std::cout << parameter.key() << '\t';
std::cout << parameter.value().sizes() << '\n';
}
tabs(level);
std::cout << ")\n";
}
//输入32x32 3通道图片
auto input = torch::rand({ 1,3,32,32 });
//输出
auto output_bilinear = torch::upsample_bilinear2d(input, { 8,8 }, false);
auto output_nearest = torch::upsample_nearest2d(input, { 5,5 });
auto output_avg = torch::adaptive_avg_pool2d(input, { 3,9 });
std::cout << output_bilinear << std::endl;
std::cout << output_nearest << std::endl;
std::cout << output_avg << std::endl;
libtorch 加载 pytorch 模块进行预测示例
void mat2tensor(const char * path, torch::Tensor &output)
{
//读取图片
cv::Mat img = cv::imread(path);
if (img.empty()) {
printf("load image failed!");
system("pause");
}
//调整大小
cv::resize(img, img, { 224,224 });
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
//浮点
img.convertTo(img, CV_32F, 1.0 / 255.0);
torch::TensorOptions option(torch::kFloat32);
auto img_tensor = torch::from_blob(img.data, { 1,img.rows,img.cols,img.channels() }, option);// opencv H x W x C torch C x H x W
img_tensor = img_tensor.permute({ 0,3,1,2 });// 调整 opencv 矩阵的维度使其和 torch 维度一致
//均值归一化
img_tensor[0][0] = img_tensor[0][0].sub_(0.485).div_(0.229);
img_tensor[0][1] = img_tensor[0][1].sub_(0.456).div_(0.224);
img_tensor[0][2] = img_tensor[0][2].sub_(0.406).div_(0.225);
output = img_tensor.clone();
}
int main()
{
torch::Tensor dog;
mat2tensor("dog.png", dog);
// Load model.
std::shared_ptr<torch::jit::script::Module> module = torch::jit::load("model.pt");
assert(module != nullptr);
std::cout << "ok\r\n" << std::endl;
// Create a vector of inputs.
std::vector<torch::jit::IValue> inputs;
torch::Tensor tensor = torch::rand({ 1, 3, 224, 224 });
inputs.push_back(dog);
// Execute the model and turn its output into a tensor.
at::Tensor output = module->forward(inputs).toTensor();
//加载标签文件
std::string label_file = "synset_words.txt";
std::fstream fs(label_file, std::ios::in);
if (!fs.is_open()) {
printf("label open failed!\r\n");
system("pause");
}
std::string line;
std::vector<std::string> labels;
while (std::getline(fs,line))
{
labels.push_back(line);
}
//排序 {1,1000} 矩阵取前10个元素(预测值),返回一个矩阵和一个矩阵的下标索引
std::tuple<torch::Tensor,torch::Tensor> result = output.topk(10, -1);
auto top_scores = std::get<0>(result).view(-1);//{1,10} 变成 {10}
auto top_idxs = std::get<1>(result).view(-1);
std::cout << top_scores << "\r\n" << top_idxs << std::endl;
//打印结果
for (int i = 0; i < 10; ++i) {
std::cout << "score: " << top_scores[i].item().toFloat() << "\t" << "label: " << labels[top_idxs[i].item().toInt()] << std::endl;
}
system("pause");
return 0;
]
torch::sort
torch::Tensor x = torch::rand({ 3,3 });
std::cout << x << std::endl;
//排序操作 true 大到小排序,false 小到大排序
auto out = x.sort(-1, true);
std::cout << std::get<0>(out) << "\r\n" << std::get<1>(out) << std::endl;
输出如下:
0.0855 0.4925 0.4323
0.8314 0.8954 0.0709
0.0996 0.3108 0.6845
[ Variable[CPUFloatType]{3,3} ]
0.4925 0.4323 0.0855
0.8954 0.8314 0.0709
0.6845 0.3108 0.0996
[ Variable[CPUFloatType]{3,3} ]
1 2 0
1 0 2
2 1 0
[ Variable[CPULongType]{3,3} ]
libtorch 哪些函数比较常用?的更多相关文章
- 总结js常用函数和常用技巧(持续更新)
学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...
- js常用函数和常用技巧
学习和工作的过程中总结的干货,包括常用函数.常用js技巧.常用正则表达式.git笔记等.为刚接触前端的童鞋们提供一个简单的查询的途径,也以此来缅怀我的前端学习之路. PS:此文档,我会持续更新. Aj ...
- Kotlin的高阶函数和常用高阶函数
Kotlin的高阶函数和常用高阶函数 文章来源:企鹅号 - Android先生 高阶函数的定义 将函数当做参数或者是返回值的函数 什么是高阶函数 可以看看我们常用的 函数: 首先我们可以知道, 是 的 ...
- 如果你也会C#,那不妨了解下F#(4):了解函数及常用函数
函数式编程其实就是按照数学上的函数运算思想来实现计算机上的运算.虽然我们不需要深入了解数学函数的知识,但应该清楚函数式编程的基础是来自于数学. 例如数学函数\(f(x) = x^2+x\),并没有指定 ...
- 分享一些关于PHP时间函数的常用时间
<?php // 各种时间函数 echo "现在:".date("Y-m-d H:i:s")."<br>"; echo & ...
- SendMessage函数的常用消息及其应用大全
来源:http://www.360doc.com/content/09/0814/10/19147_4907488.shtml,非常全面的解释. 文本框控件通常用于输入和编辑文字.它属于标准 Wind ...
- C# 8 函数 调用 常用类 时间 日期型
函数:能够独立完成某个功能的模块. 好处:1.结构更清析(编写.维护方便 ).2.代码重用.3.分工开发. 四要素:名称,输入(参数),输出(返回的类型),加工(函数体) 语法: 返回类型 函数名(参 ...
- 程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理 - by Glede
最近我们的cocos2d-x游戏项目已经进入了正式开发的阶段了,几个dev都辛苦码代码.cocos2d-x还是一套比较方便的api的,什么action啊.director啊.ccpoint啊都蛮便捷的 ...
- 程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理
原帖地址:http://www.cnblogs.com/buaashine/archive/2012/11/12/2765691.html 1.注意这是cocos2d-x中的函数,但大体上和cocos ...
随机推荐
- 基于Redis实现延时队列服务
背景 在业务发展过程中,会出现一些需要延时处理的场景,比如: a.订单下单之后超过30分钟用户未支付,需要取消订单 b.订单一些评论,如果48h用户未对商家评论,系统会自动产生一条默认评论 c.点我达 ...
- [gpio]Linux GPIO简单使用方式2-sysfs
转自:http://blog.csdn.net/cjyusha/article/details/50418862 在Linux嵌入式设备开发中,对GPIO的操作是最常用的,在一般的情况下,一般都有对应 ...
- CSS(五):背景、列表、超链接伪类、鼠标形状控制属性
一.背景属性 1.背景属性用来设置页面元素的背景样式. 2.常见背景属性 属性 描述 background-color 用来设置页面的背景色,取值如red,#ff0000 background-ima ...
- Hibernate Annotation 字段 默认值
http://emavaj.blog.163.com/blog/static/133280557201032262741999/ ——————————————————————————————————— ...
- MVC 使用IOC实现
实现步骤: 1. 实现IDependencyResolver接口并通过DependencyResolver.SetResolver告知MVC,将部分类型实例解析工作交由IoC容器来处理: using ...
- 使用jQuery模拟Google的自动提示效果
注意: 1.本功能使用SqlServler2000中的示例数据库Northwind,请打SP3或SP4补丁:2.请下载jQuery组件,河西FTP中有下载:3.本功能实现类似Google自动提示的效果 ...
- 【BZOJ】1046: [HAOI2007]上升序列(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1046 一直看错题....................... 这是要求位置的字典序啊QQQAAAQ ...
- 小结:A* & IDA* & 迭代深搜
概要: 在dfs中,如果答案的深度很小但是却很宽,而且bfs还不一定好做的情况下,我们就综合bfs的优点,结合dfs的思想,进行有限制的dfs.在这里A*.IDA*和迭代深搜都是对dfs的优化,因此放 ...
- word excel文件 存入数据库 实战
上传: private void Insert2017(HttpContext context) { if (context.Request.Files["fileword"].C ...
- pybot/robot命令参数说明【dos下执行命令pybot.bat --help查看】
Robot Framework -- A generic test automation framework Version: 3.0 (Python 3.4.0 on win32) Usage: r ...