[英]Keras program taking too much memory
我一直試圖建立一個輸出圖像二進制散列的網絡。 為此,我並排使用兩個Vgg-19網絡,並訓練傳遞兩個圖像,以便如果圖像相似則最終哈希值更近,反之亦然。 我正在使用具有12 GB RAM的Geforce GTX 1080,以下是用於訓練模型的代碼段:
#positive images
prim_model.fit(data[index][0], temp_label, epochs=1, verbose=0)
sec_model.fit(data[index][i], temp_label, epochs=1, verbose=0)
model_vars._calculate_binary([prim_model, sec_model], [index, 0, index, i])
#negative images
prim_model.fit(data[index][0], temp_label, epochs=1, verbose=0)
sec_model.fit(data[index][i], temp_label, epochs=1, verbose=0)
model_vars._calculate_binary([prim_model, sec_model], [index, 0, index, i])
這里的model_vars是一個對象,其中包含模型的所有重要變量,例如
U = a tensor of shape (64, 3200) where 64 is binary bits of output and 3200 is number of images and U represents the output of all the images from prim_model(first model)
V = a tensor of same shape which holds output of sec_model
B = a tensor of shape(16, 3200) storing the final binary values of output
現在,在每次擬合操作之后(即圖像成對傳遞,我們要向其生成哈希,而另一幅包含一個相似圖像和一個負圖像data [index] [0]是目標圖像,而在一個fit數據[index] [ i]將包含相似的圖像,而在另一種情況下,它將包含相似的圖像)。 這里的Kbit值是64現在,通過一對后,我正在使用calculate_binary函數計算B張量
for index in xrange(3200):
Q = some_calculations (a 2-d tenor of shape(16, 3200)
Q_star_c = tf.reshape(tf.transpose(Q)[:, (index)], [self.kbit, 1] ) #extracting a column from Q
U_star_c = #A column extracted from U
V_star_c = #A column extracted from V
self.U_1 = tf.concat( [ self.U[:, 0:index], self.U[:, index+1: self.total_images]] , axis=1) #Removing the column extracted above from the original now the size of U_1 is (16, 3199)
self.V_1 = #same as above
self.B = #slicing the original B tensor
#Now doing some calcultion to calculate B_star_c (binary value of index'th image
B_star_c = tf.scalar_mul(-1, \
tf.sign(tf.add(tf.matmul(tf.scalar_mul(2, self.B), \
tf.add(tf.matmul(self.U_1, U_star_c, transpose_a=True), tf.matmul(self.V_1, V_star_c, transpose_a=True)) ) , Q_star_c)) )
#Now combining the final generated binary column to the original Binary tensor making the size of B to be (16, 3200) again
self.B = tf.concat( [ self.B[:, 0:index], tf.concat( [B_star_c, self.B[:, index:self.total_images]], axis=1)], axis=1)
現在,在擬合超過100/3200張圖像之后,我的代碼內存不足。 這是由calculate_binary函數引起的(導致當我停止使用它時問題就解決了)當我使用htop來查看內存狀態時,它顯示了完整的32GB / 32GB消耗,甚至使用了交換空間。 我如何減少內存增加的問題。(我也曾嘗試將代碼移至numpy數組,但仍會出現相同的問題)
我想這是self.B的tf.concat。
因為每次函數運行時,它都會將self.B串聯起來。 您可以通過在每次迭代中打印其張量來檢查哪個張量導致了內存錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.