解决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实现ip查询示例
Mar 26 Python
Python中比较特别的除法运算和幂运算介绍
Apr 05 Python
Python的re模块正则表达式操作
May 25 Python
python3爬取各类天气信息
Feb 24 Python
在mac下查找python包存放路径site-packages的实现方法
Nov 06 Python
Python 实现Numpy中找出array中最大值所对应的行和列
Nov 26 Python
Python常用库大全及简要说明
Jan 17 Python
解决python -m pip install --upgrade pip 升级不成功问题
Mar 05 Python
浅析Django 接收所有文件,前端展示文件(包括视频,文件,图片)ajax请求
Mar 09 Python
mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法
Mar 19 Python
Python3 hashlib密码散列算法原理详解
Mar 30 Python
使用Django的JsonResponse返回数据的实现
Jan 15 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
php有效防止图片盗用、盗链的两种方法
2016/11/01 PHP
MAC下通过改apache配置文件切换php多版本的方法
2017/04/26 PHP
window.location.hash 使用说明
2010/11/08 Javascript
JavaScript中奇葩的假值示例应用
2014/03/11 Javascript
解决jquery版本冲突的有效方法
2014/09/02 Javascript
修改或扩展jQuery原生方法的代码实例
2015/01/13 Javascript
JavaScript实现同步于本地时间的动态时间显示方法
2015/02/02 Javascript
Jquery搜索父元素操作方法
2015/02/10 Javascript
js+html5绘制图片到canvas的方法
2015/06/05 Javascript
基于bootstrap3和jquery的分页插件
2015/07/31 Javascript
js显示动态时间的方法详解
2016/08/20 Javascript
js获取元素的偏移量offset简单方法(必看)
2017/07/05 Javascript
使用JavaScript实现在页面中显示距离2017年中秋节的天数
2017/09/26 Javascript
JS构造一个html文本内容成文件流形式发送到后台
2018/07/31 Javascript
JQuery特殊效果和链式调用操作示例
2019/05/13 jQuery
详解js中的几种常用设计模式
2020/07/16 Javascript
Element InputNumber 计数器的实现示例
2020/08/03 Javascript
[15:09]DOTA2国际邀请赛采访专栏:Loda
2013/08/06 DOTA
python在多玩图片上下载妹子图的实现代码
2013/08/13 Python
Python+django实现简单的文件上传
2016/08/17 Python
python 实现绘制整齐的表格
2019/11/18 Python
Python模块相关知识点小结
2020/03/09 Python
用Python 爬取猫眼电影数据分析《无名之辈》
2020/07/24 Python
pycharm永久激活超详细教程
2020/10/29 Python
德国童装购物网站:NICKI´S.com
2018/04/20 全球购物
Abbacino官网:包、钱包和女士配饰
2019/04/15 全球购物
delegate与普通函数的区别
2014/01/22 面试题
应聘护士自荐信
2013/10/21 职场文书
商务英语求职自荐信范文
2013/12/24 职场文书
十八大闭幕感言
2014/01/22 职场文书
光信息科学与技术专业职业生涯规划
2014/03/13 职场文书
党员学习正风肃纪思想汇报
2014/09/12 职场文书
思想纪律作风整顿剖析材料
2014/10/11 职场文书
迎新生晚会主持词
2015/06/30 职场文书
Python爬取科目四考试题库的方法实现
2021/03/30 Python
如何利用python实现Simhash算法
2022/06/28 Python