我正在尝试创建和训练一个识别图像中区域的网络。我已经把我的网络建立在deep learning MNIST tutorial的基础上,但是,我遗漏了一个完全连接的层(现在)。在
我的网络(尺寸反映了我正在训练128 x 128像素的输入图像):wConv1 = self.weightVariable([5, 5, 1, 32])
bConv1 = self.biasVariable([32])
x = tf.placeholder(tf.float32, [None, 16384])
yPrime = tf.placeholder(tf.float32, [None, 16384])
xImage = tf.reshape(self.x, [-1, 128, 128, 1])
#Convolutional Layer 1
hConv1 = tf.nn.relu(conv2d(xImage, wConv1) + bConv1)
hPool1 = maxPool(hConv1, 2)
# Convolutional Layer 2
wConv2 = weightVariable([5, 5, 32, 64])
bConv2 = biasVariable([64])
hConv2 = tf.nn.relu(conv2d(hPool1, wConv2) + bConv2)
hPool2 = maxPool(hConv2, 2)
# Fully Connected Layer
wFc1 = weightVariable([32 * 32 * 64, 16384])
bFc1 = biasVariable([16384])
hPool2Flat = tf.reshape(hPool2, [-1, 32 * 32 * 64])
hFc1 = tf.nn.relu(tf.matmul(hPool2Flat, wFc1) + bFc1)
# Dropout layer
keepProb = tf.placeholder(tf.float32)
hFc1Drop = tf.nn.dropout(hFc1, keepProb)
# Readout layer
y = tf.nn.softmax(tf.matmul(hPool2Flat, wFc1) + bFc1)
# Training
crossEntropy = tf.reduce_mean(-tf.reduce_sum(yPrime * tf.log(y + 1e-10), reduction_indices=[1]))
trainStep = tf.train.AdamOptimizer(learningRate).minimize(crossEntropy)
# Evaluation
p = tf.placeholder(tf.float32, [None, 16384])
q = tf.placeholder(tf.float32, [None, 16384])
correctPrediction = tf.equal(p, q)
accuracy = tf.reduce_mean(tf.cast(correctPrediction, tf.float32))
saver = tf.train.Saver(tf.trainable_variables())
# Additional functions used:
def weightVariable(self, shape, name):
initial = tf.truncated_normal(shape, stddev=0.1, name=name)
return tf.Variable(initial)
def biasVariable(self, shape, name):
initial = tf.constant(0.1, shape=shape, name=name)
return tf.Variable(initial)
def conv2d(self, x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def maxPool(self, x, poolSize):
return tf.nn.max_pool(x, ksize=[1, poolSize, poolSize, 1], strides=[1, poolSize, poolSize, 1], padding='SAME')
# Saves the current state of the network to disk
def saveNetwork(self, step=None):
folder = os.path.dirname(self.saverPath)
if not os.path.isdir(folder):
os.mkdir(folder)
if step is None:
self.saver.save(self.sess, self.saverPath, write_meta_graph=False)
else:
self.saver.save(self.sess, self.saverPath, global_step=step, write_meta_graph=False)
我能够很好地初始化和训练网络;我已经运行了10000次迭代,监视了进度,并验证了输出图像与我期望的一致。我的问题是,无论是在培训结束时还是在培训期间的检查点,我都无法保存模型。当执行保存图形的调用时,python脚本挂起,几分钟后,代码139退出,据我所知,这与内存不足或试图访问不可用的内存有关。在
我还创建了一个单层网络(基于MNIST tutorial),可以很好地训练。我能够在检查点和培训完成后保存图形。在
我做了一个粗略的计算,图形变量应该会占用大约4GB的内存,尽管我知道TensorFlow确实比预期的消耗更多的内存。我运行的是Ubuntu16.04,电脑有64GB的内存。在训练和保存期间,进程的峰值是消耗24gb的内存(根据资源监视器),这仍然远远低于可用的内存量。我也用ubuntu14.04复制了这个。在
我尝试过更小的批量,希望减少内存占用,甚至每一步只有4个图像。它仍然无法保存检查点。在
我对TensorFlow还是个新手,所以我不知道下一步该去哪里,我需要一些建议。如您所见,我将saver设置为只保存训练变量,希望这样可以减少它试图保存的文件的大小(这样做可以将简单网络的图形文件大小从4GB减小到2GB)。是不是我试图将太大的文件保存到磁盘(硬盘空间不应该是个问题,它保存到的驱动器是一个2 TB的硬盘)?当试图写入磁盘时,Python不能处理内存中那么大的文件吗?我是否认为这是一个内存问题,因为Python正在以代码139退出?在