code: def do_visualize(
      model, model_path, nr_visualize=100, output_dir="output"
  ):
      """
      Visualize some intermediate results (proposals, raw predictions) inside the pipeline.
      """
      df = get_train_dataflow()
      df.reset_state()
  
      pred = OfflinePredictor(
          PredictConfig(
              model=model,
              session_init=SmartInit(model_path),
              input_names=["image", "gt_boxes", "gt_labels"],
              output_names=[
                  "generate_{}_proposals/boxes".format(
                      "fpn" if cfg.MODE_FPN else "rpn"
                  ),
                  "generate_{}_proposals/scores".format(
                      "fpn" if cfg.MODE_FPN else "rpn"
                  ),
                  "fastrcnn_all_scores",
                  "output/boxes",
                  "output/scores",
                  "output/labels",
              ],
          )
      )
  
      if os.path.isdir(output_dir):
          shutil.rmtree(output_dir)
      fs.mkdir_p(output_dir)
      with tqdm.tqdm(total=nr_visualize) as pbar:
          for idx, dp in itertools.islice(enumerate(df), nr_visualize):
              img, gt_boxes, gt_labels = (
                  dp["image"],
                  dp["gt_boxes"],
                  dp["gt_labels"],
              )
  
              (
                  rpn_boxes,
                  rpn_scores,
                  all_scores,
                  final_boxes,
                  final_scores,
                  final_labels,
              ) = pred(img, gt_boxes, gt_labels)
  
              # draw groundtruth boxes
              gt_viz = draw_annotation(img, gt_boxes, gt_labels)
              # draw best proposals for each groundtruth, to show recall
              proposal_viz, good_proposals_ind = draw_proposal_recall(
                  img, rpn_boxes, rpn_scores, gt_boxes
              )
              # draw the scores for the above proposals
              score_viz = draw_predictions(
                  img,
                  rpn_boxes[good_proposals_ind],
                  all_scores[good_proposals_ind],
              )
  
              results = [
                  DetectionResult(*args)
                  for args in zip(
                      final_boxes,
                      final_scores,
                      final_labels,
                      [None] * len(final_labels),
                  )
              ]
              final_viz = draw_final_outputs(img, results)
  
              viz = tpviz.stack_patches(
                  [gt_viz, proposal_viz, score_viz, final_viz], 2, 2
              )
  
              if os.environ.get("DISPLAY", None):
                  tpviz.interactive_imshow(viz)
              cv2.imwrite("{}/{:03d}.png".format(output_dir, idx), viz)
              pbar.update()
  
imports: import argparse
  import itertools
  import numpy
  import os
  import shutil
  import tensorflow
  import cv2
  import tqdm
  import tensorpack.utils.viz
  from tensorpack.predict import MultiTowerOfflinePredictor, OfflinePredictor, PredictConfig
  from tensorpack.tfutils import SmartInit, get_tf_version_tuple
  from tensorpack.tfutils.export import ModelExporter
  from tensorpack.utils import fs, logger
  from dataset import DatasetRegistry, register_coco, register_balloon
  from config import config
  from config import finalize_configs
  from data import get_eval_dataflow, get_train_dataflow
  from eval import DetectionResult, multithread_predict_dataflow, predict_image
  from modeling.generalized_rcnn import ResNetC4Model, ResNetFPNModel
  from viz import draw_annotation, draw_final_outputs, draw_predictions, draw_proposal_recall, draw_final_outputs_blackwhite
label_desc: Use image node_id 1 for calling a global function with example usage: do_visualize(model, model_path, nr_visualize, output_dir) without return types