我已經建立了一個圖像分類的玩具模型。該程序結構鬆散,如cifar10 tutorial。訓練開始正常,但最終程序崩潰。爲了防止某些操作被添加到圖表中,我已經完成了該圖表,並且在tensorboard中它看起來很棒,但是沒有失敗,它最終會凍結並強制重新啓動(或者等待最終重新啓動)。退出使它看起來像一個GPU內存問題,但模型很小,應該適合。如果我分配完整的GPU內存(可以提供另外4GB),它仍然會崩潰。爲什麼這個tensorflow代碼崩潰?
數據是存儲在tfrecords文件中的256x256x3圖像和標籤。培訓功能的代碼如下所示:
def train():
with tf.Graph().as_default():
global_step = tf.contrib.framework.get_or_create_global_step()
train_images_batch, train_labels_batch = distorted_inputs(batch_size=BATCH_SIZE)
train_logits = inference(train_images_batch)
train_batch_loss = loss(train_logits, train_labels_batch)
train_op = training(train_batch_loss, global_step, 0.1)
merged = tf.summary.merge_all()
saver = tf.train.Saver(tf.global_variables())
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.75)
sess_config=tf.ConfigProto(gpu_options=gpu_options)
sess = tf.Session(config=sess_config)
train_summary_writer = tf.summary.FileWriter(
os.path.join(ROOT, 'logs', 'train'), sess.graph)
init = tf.global_variables_initializer()
sess.run(init)
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
tf.Graph().finalize()
for i in range(5540):
start_time = time.time()
summary, _, batch_loss = sess.run([merged, train_op, train_batch_loss])
duration = time.time() - start_time
train_summary_writer.add_summary(summary, i)
if i % 10 == 0:
msg = 'batch: {} loss: {:.6f} time: {:.8} sec/batch'.format(
i, batch_loss, str(time.time() - start_time))
print(msg)
coord.request_stop()
coord.join(threads)
sess.close()
損失和培訓OP是cross_entropy分別亞當優化:
def loss(logits, labels):
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits, name='cross_entropy_per_example')
xentropy_mean = tf.reduce_mean(xentropy, name='cross_entropy')
tf.add_to_collection('losses', xentropy_mean)
return xentropy_mean
def training(loss, global_step, learning_rate):
optimizer = tf.train.AdamOptimizer(learning_rate)
train_op = optimizer.minimize(loss, global_step=global_step)
return train_op
而且批與
def distorted_inputs(batch_size):
filename_queue = tf.train.string_input_producer(
['data/train.tfrecords'], num_epochs=None)
reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)
features = tf.parse_single_example(serialized_example,
features={'label': tf.FixedLenFeature([], tf.int64),
'image': tf.FixedLenFeature([], tf.string)})
label = features['label']
label = tf.cast(label, tf.int32)
image = tf.decode_raw(features['image'], tf.uint8)
image = (tf.cast(image, tf.float32)/255) - 0.5
image = tf.reshape(image, shape=[256, 256, 3])
# data augmentation
image = tf.image.random_flip_up_down(image)
image = tf.image.random_flip_left_right(image)
print('filling the queue with {} images ' \
'before starting to train'.format(MIN_QUEUE_EXAMPLES))
return _generate_batch(image, label, MIN_QUEUE_EXAMPLES, BATCH_SIZE)
和
產生def _generate_batch(image, label,
min_queue_examples=MIN_QUEUE_EXAMPLES,
batch_size=BATCH_SIZE):
images_batch, labels_batch = tf.train.shuffle_batch(
[image, label], batch_size=batch_size,
num_threads=12, capacity=min_queue_examples + 3 * BATCH_SIZE,
min_after_dequeue=min_queue_examples)
tf.summary.image('images', images_batch)
return images_batch, labels_batch
我錯過了什麼?
嘿,不太確定你在問什麼......什麼是不工作? – rmeertens
如果您正在運行Windows,則可以在windbg中加載崩潰轉儲,並檢查哪個模塊失敗。 –
該程序運行良好,直到它凍結我的機器,通常經過800次或更多的迭代(批量大小64或128,似乎並不重要)。所以我懷疑(GPU)內存泄漏(內存使用情況始終穩定),但我不清楚哪裏(如果這甚至是問題)。由於AFAIK沒有辦法在tensorflow程序運行過程中抓取詳細的GPU內存使用信息,所以我想知道那些具有更多張量流體驗的人比我看到我的代碼中有個罪魁禍首。我對圖模型還是有點不熟悉。如果有幫助,我在Tesla k40上運行Ubuntu 16.04,tf 1.0.0,python 3.5。 –