背景:我正在嘗試制作一個 GAN 來從大型資料集生成影像,并且在加載訓練資料時遇到了 OOM 問題。為了解決這個問題,我試圖傳入一個檔案目錄串列,并僅在需要時將它們作為影像讀取。
問題:我不知道如何從張量本身決議出檔案名。如果有人對如何將張量轉換回串列或以某種方式遍歷張量有任何見解。或者,如果這是解決此問題的不好方法,請告訴我
相關代碼片段:
生成資料:注意:make_file_list()回傳我要讀取的所有影像的檔案名串列
data = make_file_list(base_dir)
train_dataset = tf.data.Dataset.from_tensor_slices(data).shuffle(BUFFER_SIZE).batch(BATCH_SIZE)
train(train_dataset, EPOCHS)
訓練功能:
def train(dataset, epochs):
plot_iteration = []
gen_loss_l = []
disc_loss_l = []
for epoch in range(epochs):
start = time.time()
for image_batch in dataset:
gen_loss, disc_loss = train_step(image_batch)
訓練步驟:
@tf.function
def train_step(image_files):
noise = tf.random.normal([BATCH_SIZE, noise_dim])
images = [load_img(filepath) for filepath in image_files]
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator(noise, training=True)
錯誤:
line 250, in train_step *
images = [load_img(filepath) for filepath in image_files]
OperatorNotAllowedInGraphError: Iterating over a symbolic `tf.Tensor` is not allowed: AutoGraph did convert this function. This might indicate you are trying to use an unsupported feature
uj5u.com熱心網友回復:
移除. @tf.function_ train_step如果你train_step用@tf.function 裝飾你的,Tensorflow 會嘗試將里面的 Python 代碼轉換train_step成一個執行圖,而不是在 Eager 模式下運行。執行圖提供了加速,但也對可以執行的運算子施加了一些限制(如錯誤所述)。
要繼續@tf.function,train_step您可以train先在函式中執行迭代和加載步驟,然后將已加載的影像作為引數傳遞給,train_step而不是嘗試直接在其中加載影像train_step
def train(dataset, epochs):
plot_iteration = []
gen_loss_l = []
disc_loss_l = []
for epoch in range(epochs):
start = time.time()
for image_batch in dataset:
images = [load_img(filepath) for filepath in image_batch ]
gen_loss, disc_loss = train_step(images)
@tf.function
def train_step(images):
noise = tf.random.normal([BATCH_SIZE, noise_dim])
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator(noise, training=True)
....
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/464828.html
