解决Tensorflow sess.run导致的内存溢出问题


Posted in Python onFebruary 05, 2020

下面是调用模型进行批量测试的代码(出现溢出),开始以为导致溢出的原因是数据读入方式问题引起的,用了tf , PIL和cv等方式读入图片数据,发现越来越慢,内存占用飙升,调试时发现是sess.run这里出了问题(随着for循环进行速度越来越慢)。

# Creates graph from saved GraphDef
  create_graph(pb_path)
 
  # Init tf Session
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True
  sess = tf.Session(config=config)
  init = tf.global_variables_initializer()
  sess.run(init)
 
 
  input_image_tensor = sess.graph.get_tensor_by_name("create_inputs/batch:0") 
  output_tensor_name = sess.graph.get_tensor_by_name("conv6/out_1:0") 
 
 
  for filename in os.listdir(image_dir):
    image_path = os.path.join(image_dir, filename)
 
    start = time.time()
    image_data = cv2.imread(image_path)
    image_data = cv2.resize(image_data, (w, h))
    image_data_1 = image_data - IMG_MEAN
    input_image = np.expand_dims(image_data_1, 0)
 
    raw_output_up = tf.image.resize_bilinear(output_tensor_name, size=[h, w], align_corners=True) 
    raw_output_up = tf.argmax(raw_output_up, axis=3)
    
 
    predict_img = sess.run(raw_output_up, feed_dict={input_image_tensor: input_image})    # 1,height,width
    predict_img = np.squeeze(predict_img)   # height, width 
 
    voc_palette = visual.make_palette(3)
    masked_im = visual.vis_seg(image_data, predict_img, voc_palette)
    cv2.imwrite("%s_pred.png" % (save_dir + filename.split(".")[0]), masked_im)
 
 
    print(time.time() - start)
 
  print(">>>>>>Done")

下面是解决溢出问题的代码(将部分代码放在for循环外

# Creates graph from saved GraphDef
  create_graph(pb_path)
 
  # Init tf Session
  config = tf.ConfigProto()
  config.gpu_options.allow_growth = True
  sess = tf.Session(config=config)
  init = tf.global_variables_initializer()
  sess.run(init)
 
  input_image_tensor = sess.graph.get_tensor_by_name("create_inputs/batch:0") 
  output_tensor_name = sess.graph.get_tensor_by_name("conv6/out_1:0") 
  
##############################################################################################################
  raw_output_up = tf.image.resize_bilinear(output_tensor_name, size=[h, w], align_corners=True) 
  raw_output_up = tf.argmax(raw_output_up, axis=3)
##############################################################################################################
 
  for filename in os.listdir(image_dir):
    image_path = os.path.join(image_dir, filename)
 
    start = time.time()
    image_data = cv2.imread(image_path)
    image_data = cv2.resize(image_data, (w, h))
    image_data_1 = image_data - IMG_MEAN
    input_image = np.expand_dims(image_data_1, 0)
    
    predict_img = sess.run(raw_output_up, feed_dict={input_image_tensor: input_image})    # 1,height,width
    predict_img = np.squeeze(predict_img)   # height, width 
 
    voc_palette = visual.make_palette(3)
    masked_im = visual.vis_seg(image_data, predict_img, voc_palette)
    cv2.imwrite("%s_pred.png" % (save_dir + filename.split(".")[0]), masked_im)
    print(time.time() - start)
 
  print(">>>>>>Done")

总结:

在迭代过程中, 在sess.run的for循环中不要加入tensorflow一些op操作,会增加图节点,否则随着迭代的进行,tf的图会越来越大,最终导致溢出;

建议不要使用tf.gfile.FastGFile(image_path, 'rb').read()读入数据(有可能会造成溢出),用opencv之类读取。

以上这篇解决Tensoflow sess.run导致的内存溢出问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python encode和decode的妙用
Sep 02 Python
python线程锁(thread)学习示例
Dec 04 Python
python 计算两个日期相差多少个月实例代码
May 24 Python
利用python实现简单的邮件发送客户端示例
Dec 23 Python
django传值给模板, 再用JS接收并进行操作的实例
May 28 Python
Python的log日志功能及设置方法
Jul 11 Python
Python二维码生成识别实例详解
Jul 16 Python
Pycharm创建项目时如何自动添加头部信息
Nov 14 Python
Mac PyCharm中的.gitignore 安装设置教程
Apr 16 Python
django数据模型中null和blank的区别说明
Sep 02 Python
忆童年!用Python实现愤怒的小鸟游戏
Jun 07 Python
python读取并查看npz/npy文件数据以及数据显示方法
Apr 14 Python
解决TensorFlow训练内存不断增长,进程被杀死问题
Feb 05 #Python
浅谈tensorflow之内存暴涨问题
Feb 05 #Python
对Tensorflow中Device实例的生成和管理详解
Feb 04 #Python
关于windows下Tensorflow和pytorch安装教程
Feb 04 #Python
django3.02模板中的超链接配置实例代码
Feb 04 #Python
tensorflow自定义激活函数实例
Feb 04 #Python
pytorch对梯度进行可视化进行梯度检查教程
Feb 04 #Python
You might like
索尼SONY SRF-S83/84电路分析和打磨
2021/03/02 无线电
真正面向对象编程:PHP5.01发布
2006/10/09 PHP
MVC模式的PHP实现
2006/10/09 PHP
php面向对象全攻略 (五) 封装性
2009/09/30 PHP
PHP 登录完成后如何跳转上一访问页面
2014/01/14 PHP
PHP提高编程效率的20个要点
2015/09/23 PHP
PHP编写RESTful接口的方法
2016/02/21 PHP
javascript不同页面传值的改进版
2008/09/30 Javascript
JS 事件绑定函数代码
2010/04/28 Javascript
JS实现下拉框的动态添加(附效果)
2013/04/03 Javascript
在页面中js获取光标/鼠标的坐标及光标的像素坐标
2013/11/11 Javascript
JavaScript对IE操作的经典代码(推荐)
2014/03/10 Javascript
jQuery获取复选框被选中数量及判断选择值的方法详解
2016/05/25 Javascript
jQuery插件扩展操作入门示例
2017/01/16 Javascript
浅谈Express异步进化史
2017/09/09 Javascript
详解vue使用vue-layer-mobile组件实现toast,loading效果
2018/08/31 Javascript
js使用Promise实现简单的Ajax缓存
2018/11/14 Javascript
webpack file-loader和url-loader的区别
2019/01/15 Javascript
微信小程序-API接口安全详解
2019/07/16 Javascript
JavaScript判断浏览器版本的方法
2019/11/03 Javascript
详解vue或uni-app的跨域问题解决方案
2020/02/21 Javascript
jQuery实现滑动开关效果
2020/08/02 jQuery
对python中return和print的一些理解
2017/08/18 Python
详解python中asyncio模块
2018/03/03 Python
python中类的属性和方法介绍
2018/11/27 Python
对python 自定义协议的方法详解
2019/02/13 Python
Tensorflow tf.tile()的用法实例分析
2020/05/22 Python
施华洛世奇意大利官网:SWAROVSKI意大利
2018/07/23 全球购物
万豪国际住宅与别墅集团:Homes & Villas by Marriott International
2020/10/08 全球购物
自考生毕业自我鉴定
2013/10/10 职场文书
初三政治教学反思
2014/01/30 职场文书
打造高效课堂实施方案
2014/03/22 职场文书
法人委托书范本格式
2014/09/15 职场文书
2015年度物业公司工作总结
2015/04/27 职场文书
门卫管理制度范本
2015/08/05 职场文书
《狮子和鹿》教学反思
2016/02/16 职场文书