Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4460817
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: LINUX

2018-07-04 21:57:45

第一步,训练并保存模型:
saver_hello.py内容如下

点击(此处)折叠或打开

  1. import tensorflow as tf


  2. def helloFunc():
  3.     print("hellFunc")


  4. if __name__ == '__main__':
  5.     hello = tf.Variable(tf.constant('Hello World', name = "hello"))
  6.     #init = tf.initialize_all_variables() #deprecated
  7.     init = tf.global_variables_initializer()
  8.     sess = tf.Session()
  9.     sess.run(init)


  10.     saver = tf.train.Saver()
  11.     saver.save(sess, "./hello_model")



执行saver_hello.py
生成3个文件
hello_model.data-00000-of-00001  hello_model.index  hello_model.meta
(tensorflow) zm@linux-kl9l:~/workspace/flower/hello> ./saver_hello.py
(tensorflow) zm@linux-kl9l:~/workspace/flower/hello> ls
a.out  checkpoint  hello.cpp  hello_model.data-00000-of-00001  hello_model.index  hello_model.meta  restore_hello.py  saver_hello.py


第二步,加载模型,并测试:
restore_hello.py 内容如下 

点击(此处)折叠或打开

  1. import tensorflow as tf


  2. if __name__ == '__main__':
  3.     restore = tf.train.import_meta_graph("hello_model.meta")
  4.     sess = tf.Session()
  5.     restore.restore(sess, "hello_model")


  6.     print(sess.run(tf.get_default_graph().get_tensor_by_name("hello:0")))

执行
(tensorflow) zm@linux-kl9l:~/workspace/flower/hello> ./restore_hello.py
会打印出
b'Hello World'


opencv的加载方式如下:
main.cpp内容

点击(此处)折叠或打开

  1. #include <opencv2/opencv.hpp>

  2.     int
  3. main( int argc, char **argv )
  4. {
  5.     if (argc != 2)
  6.     {
  7.         printf( "argc %d != 2\n", argc );
  8.         exit(-1);
  9.     }

  10.     cv::dnn::Net net = cv::dnn::readNetFromTensorflow(argv[1]);

  11.     return 0;
  12. }


编译后加载:
./a.out model_data/model.ckpt-1000.index
作者:帅得不敢出门
阅读(3714) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~