【问题标题】:Keras program taking too much memoryKeras 程序占用太多内存
【发布时间】:2018-05-14 20:05:35
【问题描述】:

我一直在尝试构建一个输出图像二进制哈希的网络。 为此,我并排使用两个 Vgg-19 网络并训练传递两个图像,如果图像相似,则最终哈希值更接近,反之亦然。我正在使用带有 12 GB RAM 的 Geforce GTX 1080 这里有一段代码 sn-p 来训练模型:

#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

现在在每次拟合操作之后(即图像作为对传递,其中一个我们将生成哈希,另一个包括一个相似图像和一个负图像数据[索引] [0]是目标图像和一个拟合数据[ 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 数组仍然出现同样的问题)

【问题讨论】:

    标签: python tensorflow keras tensor


    【解决方案1】:

    我猜是self.B的tf.concat ...

    因为每次函数运行时,它都会将 self.B 越来越大。您可以通过在每次迭代中打印其大小来检查哪个张量导致了内存错误。

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 2017-09-01
      • 2013-01-10
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 2015-10-14
      • 2013-07-18
      • 2013-07-11
      相关资源
      最近更新 更多