Tensorflow,尝试和除了不处理异常

O. *_*olm 9 python image tensorflow

我是张力流的新手,我在这里遇到了一个恼人的问题.

我正在制作一个程序,用于加载tf.WholeFileReader.read(image_name_queue)从tfrecord文件中获取的图像"原始数据" ,然后对其进行解码tf.image.decode_jpeg(raw_data, channels=3),然后将其传递给一个矢量化它的函数.

主要代码

logging.info('setting up folder')
create_image_data_folder()
save_configs()

logging.info('creating graph')
filename_queue = tf.train.string_input_producer([
                                             configs.TFRECORD_IMAGES_PATH],
                                             num_epochs=1)

image_tensor, name_tensor = read_and_decode(filename_queue)
image_batch_tensor, name_batch_tensor = tf.train.shuffle_batch(
                                        [image_tensor, name_tensor],
                                        configs.BATCH_SIZE,
                                        1000 + 3 * configs.BATCH_SIZE,
                                        min_after_dequeue=1000)
image_embedding_batch_tensor = configs.IMAGE_EMBEDDING_FUNCTION(image_batch_tensor)

init = tf.initialize_all_variables()
init_local = tf.initialize_local_variables()
logging.info('starting session')
with tf.Session().as_default() as sess:
    sess.run(init)
    sess.run(init_local)
    tf.train.start_queue_runners()

    logging.info('vectorizing')
    data_points = []
    for _ in tqdm(xrange(get_n_batches())):
        name_batch = sess.run(name_batch_tensor)
        image_embedding_batch = sess.run(image_embedding_batch_tensor)
        for vector, name in zip(list(image_embedding_batch), name_batch):
            data_points.append((vector, name))

logging.info('saving')
save_pkl_file(data_points, 'vectors.pkl')
Run Code Online (Sandbox Code Playgroud)

read_and_decode函数

def read_and_decode(tfrecord_file_queue):
    logging.debug('reading image and decodes it from queue')
    reader = tf.TFRecordReader()
    _, serialized_example = reader.read(tfrecord_file_queue)
    features = tf.parse_single_example(serialized_example,
        features={
            'image': tf.FixedLenFeature([], tf.string),
            'name': tf.FixedLenFeature([], tf.string)
        }
    )
    image = process_image_data(features['image'])

    return image, features['name']
Run Code Online (Sandbox Code Playgroud)

代码正在运行,但最终它遇到了一个错误的非jpeg文件,并且引发了错误并且程序停止运行.

错误

InvalidArgumentError (see above for traceback): Invalid JPEG data, size 556663
Run Code Online (Sandbox Code Playgroud)

我想跳过这些"错误".我试着用try和包围代码except.

新代码

for _ in tqdm(xrange(get_n_batches())):
    try:
        name_batch = sess.run(name_batch_tensor)
        image_embedding_batch = sess.run(image_embedding_batch_tensor)
        for vector, name in zip(list(image_embedding_batch), name_batch):
            data_points.append((vector, name))
    except Exception as e:
        logging.warning('error occured: {}'.format(e))
Run Code Online (Sandbox Code Playgroud)

当我再次运行程序时出现相同的错误,try并且except 似乎没有处理错误.

我该如何处理这些异常?另外,如果你看到我误解了张量流"结构",请提及.

小智 0

问题是,使用“ except Exception”,您只能捕获从泛型类 Exception 继承的异常,而不是所有异常。如果你想从张量流中捕获特定的异常,你可以尝试:

try:
    # Your code
except tf.errors.InvalidArgumentError as e
    logging.warning('error occured: {}'.format(e))
Run Code Online (Sandbox Code Playgroud)

如果你想捕获任何异常:

except: # Catch all exception
    logger.exception('error occured")
Run Code Online (Sandbox Code Playgroud)

  • 这对我不起作用(同样的错误) - 即使尝试捕获任何异常也会失败。 (5认同)