caffe2 first sight
来源:互联网 发布:网络服务提供商与数据 编辑:程序博客网 时间:2024/05/28 15:43
#include <boost/lexical_cast.hpp>// Caffe2#include <google/protobuf/text_format.h>#include "caffe2/core/context.h"#include "caffe2/core/operator.h"#include "caffe2/core/predictor.h"#include "caffe2/core/tensor.h"#include "caffe2/utils/math.h"#include <caffe2/core/context.h>#include <caffe2/core/context_gpu.h>#include <caffe2/core/workspace.h>#include <gtest/gtest.h>// OpenCV#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <stdio.h>#include <stdlib.h>#include <time.h>void setgpu(caffe2::NetDef& net, int id){ net.mutable_device_option()->set_device_type(1); net.mutable_device_option()->set_cuda_gpu_id(id); for(int i = 0; i < net.op_size(); ++i){ net.mutable_op(i)->mutable_device_option()->set_device_type(1); net.mutable_op(i)->mutable_device_option()->set_cuda_gpu_id(0); }}int main(int argc, char* argv[]){ std::cout <<"1" <<std::endl; //typedef caffe2::TensorCPU tensorType; typedef caffe2::TensorCUDA TensorType; // std::string image_filename = us::any_cast<std::string>(parsedArgs["image"]); // cv::Mat img = cv::imread(image_filename, CV_LOAD_IMAGE_COLOR); // cv::Mat img = cv::Mat(64,64,CV_32FC3,128); // cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );// Create a window for display. // cv::imshow( "Display w30indow", img2 ); // Show our image inside it. // cv::waitKey(0); // Wait for a keystroke in the window caffe2::NetDef init_net, predict_net; CAFFE_ENFORCE(ReadProtoFromFile("/home/amax/Yang/caffe2/models/init_net.pb", &init_net)); setgpu(init_net, 1); std::cout <<"2" <<std::endl; CAFFE_ENFORCE(ReadProtoFromFile("/home/amax/Yang/caffe2/models/predict_net.pb", &predict_net)); setgpu(predict_net, 1); std::cout <<"3" <<std::endl; std::vector<TensorType*> outputVec; int w,h,c; w=224; h=224; c=3; TensorType input(std::vector<int>({1, c, h, w})); //// TRYING TO ACCESS TENSOR DATA (RESULTS IN ERROR FOR caffe2::TensorCUDA) // caffe2::CPUContext cpu_context; // caffe2::TensorCPU cpu_input(input, &cpu_context); // float* input_data = cpu_input.template mutable_data<float>(); float* input_data = input.mutable_data<float>(); // create input image (simply a square of size 10x10 in a 64x64 image) // test image for segmentation /*int counter = 0; for (int x = 0; x < h; x++) for (int y = 0; y < w; y++) { for (int j = 0; j < c; j++) { input_data[counter] = 0; if (x>20 && x<30 && y>20 && y<30) input_data[counter] = 1; counter++; } }*/ std::cout <<"4" <<std::endl; // show input image cv::Mat img = cv::Mat(224,224,CV_32FC3); cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );// Create a window for display. cv::imshow( "Display window", img ); // Show our image inside it. cv::waitKey(0); // Wait for a keystroke in the window // run network clock_t t1,t2; std::vector<TensorType> inputVec{input}; caffe2::Workspace ws; t1=clock(); ws.RunNetOnce(init_net); t2=clock(); std::cout <<(double)(t2-t1) / CLOCKS_PER_SEC <<std::endl; ws.CreateNet(predict_net, true); std::cout << inputVec.size() << std::endl; for (auto i = 0; i < inputVec.size(); ++i) { auto* blob = ws.GetBlob(predict_net.external_input(i)); TensorType* tensor = blob->template GetMutable<TensorType>(); tensor->ResizeLike(inputVec[i]); tensor->ShareData(inputVec[i]); } std::cout <<"5" <<std::endl; t1=clock(); CAFFE_ENFORCE(ws.RunNet(predict_net.name())); t2=clock(); std::cout <<(double)(t2-t1) / CLOCKS_PER_SEC <<std::endl; t1=clock(); for(int i = 0; i < 1000; ++i) ws.RunNet(predict_net.name()); t2=clock(); std::cout <<(double)(t2-t1) / CLOCKS_PER_SEC/1000 <<std::endl; t1=clock(); ws.RunNet(predict_net.name()); t2=clock(); std::cout <<(double)(t2-t1) / CLOCKS_PER_SEC <<std::endl; std::cout <<"6" <<std::endl; return 1;}
阅读全文
1 0
- caffe2 first sight
- First Sight at Scrum
- first sight of ibatis
- Servlet first sight
- first sight@echo
- 一见钟情fall in love at first sight
- 279.Love at first sight. 一见钟情
- sight
- Caffe2
- Caffe2
- Apple iAd:first sight(第一眼感受)
- 波兰女诗人辛波丝卡Love at First Sight《一见钟情》中英文完整对照
- 关于caffe2
- Caffe2简介
- caffe2 介绍
- caffe2 安装
- Caffe2 入门教程
- 初识caffe2
- 一张图让你看明白Nettty EventLoop是个什么东东
- 原生js多级菜单启示录
- C语言作业-4-2念整数
- iOS 3DES加密解密(一行代码搞定)
- 在SSM下使用POI实现Excel表的导入/导出
- caffe2 first sight
- Kotlin简介
- Objective-C instancetype关键字
- win32学习11.资源
- linux 安装 eclipse
- 拷贝构造函数和赋值构造函数声明为私有的作用
- 计算机组成原理(第六章:计算机的运算方法)
- 关于数据检测类型的浅谈
- 全排列(java)