Kaggle踩坑指南--2.Function call stack: distributed_function->distributed_function

在訓練模型model.fit的時候報錯了,直接上圖
在這裏插入圖片描述
其實這裏有兩個問題,一個是NotFundError, 一個是Function call stack。 但是,一般會直接在意最後一個,而忽略第一個。

一、 Function call stack

請教了大神**@日月光華**,設置GPU顯存按需申請

import tensorflow as tf
gpu = tf.config.experimental.list_physical_devices(device_type='GPU')
assert len(gpu) == 1
tf.config.experimental.set_memory_growth(gpu[0], True)

二、 NotFundError:Tensor(“arg_0:0”, shape=(), dtype=string); No such file or directory

其實,在大神回答我之前,我把第二個問題解決了以後,第一個問題直接就消失了。。。然後我看到有的小夥伴出現了第一個問題的同時,上面其實也有其他的問題,所以我覺得解決第二個問題上出現的問題纔是重點。

很明顯,我這裏是由於dataset內部數據錯誤導致的,需要重頭開始查看在哪裏數據讀取出現的問題。
我的思路如下:

1> 查看是否每個路徑都是準確的路徑

e.g. 我之前讀取jpg圖像路徑直接採用

 images = glob.glob('./data/cats/*')

因爲大致略看了路徑直接認爲裏面都是圖像,但實際讀取的時候裏面可能包含其他的文件,所以最好是做到準確:

 glob.glob('./data/cats/*.jpg')
2> 路徑是否去掉了PosixPath

輸出路徑看是否前面有其他標識,如
在這裏插入圖片描述
若有,則整體轉化爲str

train_images= [str(path) for path in train_images]
3> 數據是否真正的讀取到了

對於圖像數據,可以用以下代碼讀取數據,並顯示圖片

import random
import IPython.display as display
for n in range(3):
    image_index = random.choice(range(len(train_images)))
    display.display(display.Image(all_image_paths[image_index]))
4> 構建tensor時是否正確

這裏以Kaggle踩坑指南–1. 數據集的讀取和圖像預處理如下構建方式爲例

import tensorflow as tf
def _pre_read(img_filename, label):
    image = tf.io.read_file(img_filename)
    image = tf.image.decode_jpeg(image,channels=3)
    image = tf.image.rgb_to_grayscale(image)
    image = tf.image.resize(image,[200,200])
    image = tf.reshape(image,[200,200,1])
    image = tf.image.per_image_standardization(image) #或者:image = image/255
    label = tf.reshape(label,[1])
    return image, label
    
BATCH_SIZE = 32
train_dataset = tf.data.Dataset.from_tensor_slices((train_images,train_labels)) #這裏的train_images是圖像路徑
#在notebook中可以直接在下方新建cell中輸入train_dataset 查看是否構建正確
train_dataset = train_dataset.map(_pre_read)
train_dataset = train_dataset.shuffle(300)
train_dataset = train_dataset.repeat()
train_dataset = train_dataset.batch(BATCH_SIZE)

當然,以上主要針對個人的代碼,我的問題出現在了1>和4>中。
經驗就是,大不了全部輸入dataset都查看一下,看是哪一個讀取數據或者轉數據格式出了問題

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