2017-10-18 82 views
0

我已經提到了這個問題,但我不太瞭解Mr.rrrr先生提供的第二種方法 。克服Graphdef在張量流中不能大於2GB - 圖像轉換與tf

overcome Graphdef cannot be larger than 2GB in tensorflow

基本上,我試圖用TF內置的圖像上的圖像變換方法。我遇到了標題中提供的錯誤。 另外,我是否需要不斷爲每個迭代創建一個新的會話? 目前,這個過程有點慢,我不知道如何加快速度。

import tensorflow as tf 
import os 
from scipy.ndimage import imread 
from scipy.misc import imresize, imshow 
import matplotlib.pyplot as plt 


for fish in Fishes: 
    fish_images = os.listdir(os.path.join('C:\\Users\\Moondra\\Desktop\\Fishes', fish)) # get the image files 
    os.makedirs(SAVE_DIR + fish, exist_ok = True) 
    for num, fish_image in enumerate(fish_images): 
     image =imread(os.path.join('C:\\Users\\Moondra\\Desktop\\Fishes', fish, fish_image)) 
     new_img =tf.image.adjust_brightness(image, .4) #image transformation 
     with tf.Session() as sess: 

      new_image =sess.run(new_img) 
      imsave(os.path.join(SAVE_DIR, fish, fish +str(num)+'.jpg'), new_image) 

回答

1

這不是應該如何使用TF。

  1. 您應該創建一次圖
  2. 您應該創建會話一次

您當前的代碼在循環中執行這兩個操作,從而導致緩慢和內存問題。問題在於這樣一個事實:TF不是命令式語言,所以

new_img =tf.image.adjust_brightness(image, .4) #image transformation 

不上image.This功能的應用程序在圖形形成操作,並將參照本操作new_img。所以每次你調用這個函數,你的圖形都會增長。

因此,在僞代碼應該是:

create placeholder for image name 
create transformed image op - new_img 
create session 
for each image 
    call in a session new_img op, providing path to the placehodler using feed_dict 
相關問題