cysmith / neural-style-tf

TensorFlow (Python API) implementation of Neural Style
GNU General Public License v3.0
3.11k stars 828 forks source link

is it possible to store intermediate results? #103

Open sourcedexter opened 4 years ago

sourcedexter commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

thanks, I will try this