text: import os
  os.environ['KMP_DUPLICATE_LIB_OK']='TRUE'
  
  import torch
  torch.cuda.current_device()
  import torch.optim as optim
  
  from painter import *
  
  # img_path:输入的图片
  # canvas_color：画板的背景颜色
  # max_m_strokes和max_divide:控制笔划大小，画作最后的细致程度
  # renderer：画作的风格
  # renderer——checkpoint_dir：权重文件路径
  # Decide which device we want to run on
  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  
  def optimize_x(pt):
  
      pt._load_checkpoint()
      pt.net_G.eval()
  
      print('begin drawing...')
  
      PARAMS = np.zeros([1, 0, pt.rderr.d], np.float32)
  
      if pt.rderr.canvas_color == 'white':
          CANVAS_tmp = torch.ones([1, 3, 128, 128]).to(device)
      else:
          CANVAS_tmp = torch.zeros([1, 3, 128, 128]).to(device)
  
      for pt.m_grid in range(1, pt.max_divide + 1):
  
          pt.img_batch = utils.img2patches(pt.img_, pt.m_grid).to(device)
          pt.G_final_pred_canvas = CANVAS_tmp
  
          pt.initialize_params()
          pt.x_ctt.requires_grad = True
          pt.x_color.requires_grad = True
          pt.x_alpha.requires_grad = True
          utils.set_requires_grad(pt.net_G, False)
  
          pt.optimizer_x = optim.RMSprop([pt.x_ctt, pt.x_color, pt.x_alpha], lr=pt.lr, centered=True)
  
          pt.step_id = 0
          for pt.anchor_id in range(0, pt.m_strokes_per_block):
              pt.stroke_sampler(pt.anchor_id)
              iters_per_stroke = 40
              for i in range(iters_per_stroke):
                  pt.G_pred_canvas = CANVAS_tmp
  
                  # update x
                  pt.optimizer_x.zero_grad()
  
                  pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1)
                  pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1)
                  pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1)
  
                  pt._forward_pass()
                  pt._drawing_step_states()
                  pt._backward_x()
  
                  pt.x_ctt.data = torch.clamp(pt.x_ctt.data, 0.1, 1 - 0.1)
                  pt.x_color.data = torch.clamp(pt.x_color.data, 0, 1)
                  pt.x_alpha.data = torch.clamp(pt.x_alpha.data, 0, 1)
  
                  pt.optimizer_x.step()
                  pt.step_id += 1
  
          v = pt._normalize_strokes(pt.x)
          PARAMS = np.concatenate([PARAMS, np.reshape(v, [1, -1, pt.rderr.d])], axis=1)
          CANVAS_tmp = pt._render(PARAMS)[-1]
          CANVAS_tmp = utils.img2patches(CANVAS_tmp, pt.m_grid + 1, to_tensor=True).to(device)
  
      pt._save_stroke_params(PARAMS)
      pt.final_rendered_images = pt._render(PARAMS)
      pt._save_rendered_images()
  
  