我在Tensorflow输入管道/数据生成器中遇到Exception
的问题。我自己的脚本tfdata_generator(...)
中的生成器函数generator.py
是通过fit_generator()
中的Keras main.py
方法调用的。生成器函数如下:
def tfdata_generator(...):
image_tensor = tf.data.TFRecordDataset(path_list)
height_tensor = tf.data.Dataset.from_tensor_slices(height_list)
label_tensor = tf.data.Dataset.from_tensor_slices(label_list)
dataset = dataset.map(map_func=lambda a, b: ...) # do several mappings
dataset = dataset.apply(tf.contrib.data.unbatch())
if is_training == 'True':
dataset = dataset.shuffle(buffer_size=count)
dataset = dataset.repeat()
dataset_batched = dataset.batch(batch_size=batch_size)
iterator = dataset_batched.make_one_shot_iterator()
next_batch = iterator.get_next()
with tf.Session() as sess:
while True:
try:
images, height, labels = sess.run(next_batch)
yield ({'input_images': images, 'input_height': height}), labels
except IndexError: # tf.errors.OutOfRangeError:
print('Finished training')
break
我在控制台中收到以下错误消息:
90/90 [==============================] - 63s 697ms/step - loss: 0.0171 - acc: 0.9889 - val_loss: 0.0026 - val_acc: 1.0000
Network Training Finished!
Saved model
Exception ignored in: <generator object tfdata_generator at 0x7ff7580f56d0>
Traceback (most recent call last):
File "/util/generator.py", line 233, in tfdata_generator
break
File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1587, in __exit__
self._default_graph_context_manager.__exit__(exec_type, exec_value, exec_tb)
File "/usr/lib64/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5233, in get_controller
context.context().context_switches.pop()
File "/usr/lib/python3.6/site-packages/tensorflow/python/eager/context.py", line 141, in pop
self.stack.pop()
IndexError: pop from empty list
Exception ignored in: <generator object tfdata_generator at 0x7ff7580f5d58>
Traceback (most recent call last):
File "/util/generator.py", line 233, in tfdata_generator
break
File "/usr/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1587, in __exit__
self._default_graph_context_manager.__exit__(exec_type, exec_value, exec_tb)
File "/usr/lib64/python3.6/contextlib.py", line 99, in __exit__
self.gen.throw(type, value, traceback)
File "/usr/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 5233, in get_controller
context.context().context_switches.pop()
File "/usr/lib/python3.6/site-packages/tensorflow/python/eager/context.py", line 141, in pop
self.stack.pop()
IndexError: pop from empty list
Process finished with exit code 0
在fit_generator()
方法之后,“ Network Training结束”和“ Saveed Moedl”被打印在主屏幕上。
问题: 显然,这是发电机退出后发生的典型例外。因此,异常也将被忽略。在这种情况下,我的程序将成功完成,因此无需在Python控制台中显示被忽略的异常。简直就是丑陋。 如何摆脱被忽略的异常的打印输出?
感谢您的帮助 克里斯托夫