prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  评估时模型路径不存在直接报load op的错
  
  
  --- 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/eval.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 # GPU memory garbage collection optimization flags
  22 os.environ['FLAGS_eager_delete_tensor_gb'] = "0.0"
  23 
  24 import sys
  25 import time
  26 import argparse
  27 import functools
  28 import pprint
  29 import cv2
  30 import numpy as np
  31 import paddle
  32 import paddle.fluid as fluid
  33 
  34 from utils.config import cfg
  35 from utils.timer import Timer, calculate_eta
  36 from models.model_builder import build_model
  37 from models.model_builder import ModelPhase
  38 from reader import SegDataset
  39 from metrics import ConfusionMatrix
  40 
  41 
  42 def parse_args():
  43     parser = argparse.ArgumentParser(description='PaddleSeg model evalution')
  44     parser.add_argument(
  45         '--cfg',
  46         dest='cfg_file',
  47         help='Config file for training (and optionally testing)',
  48         default=None,
  49         type=str)
  50     parser.add_argument(
  51         '--use_gpu',
  52         dest='use_gpu',
  53         help='Use gpu or cpu',
  54         action='store_true',
  55         default=False)
  56     parser.add_argument(
  57         '--use_mpio',
  58         dest='use_mpio',
  59         help='Use multiprocess IO or not',
  60         action='store_true',
  61         default=False)
  62     parser.add_argument(
  63         'opts',
  64         help='See utils/config.py for all options',
  65         default=None,
  66         nargs=argparse.REMAINDER)
  67     if len(sys.argv) == 1:
  68         parser.print_help()
  69         sys.exit(1)
  70     return parser.parse_args()
  71 
  72 
  73 def evaluate(cfg, ckpt_dir=None, use_gpu=False, use_mpio=False, **kwargs):
  74     np.set_printoptions(precision=5, suppress=True)
  75 
  76     startup_prog = fluid.Program()
  77     test_prog = fluid.Program()
  78     dataset = SegDataset(
  79         file_list=cfg.DATASET.VAL_FILE_LIST,
  80         mode=ModelPhase.EVAL,
  81         data_dir=cfg.DATASET.DATA_DIR)
  82 
  83     def data_generator():
  84         #TODO: check is batch reader compatitable with Windows
  85         if use_mpio:
  86             data_gen = dataset.multiprocess_generator(
  87                 num_processes=cfg.DATALOADER.NUM_WORKERS,
  88                 max_queue_size=cfg.DATALOADER.BUF_SIZE)
  89         else:
  90             data_gen = dataset.generator()
  91 
  92         for b in data_gen:
  93             yield b[0], b[1], b[2]
  94 
  95     py_reader, avg_loss, pred, grts, masks = build_model(
  96         test_prog, startup_prog, phase=ModelPhase.EVAL)
  97 
  98     py_reader.decorate_sample_generator(
  99         data_generator, drop_last=False, batch_size=cfg.BATCH_SIZE)
  100 
  101     # Get device environment
  102     places = fluid.cuda_places() if use_gpu else fluid.cpu_places()
  103     place = places[0]
  104     dev_count = len(places)
  105     print("#Device count: {}".format(dev_count))
  106 
  107     exe = fluid.Executor(place)
  108     exe.run(startup_prog)
  109 
  110     test_prog = test_prog.clone(for_test=True)
  111 
  112     ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir
  113 
  114     if ckpt_dir is not None:
  115         print('load test model:', ckpt_dir)
  116         fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)
  117 
  118     # Use streaming confusion matrix to calculate mean_iou
  119     np.set_printoptions(
  120         precision=4, suppress=True, linewidth=160, floatmode="fixed")
  121     conf_mat = ConfusionMatrix(cfg.DATASET.NUM_CLASSES, streaming=True)
  122     fetch_list = [avg_loss.name, pred.name, grts.name, masks.name]
  123     num_images = 0
  124     step = 0
  125     all_step = cfg.DATASET.TEST_TOTAL_IMAGES // cfg.BATCH_SIZE + 1
  126     timer = Timer()
  127     timer.start()
  128     py_reader.start()
  129     while True:
  130         try:
  131             step += 1
  132             loss, pred, grts, masks = exe.run(
  133                 test_prog, fetch_list=fetch_list, return_numpy=True)
  134 
  135             loss = np.mean(np.array(loss))
  136 
  137             num_images += pred.shape[0]
  138             conf_mat.calculate(pred, grts, masks)
  139             _, iou = conf_mat.mean_iou()
  140             _, acc = conf_mat.accuracy()
  141 
  142             speed = 1.0 / timer.elapsed_time()
  143 
  144             print(
  145                 "[EVAL]step={} loss={:.5f} acc={:.4f} IoU={:.4f} step/sec={:.2f} | ETA {}"
  146                 .format(step, loss, acc, iou, speed,
  147                         calculate_eta(all_step - step, speed)))
  148             timer.restart()
  149             sys.stdout.flush()
  150         except fluid.core.EOFException:
  151             break
  152 
  153     category_iou, avg_iou = conf_mat.mean_iou()
  154     category_acc, avg_acc = conf_mat.accuracy()
  155     print("[EVAL]#image={} acc={:.4f} IoU={:.4f}".format(
  156         num_images, avg_acc, avg_iou))
  157     print("[EVAL]Category IoU:", category_iou)
  158     print("[EVAL]Category Acc:", category_acc)
  159     print("[EVAL]Kappa:{:.4f}".format(conf_mat.kappa()))
  160 
  161     return category_iou, avg_iou, category_acc, avg_acc
  162 
  163 
  164 def main():
  165     args = parse_args()
  166     if args.cfg_file is not None:
  167         cfg.update_from_file(args.cfg_file)
  168     if args.opts:
  169         cfg.update_from_list(args.opts)
  170     cfg.check_and_infer()
  171     print(pprint.pformat(cfg))
  172     evaluate(cfg, **args.__dict__)
  173 
  174 
  175 if __name__ == '__main__':
  176     main()
  177
  ```
  --- 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()
  
  ```
  
golden_diff: diff --git a/pdseg/eval.py b/pdseg/eval.py
  --- a/pdseg/eval.py
  +++ b/pdseg/eval.py
  @@ -111,6 +111,9 @@
   
       ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir
   
  +    if not os.path.exists(ckpt_dir):
  +        raise ValueError('The TEST.TEST_MODEL {} is not found'.format(ckpt_dir))
  +
       if ckpt_dir is not None:
           print('load test model:', ckpt_dir)
           fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)
  
verification_info: {"golden_diff": "diff --git a/pdseg/eval.py b/pdseg/eval.py\n--- a/pdseg/eval.py\n+++ b/pdseg/eval.py\n@@ -111,6 +111,9 @@\n \n     ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir\n \n+    if not os.path.exists(ckpt_dir):\n+        raise ValueError('The TEST.TEST_MODEL {} is not found'.format(ckpt_dir))\n+\n     if ckpt_dir is not None:\n         print('load test model:', ckpt_dir)\n         fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)\n", "issue": "\u8bc4\u4f30\u65f6\u6a21\u578b\u8def\u5f84\u4e0d\u5b58\u5728\u76f4\u63a5\u62a5load op\u7684\u9519\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# GPU memory garbage collection optimization flags\nos.environ['FLAGS_eager_delete_tensor_gb'] = \"0.0\"\n\nimport sys\nimport time\nimport argparse\nimport functools\nimport pprint\nimport cv2\nimport numpy as np\nimport paddle\nimport paddle.fluid as fluid\n\nfrom utils.config import cfg\nfrom utils.timer import Timer, calculate_eta\nfrom models.model_builder import build_model\nfrom models.model_builder import ModelPhase\nfrom reader import SegDataset\nfrom metrics import ConfusionMatrix\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser(description='PaddleSeg model evalution')\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',\n        dest='use_gpu',\n        help='Use gpu or cpu',\n        action='store_true',\n        default=False)\n    parser.add_argument(\n        '--use_mpio',\n        dest='use_mpio',\n        help='Use multiprocess IO or not',\n        action='store_true',\n        default=False)\n    parser.add_argument(\n        'opts',\n        help='See utils/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 evaluate(cfg, ckpt_dir=None, use_gpu=False, use_mpio=False, **kwargs):\n    np.set_printoptions(precision=5, suppress=True)\n\n    startup_prog = fluid.Program()\n    test_prog = fluid.Program()\n    dataset = SegDataset(\n        file_list=cfg.DATASET.VAL_FILE_LIST,\n        mode=ModelPhase.EVAL,\n        data_dir=cfg.DATASET.DATA_DIR)\n\n    def data_generator():\n        #TODO: check is batch reader compatitable with Windows\n        if use_mpio:\n            data_gen = dataset.multiprocess_generator(\n                num_processes=cfg.DATALOADER.NUM_WORKERS,\n                max_queue_size=cfg.DATALOADER.BUF_SIZE)\n        else:\n            data_gen = dataset.generator()\n\n        for b in data_gen:\n            yield b[0], b[1], b[2]\n\n    py_reader, avg_loss, pred, grts, masks = build_model(\n        test_prog, startup_prog, phase=ModelPhase.EVAL)\n\n    py_reader.decorate_sample_generator(\n        data_generator, drop_last=False, batch_size=cfg.BATCH_SIZE)\n\n    # Get device environment\n    places = fluid.cuda_places() if use_gpu else fluid.cpu_places()\n    place = places[0]\n    dev_count = len(places)\n    print(\"#Device count: {}\".format(dev_count))\n\n    exe = fluid.Executor(place)\n    exe.run(startup_prog)\n\n    test_prog = test_prog.clone(for_test=True)\n\n    ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir\n\n    if ckpt_dir is not None:\n        print('load test model:', ckpt_dir)\n        fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)\n\n    # Use streaming confusion matrix to calculate mean_iou\n    np.set_printoptions(\n        precision=4, suppress=True, linewidth=160, floatmode=\"fixed\")\n    conf_mat = ConfusionMatrix(cfg.DATASET.NUM_CLASSES, streaming=True)\n    fetch_list = [avg_loss.name, pred.name, grts.name, masks.name]\n    num_images = 0\n    step = 0\n    all_step = cfg.DATASET.TEST_TOTAL_IMAGES // cfg.BATCH_SIZE + 1\n    timer = Timer()\n    timer.start()\n    py_reader.start()\n    while True:\n        try:\n            step += 1\n            loss, pred, grts, masks = exe.run(\n                test_prog, fetch_list=fetch_list, return_numpy=True)\n\n            loss = np.mean(np.array(loss))\n\n            num_images += pred.shape[0]\n            conf_mat.calculate(pred, grts, masks)\n            _, iou = conf_mat.mean_iou()\n            _, acc = conf_mat.accuracy()\n\n            speed = 1.0 / timer.elapsed_time()\n\n            print(\n                \"[EVAL]step={} loss={:.5f} acc={:.4f} IoU={:.4f} step/sec={:.2f} | ETA {}\"\n                .format(step, loss, acc, iou, speed,\n                        calculate_eta(all_step - step, speed)))\n            timer.restart()\n            sys.stdout.flush()\n        except fluid.core.EOFException:\n            break\n\n    category_iou, avg_iou = conf_mat.mean_iou()\n    category_acc, avg_acc = conf_mat.accuracy()\n    print(\"[EVAL]#image={} acc={:.4f} IoU={:.4f}\".format(\n        num_images, avg_acc, avg_iou))\n    print(\"[EVAL]Category IoU:\", category_iou)\n    print(\"[EVAL]Category Acc:\", category_acc)\n    print(\"[EVAL]Kappa:{:.4f}\".format(conf_mat.kappa()))\n\n    return category_iou, avg_iou, category_acc, avg_acc\n\n\ndef 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    evaluate(cfg, **args.__dict__)\n\n\nif __name__ == '__main__':\n    main()\n", "path": "pdseg/eval.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# GPU memory garbage collection optimization flags\nos.environ['FLAGS_eager_delete_tensor_gb'] = \"0.0\"\n\nimport sys\nimport time\nimport argparse\nimport functools\nimport pprint\nimport cv2\nimport numpy as np\nimport paddle\nimport paddle.fluid as fluid\n\nfrom utils.config import cfg\nfrom utils.timer import Timer, calculate_eta\nfrom models.model_builder import build_model\nfrom models.model_builder import ModelPhase\nfrom reader import SegDataset\nfrom metrics import ConfusionMatrix\n\n\ndef parse_args():\n    parser = argparse.ArgumentParser(description='PaddleSeg model evalution')\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',\n        dest='use_gpu',\n        help='Use gpu or cpu',\n        action='store_true',\n        default=False)\n    parser.add_argument(\n        '--use_mpio',\n        dest='use_mpio',\n        help='Use multiprocess IO or not',\n        action='store_true',\n        default=False)\n    parser.add_argument(\n        'opts',\n        help='See utils/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 evaluate(cfg, ckpt_dir=None, use_gpu=False, use_mpio=False, **kwargs):\n    np.set_printoptions(precision=5, suppress=True)\n\n    startup_prog = fluid.Program()\n    test_prog = fluid.Program()\n    dataset = SegDataset(\n        file_list=cfg.DATASET.VAL_FILE_LIST,\n        mode=ModelPhase.EVAL,\n        data_dir=cfg.DATASET.DATA_DIR)\n\n    def data_generator():\n        #TODO: check is batch reader compatitable with Windows\n        if use_mpio:\n            data_gen = dataset.multiprocess_generator(\n                num_processes=cfg.DATALOADER.NUM_WORKERS,\n                max_queue_size=cfg.DATALOADER.BUF_SIZE)\n        else:\n            data_gen = dataset.generator()\n\n        for b in data_gen:\n            yield b[0], b[1], b[2]\n\n    py_reader, avg_loss, pred, grts, masks = build_model(\n        test_prog, startup_prog, phase=ModelPhase.EVAL)\n\n    py_reader.decorate_sample_generator(\n        data_generator, drop_last=False, batch_size=cfg.BATCH_SIZE)\n\n    # Get device environment\n    places = fluid.cuda_places() if use_gpu else fluid.cpu_places()\n    place = places[0]\n    dev_count = len(places)\n    print(\"#Device count: {}\".format(dev_count))\n\n    exe = fluid.Executor(place)\n    exe.run(startup_prog)\n\n    test_prog = test_prog.clone(for_test=True)\n\n    ckpt_dir = cfg.TEST.TEST_MODEL if not ckpt_dir else ckpt_dir\n\n    if not os.path.exists(ckpt_dir):\n        raise ValueError('The TEST.TEST_MODEL {} is not found'.format(ckpt_dir))\n\n    if ckpt_dir is not None:\n        print('load test model:', ckpt_dir)\n        fluid.io.load_params(exe, ckpt_dir, main_program=test_prog)\n\n    # Use streaming confusion matrix to calculate mean_iou\n    np.set_printoptions(\n        precision=4, suppress=True, linewidth=160, floatmode=\"fixed\")\n    conf_mat = ConfusionMatrix(cfg.DATASET.NUM_CLASSES, streaming=True)\n    fetch_list = [avg_loss.name, pred.name, grts.name, masks.name]\n    num_images = 0\n    step = 0\n    all_step = cfg.DATASET.TEST_TOTAL_IMAGES // cfg.BATCH_SIZE + 1\n    timer = Timer()\n    timer.start()\n    py_reader.start()\n    while True:\n        try:\n            step += 1\n            loss, pred, grts, masks = exe.run(\n                test_prog, fetch_list=fetch_list, return_numpy=True)\n\n            loss = np.mean(np.array(loss))\n\n            num_images += pred.shape[0]\n            conf_mat.calculate(pred, grts, masks)\n            _, iou = conf_mat.mean_iou()\n            _, acc = conf_mat.accuracy()\n\n            speed = 1.0 / timer.elapsed_time()\n\n            print(\n                \"[EVAL]step={} loss={:.5f} acc={:.4f} IoU={:.4f} step/sec={:.2f} | ETA {}\"\n                .format(step, loss, acc, iou, speed,\n                        calculate_eta(all_step - step, speed)))\n            timer.restart()\n            sys.stdout.flush()\n        except fluid.core.EOFException:\n            break\n\n    category_iou, avg_iou = conf_mat.mean_iou()\n    category_acc, avg_acc = conf_mat.accuracy()\n    print(\"[EVAL]#image={} acc={:.4f} IoU={:.4f}\".format(\n        num_images, avg_acc, avg_iou))\n    print(\"[EVAL]Category IoU:\", category_iou)\n    print(\"[EVAL]Category Acc:\", category_acc)\n    print(\"[EVAL]Kappa:{:.4f}\".format(conf_mat.kappa()))\n\n    return category_iou, avg_iou, category_acc, avg_acc\n\n\ndef 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    evaluate(cfg, **args.__dict__)\n\n\nif __name__ == '__main__':\n    main()\n", "path": "pdseg/eval.py"}]}