Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow, try and except doesn't handle exception

I'm new to tensorflow and I've been stuck at an annoying problem here.

I'm making a program that loads image "raw data" taken with tf.WholeFileReader.read(image_name_queue) from a tfrecord file and then decodes it with tf.image.decode_jpeg(raw_data, channels=3) and then passes it through a function that vectorizes it.

main code

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')

read_and_decode function

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']

The code is working, but eventually it comes across a bad, non-jpeg file and an error this is raised and the program stops running.

error

InvalidArgumentError (see above for traceback): Invalid JPEG data, size 556663

I want to skip these "errors". I tried to surround the code with try and except.

new code

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))

When I run the program again the same error occurs, the try and except doesn't seem to handle the error.

How can I handle these exceptions? Also, if you see that I've misunderstood the tensorflow "structure" please mention that.

like image 331
O. Edholm Avatar asked Nov 19 '16 12:11

O. Edholm


People also ask

Why is using a try-except block effective?

What is the reason for the try-except-else to exist? A try block allows you to handle an expected error. The except block should only catch exceptions you are prepared to handle. If you handle an unexpected error, your code may do the wrong thing and hide bugs.

Does try-except make code slower?

Making use of Python exception handling has a side effect, as well. Like, programs that make use try-except blocks to handle exceptions will run slightly slower, and the size of your code will increase.

Does try-except continue?

With try and except , even if an exception occurs, the process continues without terminating.

Does try have to be followed by except Python?

The Try and Except StatementsEdit. Python allows for errors and exceptions to be handled by the program. To do so, you'll need to use both the try and except statements. A minimal example is given below.


2 Answers

Well, to solve your issue use tf.data.experimental.ignore_errors available in v2.8.0

Use below transformation to produce a dataset that contains the same elements as the input, but silently drops any elements that caused an error. For example:

dataset = tf.data.Dataset.from_tensor_slices([1., 2., 0., 4.])

# Computing `tf.debugging.check_numerics(1. / 0.)` will raise an
InvalidArgumentError.
dataset = dataset.map(lambda x: tf.debugging.check_numerics(1. / x, "error"))

# Using `ignore_errors()` will drop the element that causes an error.
dataset =
    dataset.apply(tf.data.experimental.ignore_errors())  # ==> {1., 0.5, 0.2}

Refer documentation here

like image 170
sumitkanoje Avatar answered Oct 12 '22 14:10

sumitkanoje


I know this is not applicable to your example, but I stumbled upon a different scenario in which TensorFlow didn't seem to catch the exception, despite doing

try:
    # Run code that throw tensorflow error
except:
    print('This won't catch the exception...')

The thing that made the problem so hard to solve was that TensorFlow debugging pointed to the wrong line; it showed me that the error lied in the graph construction, rather than graph execution.

The concrete problem?

I tried to restore a model from a .meta file:

try:
    saver = tf.train.import_meta_graph('my_error_generating_model.meta') # tf throws err here
    graph = tf.get_default_graph()
except:
    print('This won't run')

with tf.Session() as sess:
    # This is where error is actually generated
    saver.restore(sess, tf.train.latest_checkpoint('./'))
    sess.run(...) # Propagating through graph generates a problem

The solution is, of course, to put your try-catch wrapper around the executing code instead!

like image 25
Andreas Forslöw Avatar answered Oct 12 '22 14:10

Andreas Forslöw