code: def main(args):
      # build the model from a config file and a checkpoint file
      model = init_detector(
          args.config, args.checkpoint, device=args.device
      )
      # test a single image
      pytorch_results = inference_detector(model, args.img)
      keep = pytorch_results.pred_instances.scores >= args.score_thr
      pytorch_results.pred_instances = pytorch_results.pred_instances[
          keep
      ]
  
      # init visualizer
      visualizer = VISUALIZERS.build(model.cfg.visualizer)
      # the dataset_meta is loaded from the checkpoint and
      # then pass to the model in init_detector
      visualizer.dataset_meta = model.dataset_meta
  
      # show the results
      img = mmcv.imread(args.img)
      img = mmcv.imconvert(img, "bgr", "rgb")
      pt_out_file = None
      ts_out_file = None
      if args.work_dir is not None:
          os.makedirs(args.work_dir, exist_ok=True)
          pt_out_file = os.path.join(
              args.work_dir, "pytorch_result.png"
          )
          ts_out_file = os.path.join(
              args.work_dir, "torchserve_result.png"
          )
      visualizer.add_datasample(
          "pytorch_result",
          img.copy(),
          data_sample=pytorch_results,
          draw_gt=False,
          out_file=pt_out_file,
          show=True,
          wait_time=0,
      )
  
      url = (
          "http://"
          + args.inference_addr
          + "/predictions/"
          + args.model_name
      )
      with open(args.img, "rb") as image:
          response = requests.post(url, image)
      metainfo = pytorch_results.pred_instances.metainfo
      ts_results = align_ts_output(
          response.json(), metainfo, args.device
      )
  
      visualizer.add_datasample(
          "torchserve_result",
          img,
          data_sample=ts_results,
          draw_gt=False,
          out_file=ts_out_file,
          show=True,
          wait_time=0,
      )
  
      assert torch.allclose(
          pytorch_results.pred_instances.bboxes,
          ts_results.pred_instances.bboxes,
      )
      assert torch.allclose(
          pytorch_results.pred_instances.labels,
          ts_results.pred_instances.labels,
      )
      assert torch.allclose(
          pytorch_results.pred_instances.scores,
          ts_results.pred_instances.scores,
      )
  
file_path: mmdetection/tools/deployment/test_torchserver.py
imports: import os
  from argparse import ArgumentParser
  import mmcv
  import requests
  import torch
  from mmengine.structures import InstanceData
  from mmdet.apis import inference_detector, init_detector
  from mmdet.registry import VISUALIZERS
  from mmdet.structures import DetDataSample
label_desc: Use image node_id 3 for calling a global function with example usage: main(args) without return types