TensorFlow - 无法保存检查点:Python 退出代码 139
TensorFlow - Unable to save checkpoint: Python exit code 139
我正在尝试创建和训练一个网络来识别图像中的区域。我的网络基于 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)。我可以在检查点和训练完成后保存图表。
我做了一个粗略的计算,图形变量应该占用大约 4 GB 的内存,尽管我知道 TensorFlow 确实消耗了比预期更多的内存。我是 运行ning Ubuntu 16.04,PC 有 64 GB 内存。在训练和保存期间,进程消耗 24 GB 内存(根据资源监视器)达到峰值,这仍然远低于可用量。我还使用 Ubuntu 14.04.
复制了这个
我尝试了更小的批量大小,希望减少内存占用,甚至减少到每步只有 4 张图像。仍然无法保存检查点。
我对 TensorFlow 还是很陌生,所以我不确定下一步该去哪里,可以使用一些建议。如您所见,我已将保护程序设置为仅保存训练变量,希望这样可以减少它试图保存的文件的大小(这样做可以将我的简单网络的图形文件大小从 4 GB 减少到 2 GB ).是不是我试图将太大的文件保存到磁盘(硬盘驱动器 space 应该不是问题,它保存到的驱动器是一个 2 TB 的硬盘)?尝试写入磁盘时,Python 是否无法处理内存中那么大的文件?由于 Python 以代码 139 退出,我是否认为这是一个内存问题?
您的进程似乎因分段错误而崩溃。 tf.train.Saver.save()
method calls some C++ code that serializes all of your variables to a file. This serialization format has a 2GB limit for the largest tensor (because it serializes each variable into a Protocol Buffer, which has a 2GB maximum record size). Your weight variable wFc1
is 4GB in size. I suspect the failure is happening around this line; the fact that it crashes this way is a bug.
一种可能的解决方案是对大变量进行分片。例如:
wFc1_0 = weight_variable([32 * 32 * 64, 4096])
wFc1_1 = weight_variable([32 * 32 * 64, 4096])
wFc1_2 = weight_variable([32 * 32 * 64, 4096])
wFc1_3 = weight_variable([32 * 32 * 64, 4096])
# ...
# Readout layer
y = tf.nn.softmax(
tf.concat(1, [tf.matmul(hPool2Flat, wFc1_0),
tf.matmul(hPool2Flat, wFc1_1),
tf.matmul(hPool2Flat, wFc1_2),
tf.matmul(hPool2Flat, wFc1_3),
]) + bFc1)
这可能不是最有效的分片,因此值得在这里进行试验。由于您有大量 类,您可能会发现 TensorFlow 的一些 sampled loss functions(支持分片权重)效率更高。
我正在尝试创建和训练一个网络来识别图像中的区域。我的网络基于 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)。我可以在检查点和训练完成后保存图表。
我做了一个粗略的计算,图形变量应该占用大约 4 GB 的内存,尽管我知道 TensorFlow 确实消耗了比预期更多的内存。我是 运行ning Ubuntu 16.04,PC 有 64 GB 内存。在训练和保存期间,进程消耗 24 GB 内存(根据资源监视器)达到峰值,这仍然远低于可用量。我还使用 Ubuntu 14.04.
复制了这个我尝试了更小的批量大小,希望减少内存占用,甚至减少到每步只有 4 张图像。仍然无法保存检查点。
我对 TensorFlow 还是很陌生,所以我不确定下一步该去哪里,可以使用一些建议。如您所见,我已将保护程序设置为仅保存训练变量,希望这样可以减少它试图保存的文件的大小(这样做可以将我的简单网络的图形文件大小从 4 GB 减少到 2 GB ).是不是我试图将太大的文件保存到磁盘(硬盘驱动器 space 应该不是问题,它保存到的驱动器是一个 2 TB 的硬盘)?尝试写入磁盘时,Python 是否无法处理内存中那么大的文件?由于 Python 以代码 139 退出,我是否认为这是一个内存问题?
您的进程似乎因分段错误而崩溃。 tf.train.Saver.save()
method calls some C++ code that serializes all of your variables to a file. This serialization format has a 2GB limit for the largest tensor (because it serializes each variable into a Protocol Buffer, which has a 2GB maximum record size). Your weight variable wFc1
is 4GB in size. I suspect the failure is happening around this line; the fact that it crashes this way is a bug.
一种可能的解决方案是对大变量进行分片。例如:
wFc1_0 = weight_variable([32 * 32 * 64, 4096])
wFc1_1 = weight_variable([32 * 32 * 64, 4096])
wFc1_2 = weight_variable([32 * 32 * 64, 4096])
wFc1_3 = weight_variable([32 * 32 * 64, 4096])
# ...
# Readout layer
y = tf.nn.softmax(
tf.concat(1, [tf.matmul(hPool2Flat, wFc1_0),
tf.matmul(hPool2Flat, wFc1_1),
tf.matmul(hPool2Flat, wFc1_2),
tf.matmul(hPool2Flat, wFc1_3),
]) + bFc1)
这可能不是最有效的分片,因此值得在这里进行试验。由于您有大量 类,您可能会发现 TensorFlow 的一些 sampled loss functions(支持分片权重)效率更高。