解决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 相关文章推荐
Windows下使Python2.x版本的解释器与3.x共存的方法
Oct 25 Python
python脚本实现xls(xlsx)转成csv
Apr 10 Python
windows及linux环境下永久修改pip镜像源的方法
Nov 28 Python
python机器学习理论与实战(六)支持向量机
Jan 19 Python
分析python动态规划的递归、非递归实现
Mar 04 Python
使用Python监视指定目录下文件变更的方法
Oct 15 Python
Python2和Python3中urllib库中urlencode的使用注意事项
Nov 26 Python
django认证系统 Authentication使用详解
Jul 22 Python
解决Django Static内容不能加载显示的问题
Jul 28 Python
如何基于线程池提升request模块效率
Apr 18 Python
Python简单实现词云图代码及步骤解析
Jun 04 Python
Python3 + Appium + 安卓模拟器实现APP自动化测试并生成测试报告
Jan 27 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删除txt文件指定行及按行读取txt文档数据的方法
2017/01/30 PHP
php7 图形用户界面GUI 开发示例
2020/02/22 PHP
javascript 在网页中的运用(asp.net)
2009/11/23 Javascript
Jquery Ajax的Get方式时需要注意URL地方
2011/04/07 Javascript
JS仿flash上传头像效果实现代码
2011/07/18 Javascript
jq实现酷炫的鼠标经过图片翻滚效果
2014/03/12 Javascript
图文详解Javascript中的上下文和作用域
2017/02/15 Javascript
js实现1,2,3,5数字按照概率生成
2017/09/12 Javascript
Angular CLI 安装和使用教程
2017/09/13 Javascript
原生JS上传大文件显示进度条 php上传文件代码
2020/03/27 Javascript
jquery实现左右轮播切换效果
2018/01/01 jQuery
vue路由拦截及页面跳转的设置方法
2018/05/24 Javascript
clipboard.js在移动端复制失败的解决方法
2018/06/13 Javascript
解决vue移动端适配问题
2018/12/12 Javascript
JavaScript实现点击图片换背景
2020/11/20 Javascript
python编写网页爬虫脚本并实现APScheduler调度
2014/07/28 Python
使用IPython来操作Docker容器的入门指引
2015/04/08 Python
在Python的Bottle框架中使用微信API的示例
2015/04/23 Python
详解Python 多线程 Timer定时器/延迟执行、Event事件
2019/06/27 Python
Python中一个for循环循环多个变量的示例
2019/07/16 Python
Python (Win)readline和tab补全的安装方法
2019/08/27 Python
Python生成个性签名图片获取GUI过程解析
2019/12/16 Python
Python 元组拆包示例(Tuple Unpacking)
2019/12/24 Python
详解python os.path.exists判断文件或文件夹是否存在
2020/11/16 Python
python 窃取摄像头照片的实现示例
2021/01/08 Python
HTML5 video标签(播放器)学习笔记(二):播放控制
2015/04/24 HTML / CSS
美国在线家居装饰店:Belle&June
2018/10/24 全球购物
Magee 1866官网:Donegal粗花呢外套和大衣专家
2019/11/01 全球购物
万户网络JAVA程序员岗位招聘笔试试卷
2013/01/08 面试题
Delphi工程师笔试题
2013/09/21 面试题
培训自我鉴定
2014/01/31 职场文书
《画家乡》教学反思
2014/04/22 职场文书
校园元旦活动总结
2014/07/09 职场文书
幼儿教师继续教育培训心得体会
2016/01/19 职场文书
Python3.8官网文档之类的基础语法阅读
2021/09/04 Python
Python列表的索引与切片
2022/04/07 Python