Skip to content

Instantly share code, notes, and snippets.

@SubhadityaMukherjee
Created January 21, 2020 16:41
Show Gist options
  • Save SubhadityaMukherjee/7885f0aa1391c7d4c9da8e8f9a926eba to your computer and use it in GitHub Desktop.
Save SubhadityaMukherjee/7885f0aa1391c7d4c9da8e8f9a926eba to your computer and use it in GitHub Desktop.
done

run_deep_dream_simple

  • We have to first pre process the image to be able to work with the pre processed network. (Basically converts it into the format inception was trained)
  • We then convert it into a tensor and split the execution into parts
  • We simply use the deepdream function to calculate the losses
  • We use the deprocess function to convert it back into an image from a tensor
  • clear_output is just a function to make sure the notebook doesnt get flooded with outputs and only the latest output is kept
def run_deep_dream_simple(img, steps=100, step_size=0.01):

    img = tf.keras.applications.inception_v3.preprocess_input(img)
    img = tf.convert_to_tensor(img)
    step_size = tf.convert_to_tensor(step_size)
    steps_remaining = steps
    step = 0
    while steps_remaining:
        if steps_remaining > 100:
            run_steps = tf.constant(100)
        else:
            run_steps = tf.constant(steps_remaining)
        steps_remaining -= run_steps
        step += run_steps

        loss, img = deepdream(img, run_steps, tf.constant(step_size))

        display.clear_output(wait=True)
        show(deprocess(img))
        print("Step {}, loss {}".format(step, loss))

    result = deprocess(img)
    display.clear_output(wait=True)
    show(result)

    return result

dream_img = run_deep_dream_simple(img=original_img, steps=100, step_size=0.01)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment