浏览代码

modify explanation

sunyanfang01 5 年之前
父节点
当前提交
6c503961e7
共有 43 个文件被更改,包括 139 次插入39 次删除
  1. 1 0
      paddlex/__init__.py
  2. 5 3
      paddlex/cv/models/classifier.py
  3. 15 20
      paddlex/cv/models/explanation/as_data_reader/data_path_utils.py
  4. 16 2
      paddlex/cv/models/explanation/as_data_reader/readers.py
  5. 14 0
      paddlex/cv/models/explanation/core/_session_preparation.py
  6. 14 0
      paddlex/cv/models/explanation/core/explanation.py
  7. 14 0
      paddlex/cv/models/explanation/core/explanation_algorithms.py
  8. 14 3
      paddlex/cv/models/explanation/core/lime_base.py
  9. 13 3
      paddlex/cv/models/explanation/visualize.py
  10. 二进制
      paddlex/cv/nets/__pycache__/__init__.cpython-37.pyc
  11. 二进制
      paddlex/cv/nets/__pycache__/backbone_utils.cpython-37.pyc
  12. 二进制
      paddlex/cv/nets/__pycache__/darknet.cpython-37.pyc
  13. 二进制
      paddlex/cv/nets/__pycache__/densenet.cpython-37.pyc
  14. 二进制
      paddlex/cv/nets/__pycache__/mobilenet_v1.cpython-37.pyc
  15. 二进制
      paddlex/cv/nets/__pycache__/mobilenet_v2.cpython-37.pyc
  16. 二进制
      paddlex/cv/nets/__pycache__/mobilenet_v3.cpython-37.pyc
  17. 二进制
      paddlex/cv/nets/__pycache__/resnet.cpython-37.pyc
  18. 二进制
      paddlex/cv/nets/__pycache__/shufflenet_v2.cpython-37.pyc
  19. 二进制
      paddlex/cv/nets/__pycache__/xception.cpython-37.pyc
  20. 2 1
      paddlex/cv/nets/darknet.py
  21. 2 1
      paddlex/cv/nets/densenet.py
  22. 二进制
      paddlex/cv/nets/detection/__pycache__/__init__.cpython-37.pyc
  23. 二进制
      paddlex/cv/nets/detection/__pycache__/bbox_head.cpython-37.pyc
  24. 二进制
      paddlex/cv/nets/detection/__pycache__/faster_rcnn.cpython-37.pyc
  25. 二进制
      paddlex/cv/nets/detection/__pycache__/fpn.cpython-37.pyc
  26. 二进制
      paddlex/cv/nets/detection/__pycache__/mask_head.cpython-37.pyc
  27. 二进制
      paddlex/cv/nets/detection/__pycache__/mask_rcnn.cpython-37.pyc
  28. 二进制
      paddlex/cv/nets/detection/__pycache__/roi_extractor.cpython-37.pyc
  29. 二进制
      paddlex/cv/nets/detection/__pycache__/rpn_head.cpython-37.pyc
  30. 二进制
      paddlex/cv/nets/detection/__pycache__/yolo_v3.cpython-37.pyc
  31. 2 1
      paddlex/cv/nets/mobilenet_v1.py
  32. 2 0
      paddlex/cv/nets/mobilenet_v2.py
  33. 2 1
      paddlex/cv/nets/mobilenet_v3.py
  34. 1 3
      paddlex/cv/nets/resnet.py
  35. 二进制
      paddlex/cv/nets/segmentation/__pycache__/__init__.cpython-37.pyc
  36. 二进制
      paddlex/cv/nets/segmentation/__pycache__/deeplabv3p.cpython-37.pyc
  37. 二进制
      paddlex/cv/nets/segmentation/__pycache__/unet.cpython-37.pyc
  38. 二进制
      paddlex/cv/nets/segmentation/model_utils/__pycache__/__init__.cpython-37.pyc
  39. 二进制
      paddlex/cv/nets/segmentation/model_utils/__pycache__/libs.cpython-37.pyc
  40. 二进制
      paddlex/cv/nets/segmentation/model_utils/__pycache__/loss.cpython-37.pyc
  41. 2 0
      paddlex/cv/nets/shufflenet_v2.py
  42. 2 1
      paddlex/cv/nets/xception.py
  43. 18 0
      paddlex/explanation.py

+ 1 - 0
paddlex/__init__.py

@@ -28,6 +28,7 @@ from . import seg
 from . import cls
 from . import slim
 from . import tools
+from . import explanation
 
 try:
     import pycocotools

+ 5 - 3
paddlex/cv/models/classifier.py

@@ -60,10 +60,12 @@ class BaseClassifier(BaseAPI):
         if mode != 'test':
             label = fluid.data(dtype='int64', shape=[None, 1], name='label')
         model = getattr(paddlex.cv.nets, str.lower(self.model_name))
-        net_out, feat = model(image, num_classes=self.num_classes)
-        softmax_out = fluid.layers.softmax(net_out, use_cudnn=False)
+        net_out = model(image, num_classes=self.num_classes)
+        softmax_out = fluid.layers.softmax(net_out['logits'], use_cudnn=False)
         inputs = OrderedDict([('image', image)])
-        outputs = OrderedDict([('predict', softmax_out), ('net_out', feat[-1])])
+        outputs = net_out
+        outputs.update({'predict': softmax_out})
+        outputs.move_to_end('predict', last=False)
         if mode != 'test':
             cost = fluid.layers.cross_entropy(input=softmax_out, label=label)
             avg_cost = fluid.layers.mean(cost)

+ 15 - 20
paddlex/cv/models/explanation/as_data_reader/data_path_utils.py

@@ -1,27 +1,22 @@
-import os
-
-
-def imagenet_val_files_and_labels(dataset_directory):
-    classes = open(os.path.join(dataset_directory, 'imagenet_lsvrc_2015_synsets.txt')).readlines()
-    class_to_indx = {classes[i].split('\n')[0]: i for i in range(len(classes))}
-
-    images_path = os.path.join(dataset_directory, 'val')
-    filenames = []
-    labels = []
-    lines = open(os.path.join(dataset_directory, 'imagenet_2012_validation_synset_labels.txt'), 'r').readlines()
-    for i, line in enumerate(lines):
-        class_name = line.split('\n')[0]
-        a = 'ILSVRC2012_val_%08d.JPEG' % (i + 1)
-        filenames.append(f'{images_path}/{a}')
-        labels.append(class_to_indx[class_name])
-        # print(filenames[-1], labels[-1])
-
-    return filenames, labels
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
 
+import os
 
 def _find_classes(dir):
     # Faster and available in Python 3.5 and above
     classes = [d.name for d in os.scandir(dir) if d.is_dir()]
     classes.sort()
     class_to_idx = {classes[i]: i for i in range(len(classes))}
-    return classes, class_to_idx
+    return classes, class_to_idx

+ 16 - 2
paddlex/cv/models/explanation/as_data_reader/readers.py

@@ -1,10 +1,24 @@
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+
 import os
-import sys; sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
+import sys
 import cv2
 import numpy as np
 import six
 import glob
-from as_data_reader.data_path_utils import _find_classes
+from .data_path_utils import _find_classes
 from PIL import Image
 
 

+ 14 - 0
paddlex/cv/models/explanation/core/_session_preparation.py

@@ -1,3 +1,17 @@
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+
 import os
 import paddle.fluid as fluid
 import numpy as np

+ 14 - 0
paddlex/cv/models/explanation/core/explanation.py

@@ -1,3 +1,17 @@
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+
 from .explanation_algorithms import CAM, LIME, NormLIME
 
 

+ 14 - 0
paddlex/cv/models/explanation/core/explanation_algorithms.py

@@ -1,3 +1,17 @@
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+
 import os
 import numpy as np
 import time

+ 14 - 3
paddlex/cv/models/explanation/core/lime_base.py

@@ -1,6 +1,17 @@
-"""
-Contains abstract functionality for learning locally linear sparse model.
-"""
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+
 from __future__ import print_function
 import numpy as np
 import scipy as sp

+ 13 - 3
paddlex/cv/models/explanation/visualize.py

@@ -1,6 +1,16 @@
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
+#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+#Licensed under the Apache License, Version 2.0 (the "License");
+#you may not use this file except in compliance with the License.
+#You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
 
 import os
 import cv2

二进制
paddlex/cv/nets/__pycache__/__init__.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/backbone_utils.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/darknet.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/densenet.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/mobilenet_v1.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/mobilenet_v2.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/mobilenet_v3.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/resnet.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/shufflenet_v2.cpython-37.pyc


二进制
paddlex/cv/nets/__pycache__/xception.cpython-37.pyc


+ 2 - 1
paddlex/cv/nets/darknet.py

@@ -18,6 +18,7 @@ from __future__ import print_function
 
 import six
 import math
+from collections import OrderedDict
 
 from paddle import fluid
 from paddle.fluid.param_attr import ParamAttr
@@ -182,6 +183,6 @@ class DarkNet(object):
                     initializer=fluid.initializer.Uniform(-stdv, stdv),
                     name='fc_weights'),
                 bias_attr=ParamAttr(name='fc_offset'))
-            return out
+            return OrderedDict([('logits', out)])
 
         return blocks

+ 2 - 1
paddlex/cv/nets/densenet.py

@@ -15,6 +15,7 @@ from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function
 
+from collections import OrderedDict
 import paddle
 import paddle.fluid as fluid
 import math
@@ -96,7 +97,7 @@ class DenseNet(object):
                     initializer=fluid.initializer.Uniform(-stdv, stdv),
                     name="fc_weights"),
                 bias_attr=ParamAttr(name='fc_offset'))
-            return out
+            return OrderedDict([('logits', out)])
 
     def make_transition(self, input, num_output_features, name=None):
         bn_ac = fluid.layers.batch_norm(

二进制
paddlex/cv/nets/detection/__pycache__/__init__.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/bbox_head.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/faster_rcnn.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/fpn.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/mask_head.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/mask_rcnn.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/roi_extractor.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/rpn_head.cpython-37.pyc


二进制
paddlex/cv/nets/detection/__pycache__/yolo_v3.cpython-37.pyc


+ 2 - 1
paddlex/cv/nets/mobilenet_v1.py

@@ -16,6 +16,7 @@ from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function
 
+from collections import OrderedDict
 from paddle import fluid
 from paddle.fluid.param_attr import ParamAttr
 from paddle.fluid.regularizer import L2Decay
@@ -196,7 +197,7 @@ class MobileNetV1(object):
                 param_attr=ParamAttr(
                     initializer=fluid.initializer.MSRA(), name="fc7_weights"),
                 bias_attr=ParamAttr(name="fc7_offset"))
-            return output
+            return OrderedDict([('logits', out)])
 
         if not self.with_extra_blocks:
             return blocks

+ 2 - 0
paddlex/cv/nets/mobilenet_v2.py

@@ -14,6 +14,7 @@
 from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function
+from collections import OrderedDict
 import paddle.fluid as fluid
 from paddle.fluid.param_attr import ParamAttr
 
@@ -109,6 +110,7 @@ class MobileNetV2:
                 size=self.num_classes,
                 param_attr=ParamAttr(name='fc10_weights'),
                 bias_attr=ParamAttr(name='fc10_offset'))
+            return OrderedDict([('logits', output)])
         return output
 
     def modify_bottle_params(self, output_stride=None):

+ 2 - 1
paddlex/cv/nets/mobilenet_v3.py

@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+from collections import OrderedDict
 import paddle.fluid as fluid
 from paddle.fluid.param_attr import ParamAttr
 from paddle.fluid.regularizer import L2Decay
@@ -327,7 +328,7 @@ class MobileNetV3():
                                   size=self.num_classes,
                                   param_attr=ParamAttr(name='fc_weights'),
                                   bias_attr=ParamAttr(name='fc_offset'))            
-            return out
+            return OrderedDict([('logits', out)])
 
         if not self.with_extra_blocks:
             return blocks

+ 1 - 3
paddlex/cv/nets/resnet.py

@@ -120,7 +120,6 @@ class ResNet(object):
         self.num_classes = num_classes
         self.lr_mult_list = lr_mult_list
         self.curr_stage = 0
-        self.features = []
 
     def _conv_offset(self,
                      input,
@@ -475,8 +474,7 @@ class ResNet(object):
                 size=self.num_classes,
                 param_attr=fluid.param_attr.ParamAttr(
                     initializer=fluid.initializer.Uniform(-stdv, stdv)))
-            self.features.append(out)
-            return out, self.features
+            return OrderedDict([('logits', out)])
 
         return OrderedDict([('res{}_sum'.format(self.feature_maps[idx]), feat)
                             for idx, feat in enumerate(res_endpoints)])

二进制
paddlex/cv/nets/segmentation/__pycache__/__init__.cpython-37.pyc


二进制
paddlex/cv/nets/segmentation/__pycache__/deeplabv3p.cpython-37.pyc


二进制
paddlex/cv/nets/segmentation/__pycache__/unet.cpython-37.pyc


二进制
paddlex/cv/nets/segmentation/model_utils/__pycache__/__init__.cpython-37.pyc


二进制
paddlex/cv/nets/segmentation/model_utils/__pycache__/libs.cpython-37.pyc


二进制
paddlex/cv/nets/segmentation/model_utils/__pycache__/loss.cpython-37.pyc


+ 2 - 0
paddlex/cv/nets/shufflenet_v2.py

@@ -15,6 +15,7 @@
 from __future__ import absolute_import
 from __future__ import division
 from __future__ import print_function
+from collections import OrderedDict
 import paddle.fluid as fluid
 from paddle.fluid.initializer import MSRA
 from paddle.fluid.param_attr import ParamAttr
@@ -101,6 +102,7 @@ class ShuffleNetV2():
                 size=self.num_classes,
                 param_attr=ParamAttr(initializer=MSRA(), name='fc6_weights'),
                 bias_attr=ParamAttr(name='fc6_offset'))
+            return OrderedDict([('logits', output)])
         return output
 
     def conv_bn_layer(self,

+ 2 - 1
paddlex/cv/nets/xception.py

@@ -19,6 +19,7 @@ from __future__ import print_function
 import contextlib
 import paddle
 import math
+from collections import OrderedDict
 import paddle.fluid as fluid
 from .segmentation.model_utils.libs import scope, name_scope
 from .segmentation.model_utils.libs import bn, bn_relu, relu
@@ -104,7 +105,7 @@ class Xception():
                         initializer=fluid.initializer.Uniform(-stdv, stdv)),
                     bias_attr=fluid.param_attr.ParamAttr(name='bias'))
 
-            return out
+            return OrderedDict([('logits', out)])
         else:
             return data
 

+ 18 - 0
paddlex/explanation.py

@@ -0,0 +1,18 @@
+# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+from __future__ import absolute_import
+from .cv.models.explanation import visualize
+
+visualize = visualize.visualize