最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問(wèn)題

 更新時(shí)間:2020年02月05日 09:57:07   作者:hustwayne  
今天小編就為大家分享一篇解決Tensorflow sess.run導(dǎo)致的內(nèi)存溢出問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

下面是調(diào)用模型進(jìn)行批量測(cè)試的代碼(出現(xiàn)溢出),開(kāi)始以為導(dǎo)致溢出的原因是數(shù)據(jù)讀入方式問(wèn)題引起的,用了tf , PIL和cv等方式讀入圖片數(shù)據(jù),發(fā)現(xiàn)越來(lái)越慢,內(nèi)存占用飆升,調(diào)試時(shí)發(fā)現(xiàn)是sess.run這里出了問(wèn)題(隨著for循環(huán)進(jìn)行速度越來(lái)越慢)。

  # 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")

下面是解決溢出問(wèn)題的代碼(將部分代碼放在for循環(huán)外

  # 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")

總結(jié):

在迭代過(guò)程中, 在sess.run的for循環(huán)中不要加入tensorflow一些op操作,會(huì)增加圖節(jié)點(diǎn),否則隨著迭代的進(jìn)行,tf的圖會(huì)越來(lái)越大,最終導(dǎo)致溢出;

建議不要使用tf.gfile.FastGFile(image_path, 'rb').read()讀入數(shù)據(jù)(有可能會(huì)造成溢出),用opencv之類(lèi)讀取。

以上這篇解決Tensoflow sess.run導(dǎo)致的內(nèi)存溢出問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

南郑县| 天等县| 沛县| 临清市| 贵定县| 荥经县| 马边| 芦山县| 康保县| 集贤县| 高安市| 清河县| 景东| 枣强县| 顺平县| 铜山县| 兴城市| 苏尼特右旗| 忻城县| 开平市| 金溪县| 西乌珠穆沁旗| 宁强县| 泌阳县| 神池县| 达日县| 通渭县| 鄱阳县| 巩义市| 峨山| 上林县| 洪泽县| 时尚| 马山县| 白沙| 丹棱县| 邢台县| 沙坪坝区| 雷波县| 古交市| 长宁区|