我試圖將大量 (54K) 的 512x512x3 .png 影像讀取到一個陣列中,然后創建一個資料集并提供給 Keras 模型。我正在使用下面的代碼,但是我收到指向我代碼第四行的 cv2.OutofMemory 錯誤(大約影像 50K...)。我一直在閱讀有關它的一些內容,并且:我使用的是 64 位版本,并且無法調整影像大小,因為它是固定輸入表示。有什么可以從記憶體管理方面做的事情來使它作業嗎?
''' #Images (512x512x3) X_data = [] files = glob.glob ('C:\Users\77901677\Projects\images1\*.png') for myFile in files: image = cv2.imread (myFile) X_data.附加(影像)
dataset_image = np.array(X_data)
# Annontations (multilabel) 512x512x2
Y_data = []
files = glob.glob ('C:\\Users\\77901677\\Projects\\annotations1\\*.png')
for myFile in files:
mask = cv2.imread (myFile)
# Gets rid of first channel which is empty
mask = mask[:,:,1:]
Y_data.append (mask)
dataset_mask = np.array(Y_data)
'''
歡迎任何想法或建議
uj5u.com熱心網友回復:
您可以通過減少一個變數來減少記憶體,因為您目前有 2 倍的陣列。
您可以yield為此使用,從而創建一個生成器,它一次只加載一個檔案,而不是將其全部存盤在輔助變數中。
def myGenerator():
files = glob.glob ('C:\\Users\\77901677\\Projects\\annotations1\\*.png')
for myFile in files:
mask = cv2.imread (myFile)
# Gets rid of first channel which is empty
yield mask[:,:,1:]
# initialise your numpy array here
yData = np.zeros(NxHxWxC)
# initialise the generator
mygenerator = myGenerator() # create a generator
for I, data in enumerate(myGenerator):
yData[I,::] = data # load the data
但是,這對您來說并不是最佳選擇。如果您計劃在下一步中訓練模型,您肯定會遇到記憶體問題。在 keras 中,您還可以實作一個 Keras 序列生成器,它將在訓練階段將您的檔案分批(類似于這個產量生成器)加載到您的模型中。我在這里推薦這篇文章,它演示了它的簡單實作,它是我用于 keras/tf 模型管道的內容。
在為我們的模型提供大量資料時使用生成器是一種很好的做法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/337543.html
上一篇:構建用于引數預測的自動編碼器網路
