I'm attempting to augment data using TF, but I've gone about things completely wrong. Can someone help me refactor my code?

So here's what I have written so far. You can probably see very quickly where things are going wrong. Please don't judge me too harshly.

# Adjust image brightness/contrast and add noise
pngs = ['foo.png', 'bar.png', ]
contrast_range = (0.9, 1.1)
brightness_range = (-0.025, 0.025)
noise_range = (0, 5)
augmentations_per_image = 5

with tf.Session() as sess:
    for png in tqdm(pngs):
        img = tf.image.decode_png(tf.read_file(png), channels=1)
        for aug_num in range(augmentations_per_image):
            # Get the particular augmentation values for this image
            delta_contrast = random.uniform(*contrast_range)
            delta_brightness = random.uniform(*brightness_range)
            delta_noise = random.uniform(*noise_range)

            # Generate the noise
            noise = tf.random_normal(
             shape=tf.shape(img), mean=0.0, stddev=delta_noise
            )
            noise = tf.cast(noise, tf.int32)

            # Apply the augmentations
            new_img = tf.image.adjust_contrast(img, delta_contrast)
            new_img = tf.image.adjust_brightness(new_img, delta_brightness)
            new_img = tf.cast(new_img, tf.int32)
            new_img = tf.add(new_img, noise)
            new_img = tf.clip_by_value(new_img, 0, 255)
            new_img = tf.cast(new_img, tf.uint8)

            # Save image
            new_img = tf.image.encode_png(new_img)
            filename = png.replace('.png', '_aug_{}.png'.format(aug_num))
            write_new_png = tf.write_file(filename, new_img)
            sess.run(write_new_png)

As you can probably tell, because I keep adding new graph nodes in the session, the program quickly grinds to a halt. From what I understand, I need to move the graph-defining parts outside of the session, and then pass each image through the pipeline, so to speak.

However, I'm not really sure how to do that. I tried spinning off the tf.___ bits into their own functions, but I ran into problems with those functions not being called, or sess.run complaining about return values. I then tried moving all the new_img bits outside of the session into the global scope, but then I couldn't figure out how to pass my img variable into that, so I kept getting NameErrors.

Basically, I'm just pretty clueless. I haven't really done much with TF, but my coworker left, and there's no one else to do it, so here I am. Can someone point me in the right direction for what I'm trying to do? I'm not asking for you to fix my code, I'd just like to get a sense of how I'm supposed to tackle this problem, and then I can handle the details myself.

Thanks, and apologies for the dumb question.

Posted Feb 20, 2019, 10:44 PM UTC

11
honeybooboo1989•

First of all, I think you need to read this documentation. It is pretty helpful. Yes, first you need to create your computational graph and then you can execute it. This page about data augmentation techniques in CNN will definitely help. As you can see on that page, the author first create the graph and in a session, by feeding data, he executes the graph and gets the result.

1