We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
icnet加载model_1000预训练模型进行训练报错
1.使用如下指令训练：python train.py --batch_size=16 --use_gpu=True --init_model="./model_1000/"  --checkpoint_path="./chkpnt/"
其中：/model_1000为文档中提供的预训练模型，其中并无conv6参数。
<img width="970" alt="bfd08a979fcb4f72bb443fbca3f53d3f" src="https://user-images.githubusercontent.com/37854899/54199980-24704000-4505-11e9-9bf4-70dc09b0cc86.png">
2. 文档中eval和infer中的model_path建议改为./chkpnt/100，与训练输出保持一致
![image](https://user-images.githubusercontent.com/37854899/54200167-aceee080-4505-11e9-9d1a-535a8a09df74.png)
![image](https://user-images.githubusercontent.com/37854899/54200175-b2e4c180-4505-11e9-8cb7-68e5020ebc60.png)



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `PaddleCV/icnet/infer.py`
Content:
```
1 """Infer for ICNet model."""
2 from __future__ import print_function
3 import cityscape
4 import argparse
5 import functools
6 import sys
7 import os
8 import cv2
9 
10 import paddle.fluid as fluid
11 import paddle
12 from icnet import icnet
13 from utils import add_arguments, print_arguments, get_feeder_data
14 from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter
15 from paddle.fluid.initializer import init_on_cpu
16 import numpy as np
17 
18 IMG_MEAN = np.array((103.939, 116.779, 123.68), dtype=np.float32)
19 parser = argparse.ArgumentParser(description=__doc__)
20 add_arg = functools.partial(add_arguments, argparser=parser)
21 # yapf: disable
22 add_arg('model_path',        str,   None,         "Model path.")
23 add_arg('images_list',       str,   None,         "List file with images to be infered.")
24 add_arg('images_path',       str,   None,         "The images path.")
25 add_arg('out_path',          str,   "./output",         "Output path.")
26 add_arg('use_gpu',           bool,  True,       "Whether use GPU to test.")
27 # yapf: enable
28 
29 data_shape = [3, 1024, 2048]
30 num_classes = 19
31 
32 label_colours = [
33     [128, 64, 128],
34     [244, 35, 231],
35     [69, 69, 69]
36     # 0 = road, 1 = sidewalk, 2 = building
37     ,
38     [102, 102, 156],
39     [190, 153, 153],
40     [153, 153, 153]
41     # 3 = wall, 4 = fence, 5 = pole
42     ,
43     [250, 170, 29],
44     [219, 219, 0],
45     [106, 142, 35]
46     # 6 = traffic light, 7 = traffic sign, 8 = vegetation
47     ,
48     [152, 250, 152],
49     [69, 129, 180],
50     [219, 19, 60]
51     # 9 = terrain, 10 = sky, 11 = person
52     ,
53     [255, 0, 0],
54     [0, 0, 142],
55     [0, 0, 69]
56     # 12 = rider, 13 = car, 14 = truck
57     ,
58     [0, 60, 100],
59     [0, 79, 100],
60     [0, 0, 230]
61     # 15 = bus, 16 = train, 17 = motocycle
62     ,
63     [119, 10, 32]
64 ]
65 
66 # 18 = bicycle
67 
68 
69 def color(input):
70     """
71     Convert infered result to color image.
72     """
73     result = []
74     for i in input.flatten():
75         result.append(
76             [label_colours[i][2], label_colours[i][1], label_colours[i][0]])
77     result = np.array(result).reshape([input.shape[0], input.shape[1], 3])
78     return result
79 
80 
81 def infer(args):
82     data_shape = cityscape.test_data_shape()
83     num_classes = cityscape.num_classes()
84     # define network
85     images = fluid.layers.data(name='image', shape=data_shape, dtype='float32')
86     _, _, sub124_out = icnet(images, num_classes,
87                              np.array(data_shape[1:]).astype("float32"))
88     predict = fluid.layers.resize_bilinear(
89         sub124_out, out_shape=data_shape[1:3])
90     predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1])
91     predict = fluid.layers.reshape(predict, shape=[-1, num_classes])
92     _, predict = fluid.layers.topk(predict, k=1)
93     predict = fluid.layers.reshape(
94         predict,
95         shape=[data_shape[1], data_shape[2], -1])  # batch_size should be 1
96     inference_program = fluid.default_main_program().clone(for_test=True)
97     # prepare environment
98     place = fluid.CPUPlace()
99     if args.use_gpu:
100         place = fluid.CUDAPlace(0)
101     exe = fluid.Executor(place)
102     exe.run(fluid.default_startup_program())
103     assert os.path.exists(args.model_path)
104     fluid.io.load_params(exe, args.model_path)
105     print("loaded model from: %s" % args.model_path)
106     sys.stdout.flush()
107 
108     if not os.path.isdir(args.out_path):
109         os.makedirs(args.out_path)
110 
111     for line in open(args.images_list):
112         image_file = args.images_path + "/" + line.strip()
113         filename = os.path.basename(image_file)
114         image = paddle.dataset.image.load_image(
115             image_file, is_color=True).astype("float32")
116         image -= IMG_MEAN
117         img = paddle.dataset.image.to_chw(image)[np.newaxis, :]
118         image_t = fluid.core.LoDTensor()
119         image_t.set(img, place)
120         result = exe.run(inference_program,
121                          feed={"image": image_t},
122                          fetch_list=[predict])
123         cv2.imwrite(args.out_path + "/" + filename + "_result.png",
124                     color(result[0]))
125 
126 
127 def main():
128     args = parser.parse_args()
129     print_arguments(args)
130     infer(args)
131 
132 
133 if __name__ == "__main__":
134     main()
135
```

Path: `PaddleCV/icnet/train.py`
Content:
```
1 """Trainer for ICNet model."""
2 from __future__ import absolute_import
3 from __future__ import division
4 from __future__ import print_function
5 from icnet import icnet
6 import cityscape
7 import argparse
8 import functools
9 import sys
10 import os
11 import time
12 import paddle.fluid as fluid
13 import numpy as np
14 from utils import add_arguments, print_arguments, get_feeder_data
15 from paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter
16 from paddle.fluid.initializer import init_on_cpu
17 
18 if 'ce_mode' in os.environ:
19     np.random.seed(10)
20     fluid.default_startup_program().random_seed = 90
21 
22 parser = argparse.ArgumentParser(description=__doc__)
23 add_arg = functools.partial(add_arguments, argparser=parser)
24 # yapf: disable
25 add_arg('batch_size',        int,   16,         "Minibatch size.")
26 add_arg('checkpoint_path',   str,   None,       "Checkpoint svae path.")
27 add_arg('init_model',        str,   None,       "Pretrain model path.")
28 add_arg('use_gpu',           bool,  True,       "Whether use GPU to train.")
29 add_arg('random_mirror',     bool,  True,       "Whether prepare by random mirror.")
30 add_arg('random_scaling',    bool,  True,       "Whether prepare by random scaling.")
31 # yapf: enable
32 
33 LAMBDA1 = 0.16
34 LAMBDA2 = 0.4
35 LAMBDA3 = 1.0
36 LEARNING_RATE = 0.003
37 POWER = 0.9
38 LOG_PERIOD = 100
39 CHECKPOINT_PERIOD = 100
40 TOTAL_STEP = 100
41 
42 no_grad_set = []
43 
44 
45 def create_loss(predict, label, mask, num_classes):
46     predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1])
47     predict = fluid.layers.reshape(predict, shape=[-1, num_classes])
48     label = fluid.layers.reshape(label, shape=[-1, 1])
49     predict = fluid.layers.gather(predict, mask)
50     label = fluid.layers.gather(label, mask)
51     label = fluid.layers.cast(label, dtype="int64")
52     loss = fluid.layers.softmax_with_cross_entropy(predict, label)
53     no_grad_set.append(label.name)
54     return fluid.layers.reduce_mean(loss)
55 
56 
57 def poly_decay():
58     global_step = _decay_step_counter()
59     with init_on_cpu():
60         decayed_lr = LEARNING_RATE * (fluid.layers.pow(
61             (1 - global_step / TOTAL_STEP), POWER))
62     return decayed_lr
63 
64 
65 def train(args):
66     data_shape = cityscape.train_data_shape()
67     num_classes = cityscape.num_classes()
68     # define network
69     images = fluid.layers.data(name='image', shape=data_shape, dtype='float32')
70     label_sub1 = fluid.layers.data(name='label_sub1', shape=[1], dtype='int32')
71     label_sub2 = fluid.layers.data(name='label_sub2', shape=[1], dtype='int32')
72     label_sub4 = fluid.layers.data(name='label_sub4', shape=[1], dtype='int32')
73     mask_sub1 = fluid.layers.data(name='mask_sub1', shape=[-1], dtype='int32')
74     mask_sub2 = fluid.layers.data(name='mask_sub2', shape=[-1], dtype='int32')
75     mask_sub4 = fluid.layers.data(name='mask_sub4', shape=[-1], dtype='int32')
76 
77     sub4_out, sub24_out, sub124_out = icnet(
78         images, num_classes, np.array(data_shape[1:]).astype("float32"))
79     loss_sub4 = create_loss(sub4_out, label_sub4, mask_sub4, num_classes)
80     loss_sub24 = create_loss(sub24_out, label_sub2, mask_sub2, num_classes)
81     loss_sub124 = create_loss(sub124_out, label_sub1, mask_sub1, num_classes)
82     reduced_loss = LAMBDA1 * loss_sub4 + LAMBDA2 * loss_sub24 + LAMBDA3 * loss_sub124
83 
84     regularizer = fluid.regularizer.L2Decay(0.0001)
85     optimizer = fluid.optimizer.Momentum(
86         learning_rate=poly_decay(), momentum=0.9, regularization=regularizer)
87     _, params_grads = optimizer.minimize(reduced_loss, no_grad_set=no_grad_set)
88 
89     # prepare environment
90     place = fluid.CPUPlace()
91     if args.use_gpu:
92         place = fluid.CUDAPlace(0)
93     exe = fluid.Executor(place)
94 
95     exe.run(fluid.default_startup_program())
96 
97     if args.init_model is not None:
98         print("load model from: %s" % args.init_model)
99         sys.stdout.flush()
100         fluid.io.load_params(exe, args.init_model)
101 
102     iter_id = 0
103     t_loss = 0.
104     sub4_loss = 0.
105     sub24_loss = 0.
106     sub124_loss = 0.
107     train_reader = cityscape.train(
108         args.batch_size, flip=args.random_mirror, scaling=args.random_scaling)
109     start_time = time.time()
110     while True:
111         # train a pass
112         for data in train_reader():
113             if iter_id > TOTAL_STEP:
114                 end_time = time.time()
115                 print("kpis	train_duration	%f" % (end_time - start_time))
116                 return
117             iter_id += 1
118             results = exe.run(
119                 feed=get_feeder_data(data, place),
120                 fetch_list=[reduced_loss, loss_sub4, loss_sub24, loss_sub124])
121             t_loss += results[0]
122             sub4_loss += results[1]
123             sub24_loss += results[2]
124             sub124_loss += results[3]
125             # training log
126             if iter_id % LOG_PERIOD == 0:
127                 print(
128                     "Iter[%d]; train loss: %.3f; sub4_loss: %.3f; sub24_loss: %.3f; sub124_loss: %.3f"
129                     % (iter_id, t_loss / LOG_PERIOD, sub4_loss / LOG_PERIOD,
130                        sub24_loss / LOG_PERIOD, sub124_loss / LOG_PERIOD))
131                 print("kpis	train_cost	%f" % (t_loss / LOG_PERIOD))
132 
133                 t_loss = 0.
134                 sub4_loss = 0.
135                 sub24_loss = 0.
136                 sub124_loss = 0.
137                 sys.stdout.flush()
138 
139             if iter_id % CHECKPOINT_PERIOD == 0 and args.checkpoint_path is not None:
140                 dir_name = args.checkpoint_path + "/" + str(iter_id)
141                 fluid.io.save_persistables(exe, dirname=dir_name)
142                 print("Saved checkpoint: %s" % (dir_name))
143 
144 
145 def main():
146     args = parser.parse_args()
147     print_arguments(args)
148     train(args)
149 
150 
151 if __name__ == "__main__":
152     main()
153
```
--- 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/PaddleCV/icnet/infer.py b/PaddleCV/icnet/infer.py
--- a/PaddleCV/icnet/infer.py
+++ b/PaddleCV/icnet/infer.py
@@ -122,6 +122,7 @@
                          fetch_list=[predict])
         cv2.imwrite(args.out_path + "/" + filename + "_result.png",
                     color(result[0]))
+    print("Saved images into: %s" % args.out_path)
 
 
 def main():
diff --git a/PaddleCV/icnet/train.py b/PaddleCV/icnet/train.py
--- a/PaddleCV/icnet/train.py
+++ b/PaddleCV/icnet/train.py
@@ -96,8 +96,11 @@
 
     if args.init_model is not None:
         print("load model from: %s" % args.init_model)
-        sys.stdout.flush()
-        fluid.io.load_params(exe, args.init_model)
+
+        def if_exist(var):
+            return os.path.exists(os.path.join(args.init_model, var.name))
+
+        fluid.io.load_vars(exe, args.init_model, predicate=if_exist)
 
     iter_id = 0
     t_loss = 0.

{"golden_diff": "diff --git a/PaddleCV/icnet/infer.py b/PaddleCV/icnet/infer.py\n--- a/PaddleCV/icnet/infer.py\n+++ b/PaddleCV/icnet/infer.py\n@@ -122,6 +122,7 @@\n                          fetch_list=[predict])\n         cv2.imwrite(args.out_path + \"/\" + filename + \"_result.png\",\n                     color(result[0]))\n+    print(\"Saved images into: %s\" % args.out_path)\n \n \n def main():\ndiff --git a/PaddleCV/icnet/train.py b/PaddleCV/icnet/train.py\n--- a/PaddleCV/icnet/train.py\n+++ b/PaddleCV/icnet/train.py\n@@ -96,8 +96,11 @@\n \n     if args.init_model is not None:\n         print(\"load model from: %s\" % args.init_model)\n-        sys.stdout.flush()\n-        fluid.io.load_params(exe, args.init_model)\n+\n+        def if_exist(var):\n+            return os.path.exists(os.path.join(args.init_model, var.name))\n+\n+        fluid.io.load_vars(exe, args.init_model, predicate=if_exist)\n \n     iter_id = 0\n     t_loss = 0.\n", "issue": "icnet\u52a0\u8f7dmodel_1000\u9884\u8bad\u7ec3\u6a21\u578b\u8fdb\u884c\u8bad\u7ec3\u62a5\u9519\n1.\u4f7f\u7528\u5982\u4e0b\u6307\u4ee4\u8bad\u7ec3\uff1apython train.py --batch_size=16 --use_gpu=True --init_model=\"./model_1000/\"  --checkpoint_path=\"./chkpnt/\"\r\n\u5176\u4e2d\uff1a/model_1000\u4e3a\u6587\u6863\u4e2d\u63d0\u4f9b\u7684\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u5176\u4e2d\u5e76\u65e0conv6\u53c2\u6570\u3002\r\n<img width=\"970\" alt=\"bfd08a979fcb4f72bb443fbca3f53d3f\" src=\"https://user-images.githubusercontent.com/37854899/54199980-24704000-4505-11e9-9bf4-70dc09b0cc86.png\">\r\n2. \u6587\u6863\u4e2deval\u548cinfer\u4e2d\u7684model_path\u5efa\u8bae\u6539\u4e3a./chkpnt/100\uff0c\u4e0e\u8bad\u7ec3\u8f93\u51fa\u4fdd\u6301\u4e00\u81f4\r\n![image](https://user-images.githubusercontent.com/37854899/54200167-aceee080-4505-11e9-9d1a-535a8a09df74.png)\r\n![image](https://user-images.githubusercontent.com/37854899/54200175-b2e4c180-4505-11e9-8cb7-68e5020ebc60.png)\r\n\r\n\n", "before_files": [{"content": "\"\"\"Infer for ICNet model.\"\"\"\nfrom __future__ import print_function\nimport cityscape\nimport argparse\nimport functools\nimport sys\nimport os\nimport cv2\n\nimport paddle.fluid as fluid\nimport paddle\nfrom icnet import icnet\nfrom utils import add_arguments, print_arguments, get_feeder_data\nfrom paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter\nfrom paddle.fluid.initializer import init_on_cpu\nimport numpy as np\n\nIMG_MEAN = np.array((103.939, 116.779, 123.68), dtype=np.float32)\nparser = argparse.ArgumentParser(description=__doc__)\nadd_arg = functools.partial(add_arguments, argparser=parser)\n# yapf: disable\nadd_arg('model_path',        str,   None,         \"Model path.\")\nadd_arg('images_list',       str,   None,         \"List file with images to be infered.\")\nadd_arg('images_path',       str,   None,         \"The images path.\")\nadd_arg('out_path',          str,   \"./output\",         \"Output path.\")\nadd_arg('use_gpu',           bool,  True,       \"Whether use GPU to test.\")\n# yapf: enable\n\ndata_shape = [3, 1024, 2048]\nnum_classes = 19\n\nlabel_colours = [\n    [128, 64, 128],\n    [244, 35, 231],\n    [69, 69, 69]\n    # 0 = road, 1 = sidewalk, 2 = building\n    ,\n    [102, 102, 156],\n    [190, 153, 153],\n    [153, 153, 153]\n    # 3 = wall, 4 = fence, 5 = pole\n    ,\n    [250, 170, 29],\n    [219, 219, 0],\n    [106, 142, 35]\n    # 6 = traffic light, 7 = traffic sign, 8 = vegetation\n    ,\n    [152, 250, 152],\n    [69, 129, 180],\n    [219, 19, 60]\n    # 9 = terrain, 10 = sky, 11 = person\n    ,\n    [255, 0, 0],\n    [0, 0, 142],\n    [0, 0, 69]\n    # 12 = rider, 13 = car, 14 = truck\n    ,\n    [0, 60, 100],\n    [0, 79, 100],\n    [0, 0, 230]\n    # 15 = bus, 16 = train, 17 = motocycle\n    ,\n    [119, 10, 32]\n]\n\n# 18 = bicycle\n\n\ndef color(input):\n    \"\"\"\n    Convert infered result to color image.\n    \"\"\"\n    result = []\n    for i in input.flatten():\n        result.append(\n            [label_colours[i][2], label_colours[i][1], label_colours[i][0]])\n    result = np.array(result).reshape([input.shape[0], input.shape[1], 3])\n    return result\n\n\ndef infer(args):\n    data_shape = cityscape.test_data_shape()\n    num_classes = cityscape.num_classes()\n    # define network\n    images = fluid.layers.data(name='image', shape=data_shape, dtype='float32')\n    _, _, sub124_out = icnet(images, num_classes,\n                             np.array(data_shape[1:]).astype(\"float32\"))\n    predict = fluid.layers.resize_bilinear(\n        sub124_out, out_shape=data_shape[1:3])\n    predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1])\n    predict = fluid.layers.reshape(predict, shape=[-1, num_classes])\n    _, predict = fluid.layers.topk(predict, k=1)\n    predict = fluid.layers.reshape(\n        predict,\n        shape=[data_shape[1], data_shape[2], -1])  # batch_size should be 1\n    inference_program = fluid.default_main_program().clone(for_test=True)\n    # prepare environment\n    place = fluid.CPUPlace()\n    if args.use_gpu:\n        place = fluid.CUDAPlace(0)\n    exe = fluid.Executor(place)\n    exe.run(fluid.default_startup_program())\n    assert os.path.exists(args.model_path)\n    fluid.io.load_params(exe, args.model_path)\n    print(\"loaded model from: %s\" % args.model_path)\n    sys.stdout.flush()\n\n    if not os.path.isdir(args.out_path):\n        os.makedirs(args.out_path)\n\n    for line in open(args.images_list):\n        image_file = args.images_path + \"/\" + line.strip()\n        filename = os.path.basename(image_file)\n        image = paddle.dataset.image.load_image(\n            image_file, is_color=True).astype(\"float32\")\n        image -= IMG_MEAN\n        img = paddle.dataset.image.to_chw(image)[np.newaxis, :]\n        image_t = fluid.core.LoDTensor()\n        image_t.set(img, place)\n        result = exe.run(inference_program,\n                         feed={\"image\": image_t},\n                         fetch_list=[predict])\n        cv2.imwrite(args.out_path + \"/\" + filename + \"_result.png\",\n                    color(result[0]))\n\n\ndef main():\n    args = parser.parse_args()\n    print_arguments(args)\n    infer(args)\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "PaddleCV/icnet/infer.py"}, {"content": "\"\"\"Trainer for ICNet model.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\nfrom icnet import icnet\nimport cityscape\nimport argparse\nimport functools\nimport sys\nimport os\nimport time\nimport paddle.fluid as fluid\nimport numpy as np\nfrom utils import add_arguments, print_arguments, get_feeder_data\nfrom paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter\nfrom paddle.fluid.initializer import init_on_cpu\n\nif 'ce_mode' in os.environ:\n    np.random.seed(10)\n    fluid.default_startup_program().random_seed = 90\n\nparser = argparse.ArgumentParser(description=__doc__)\nadd_arg = functools.partial(add_arguments, argparser=parser)\n# yapf: disable\nadd_arg('batch_size',        int,   16,         \"Minibatch size.\")\nadd_arg('checkpoint_path',   str,   None,       \"Checkpoint svae path.\")\nadd_arg('init_model',        str,   None,       \"Pretrain model path.\")\nadd_arg('use_gpu',           bool,  True,       \"Whether use GPU to train.\")\nadd_arg('random_mirror',     bool,  True,       \"Whether prepare by random mirror.\")\nadd_arg('random_scaling',    bool,  True,       \"Whether prepare by random scaling.\")\n# yapf: enable\n\nLAMBDA1 = 0.16\nLAMBDA2 = 0.4\nLAMBDA3 = 1.0\nLEARNING_RATE = 0.003\nPOWER = 0.9\nLOG_PERIOD = 100\nCHECKPOINT_PERIOD = 100\nTOTAL_STEP = 100\n\nno_grad_set = []\n\n\ndef create_loss(predict, label, mask, num_classes):\n    predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1])\n    predict = fluid.layers.reshape(predict, shape=[-1, num_classes])\n    label = fluid.layers.reshape(label, shape=[-1, 1])\n    predict = fluid.layers.gather(predict, mask)\n    label = fluid.layers.gather(label, mask)\n    label = fluid.layers.cast(label, dtype=\"int64\")\n    loss = fluid.layers.softmax_with_cross_entropy(predict, label)\n    no_grad_set.append(label.name)\n    return fluid.layers.reduce_mean(loss)\n\n\ndef poly_decay():\n    global_step = _decay_step_counter()\n    with init_on_cpu():\n        decayed_lr = LEARNING_RATE * (fluid.layers.pow(\n            (1 - global_step / TOTAL_STEP), POWER))\n    return decayed_lr\n\n\ndef train(args):\n    data_shape = cityscape.train_data_shape()\n    num_classes = cityscape.num_classes()\n    # define network\n    images = fluid.layers.data(name='image', shape=data_shape, dtype='float32')\n    label_sub1 = fluid.layers.data(name='label_sub1', shape=[1], dtype='int32')\n    label_sub2 = fluid.layers.data(name='label_sub2', shape=[1], dtype='int32')\n    label_sub4 = fluid.layers.data(name='label_sub4', shape=[1], dtype='int32')\n    mask_sub1 = fluid.layers.data(name='mask_sub1', shape=[-1], dtype='int32')\n    mask_sub2 = fluid.layers.data(name='mask_sub2', shape=[-1], dtype='int32')\n    mask_sub4 = fluid.layers.data(name='mask_sub4', shape=[-1], dtype='int32')\n\n    sub4_out, sub24_out, sub124_out = icnet(\n        images, num_classes, np.array(data_shape[1:]).astype(\"float32\"))\n    loss_sub4 = create_loss(sub4_out, label_sub4, mask_sub4, num_classes)\n    loss_sub24 = create_loss(sub24_out, label_sub2, mask_sub2, num_classes)\n    loss_sub124 = create_loss(sub124_out, label_sub1, mask_sub1, num_classes)\n    reduced_loss = LAMBDA1 * loss_sub4 + LAMBDA2 * loss_sub24 + LAMBDA3 * loss_sub124\n\n    regularizer = fluid.regularizer.L2Decay(0.0001)\n    optimizer = fluid.optimizer.Momentum(\n        learning_rate=poly_decay(), momentum=0.9, regularization=regularizer)\n    _, params_grads = optimizer.minimize(reduced_loss, no_grad_set=no_grad_set)\n\n    # prepare environment\n    place = fluid.CPUPlace()\n    if args.use_gpu:\n        place = fluid.CUDAPlace(0)\n    exe = fluid.Executor(place)\n\n    exe.run(fluid.default_startup_program())\n\n    if args.init_model is not None:\n        print(\"load model from: %s\" % args.init_model)\n        sys.stdout.flush()\n        fluid.io.load_params(exe, args.init_model)\n\n    iter_id = 0\n    t_loss = 0.\n    sub4_loss = 0.\n    sub24_loss = 0.\n    sub124_loss = 0.\n    train_reader = cityscape.train(\n        args.batch_size, flip=args.random_mirror, scaling=args.random_scaling)\n    start_time = time.time()\n    while True:\n        # train a pass\n        for data in train_reader():\n            if iter_id > TOTAL_STEP:\n                end_time = time.time()\n                print(\"kpis\ttrain_duration\t%f\" % (end_time - start_time))\n                return\n            iter_id += 1\n            results = exe.run(\n                feed=get_feeder_data(data, place),\n                fetch_list=[reduced_loss, loss_sub4, loss_sub24, loss_sub124])\n            t_loss += results[0]\n            sub4_loss += results[1]\n            sub24_loss += results[2]\n            sub124_loss += results[3]\n            # training log\n            if iter_id % LOG_PERIOD == 0:\n                print(\n                    \"Iter[%d]; train loss: %.3f; sub4_loss: %.3f; sub24_loss: %.3f; sub124_loss: %.3f\"\n                    % (iter_id, t_loss / LOG_PERIOD, sub4_loss / LOG_PERIOD,\n                       sub24_loss / LOG_PERIOD, sub124_loss / LOG_PERIOD))\n                print(\"kpis\ttrain_cost\t%f\" % (t_loss / LOG_PERIOD))\n\n                t_loss = 0.\n                sub4_loss = 0.\n                sub24_loss = 0.\n                sub124_loss = 0.\n                sys.stdout.flush()\n\n            if iter_id % CHECKPOINT_PERIOD == 0 and args.checkpoint_path is not None:\n                dir_name = args.checkpoint_path + \"/\" + str(iter_id)\n                fluid.io.save_persistables(exe, dirname=dir_name)\n                print(\"Saved checkpoint: %s\" % (dir_name))\n\n\ndef main():\n    args = parser.parse_args()\n    print_arguments(args)\n    train(args)\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "PaddleCV/icnet/train.py"}], "after_files": [{"content": "\"\"\"Infer for ICNet model.\"\"\"\nfrom __future__ import print_function\nimport cityscape\nimport argparse\nimport functools\nimport sys\nimport os\nimport cv2\n\nimport paddle.fluid as fluid\nimport paddle\nfrom icnet import icnet\nfrom utils import add_arguments, print_arguments, get_feeder_data\nfrom paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter\nfrom paddle.fluid.initializer import init_on_cpu\nimport numpy as np\n\nIMG_MEAN = np.array((103.939, 116.779, 123.68), dtype=np.float32)\nparser = argparse.ArgumentParser(description=__doc__)\nadd_arg = functools.partial(add_arguments, argparser=parser)\n# yapf: disable\nadd_arg('model_path',        str,   None,         \"Model path.\")\nadd_arg('images_list',       str,   None,         \"List file with images to be infered.\")\nadd_arg('images_path',       str,   None,         \"The images path.\")\nadd_arg('out_path',          str,   \"./output\",         \"Output path.\")\nadd_arg('use_gpu',           bool,  True,       \"Whether use GPU to test.\")\n# yapf: enable\n\ndata_shape = [3, 1024, 2048]\nnum_classes = 19\n\nlabel_colours = [\n    [128, 64, 128],\n    [244, 35, 231],\n    [69, 69, 69]\n    # 0 = road, 1 = sidewalk, 2 = building\n    ,\n    [102, 102, 156],\n    [190, 153, 153],\n    [153, 153, 153]\n    # 3 = wall, 4 = fence, 5 = pole\n    ,\n    [250, 170, 29],\n    [219, 219, 0],\n    [106, 142, 35]\n    # 6 = traffic light, 7 = traffic sign, 8 = vegetation\n    ,\n    [152, 250, 152],\n    [69, 129, 180],\n    [219, 19, 60]\n    # 9 = terrain, 10 = sky, 11 = person\n    ,\n    [255, 0, 0],\n    [0, 0, 142],\n    [0, 0, 69]\n    # 12 = rider, 13 = car, 14 = truck\n    ,\n    [0, 60, 100],\n    [0, 79, 100],\n    [0, 0, 230]\n    # 15 = bus, 16 = train, 17 = motocycle\n    ,\n    [119, 10, 32]\n]\n\n# 18 = bicycle\n\n\ndef color(input):\n    \"\"\"\n    Convert infered result to color image.\n    \"\"\"\n    result = []\n    for i in input.flatten():\n        result.append(\n            [label_colours[i][2], label_colours[i][1], label_colours[i][0]])\n    result = np.array(result).reshape([input.shape[0], input.shape[1], 3])\n    return result\n\n\ndef infer(args):\n    data_shape = cityscape.test_data_shape()\n    num_classes = cityscape.num_classes()\n    # define network\n    images = fluid.layers.data(name='image', shape=data_shape, dtype='float32')\n    _, _, sub124_out = icnet(images, num_classes,\n                             np.array(data_shape[1:]).astype(\"float32\"))\n    predict = fluid.layers.resize_bilinear(\n        sub124_out, out_shape=data_shape[1:3])\n    predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1])\n    predict = fluid.layers.reshape(predict, shape=[-1, num_classes])\n    _, predict = fluid.layers.topk(predict, k=1)\n    predict = fluid.layers.reshape(\n        predict,\n        shape=[data_shape[1], data_shape[2], -1])  # batch_size should be 1\n    inference_program = fluid.default_main_program().clone(for_test=True)\n    # prepare environment\n    place = fluid.CPUPlace()\n    if args.use_gpu:\n        place = fluid.CUDAPlace(0)\n    exe = fluid.Executor(place)\n    exe.run(fluid.default_startup_program())\n    assert os.path.exists(args.model_path)\n    fluid.io.load_params(exe, args.model_path)\n    print(\"loaded model from: %s\" % args.model_path)\n    sys.stdout.flush()\n\n    if not os.path.isdir(args.out_path):\n        os.makedirs(args.out_path)\n\n    for line in open(args.images_list):\n        image_file = args.images_path + \"/\" + line.strip()\n        filename = os.path.basename(image_file)\n        image = paddle.dataset.image.load_image(\n            image_file, is_color=True).astype(\"float32\")\n        image -= IMG_MEAN\n        img = paddle.dataset.image.to_chw(image)[np.newaxis, :]\n        image_t = fluid.core.LoDTensor()\n        image_t.set(img, place)\n        result = exe.run(inference_program,\n                         feed={\"image\": image_t},\n                         fetch_list=[predict])\n        cv2.imwrite(args.out_path + \"/\" + filename + \"_result.png\",\n                    color(result[0]))\n    print(\"Saved images into: %s\" % args.out_path)\n\n\ndef main():\n    args = parser.parse_args()\n    print_arguments(args)\n    infer(args)\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "PaddleCV/icnet/infer.py"}, {"content": "\"\"\"Trainer for ICNet model.\"\"\"\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\nfrom icnet import icnet\nimport cityscape\nimport argparse\nimport functools\nimport sys\nimport os\nimport time\nimport paddle.fluid as fluid\nimport numpy as np\nfrom utils import add_arguments, print_arguments, get_feeder_data\nfrom paddle.fluid.layers.learning_rate_scheduler import _decay_step_counter\nfrom paddle.fluid.initializer import init_on_cpu\n\nif 'ce_mode' in os.environ:\n    np.random.seed(10)\n    fluid.default_startup_program().random_seed = 90\n\nparser = argparse.ArgumentParser(description=__doc__)\nadd_arg = functools.partial(add_arguments, argparser=parser)\n# yapf: disable\nadd_arg('batch_size',        int,   16,         \"Minibatch size.\")\nadd_arg('checkpoint_path',   str,   None,       \"Checkpoint svae path.\")\nadd_arg('init_model',        str,   None,       \"Pretrain model path.\")\nadd_arg('use_gpu',           bool,  True,       \"Whether use GPU to train.\")\nadd_arg('random_mirror',     bool,  True,       \"Whether prepare by random mirror.\")\nadd_arg('random_scaling',    bool,  True,       \"Whether prepare by random scaling.\")\n# yapf: enable\n\nLAMBDA1 = 0.16\nLAMBDA2 = 0.4\nLAMBDA3 = 1.0\nLEARNING_RATE = 0.003\nPOWER = 0.9\nLOG_PERIOD = 100\nCHECKPOINT_PERIOD = 100\nTOTAL_STEP = 100\n\nno_grad_set = []\n\n\ndef create_loss(predict, label, mask, num_classes):\n    predict = fluid.layers.transpose(predict, perm=[0, 2, 3, 1])\n    predict = fluid.layers.reshape(predict, shape=[-1, num_classes])\n    label = fluid.layers.reshape(label, shape=[-1, 1])\n    predict = fluid.layers.gather(predict, mask)\n    label = fluid.layers.gather(label, mask)\n    label = fluid.layers.cast(label, dtype=\"int64\")\n    loss = fluid.layers.softmax_with_cross_entropy(predict, label)\n    no_grad_set.append(label.name)\n    return fluid.layers.reduce_mean(loss)\n\n\ndef poly_decay():\n    global_step = _decay_step_counter()\n    with init_on_cpu():\n        decayed_lr = LEARNING_RATE * (fluid.layers.pow(\n            (1 - global_step / TOTAL_STEP), POWER))\n    return decayed_lr\n\n\ndef train(args):\n    data_shape = cityscape.train_data_shape()\n    num_classes = cityscape.num_classes()\n    # define network\n    images = fluid.layers.data(name='image', shape=data_shape, dtype='float32')\n    label_sub1 = fluid.layers.data(name='label_sub1', shape=[1], dtype='int32')\n    label_sub2 = fluid.layers.data(name='label_sub2', shape=[1], dtype='int32')\n    label_sub4 = fluid.layers.data(name='label_sub4', shape=[1], dtype='int32')\n    mask_sub1 = fluid.layers.data(name='mask_sub1', shape=[-1], dtype='int32')\n    mask_sub2 = fluid.layers.data(name='mask_sub2', shape=[-1], dtype='int32')\n    mask_sub4 = fluid.layers.data(name='mask_sub4', shape=[-1], dtype='int32')\n\n    sub4_out, sub24_out, sub124_out = icnet(\n        images, num_classes, np.array(data_shape[1:]).astype(\"float32\"))\n    loss_sub4 = create_loss(sub4_out, label_sub4, mask_sub4, num_classes)\n    loss_sub24 = create_loss(sub24_out, label_sub2, mask_sub2, num_classes)\n    loss_sub124 = create_loss(sub124_out, label_sub1, mask_sub1, num_classes)\n    reduced_loss = LAMBDA1 * loss_sub4 + LAMBDA2 * loss_sub24 + LAMBDA3 * loss_sub124\n\n    regularizer = fluid.regularizer.L2Decay(0.0001)\n    optimizer = fluid.optimizer.Momentum(\n        learning_rate=poly_decay(), momentum=0.9, regularization=regularizer)\n    _, params_grads = optimizer.minimize(reduced_loss, no_grad_set=no_grad_set)\n\n    # prepare environment\n    place = fluid.CPUPlace()\n    if args.use_gpu:\n        place = fluid.CUDAPlace(0)\n    exe = fluid.Executor(place)\n\n    exe.run(fluid.default_startup_program())\n\n    if args.init_model is not None:\n        print(\"load model from: %s\" % args.init_model)\n\n        def if_exist(var):\n            return os.path.exists(os.path.join(args.init_model, var.name))\n\n        fluid.io.load_vars(exe, args.init_model, predicate=if_exist)\n\n    iter_id = 0\n    t_loss = 0.\n    sub4_loss = 0.\n    sub24_loss = 0.\n    sub124_loss = 0.\n    train_reader = cityscape.train(\n        args.batch_size, flip=args.random_mirror, scaling=args.random_scaling)\n    start_time = time.time()\n    while True:\n        # train a pass\n        for data in train_reader():\n            if iter_id > TOTAL_STEP:\n                end_time = time.time()\n                print(\"kpis\ttrain_duration\t%f\" % (end_time - start_time))\n                return\n            iter_id += 1\n            results = exe.run(\n                feed=get_feeder_data(data, place),\n                fetch_list=[reduced_loss, loss_sub4, loss_sub24, loss_sub124])\n            t_loss += results[0]\n            sub4_loss += results[1]\n            sub24_loss += results[2]\n            sub124_loss += results[3]\n            # training log\n            if iter_id % LOG_PERIOD == 0:\n                print(\n                    \"Iter[%d]; train loss: %.3f; sub4_loss: %.3f; sub24_loss: %.3f; sub124_loss: %.3f\"\n                    % (iter_id, t_loss / LOG_PERIOD, sub4_loss / LOG_PERIOD,\n                       sub24_loss / LOG_PERIOD, sub124_loss / LOG_PERIOD))\n                print(\"kpis\ttrain_cost\t%f\" % (t_loss / LOG_PERIOD))\n\n                t_loss = 0.\n                sub4_loss = 0.\n                sub24_loss = 0.\n                sub124_loss = 0.\n                sys.stdout.flush()\n\n            if iter_id % CHECKPOINT_PERIOD == 0 and args.checkpoint_path is not None:\n                dir_name = args.checkpoint_path + \"/\" + str(iter_id)\n                fluid.io.save_persistables(exe, dirname=dir_name)\n                print(\"Saved checkpoint: %s\" % (dir_name))\n\n\ndef main():\n    args = parser.parse_args()\n    print_arguments(args)\n    train(args)\n\n\nif __name__ == \"__main__\":\n    main()\n", "path": "PaddleCV/icnet/train.py"}]}