Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to store intermediate results? #103

Open
sourcedexter opened this issue Feb 29, 2020 · 2 comments
Open

is it possible to store intermediate results? #103

sourcedexter opened this issue Feb 29, 2020 · 2 comments

Comments

@sourcedexter
Copy link

I have been trying to find out how and when the code stores the image. I wanted to know if it's possible to store results, say,for every 100 iterations?
if yes, how can I do it?

@frostbitten
Copy link

frostbitten commented May 25, 2020

I looked into doing this myself a while back. I couldn't find a way to make it work with the default lbfgs optimizer. I think it's related to: https://stackoverflow.com/questions/44685228/how-to-get-loss-function-history-using-tf-contrib-opt-scipyoptimizerinterface

I thought there would have been a way since it displays a notice every print_iterations but that's built into the optimizer.

Then I realized with the adam optimizer the printing of iterations was coded into a loop right there in neural_style.py. But, I didn't want to bother with the adam optimizer because I don't know why. I honestly haven't tried it at all.

Anyway, that's old news as I just now tried implementing output during the print_iteration step with the adam optimizer and it was a success!

result

I am not as impressed with the results from adam as lbfgs, though adam was super fast:

adam @ 3000 iterations
result

lbfgs @ 1000 iterations
result

Find the modifications I did below to get intermediate results. These are just the edits, not the whole files.

neural_style.py

#replacement function
def minimize_with_adam(sess, net, optimizer, init_img, loss, content_img):
  if args.verbose: print('\nMINIMIZING LOSS USING: ADAM OPTIMIZER')
  train_op = optimizer.minimize(loss)
  init_op = tf.global_variables_initializer()
  sess.run(init_op)
  sess.run(net['input'].assign(init_img))
  iterations = 0
  while (iterations < args.max_iterations):
    sess.run(train_op)
    if iterations % args.print_iterations == 0 and args.verbose:
      curr_loss = loss.eval()
      print("At iterate {}\tf=  {}".format(iterations, curr_loss))
      output_img = sess.run(net['input'])
      if args.original_colors:
        output_img = convert_to_original_colors(np.copy(content_img), output_img)
      write_image_output_i(iterations, output_img)
    iterations += 1

#new function
def write_image_output_i(iterations, output_img):
  out_dir = os.path.join(args.img_output_dir, args.img_name)
  maybe_make_directory(out_dir)
  img_path = os.path.join(out_dir, args.img_name+'_'+str(iterations)+'.png')
  write_image(img_path, output_img)

stylize_image.sh

echo "Rendering stylized image. This may take a while..."
python neural_style.py \
--content_img "${content_filename}" \
--content_img_dir "${content_dir}" \
--style_imgs "${style_filename}" \
--style_imgs_dir "${style_dir}" \
--device "${device}" \
--optimizer adam \
--print_iterations 10 \
--verbose;

@sourcedexter
Copy link
Author

thanks, I will try this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants