We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
labelme2seg.py工具生成的png文件是全黑的
您好，我使用labelme进行标注，然后使用labelme2seg.py生成数据集，但是生成的png文件打开之后是全黑的，看不到标注的物体，麻烦帮忙看看原因，谢谢。

下面附上labelme生成的json文件：
{
  "version": "3.16.7",
  "flags": {},
  "shapes": [
    {
      "label": "test",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          130.10619469026545,
          275.87610619469024
        ],
        [
          261.0796460176991,
          419.2389380530973
        ],
        [
          492.9380530973451,
          430.74336283185835
        ],
        [
          507.98230088495563,
          297.99999999999994
        ],
        [
          348.6902654867256,
          183.84070796460176
        ],
        [
          191.16814159292034,
          236.93805309734512
        ]
      ],
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "ttt",
      "line_color": null,
      "fill_color": null,
      "points": [
        [
          211.52212389380526,
          482.9557522123893
        ],
        [
          247.80530973451323,
          602.4247787610619
        ],
        [
          412.4070796460177,
          623.6637168141592
        ],
        [
          446.0353982300884,
          513.929203539823
        ],
        [
          425.68141592920347,
          459.9469026548672
        ],
        [
          262.84955752212386,
          460.8318584070796
        ]
      ],
      "shape_type": "polygon",
      "flags": {}
    }
  ],
  "lineColor": [
    0,
    255,
    0,
    128
  ],
  "fillColor": [
    255,
    0,
    0,
    128
  ],
  "imagePath": "6.jpg",
  "imageData": "太多，省略",
  "imageHeight": 694,
  "imageWidth": 685
}

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pdseg/vis.py`
Content:
```
1 # coding: utf8
2 # copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #    http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 
20 import os
21 
22 # GPU memory garbage collection optimization flags
23 os.environ['FLAGS_eager_delete_tensor_gb'] = "0.0"
24 
25 import sys
26 import argparse
27 import pprint
28 import cv2
29 import numpy as np
30 import paddle.fluid as fluid
31 
32 from PIL import Image as PILImage
33 from utils.config import cfg
34 from reader import SegDataset
35 from models.model_builder import build_model
36 from models.model_builder import ModelPhase
37 from tools.gray2pseudo_color import get_color_map_list
38 
39 
40 def parse_args():
41     parser = argparse.ArgumentParser(description='PaddeSeg visualization tools')
42     parser.add_argument(
43         '--cfg',
44         dest='cfg_file',
45         help='Config file for training (and optionally testing)',
46         default=None,
47         type=str)
48     parser.add_argument(
49         '--use_gpu', dest='use_gpu', help='Use gpu or cpu', action='store_true')
50     parser.add_argument(
51         '--vis_dir',
52         dest='vis_dir',
53         help='visual save dir',
54         type=str,
55         default='visual')
56     parser.add_argument(
57         '--local_test',
58         dest='local_test',
59         help='if in local test mode, only visualize 5 images for testing',
60         action='store_true')
61     parser.add_argument(
62         'opts',
63         help='See config.py for all options',
64         default=None,
65         nargs=argparse.REMAINDER)
66     if len(sys.argv) == 1:
67         parser.print_help()
68         sys.exit(1)
69     return parser.parse_args()
70 
71 
72 def makedirs(directory):
73     if not os.path.exists(directory):
74         os.makedirs(directory)
75 
76 
77 def to_png_fn(fn):
78     """
79     Append png as filename postfix
80     """
81     directory, filename = os.path.split(fn)
82     basename, ext = os.path.splitext(filename)
83 
84     return basename + ".png"
85 
86 
87 def visualize(cfg,
88               vis_file_list=None,
89               use_gpu=False,
90               vis_dir="visual_predict",
91               ckpt_dir=None,
92               log_writer=None,
93               local_test=False,
94               **kwargs):
95     if vis_file_list is None:
96         vis_file_list = cfg.DATASET.TEST_FILE_LIST
97     dataset = SegDataset(
98         file_list=vis_file_list,
99         mode=ModelPhase.VISUAL,
100         data_dir=cfg.DATASET.DATA_DIR)
101 
102     startup_prog = fluid.Program()
103     test_prog = fluid.Program()
104     pred, logit = build_model(test_prog, startup_prog, phase=ModelPhase.VISUAL)
105     # Clone forward graph
106     test_prog = test_prog.clone(for_test=True)
107 
108     # Generator full colormap for maximum 256 classes
109     color_map = get_color_map_list(256)
110 
111     # Get device environment
112     place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
113     exe = fluid.Executor(place)
114     exe.run(startup_prog)
115 
116     ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir
117 
118     fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)
119 
120     save_dir = os.path.join('visual', vis_dir)
121     makedirs(save_dir)
122 
123     fetch_list = [pred.name]
124     test_reader = dataset.batch(dataset.generator, batch_size=1, is_test=True)
125     img_cnt = 0
126     for imgs, grts, img_names, valid_shapes, org_shapes in test_reader:
127         pred_shape = (imgs.shape[2], imgs.shape[3])
128         pred, = exe.run(
129             program=test_prog,
130             feed={'image': imgs},
131             fetch_list=fetch_list,
132             return_numpy=True)
133 
134         num_imgs = pred.shape[0]
135         # TODO: use multi-thread to write images
136         for i in range(num_imgs):
137             # Add more comments
138             res_map = np.squeeze(pred[i, :, :, :]).astype(np.uint8)
139             img_name = img_names[i]
140             res_shape = (res_map.shape[0], res_map.shape[1])
141             if res_shape[0] != pred_shape[0] or res_shape[1] != pred_shape[1]:
142                 res_map = cv2.resize(
143                     res_map, pred_shape, interpolation=cv2.INTER_NEAREST)
144             valid_shape = (valid_shapes[i, 0], valid_shapes[i, 1])
145             res_map = res_map[0:valid_shape[0], 0:valid_shape[1]]
146             org_shape = (org_shapes[i, 0], org_shapes[i, 1])
147             res_map = cv2.resize(
148                 res_map, (org_shape[1], org_shape[0]),
149                 interpolation=cv2.INTER_NEAREST)
150 
151             png_fn = to_png_fn(img_name)
152 
153             # colorful segment result visualization
154             vis_fn = os.path.join(save_dir, png_fn)
155             dirname = os.path.dirname(vis_fn)
156             makedirs(dirname)
157 
158             pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='P')
159             pred_mask.putpalette(color_map)
160             pred_mask.save(vis_fn)
161 
162             img_cnt += 1
163             print("#{} visualize image path: {}".format(img_cnt, vis_fn))
164 
165             # Use Tensorboard to visualize image
166             if log_writer is not None:
167                 # Calulate epoch from ckpt_dir folder name
168                 epoch = int(os.path.split(ckpt_dir)[-1])
169                 print("Tensorboard visualization epoch", epoch)
170 
171                 pred_mask_np = np.array(pred_mask.convert("RGB"))
172                 log_writer.add_image(
173                     "Predict/{}".format(img_name),
174                     pred_mask_np,
175                     epoch,
176                     dataformats='HWC')
177                 # Original image
178                 # BGR->RGB
179                 img = cv2.imread(
180                     os.path.join(cfg.DATASET.DATA_DIR, img_name))[..., ::-1]
181                 log_writer.add_image(
182                     "Images/{}".format(img_name),
183                     img,
184                     epoch,
185                     dataformats='HWC')
186                 # add ground truth (label) images
187                 grt = grts[i]
188                 if grt is not None:
189                     grt = grt[0:valid_shape[0], 0:valid_shape[1]]
190                     grt_pil = PILImage.fromarray(grt.astype(np.uint8), mode='P')
191                     grt_pil.putpalette(color_map)
192                     grt_pil = grt_pil.resize((org_shape[1], org_shape[0]))
193                     grt = np.array(grt_pil.convert("RGB"))
194                     log_writer.add_image(
195                         "Label/{}".format(img_name),
196                         grt,
197                         epoch,
198                         dataformats='HWC')
199 
200         # If in local_test mode, only visualize 5 images just for testing
201         # procedure
202         if local_test and img_cnt >= 5:
203             break
204 
205 
206 if __name__ == '__main__':
207     args = parse_args()
208     if args.cfg_file is not None:
209         cfg.update_from_file(args.cfg_file)
210     if args.opts:
211         cfg.update_from_list(args.opts)
212     cfg.check_and_infer()
213     print(pprint.pformat(cfg))
214     visualize(cfg, **args.__dict__)
215
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pdseg/vis.py b/pdseg/vis.py
--- a/pdseg/vis.py
+++ b/pdseg/vis.py
@@ -87,7 +87,7 @@
 def visualize(cfg,
               vis_file_list=None,
               use_gpu=False,
-              vis_dir="visual_predict",
+              vis_dir="visual",
               ckpt_dir=None,
               log_writer=None,
               local_test=False,
@@ -117,7 +117,7 @@
 
     fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)
 
-    save_dir = os.path.join('visual', vis_dir)
+    save_dir = vis_dir
     makedirs(save_dir)
 
     fetch_list = [pred.name]

{"golden_diff": "diff --git a/pdseg/vis.py b/pdseg/vis.py\n--- a/pdseg/vis.py\n+++ b/pdseg/vis.py\n@@ -87,7 +87,7 @@\n def visualize(cfg,\n               vis_file_list=None,\n               use_gpu=False,\n-              vis_dir=\"visual_predict\",\n+              vis_dir=\"visual\",\n               ckpt_dir=None,\n               log_writer=None,\n               local_test=False,\n@@ -117,7 +117,7 @@\n \n     fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)\n \n-    save_dir = os.path.join('visual', vis_dir)\n+    save_dir = vis_dir\n     makedirs(save_dir)\n \n     fetch_list = [pred.name]\n", "issue": "labelme2seg.py\u5de5\u5177\u751f\u6210\u7684png\u6587\u4ef6\u662f\u5168\u9ed1\u7684\n\u60a8\u597d\uff0c\u6211\u4f7f\u7528labelme\u8fdb\u884c\u6807\u6ce8\uff0c\u7136\u540e\u4f7f\u7528labelme2seg.py\u751f\u6210\u6570\u636e\u96c6\uff0c\u4f46\u662f\u751f\u6210\u7684png\u6587\u4ef6\u6253\u5f00\u4e4b\u540e\u662f\u5168\u9ed1\u7684\uff0c\u770b\u4e0d\u5230\u6807\u6ce8\u7684\u7269\u4f53\uff0c\u9ebb\u70e6\u5e2e\u5fd9\u770b\u770b\u539f\u56e0\uff0c\u8c22\u8c22\u3002\r\n\r\n\u4e0b\u9762\u9644\u4e0alabelme\u751f\u6210\u7684json\u6587\u4ef6\uff1a\r\n{\r\n  \"version\": \"3.16.7\",\r\n  \"flags\": {},\r\n  \"shapes\": [\r\n    {\r\n      \"label\": \"test\",\r\n      \"line_color\": null,\r\n      \"fill_color\": null,\r\n      \"points\": [\r\n        [\r\n          130.10619469026545,\r\n          275.87610619469024\r\n        ],\r\n        [\r\n          261.0796460176991,\r\n          419.2389380530973\r\n        ],\r\n        [\r\n          492.9380530973451,\r\n          430.74336283185835\r\n        ],\r\n        [\r\n          507.98230088495563,\r\n          297.99999999999994\r\n        ],\r\n        [\r\n          348.6902654867256,\r\n          183.84070796460176\r\n        ],\r\n        [\r\n          191.16814159292034,\r\n          236.93805309734512\r\n        ]\r\n      ],\r\n      \"shape_type\": \"polygon\",\r\n      \"flags\": {}\r\n    },\r\n    {\r\n      \"label\": \"ttt\",\r\n      \"line_color\": null,\r\n      \"fill_color\": null,\r\n      \"points\": [\r\n        [\r\n          211.52212389380526,\r\n          482.9557522123893\r\n        ],\r\n        [\r\n          247.80530973451323,\r\n          602.4247787610619\r\n        ],\r\n        [\r\n          412.4070796460177,\r\n          623.6637168141592\r\n        ],\r\n        [\r\n          446.0353982300884,\r\n          513.929203539823\r\n        ],\r\n        [\r\n          425.68141592920347,\r\n          459.9469026548672\r\n        ],\r\n        [\r\n          262.84955752212386,\r\n          460.8318584070796\r\n        ]\r\n      ],\r\n      \"shape_type\": \"polygon\",\r\n      \"flags\": {}\r\n    }\r\n  ],\r\n  \"lineColor\": [\r\n    0,\r\n    255,\r\n    0,\r\n    128\r\n  ],\r\n  \"fillColor\": [\r\n    255,\r\n    0,\r\n    0,\r\n    128\r\n  ],\r\n  \"imagePath\": \"6.jpg\",\r\n  \"imageData\": \"\u592a\u591a\uff0c\u7701\u7565\",\r\n  \"imageHeight\": 694,\r\n  \"imageWidth\": 685\r\n}\n", "before_files": [{"content": "# coding: utf8\n# copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\n\n# GPU memory garbage collection optimization flags\nos.environ['FLAGS_eager_delete_tensor_gb'] = \"0.0\"\n\nimport sys\nimport argparse\nimport pprint\nimport cv2\nimport numpy as np\nimport paddle.fluid as fluid\n\nfrom PIL import Image as PILImage\nfrom utils.config import cfg\nfrom reader import SegDataset\nfrom models.model_builder import build_model\nfrom models.model_builder import ModelPhase\nfrom tools.gray2pseudo_color import get_color_map_list\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser(description='PaddeSeg visualization tools')\n    parser.add_argument(\n        '--cfg',\n        dest='cfg_file',\n        help='Config file for training (and optionally testing)',\n        default=None,\n        type=str)\n    parser.add_argument(\n        '--use_gpu', dest='use_gpu', help='Use gpu or cpu', action='store_true')\n    parser.add_argument(\n        '--vis_dir',\n        dest='vis_dir',\n        help='visual save dir',\n        type=str,\n        default='visual')\n    parser.add_argument(\n        '--local_test',\n        dest='local_test',\n        help='if in local test mode, only visualize 5 images for testing',\n        action='store_true')\n    parser.add_argument(\n        'opts',\n        help='See config.py for all options',\n        default=None,\n        nargs=argparse.REMAINDER)\n    if len(sys.argv) == 1:\n        parser.print_help()\n        sys.exit(1)\n    return parser.parse_args()\n\n\ndef makedirs(directory):\n    if not os.path.exists(directory):\n        os.makedirs(directory)\n\n\ndef to_png_fn(fn):\n    \"\"\"\n    Append png as filename postfix\n    \"\"\"\n    directory, filename = os.path.split(fn)\n    basename, ext = os.path.splitext(filename)\n\n    return basename + \".png\"\n\n\ndef visualize(cfg,\n              vis_file_list=None,\n              use_gpu=False,\n              vis_dir=\"visual_predict\",\n              ckpt_dir=None,\n              log_writer=None,\n              local_test=False,\n              **kwargs):\n    if vis_file_list is None:\n        vis_file_list = cfg.DATASET.TEST_FILE_LIST\n    dataset = SegDataset(\n        file_list=vis_file_list,\n        mode=ModelPhase.VISUAL,\n        data_dir=cfg.DATASET.DATA_DIR)\n\n    startup_prog = fluid.Program()\n    test_prog = fluid.Program()\n    pred, logit = build_model(test_prog, startup_prog, phase=ModelPhase.VISUAL)\n    # Clone forward graph\n    test_prog = test_prog.clone(for_test=True)\n\n    # Generator full colormap for maximum 256 classes\n    color_map = get_color_map_list(256)\n\n    # Get device environment\n    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n    exe.run(startup_prog)\n\n    ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir\n\n    fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)\n\n    save_dir = os.path.join('visual', vis_dir)\n    makedirs(save_dir)\n\n    fetch_list = [pred.name]\n    test_reader = dataset.batch(dataset.generator, batch_size=1, is_test=True)\n    img_cnt = 0\n    for imgs, grts, img_names, valid_shapes, org_shapes in test_reader:\n        pred_shape = (imgs.shape[2], imgs.shape[3])\n        pred, = exe.run(\n            program=test_prog,\n            feed={'image': imgs},\n            fetch_list=fetch_list,\n            return_numpy=True)\n\n        num_imgs = pred.shape[0]\n        # TODO: use multi-thread to write images\n        for i in range(num_imgs):\n            # Add more comments\n            res_map = np.squeeze(pred[i, :, :, :]).astype(np.uint8)\n            img_name = img_names[i]\n            res_shape = (res_map.shape[0], res_map.shape[1])\n            if res_shape[0] != pred_shape[0] or res_shape[1] != pred_shape[1]:\n                res_map = cv2.resize(\n                    res_map, pred_shape, interpolation=cv2.INTER_NEAREST)\n            valid_shape = (valid_shapes[i, 0], valid_shapes[i, 1])\n            res_map = res_map[0:valid_shape[0], 0:valid_shape[1]]\n            org_shape = (org_shapes[i, 0], org_shapes[i, 1])\n            res_map = cv2.resize(\n                res_map, (org_shape[1], org_shape[0]),\n                interpolation=cv2.INTER_NEAREST)\n\n            png_fn = to_png_fn(img_name)\n\n            # colorful segment result visualization\n            vis_fn = os.path.join(save_dir, png_fn)\n            dirname = os.path.dirname(vis_fn)\n            makedirs(dirname)\n\n            pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='P')\n            pred_mask.putpalette(color_map)\n            pred_mask.save(vis_fn)\n\n            img_cnt += 1\n            print(\"#{} visualize image path: {}\".format(img_cnt, vis_fn))\n\n            # Use Tensorboard to visualize image\n            if log_writer is not None:\n                # Calulate epoch from ckpt_dir folder name\n                epoch = int(os.path.split(ckpt_dir)[-1])\n                print(\"Tensorboard visualization epoch\", epoch)\n\n                pred_mask_np = np.array(pred_mask.convert(\"RGB\"))\n                log_writer.add_image(\n                    \"Predict/{}\".format(img_name),\n                    pred_mask_np,\n                    epoch,\n                    dataformats='HWC')\n                # Original image\n                # BGR->RGB\n                img = cv2.imread(\n                    os.path.join(cfg.DATASET.DATA_DIR, img_name))[..., ::-1]\n                log_writer.add_image(\n                    \"Images/{}\".format(img_name),\n                    img,\n                    epoch,\n                    dataformats='HWC')\n                # add ground truth (label) images\n                grt = grts[i]\n                if grt is not None:\n                    grt = grt[0:valid_shape[0], 0:valid_shape[1]]\n                    grt_pil = PILImage.fromarray(grt.astype(np.uint8), mode='P')\n                    grt_pil.putpalette(color_map)\n                    grt_pil = grt_pil.resize((org_shape[1], org_shape[0]))\n                    grt = np.array(grt_pil.convert(\"RGB\"))\n                    log_writer.add_image(\n                        \"Label/{}\".format(img_name),\n                        grt,\n                        epoch,\n                        dataformats='HWC')\n\n        # If in local_test mode, only visualize 5 images just for testing\n        # procedure\n        if local_test and img_cnt >= 5:\n            break\n\n\nif __name__ == '__main__':\n    args = parse_args()\n    if args.cfg_file is not None:\n        cfg.update_from_file(args.cfg_file)\n    if args.opts:\n        cfg.update_from_list(args.opts)\n    cfg.check_and_infer()\n    print(pprint.pformat(cfg))\n    visualize(cfg, **args.__dict__)\n", "path": "pdseg/vis.py"}], "after_files": [{"content": "# coding: utf8\n# copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#    http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport os\n\n# GPU memory garbage collection optimization flags\nos.environ['FLAGS_eager_delete_tensor_gb'] = \"0.0\"\n\nimport sys\nimport argparse\nimport pprint\nimport cv2\nimport numpy as np\nimport paddle.fluid as fluid\n\nfrom PIL import Image as PILImage\nfrom utils.config import cfg\nfrom reader import SegDataset\nfrom models.model_builder import build_model\nfrom models.model_builder import ModelPhase\nfrom tools.gray2pseudo_color import get_color_map_list\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser(description='PaddeSeg visualization tools')\n    parser.add_argument(\n        '--cfg',\n        dest='cfg_file',\n        help='Config file for training (and optionally testing)',\n        default=None,\n        type=str)\n    parser.add_argument(\n        '--use_gpu', dest='use_gpu', help='Use gpu or cpu', action='store_true')\n    parser.add_argument(\n        '--vis_dir',\n        dest='vis_dir',\n        help='visual save dir',\n        type=str,\n        default='visual')\n    parser.add_argument(\n        '--local_test',\n        dest='local_test',\n        help='if in local test mode, only visualize 5 images for testing',\n        action='store_true')\n    parser.add_argument(\n        'opts',\n        help='See config.py for all options',\n        default=None,\n        nargs=argparse.REMAINDER)\n    if len(sys.argv) == 1:\n        parser.print_help()\n        sys.exit(1)\n    return parser.parse_args()\n\n\ndef makedirs(directory):\n    if not os.path.exists(directory):\n        os.makedirs(directory)\n\n\ndef to_png_fn(fn):\n    \"\"\"\n    Append png as filename postfix\n    \"\"\"\n    directory, filename = os.path.split(fn)\n    basename, ext = os.path.splitext(filename)\n\n    return basename + \".png\"\n\n\ndef visualize(cfg,\n              vis_file_list=None,\n              use_gpu=False,\n              vis_dir=\"visual\",\n              ckpt_dir=None,\n              log_writer=None,\n              local_test=False,\n              **kwargs):\n    if vis_file_list is None:\n        vis_file_list = cfg.DATASET.TEST_FILE_LIST\n    dataset = SegDataset(\n        file_list=vis_file_list,\n        mode=ModelPhase.VISUAL,\n        data_dir=cfg.DATASET.DATA_DIR)\n\n    startup_prog = fluid.Program()\n    test_prog = fluid.Program()\n    pred, logit = build_model(test_prog, startup_prog, phase=ModelPhase.VISUAL)\n    # Clone forward graph\n    test_prog = test_prog.clone(for_test=True)\n\n    # Generator full colormap for maximum 256 classes\n    color_map = get_color_map_list(256)\n\n    # Get device environment\n    place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()\n    exe = fluid.Executor(place)\n    exe.run(startup_prog)\n\n    ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir\n\n    fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)\n\n    save_dir = vis_dir\n    makedirs(save_dir)\n\n    fetch_list = [pred.name]\n    test_reader = dataset.batch(dataset.generator, batch_size=1, is_test=True)\n    img_cnt = 0\n    for imgs, grts, img_names, valid_shapes, org_shapes in test_reader:\n        pred_shape = (imgs.shape[2], imgs.shape[3])\n        pred, = exe.run(\n            program=test_prog,\n            feed={'image': imgs},\n            fetch_list=fetch_list,\n            return_numpy=True)\n\n        num_imgs = pred.shape[0]\n        # TODO: use multi-thread to write images\n        for i in range(num_imgs):\n            # Add more comments\n            res_map = np.squeeze(pred[i, :, :, :]).astype(np.uint8)\n            img_name = img_names[i]\n            res_shape = (res_map.shape[0], res_map.shape[1])\n            if res_shape[0] != pred_shape[0] or res_shape[1] != pred_shape[1]:\n                res_map = cv2.resize(\n                    res_map, pred_shape, interpolation=cv2.INTER_NEAREST)\n            valid_shape = (valid_shapes[i, 0], valid_shapes[i, 1])\n            res_map = res_map[0:valid_shape[0], 0:valid_shape[1]]\n            org_shape = (org_shapes[i, 0], org_shapes[i, 1])\n            res_map = cv2.resize(\n                res_map, (org_shape[1], org_shape[0]),\n                interpolation=cv2.INTER_NEAREST)\n\n            png_fn = to_png_fn(img_name)\n\n            # colorful segment result visualization\n            vis_fn = os.path.join(save_dir, png_fn)\n            dirname = os.path.dirname(vis_fn)\n            makedirs(dirname)\n\n            pred_mask = PILImage.fromarray(res_map.astype(np.uint8), mode='P')\n            pred_mask.putpalette(color_map)\n            pred_mask.save(vis_fn)\n\n            img_cnt += 1\n            print(\"#{} visualize image path: {}\".format(img_cnt, vis_fn))\n\n            # Use Tensorboard to visualize image\n            if log_writer is not None:\n                # Calulate epoch from ckpt_dir folder name\n                epoch = int(os.path.split(ckpt_dir)[-1])\n                print(\"Tensorboard visualization epoch\", epoch)\n\n                pred_mask_np = np.array(pred_mask.convert(\"RGB\"))\n                log_writer.add_image(\n                    \"Predict/{}\".format(img_name),\n                    pred_mask_np,\n                    epoch,\n                    dataformats='HWC')\n                # Original image\n                # BGR->RGB\n                img = cv2.imread(\n                    os.path.join(cfg.DATASET.DATA_DIR, img_name))[..., ::-1]\n                log_writer.add_image(\n                    \"Images/{}\".format(img_name),\n                    img,\n                    epoch,\n                    dataformats='HWC')\n                # add ground truth (label) images\n                grt = grts[i]\n                if grt is not None:\n                    grt = grt[0:valid_shape[0], 0:valid_shape[1]]\n                    grt_pil = PILImage.fromarray(grt.astype(np.uint8), mode='P')\n                    grt_pil.putpalette(color_map)\n                    grt_pil = grt_pil.resize((org_shape[1], org_shape[0]))\n                    grt = np.array(grt_pil.convert(\"RGB\"))\n                    log_writer.add_image(\n                        \"Label/{}\".format(img_name),\n                        grt,\n                        epoch,\n                        dataformats='HWC')\n\n        # If in local_test mode, only visualize 5 images just for testing\n        # procedure\n        if local_test and img_cnt >= 5:\n            break\n\n\nif __name__ == '__main__':\n    args = parse_args()\n    if args.cfg_file is not None:\n        cfg.update_from_file(args.cfg_file)\n    if args.opts:\n        cfg.update_from_list(args.opts)\n    cfg.check_and_infer()\n    print(pprint.pformat(cfg))\n    visualize(cfg, **args.__dict__)\n", "path": "pdseg/vis.py"}]}