parameters: (args, callback=None)
docstring: Train a neural style network.
      Args are from argparse and control input, output, hyper-parameters.
      callback allows for display of training progress.
docstring_summary: Train a neural style network.
      Args are from argparse and control input, output, hyper-parameters.
      callback allows for display of training progress.
docstring_tokens: Train a neural style network . Args are from argparse and control input output hyper - parameters . callback allows for display of training progress .
function: def train_nstyle(args, callback=None):
      """Train a neural style network.
      Args are from argparse and control input, output, hyper-parameters.
      callback allows for display of training progress.
      """
      # input
      dev = mx.gpu(args.gpu) if args.gpu >= 0 else mx.cpu()
      content_np = PreprocessContentImage(args.content_image, args.max_long_edge)
      style_np = PreprocessStyleImage(args.style_image, shape=content_np.shape)
      size = content_np.shape[2:]
  
      # model
      Executor = namedtuple('Executor', ['executor', 'data', 'data_grad'])
  
      model_module =  importlib.import_module('model_' + args.model)
      style, content = model_module.get_symbol()
      gram, gscale = style_gram_symbol(size, style)
      model_executor = model_module.get_executor(gram, content, size, dev)
      model_executor.data[:] = style_np
      model_executor.executor.forward()
      style_array = []
      for i in range(len(model_executor.style)):
          style_array.append(model_executor.style[i].copyto(mx.cpu()))
  
      model_executor.data[:] = content_np
      model_executor.executor.forward()
      content_array = model_executor.content.copyto(mx.cpu())
  
      # delete the executor
      del model_executor
  
      style_loss, content_loss = get_loss(gram, content)
      model_executor = model_module.get_executor(
          style_loss, content_loss, size, dev)
  
      grad_array = []
      for i in range(len(style_array)):
          style_array[i].copyto(model_executor.arg_dict["target_gram_%d" % i])
          grad_array.append(mx.nd.ones((1,), dev) * (float(args.style_weight) / gscale[i]))
      grad_array.append(mx.nd.ones((1,), dev) * (float(args.content_weight)))
  
      print([x.asscalar() for x in grad_array])
      content_array.copyto(model_executor.arg_dict["target_content"])
  
      # train
      # initialize img with random noise
      img = mx.nd.zeros(content_np.shape, ctx=dev)
      img[:] = mx.rnd.uniform(-0.1, 0.1, img.shape)
  
      lr = mx.lr_scheduler.FactorScheduler(step=args.lr_sched_delay,
              factor=args.lr_sched_factor)
  
      optimizer = mx.optimizer.NAG(
          learning_rate = args.lr,
          wd = 0.0001,
          momentum=0.95,
          lr_scheduler = lr)
      optim_state = optimizer.create_state(0, img)
  
      logging.info('start training arguments %s', args)
      old_img = img.copyto(dev)
      clip_norm = 1 * np.prod(img.shape)
      tv_grad_executor = get_tv_grad_executor(img, dev, args.tv_weight)
  
      for e in range(args.max_num_epochs):
          img.copyto(model_executor.data)
          model_executor.executor.forward()
          model_executor.executor.backward(grad_array)
          gnorm = mx.nd.norm(model_executor.data_grad).asscalar()
          if gnorm > clip_norm:
              model_executor.data_grad[:] *= clip_norm / gnorm
  
          if tv_grad_executor is not None:
              tv_grad_executor.forward()
              optimizer.update(0, img,
                               model_executor.data_grad + tv_grad_executor.outputs[0],
                               optim_state)
          else:
              optimizer.update(0, img, model_executor.data_grad, optim_state)
          new_img = img
          eps = (mx.nd.norm(old_img - new_img) / mx.nd.norm(new_img)).asscalar()
  
          old_img = new_img.copyto(dev)
          logging.info('epoch %d, relative change %f', e, eps)
          if eps < args.stop_eps:
              logging.info('eps < args.stop_eps, training finished')
              break
  
          if callback:
              cbdata = {
                  'eps': eps,
                  'epoch': e+1,
              }
          if (e+1) % args.save_epochs == 0:
              outfn = args.output_dir + 'e_'+str(e+1)+'.jpg'
              npimg = new_img.asnumpy()
              SaveImage(npimg, outfn, args.remove_noise)
              if callback:
                  cbdata['filename'] = outfn
                  cbdata['img'] = npimg
          if callback:
              callback(cbdata)
  
      final_fn = args.output_dir + '/final.jpg'
      SaveImage(new_img.asnumpy(), final_fn)
function_tokens: def train_nstyle ( args , callback = None ) : # input dev = mx . gpu ( args . gpu ) if args . gpu >= 0 else mx . cpu ( ) content_np = PreprocessContentImage ( args . content_image , args . max_long_edge ) style_np = PreprocessStyleImage ( args . style_image , shape = content_np . shape ) size = content_np . shape [ 2 : ] # model Executor = namedtuple ( 'Executor' , [ 'executor' , 'data' , 'data_grad' ] ) model_module = importlib . import_module ( 'model_' + args . model ) style , content = model_module . get_symbol ( ) gram , gscale = style_gram_symbol ( size , style ) model_executor = model_module . get_executor ( gram , content , size , dev ) model_executor . data [ : ] = style_np model_executor . executor . forward ( ) style_array = [ ] for i in range ( len ( model_executor . style ) ) : style_array . append ( model_executor . style [ i ] . copyto ( mx . cpu ( ) ) ) model_executor . data [ : ] = content_np model_executor . executor . forward ( ) content_array = model_executor . content . copyto ( mx . cpu ( ) ) # delete the executor del model_executor style_loss , content_loss = get_loss ( gram , content ) model_executor = model_module . get_executor ( style_loss , content_loss , size , dev ) grad_array = [ ] for i in range ( len ( style_array ) ) : style_array [ i ] . copyto ( model_executor . arg_dict [ "target_gram_%d" % i ] ) grad_array . append ( mx . nd . ones ( ( 1 , ) , dev ) * ( float ( args . style_weight ) / gscale [ i ] ) ) grad_array . append ( mx . nd . ones ( ( 1 , ) , dev ) * ( float ( args . content_weight ) ) ) print ( [ x . asscalar ( ) for x in grad_array ] ) content_array . copyto ( model_executor . arg_dict [ "target_content" ] ) # train # initialize img with random noise img = mx . nd . zeros ( content_np . shape , ctx = dev ) img [ : ] = mx . rnd . uniform ( - 0.1 , 0.1 , img . shape ) lr = mx . lr_scheduler . FactorScheduler ( step = args . lr_sched_delay , factor = args . lr_sched_factor ) optimizer = mx . optimizer . NAG ( learning_rate = args . lr , wd = 0.0001 , momentum = 0.95 , lr_scheduler = lr ) optim_state = optimizer . create_state ( 0 , img ) logging . info ( 'start training arguments %s' , args ) old_img = img . copyto ( dev ) clip_norm = 1 * np . prod ( img . shape ) tv_grad_executor = get_tv_grad_executor ( img , dev , args . tv_weight ) for e in range ( args . max_num_epochs ) : img . copyto ( model_executor . data ) model_executor . executor . forward ( ) model_executor . executor . backward ( grad_array ) gnorm = mx . nd . norm ( model_executor . data_grad ) . asscalar ( ) if gnorm > clip_norm : model_executor . data_grad [ : ] *= clip_norm / gnorm if tv_grad_executor is not None : tv_grad_executor . forward ( ) optimizer . update ( 0 , img , model_executor . data_grad + tv_grad_executor . outputs [ 0 ] , optim_state ) else : optimizer . update ( 0 , img , model_executor . data_grad , optim_state ) new_img = img eps = ( mx . nd . norm ( old_img - new_img ) / mx . nd . norm ( new_img ) ) . asscalar ( ) old_img = new_img . copyto ( dev ) logging . info ( 'epoch %d, relative change %f' , e , eps ) if eps < args . stop_eps : logging . info ( 'eps < args.stop_eps, training finished' ) break if callback : cbdata = { 'eps' : eps , 'epoch' : e + 1 , } if ( e + 1 ) % args . save_epochs == 0 : outfn = args . output_dir + 'e_' + str ( e + 1 ) + '.jpg' npimg = new_img . asnumpy ( ) SaveImage ( npimg , outfn , args . remove_noise ) if callback : cbdata [ 'filename' ] = outfn cbdata [ 'img' ] = npimg if callback : callback ( cbdata ) final_fn = args . output_dir + '/final.jpg' SaveImage ( new_img . asnumpy ( ) , final_fn )