国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

解決Tensorflow sess.run導致的內存溢出問題

2020-02-15 21:23:38
字體:
來源:轉載
供稿:網友

下面是調用模型進行批量測試的代碼(出現溢出),開始以為導致溢出的原因是數據讀入方式問題引起的,用了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")            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 剑川县| 万荣县| 新巴尔虎左旗| 莱西市| 如东县| 漳浦县| 文水县| 抚顺市| 丹阳市| 宜都市| 长顺县| 江永县| 当雄县| 永福县| 巴青县| 宝应县| 绿春县| 蕲春县| 贡嘎县| 土默特左旗| 大同市| 雷州市| 石狮市| 涞源县| 溧水县| 古丈县| 沅陵县| 菏泽市| 泌阳县| 玉山县| 松溪县| 西乌珠穆沁旗| 县级市| 玉龙| 萨迦县| 泰顺县| 清水县| 象山县| 永安市| 社旗县| 永安市|