TF預測服務接口上線後內存泄漏'std::bad_alloc'等問題集錦

樓主環境:

	python3.6.8
	centos 7.2
	tensorflow 1.14.0

一 泄漏位置查找方法:

報錯部分原文:

	 terminate called after throwing an instance of 'std::bad_alloc'

工具:pympler

from pympler import tracker,summary,muppy

在web主程序添加:
memory_tracker = tracker.SummaryTracker()

在接口返回前添加:
memory_tracker.print_diff()

反人類辦法:把懷疑的模塊一個一個替換着註釋掉,找到內存泄漏模塊,
然後再進行代碼排查

二 由tf.train.Saver()引起的

在session開始前進行模型的重新設置,
tf.reset_default_graph()

解決辦法:

在with tf.Session() as sess: 之後同時也要在with的範圍以外(注意),添加

tf.reset_default_graph()

代碼來重置默認的圖,這樣就能解決下一步執行代碼
self.sess = tf.Session()
self.saver = tf.train.import_meta_graph(’./Model/model.ckpt.meta’)
self.saver.restore(self.sess, tf.train.latest_checkpoint(’./Model/’))

三 OOM問題

報錯原文:

tensorflow:OOM when allocating tensor with shape[225,256,256,36] and type float on /job:localhost/re

原因是GPU OOM內存不夠,因此可嘗試改成批處理。即將訓練的數據塊調小。

參考鏈接:
https://stackoverflow.com/questions/39076388/tensorflow-deep-mnist-resource-exhausted-oom-when-allocating-tensor-with-shape

https://github.com/tensorflow/tensorflow/issues/609

四 編碼問題

報錯部分原文:

SyntaxError: Non-UTF-8 code starting with '\xe5' in file

原因:編碼格式不對

把相應Python程序文件的編碼轉成UTF-8格式
1、在文件第一行添加# encoding:utf-8

或2.在文件第一行添加# -- coding: gbk --

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章