From 1ac66f63a0075a33fe85e04f4888cb81bc085747 Mon Sep 17 00:00:00 2001 From: Gaohan123 <547917980@qq.com> Date: Wed, 21 Oct 2020 09:33:27 +0800 Subject: [PATCH] init version for file parser modification for parser modification for parser with containers fix bugs for parser fix value vacancy for parser fix typos for parser adaptation for proto modification rearrange image data from events fix small typo add comments and some error message delete example modify init setting of parser modify some structure of parser fix pylint warnings modify parser for parsing one event every time modify for review add some comments modify proto file modify for open source comments --- .../proto_files/mindinsight_summary.proto | 51 ++ .../proto_files/mindinsight_summary_pb2.py | 559 +++++++++++++++--- mindinsight/explainer/__init__.py | 21 + mindinsight/explainer/common/__init__.py | 15 + mindinsight/explainer/common/enums.py | 52 ++ mindinsight/explainer/common/log.py | 19 + mindinsight/explainer/manager/__init__.py | 15 + .../explainer/manager/explain_parser.py | 222 +++++++ 8 files changed, 880 insertions(+), 74 deletions(-) create mode 100644 mindinsight/explainer/__init__.py create mode 100644 mindinsight/explainer/common/__init__.py create mode 100644 mindinsight/explainer/common/enums.py create mode 100644 mindinsight/explainer/common/log.py create mode 100644 mindinsight/explainer/manager/__init__.py create mode 100644 mindinsight/explainer/manager/explain_parser.py diff --git a/mindinsight/datavisual/proto_files/mindinsight_summary.proto b/mindinsight/datavisual/proto_files/mindinsight_summary.proto index 2b79907b..de562bbb 100644 --- a/mindinsight/datavisual/proto_files/mindinsight_summary.proto +++ b/mindinsight/datavisual/proto_files/mindinsight_summary.proto @@ -39,6 +39,9 @@ message Event { // Summary data Summary summary = 5; + + Explain explain = 6; + } } @@ -101,3 +104,51 @@ message Summary { // Set of values for the summary. repeated Value value = 1; } + + +message Explain { + message Inference{ + repeated float ground_truth_prob = 1; + repeated int32 predicted_label = 2; + repeated float predicted_prob = 3; + } + + message Explanation{ + optional string explain_method = 1; + optional int32 label = 2; + optional bytes hitmap = 3; + } + + message Benchmark{ + message TotalScore{ + optional string benchmark_method = 1; + optional float score = 2; + } + message LabelScore{ + repeated float score = 1; + optional string benchmark_method = 2; + } + + optional string explain_method = 1; + repeated TotalScore total_score = 2; + repeated LabelScore label_score = 3; + } + + message Metadata{ + repeated string label = 1; + repeated string explain_method = 2; + repeated string benchmark_method = 3; + } + + optional string image_id = 1; // The Metadata and image id must have one fill in + optional bytes image_data = 2; + repeated int32 ground_truth_label = 3; + + + optional Inference inference = 4; + repeated Explanation explanation = 5; + repeated Benchmark benchmark = 6; + + optional Metadata metadata = 7; + optional string status = 8; // enum value: run, end +} \ No newline at end of file diff --git a/mindinsight/datavisual/proto_files/mindinsight_summary_pb2.py b/mindinsight/datavisual/proto_files/mindinsight_summary_pb2.py index ba45bbb9..11819f10 100644 --- a/mindinsight/datavisual/proto_files/mindinsight_summary_pb2.py +++ b/mindinsight/datavisual/proto_files/mindinsight_summary_pb2.py @@ -1,11 +1,13 @@ -# -*- coding: utf-8 -*- # Generated by the protocol buffer compiler. DO NOT EDIT! # source: mindinsight_summary.proto +import sys +_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1')) from google.protobuf import descriptor as _descriptor from google.protobuf import message as _message from google.protobuf import reflection as _reflection from google.protobuf import symbol_database as _symbol_database +from google.protobuf import descriptor_pb2 # @@protoc_insertion_point(imports) _sym_db = _symbol_database.Default() @@ -18,8 +20,7 @@ DESCRIPTOR = _descriptor.FileDescriptor( name='mindinsight_summary.proto', package='mindinsight', syntax='proto2', - serialized_options=b'\370\001\001', - serialized_pb=b'\n\x19mindinsight_summary.proto\x12\x0bmindinsight\x1a\x18mindinsight_anf_ir.proto\"\x9a\x01\n\x05\x45vent\x12\x11\n\twall_time\x18\x01 \x02(\x01\x12\x0c\n\x04step\x18\x02 \x01(\x03\x12\x11\n\x07version\x18\x03 \x01(\tH\x00\x12,\n\tgraph_def\x18\x04 \x01(\x0b\x32\x17.mindinsight.GraphProtoH\x00\x12\'\n\x07summary\x18\x05 \x01(\x0b\x32\x14.mindinsight.SummaryH\x00\x42\x06\n\x04what\"\xc0\x04\n\x07Summary\x12)\n\x05value\x18\x01 \x03(\x0b\x32\x1a.mindinsight.Summary.Value\x1aQ\n\x05Image\x12\x0e\n\x06height\x18\x01 \x02(\x05\x12\r\n\x05width\x18\x02 \x02(\x05\x12\x12\n\ncolorspace\x18\x03 \x02(\x05\x12\x15\n\rencoded_image\x18\x04 \x02(\x0c\x1a\xf0\x01\n\tHistogram\x12\x36\n\x07\x62uckets\x18\x01 \x03(\x0b\x32%.mindinsight.Summary.Histogram.bucket\x12\x11\n\tnan_count\x18\x02 \x01(\x03\x12\x15\n\rpos_inf_count\x18\x03 \x01(\x03\x12\x15\n\rneg_inf_count\x18\x04 \x01(\x03\x12\x0b\n\x03max\x18\x05 \x01(\x01\x12\x0b\n\x03min\x18\x06 \x01(\x01\x12\x0b\n\x03sum\x18\x07 \x01(\x01\x12\r\n\x05\x63ount\x18\x08 \x01(\x03\x1a\x34\n\x06\x62ucket\x12\x0c\n\x04left\x18\x01 \x02(\x01\x12\r\n\x05width\x18\x02 \x02(\x01\x12\r\n\x05\x63ount\x18\x03 \x02(\x03\x1a\xc3\x01\n\x05Value\x12\x0b\n\x03tag\x18\x01 \x02(\t\x12\x16\n\x0cscalar_value\x18\x03 \x01(\x02H\x00\x12+\n\x05image\x18\x04 \x01(\x0b\x32\x1a.mindinsight.Summary.ImageH\x00\x12*\n\x06tensor\x18\x08 \x01(\x0b\x32\x18.mindinsight.TensorProtoH\x00\x12\x33\n\thistogram\x18\t \x01(\x0b\x32\x1e.mindinsight.Summary.HistogramH\x00\x42\x07\n\x05valueB\x03\xf8\x01\x01' + serialized_pb=_b('\n\x19mindinsight_summary.proto\x12\x0bmindinsight\x1a\x18mindinsight_anf_ir.proto\"\xc3\x01\n\x05\x45vent\x12\x11\n\twall_time\x18\x01 \x02(\x01\x12\x0c\n\x04step\x18\x02 \x01(\x03\x12\x11\n\x07version\x18\x03 \x01(\tH\x00\x12,\n\tgraph_def\x18\x04 \x01(\x0b\x32\x17.mindinsight.GraphProtoH\x00\x12\'\n\x07summary\x18\x05 \x01(\x0b\x32\x14.mindinsight.SummaryH\x00\x12\'\n\x07\x65xplain\x18\x06 \x01(\x0b\x32\x14.mindinsight.ExplainH\x00\x42\x06\n\x04what\"\xc0\x04\n\x07Summary\x12)\n\x05value\x18\x01 \x03(\x0b\x32\x1a.mindinsight.Summary.Value\x1aQ\n\x05Image\x12\x0e\n\x06height\x18\x01 \x02(\x05\x12\r\n\x05width\x18\x02 \x02(\x05\x12\x12\n\ncolorspace\x18\x03 \x02(\x05\x12\x15\n\rencoded_image\x18\x04 \x02(\x0c\x1a\xf0\x01\n\tHistogram\x12\x36\n\x07\x62uckets\x18\x01 \x03(\x0b\x32%.mindinsight.Summary.Histogram.bucket\x12\x11\n\tnan_count\x18\x02 \x01(\x03\x12\x15\n\rpos_inf_count\x18\x03 \x01(\x03\x12\x15\n\rneg_inf_count\x18\x04 \x01(\x03\x12\x0b\n\x03max\x18\x05 \x01(\x01\x12\x0b\n\x03min\x18\x06 \x01(\x01\x12\x0b\n\x03sum\x18\x07 \x01(\x01\x12\r\n\x05\x63ount\x18\x08 \x01(\x03\x1a\x34\n\x06\x62ucket\x12\x0c\n\x04left\x18\x01 \x02(\x01\x12\r\n\x05width\x18\x02 \x02(\x01\x12\r\n\x05\x63ount\x18\x03 \x02(\x03\x1a\xc3\x01\n\x05Value\x12\x0b\n\x03tag\x18\x01 \x02(\t\x12\x16\n\x0cscalar_value\x18\x03 \x01(\x02H\x00\x12+\n\x05image\x18\x04 \x01(\x0b\x32\x1a.mindinsight.Summary.ImageH\x00\x12*\n\x06tensor\x18\x08 \x01(\x0b\x32\x18.mindinsight.TensorProtoH\x00\x12\x33\n\thistogram\x18\t \x01(\x0b\x32\x1e.mindinsight.Summary.HistogramH\x00\x42\x07\n\x05value\"\xa9\x06\n\x07\x45xplain\x12\x10\n\x08image_id\x18\x01 \x01(\t\x12\x12\n\nimage_data\x18\x02 \x01(\x0c\x12\x1a\n\x12ground_truth_label\x18\x03 \x03(\x05\x12\x31\n\tinference\x18\x04 \x01(\x0b\x32\x1e.mindinsight.Explain.Inference\x12\x35\n\x0b\x65xplanation\x18\x05 \x03(\x0b\x32 .mindinsight.Explain.Explanation\x12\x31\n\tbenchmark\x18\x06 \x03(\x0b\x32\x1e.mindinsight.Explain.Benchmark\x12/\n\x08metadata\x18\x07 \x01(\x0b\x32\x1d.mindinsight.Explain.Metadata\x12\x0e\n\x06status\x18\x08 \x01(\t\x1aW\n\tInference\x12\x19\n\x11ground_truth_prob\x18\x01 \x03(\x02\x12\x17\n\x0fpredicted_label\x18\x02 \x03(\x05\x12\x16\n\x0epredicted_prob\x18\x03 \x03(\x02\x1a\x44\n\x0b\x45xplanation\x12\x16\n\x0e\x65xplain_method\x18\x01 \x01(\t\x12\r\n\x05label\x18\x02 \x01(\x05\x12\x0e\n\x06hitmap\x18\x03 \x01(\x0c\x1a\x91\x02\n\tBenchmark\x12\x16\n\x0e\x65xplain_method\x18\x01 \x01(\t\x12>\n\x0btotal_score\x18\x02 \x03(\x0b\x32).mindinsight.Explain.Benchmark.TotalScore\x12>\n\x0blabel_score\x18\x03 \x03(\x0b\x32).mindinsight.Explain.Benchmark.LabelScore\x1a\x35\n\nTotalScore\x12\x18\n\x10\x62\x65nchmark_method\x18\x01 \x01(\t\x12\r\n\x05score\x18\x02 \x01(\x02\x1a\x35\n\nLabelScore\x12\r\n\x05score\x18\x01 \x03(\x02\x12\x18\n\x10\x62\x65nchmark_method\x18\x02 \x01(\t\x1aK\n\x08Metadata\x12\r\n\x05label\x18\x01 \x03(\t\x12\x16\n\x0e\x65xplain_method\x18\x02 \x03(\t\x12\x18\n\x10\x62\x65nchmark_method\x18\x03 \x03(\tB\x03\xf8\x01\x01') , dependencies=[mindinsight__anf__ir__pb2.DESCRIPTOR,]) @@ -39,42 +40,49 @@ _EVENT = _descriptor.Descriptor( has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='step', full_name='mindinsight.Event.step', index=1, number=2, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='version', full_name='mindinsight.Event.version', index=2, number=3, type=9, cpp_type=9, label=1, - has_default_value=False, default_value=b"".decode('utf-8'), + has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='graph_def', full_name='mindinsight.Event.graph_def', index=3, number=4, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='summary', full_name='mindinsight.Event.summary', index=4, number=5, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='explain', full_name='mindinsight.Event.explain', index=5, + number=6, type=11, cpp_type=10, label=1, + has_default_value=False, default_value=None, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], - serialized_options=None, + options=None, is_extendable=False, syntax='proto2', extension_ranges=[], @@ -84,7 +92,7 @@ _EVENT = _descriptor.Descriptor( index=0, containing_type=None, fields=[]), ], serialized_start=69, - serialized_end=223, + serialized_end=264, ) @@ -101,42 +109,42 @@ _SUMMARY_IMAGE = _descriptor.Descriptor( has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='width', full_name='mindinsight.Summary.Image.width', index=1, number=2, type=5, cpp_type=1, label=2, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='colorspace', full_name='mindinsight.Summary.Image.colorspace', index=2, number=3, type=5, cpp_type=1, label=2, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='encoded_image', full_name='mindinsight.Summary.Image.encoded_image', index=3, number=4, type=12, cpp_type=9, label=2, - has_default_value=False, default_value=b"", + has_default_value=False, default_value=_b(""), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], - serialized_options=None, + options=None, is_extendable=False, syntax='proto2', extension_ranges=[], oneofs=[ ], - serialized_start=280, - serialized_end=361, + serialized_start=321, + serialized_end=402, ) _SUMMARY_HISTOGRAM_BUCKET = _descriptor.Descriptor( @@ -152,35 +160,35 @@ _SUMMARY_HISTOGRAM_BUCKET = _descriptor.Descriptor( has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='width', full_name='mindinsight.Summary.Histogram.bucket.width', index=1, number=2, type=1, cpp_type=5, label=2, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='count', full_name='mindinsight.Summary.Histogram.bucket.count', index=2, number=3, type=3, cpp_type=2, label=2, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], - serialized_options=None, + options=None, is_extendable=False, syntax='proto2', extension_ranges=[], oneofs=[ ], - serialized_start=552, - serialized_end=604, + serialized_start=593, + serialized_end=645, ) _SUMMARY_HISTOGRAM = _descriptor.Descriptor( @@ -196,70 +204,70 @@ _SUMMARY_HISTOGRAM = _descriptor.Descriptor( has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='nan_count', full_name='mindinsight.Summary.Histogram.nan_count', index=1, number=2, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='pos_inf_count', full_name='mindinsight.Summary.Histogram.pos_inf_count', index=2, number=3, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='neg_inf_count', full_name='mindinsight.Summary.Histogram.neg_inf_count', index=3, number=4, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='max', full_name='mindinsight.Summary.Histogram.max', index=4, number=5, type=1, cpp_type=5, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='min', full_name='mindinsight.Summary.Histogram.min', index=5, number=6, type=1, cpp_type=5, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='sum', full_name='mindinsight.Summary.Histogram.sum', index=6, number=7, type=1, cpp_type=5, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='count', full_name='mindinsight.Summary.Histogram.count', index=7, number=8, type=3, cpp_type=2, label=1, has_default_value=False, default_value=0, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[_SUMMARY_HISTOGRAM_BUCKET, ], enum_types=[ ], - serialized_options=None, + options=None, is_extendable=False, syntax='proto2', extension_ranges=[], oneofs=[ ], - serialized_start=364, - serialized_end=604, + serialized_start=405, + serialized_end=645, ) _SUMMARY_VALUE = _descriptor.Descriptor( @@ -272,45 +280,45 @@ _SUMMARY_VALUE = _descriptor.Descriptor( _descriptor.FieldDescriptor( name='tag', full_name='mindinsight.Summary.Value.tag', index=0, number=1, type=9, cpp_type=9, label=2, - has_default_value=False, default_value=b"".decode('utf-8'), + has_default_value=False, default_value=_b("").decode('utf-8'), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='scalar_value', full_name='mindinsight.Summary.Value.scalar_value', index=1, number=3, type=2, cpp_type=6, label=1, has_default_value=False, default_value=float(0), message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='image', full_name='mindinsight.Summary.Value.image', index=2, number=4, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='tensor', full_name='mindinsight.Summary.Value.tensor', index=3, number=8, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), _descriptor.FieldDescriptor( name='histogram', full_name='mindinsight.Summary.Value.histogram', index=4, number=9, type=11, cpp_type=10, label=1, has_default_value=False, default_value=None, message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[], enum_types=[ ], - serialized_options=None, + options=None, is_extendable=False, syntax='proto2', extension_ranges=[], @@ -319,8 +327,8 @@ _SUMMARY_VALUE = _descriptor.Descriptor( name='value', full_name='mindinsight.Summary.Value.value', index=0, containing_type=None, fields=[]), ], - serialized_start=607, - serialized_end=802, + serialized_start=648, + serialized_end=843, ) _SUMMARY = _descriptor.Descriptor( @@ -336,25 +344,356 @@ _SUMMARY = _descriptor.Descriptor( has_default_value=False, default_value=[], message_type=None, enum_type=None, containing_type=None, is_extension=False, extension_scope=None, - serialized_options=None, file=DESCRIPTOR), + options=None, file=DESCRIPTOR), ], extensions=[ ], nested_types=[_SUMMARY_IMAGE, _SUMMARY_HISTOGRAM, _SUMMARY_VALUE, ], enum_types=[ ], - serialized_options=None, + options=None, is_extendable=False, syntax='proto2', extension_ranges=[], oneofs=[ ], - serialized_start=226, - serialized_end=802, + serialized_start=267, + serialized_end=843, +) + + +_EXPLAIN_INFERENCE = _descriptor.Descriptor( + name='Inference', + full_name='mindinsight.Explain.Inference', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='ground_truth_prob', full_name='mindinsight.Explain.Inference.ground_truth_prob', index=0, + number=1, type=2, cpp_type=6, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='predicted_label', full_name='mindinsight.Explain.Inference.predicted_label', index=1, + number=2, type=5, cpp_type=1, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='predicted_prob', full_name='mindinsight.Explain.Inference.predicted_prob', index=2, + number=3, type=2, cpp_type=6, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=1145, + serialized_end=1232, +) + +_EXPLAIN_EXPLANATION = _descriptor.Descriptor( + name='Explanation', + full_name='mindinsight.Explain.Explanation', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='explain_method', full_name='mindinsight.Explain.Explanation.explain_method', index=0, + number=1, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='label', full_name='mindinsight.Explain.Explanation.label', index=1, + number=2, type=5, cpp_type=1, label=1, + has_default_value=False, default_value=0, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='hitmap', full_name='mindinsight.Explain.Explanation.hitmap', index=2, + number=3, type=12, cpp_type=9, label=1, + has_default_value=False, default_value=_b(""), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=1234, + serialized_end=1302, +) + +_EXPLAIN_BENCHMARK_TOTALSCORE = _descriptor.Descriptor( + name='TotalScore', + full_name='mindinsight.Explain.Benchmark.TotalScore', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='benchmark_method', full_name='mindinsight.Explain.Benchmark.TotalScore.benchmark_method', index=0, + number=1, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='score', full_name='mindinsight.Explain.Benchmark.TotalScore.score', index=1, + number=2, type=2, cpp_type=6, label=1, + has_default_value=False, default_value=float(0), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=1470, + serialized_end=1523, +) + +_EXPLAIN_BENCHMARK_LABELSCORE = _descriptor.Descriptor( + name='LabelScore', + full_name='mindinsight.Explain.Benchmark.LabelScore', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='score', full_name='mindinsight.Explain.Benchmark.LabelScore.score', index=0, + number=1, type=2, cpp_type=6, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='benchmark_method', full_name='mindinsight.Explain.Benchmark.LabelScore.benchmark_method', index=1, + number=2, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=1525, + serialized_end=1578, +) + +_EXPLAIN_BENCHMARK = _descriptor.Descriptor( + name='Benchmark', + full_name='mindinsight.Explain.Benchmark', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='explain_method', full_name='mindinsight.Explain.Benchmark.explain_method', index=0, + number=1, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='total_score', full_name='mindinsight.Explain.Benchmark.total_score', index=1, + number=2, type=11, cpp_type=10, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='label_score', full_name='mindinsight.Explain.Benchmark.label_score', index=2, + number=3, type=11, cpp_type=10, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[_EXPLAIN_BENCHMARK_TOTALSCORE, _EXPLAIN_BENCHMARK_LABELSCORE, ], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=1305, + serialized_end=1578, +) + +_EXPLAIN_METADATA = _descriptor.Descriptor( + name='Metadata', + full_name='mindinsight.Explain.Metadata', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='label', full_name='mindinsight.Explain.Metadata.label', index=0, + number=1, type=9, cpp_type=9, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='explain_method', full_name='mindinsight.Explain.Metadata.explain_method', index=1, + number=2, type=9, cpp_type=9, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='benchmark_method', full_name='mindinsight.Explain.Metadata.benchmark_method', index=2, + number=3, type=9, cpp_type=9, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=1580, + serialized_end=1655, +) + +_EXPLAIN = _descriptor.Descriptor( + name='Explain', + full_name='mindinsight.Explain', + filename=None, + file=DESCRIPTOR, + containing_type=None, + fields=[ + _descriptor.FieldDescriptor( + name='image_id', full_name='mindinsight.Explain.image_id', index=0, + number=1, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='image_data', full_name='mindinsight.Explain.image_data', index=1, + number=2, type=12, cpp_type=9, label=1, + has_default_value=False, default_value=_b(""), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='ground_truth_label', full_name='mindinsight.Explain.ground_truth_label', index=2, + number=3, type=5, cpp_type=1, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='inference', full_name='mindinsight.Explain.inference', index=3, + number=4, type=11, cpp_type=10, label=1, + has_default_value=False, default_value=None, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='explanation', full_name='mindinsight.Explain.explanation', index=4, + number=5, type=11, cpp_type=10, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='benchmark', full_name='mindinsight.Explain.benchmark', index=5, + number=6, type=11, cpp_type=10, label=3, + has_default_value=False, default_value=[], + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='metadata', full_name='mindinsight.Explain.metadata', index=6, + number=7, type=11, cpp_type=10, label=1, + has_default_value=False, default_value=None, + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + _descriptor.FieldDescriptor( + name='status', full_name='mindinsight.Explain.status', index=7, + number=8, type=9, cpp_type=9, label=1, + has_default_value=False, default_value=_b("").decode('utf-8'), + message_type=None, enum_type=None, containing_type=None, + is_extension=False, extension_scope=None, + options=None, file=DESCRIPTOR), + ], + extensions=[ + ], + nested_types=[_EXPLAIN_INFERENCE, _EXPLAIN_EXPLANATION, _EXPLAIN_BENCHMARK, _EXPLAIN_METADATA, ], + enum_types=[ + ], + options=None, + is_extendable=False, + syntax='proto2', + extension_ranges=[], + oneofs=[ + ], + serialized_start=846, + serialized_end=1655, ) _EVENT.fields_by_name['graph_def'].message_type = mindinsight__anf__ir__pb2._GRAPHPROTO _EVENT.fields_by_name['summary'].message_type = _SUMMARY +_EVENT.fields_by_name['explain'].message_type = _EXPLAIN _EVENT.oneofs_by_name['what'].fields.append( _EVENT.fields_by_name['version']) _EVENT.fields_by_name['version'].containing_oneof = _EVENT.oneofs_by_name['what'] @@ -364,6 +703,9 @@ _EVENT.fields_by_name['graph_def'].containing_oneof = _EVENT.oneofs_by_name['wha _EVENT.oneofs_by_name['what'].fields.append( _EVENT.fields_by_name['summary']) _EVENT.fields_by_name['summary'].containing_oneof = _EVENT.oneofs_by_name['what'] +_EVENT.oneofs_by_name['what'].fields.append( + _EVENT.fields_by_name['explain']) +_EVENT.fields_by_name['explain'].containing_oneof = _EVENT.oneofs_by_name['what'] _SUMMARY_IMAGE.containing_type = _SUMMARY _SUMMARY_HISTOGRAM_BUCKET.containing_type = _SUMMARY_HISTOGRAM _SUMMARY_HISTOGRAM.fields_by_name['buckets'].message_type = _SUMMARY_HISTOGRAM_BUCKET @@ -385,56 +727,125 @@ _SUMMARY_VALUE.oneofs_by_name['value'].fields.append( _SUMMARY_VALUE.fields_by_name['histogram']) _SUMMARY_VALUE.fields_by_name['histogram'].containing_oneof = _SUMMARY_VALUE.oneofs_by_name['value'] _SUMMARY.fields_by_name['value'].message_type = _SUMMARY_VALUE +_EXPLAIN_INFERENCE.containing_type = _EXPLAIN +_EXPLAIN_EXPLANATION.containing_type = _EXPLAIN +_EXPLAIN_BENCHMARK_TOTALSCORE.containing_type = _EXPLAIN_BENCHMARK +_EXPLAIN_BENCHMARK_LABELSCORE.containing_type = _EXPLAIN_BENCHMARK +_EXPLAIN_BENCHMARK.fields_by_name['total_score'].message_type = _EXPLAIN_BENCHMARK_TOTALSCORE +_EXPLAIN_BENCHMARK.fields_by_name['label_score'].message_type = _EXPLAIN_BENCHMARK_LABELSCORE +_EXPLAIN_BENCHMARK.containing_type = _EXPLAIN +_EXPLAIN_METADATA.containing_type = _EXPLAIN +_EXPLAIN.fields_by_name['inference'].message_type = _EXPLAIN_INFERENCE +_EXPLAIN.fields_by_name['explanation'].message_type = _EXPLAIN_EXPLANATION +_EXPLAIN.fields_by_name['benchmark'].message_type = _EXPLAIN_BENCHMARK +_EXPLAIN.fields_by_name['metadata'].message_type = _EXPLAIN_METADATA DESCRIPTOR.message_types_by_name['Event'] = _EVENT DESCRIPTOR.message_types_by_name['Summary'] = _SUMMARY +DESCRIPTOR.message_types_by_name['Explain'] = _EXPLAIN _sym_db.RegisterFileDescriptor(DESCRIPTOR) -Event = _reflection.GeneratedProtocolMessageType('Event', (_message.Message,), { - 'DESCRIPTOR' : _EVENT, - '__module__' : 'mindinsight_summary_pb2' +Event = _reflection.GeneratedProtocolMessageType('Event', (_message.Message,), dict( + DESCRIPTOR = _EVENT, + __module__ = 'mindinsight_summary_pb2' # @@protoc_insertion_point(class_scope:mindinsight.Event) - }) + )) _sym_db.RegisterMessage(Event) -Summary = _reflection.GeneratedProtocolMessageType('Summary', (_message.Message,), { +Summary = _reflection.GeneratedProtocolMessageType('Summary', (_message.Message,), dict( - 'Image' : _reflection.GeneratedProtocolMessageType('Image', (_message.Message,), { - 'DESCRIPTOR' : _SUMMARY_IMAGE, - '__module__' : 'mindinsight_summary_pb2' + Image = _reflection.GeneratedProtocolMessageType('Image', (_message.Message,), dict( + DESCRIPTOR = _SUMMARY_IMAGE, + __module__ = 'mindinsight_summary_pb2' # @@protoc_insertion_point(class_scope:mindinsight.Summary.Image) - }) + )) , - 'Histogram' : _reflection.GeneratedProtocolMessageType('Histogram', (_message.Message,), { + Histogram = _reflection.GeneratedProtocolMessageType('Histogram', (_message.Message,), dict( - 'bucket' : _reflection.GeneratedProtocolMessageType('bucket', (_message.Message,), { - 'DESCRIPTOR' : _SUMMARY_HISTOGRAM_BUCKET, - '__module__' : 'mindinsight_summary_pb2' + bucket = _reflection.GeneratedProtocolMessageType('bucket', (_message.Message,), dict( + DESCRIPTOR = _SUMMARY_HISTOGRAM_BUCKET, + __module__ = 'mindinsight_summary_pb2' # @@protoc_insertion_point(class_scope:mindinsight.Summary.Histogram.bucket) - }) + )) , - 'DESCRIPTOR' : _SUMMARY_HISTOGRAM, - '__module__' : 'mindinsight_summary_pb2' + DESCRIPTOR = _SUMMARY_HISTOGRAM, + __module__ = 'mindinsight_summary_pb2' # @@protoc_insertion_point(class_scope:mindinsight.Summary.Histogram) - }) + )) , - 'Value' : _reflection.GeneratedProtocolMessageType('Value', (_message.Message,), { - 'DESCRIPTOR' : _SUMMARY_VALUE, - '__module__' : 'mindinsight_summary_pb2' + Value = _reflection.GeneratedProtocolMessageType('Value', (_message.Message,), dict( + DESCRIPTOR = _SUMMARY_VALUE, + __module__ = 'mindinsight_summary_pb2' # @@protoc_insertion_point(class_scope:mindinsight.Summary.Value) - }) + )) , - 'DESCRIPTOR' : _SUMMARY, - '__module__' : 'mindinsight_summary_pb2' + DESCRIPTOR = _SUMMARY, + __module__ = 'mindinsight_summary_pb2' # @@protoc_insertion_point(class_scope:mindinsight.Summary) - }) + )) _sym_db.RegisterMessage(Summary) _sym_db.RegisterMessage(Summary.Image) _sym_db.RegisterMessage(Summary.Histogram) _sym_db.RegisterMessage(Summary.Histogram.bucket) _sym_db.RegisterMessage(Summary.Value) +Explain = _reflection.GeneratedProtocolMessageType('Explain', (_message.Message,), dict( + + Inference = _reflection.GeneratedProtocolMessageType('Inference', (_message.Message,), dict( + DESCRIPTOR = _EXPLAIN_INFERENCE, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain.Inference) + )) + , + + Explanation = _reflection.GeneratedProtocolMessageType('Explanation', (_message.Message,), dict( + DESCRIPTOR = _EXPLAIN_EXPLANATION, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain.Explanation) + )) + , + + Benchmark = _reflection.GeneratedProtocolMessageType('Benchmark', (_message.Message,), dict( + + TotalScore = _reflection.GeneratedProtocolMessageType('TotalScore', (_message.Message,), dict( + DESCRIPTOR = _EXPLAIN_BENCHMARK_TOTALSCORE, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain.Benchmark.TotalScore) + )) + , + + LabelScore = _reflection.GeneratedProtocolMessageType('LabelScore', (_message.Message,), dict( + DESCRIPTOR = _EXPLAIN_BENCHMARK_LABELSCORE, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain.Benchmark.LabelScore) + )) + , + DESCRIPTOR = _EXPLAIN_BENCHMARK, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain.Benchmark) + )) + , + + Metadata = _reflection.GeneratedProtocolMessageType('Metadata', (_message.Message,), dict( + DESCRIPTOR = _EXPLAIN_METADATA, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain.Metadata) + )) + , + DESCRIPTOR = _EXPLAIN, + __module__ = 'mindinsight_summary_pb2' + # @@protoc_insertion_point(class_scope:mindinsight.Explain) + )) +_sym_db.RegisterMessage(Explain) +_sym_db.RegisterMessage(Explain.Inference) +_sym_db.RegisterMessage(Explain.Explanation) +_sym_db.RegisterMessage(Explain.Benchmark) +_sym_db.RegisterMessage(Explain.Benchmark.TotalScore) +_sym_db.RegisterMessage(Explain.Benchmark.LabelScore) +_sym_db.RegisterMessage(Explain.Metadata) + -DESCRIPTOR._options = None +DESCRIPTOR.has_options = True +DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\370\001\001')) # @@protoc_insertion_point(module_scope) diff --git a/mindinsight/explainer/__init__.py b/mindinsight/explainer/__init__.py new file mode 100644 index 00000000..9d560b89 --- /dev/null +++ b/mindinsight/explainer/__init__.py @@ -0,0 +1,21 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +""" +Explainer module introduction. + +This module provides Python APIs to retrieve explain info and provide analysis for explanation of +training and models. + +""" diff --git a/mindinsight/explainer/common/__init__.py b/mindinsight/explainer/common/__init__.py new file mode 100644 index 00000000..6fdeb676 --- /dev/null +++ b/mindinsight/explainer/common/__init__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""The module define public methods for explanation""" diff --git a/mindinsight/explainer/common/enums.py b/mindinsight/explainer/common/enums.py new file mode 100644 index 00000000..74fb10c1 --- /dev/null +++ b/mindinsight/explainer/common/enums.py @@ -0,0 +1,52 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""Enums for explain data.""" +import enum + +class BaseEnum(enum.Enum): + + @classmethod + def list_members(cls): + """List all members.""" + return [member.value for member in cls] + + +class DataManagerStatus(BaseEnum): + """Data manager status.""" + INIT = 'INIT' + LOADING = 'LOADING' + DONE = 'DONE' + INVALID = 'INVALID' + + +class PluginNameEnum(BaseEnum): + """Plugin Name Enum.""" + EXPLAIN = 'explain' + IMAGE_ID = 'image_id' + BENCHMARK = 'benchmark' + METADATA = 'metadata' + IMAGE_DATA = 'image_data' + GROUND_TRUTH_LABEL = 'ground_truth_label' + INFERENCE = 'inference' + EXPLANATION = 'explanation' + STATUS = 'status' + + +@enum.unique +class CacheStatus(enum.Enum): + """Train job cache status.""" + NOT_IN_CACHE = "NOT_IN_CACHE" + CACHING = "CACHING" + CACHED = "CACHED" diff --git a/mindinsight/explainer/common/log.py b/mindinsight/explainer/common/log.py new file mode 100644 index 00000000..2e303d87 --- /dev/null +++ b/mindinsight/explainer/common/log.py @@ -0,0 +1,19 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""Create a logger for explain data.""" +from mindinsight.utils.log import setup_logger + +logger = setup_logger("explainer", "explainer") +restful_logger = setup_logger("restful_api", "restful_api") diff --git a/mindinsight/explainer/manager/__init__.py b/mindinsight/explainer/manager/__init__.py new file mode 100644 index 00000000..24b23698 --- /dev/null +++ b/mindinsight/explainer/manager/__init__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +"""manager of explainer helps to parse data from log file and organize them for request""" diff --git a/mindinsight/explainer/manager/explain_parser.py b/mindinsight/explainer/manager/explain_parser.py new file mode 100644 index 00000000..b08c0883 --- /dev/null +++ b/mindinsight/explainer/manager/explain_parser.py @@ -0,0 +1,222 @@ +# Copyright 2020 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================ +""" +File parser for MindExplain data. + +This module is used to parse the MindExplain log file. +""" +import re +import collections + +from google.protobuf.message import DecodeError + +from mindinsight.datavisual.common import exceptions +from mindinsight.explainer.common.enums import PluginNameEnum +from mindinsight.explainer.common.log import logger +from mindinsight.datavisual.data_access.file_handler import FileHandler +from mindinsight.datavisual.data_transform.ms_data_loader import _SummaryParser +from mindinsight.datavisual.proto_files import mindinsight_summary_pb2 as summary_pb2 +from mindinsight.utils.exceptions import UnknownError + +HEADER_SIZE = 8 +CRC_STR_SIZE = 4 +MAX_EVENT_STRING = 500000000 +ImageDataContainer = collections.namedtuple('ImageDataContainer', + ['image_id', 'image_data', 'ground_truth_label', + 'inference', 'explanation', 'status']) +BenchmarkContainer = collections.namedtuple('BenchmarkContainer', ['benchmark', 'status']) +MetadataContainer = collections.namedtuple('MetadataContainer', ['metadata', 'status']) + + +class _ExplainParser(_SummaryParser): + """The summary file parser.""" + def __init__(self, summary_dir): + super(_ExplainParser, self).__init__(summary_dir) + self._latest_filename = '' + + def parse_explain(self, filenames): + """ + Load summary file and parse file content. + + Args: + filenames (list[str]): File name list. + Returns: + bool, True if all the summary files are finished loading. + """ + summary_files = self.filter_files(filenames) + summary_files = self.sort_files(summary_files) + + is_end = False + is_clean = False + event_data = {} + filename = summary_files[-1] + + file_path = FileHandler.join(self._summary_dir, filename) + if filename != self._latest_filename: + self._summary_file_handler = FileHandler(file_path, 'rb') + self._latest_filename = filename + self._latest_file_size = 0 + is_clean = True + + new_size = FileHandler.file_stat(file_path).size + if new_size == self._latest_file_size: + is_end = True + return is_clean, is_end, event_data + + while True: + start_offset = self._summary_file_handler.offset + try: + event_str = self._event_load(self._summary_file_handler) + if event_str is None: + self._summary_file_handler.reset_offset(start_offset) + is_end = True + return is_clean, is_end, event_data + if len(event_str) > MAX_EVENT_STRING: + logger.warning("file_path: %s, event string: %d exceeds %d and drop it.", + self._summary_file_handler.file_path, len(event_str), MAX_EVENT_STRING) + continue + + field_list, tensor_value_list = self._event_decode(event_str) + for field, tensor_value in zip(field_list, tensor_value_list): + event_data[field] = tensor_value + logger.info("Parse summary file offset %d, file path: %s.", self._latest_file_size, file_path) + return is_clean, is_end, event_data + + except exceptions.CRCFailedError: + self._summary_file_handler.reset_offset(start_offset) + is_end = True + logger.warning("Check crc failed and ignore this file, file_path=%s, " + "offset=%s.", self._summary_file_handler.file_path, self._summary_file_handler.offset) + return is_clean, is_end, event_data + except (OSError, DecodeError, exceptions.MindInsightException) as ex: + is_end = True + logger.warning("Parse log file fail, and ignore this file, detail: %r," + "file path: %s.", str(ex), self._summary_file_handler.file_path) + return is_clean, is_end, event_data + except Exception as ex: + logger.exception(ex) + raise UnknownError(str(ex)) + + def filter_files(self, filenames): + """ + Gets a list of summary files. + + Args: + filenames (list[str]): File name list, like [filename1, filename2]. + + Returns: + list[str], filename list. + """ + return list(filter( + lambda filename: (re.search(r'summary\.\d+', filename) + and filename.endswith("_explain")), filenames)) + + @staticmethod + def _event_decode(event_str): + """ + Transform `Event` data to tensor_event and update it to EventsData. + + Args: + event_str (str): Message event string in summary proto, data read from file handler. + """ + + logger.debug("Start to parse event string. Event string len: %s.", len(event_str)) + event = summary_pb2.Event.FromString(event_str) + logger.debug("Deserialize event string completed.") + + fields = { + 'image_id': PluginNameEnum.IMAGE_ID, + 'benchmark': PluginNameEnum.BENCHMARK, + 'metadata': PluginNameEnum.METADATA + } + + tensor_event_value = getattr(event, 'explain') + + field_list = [] + tensor_value_list = [] + for field in fields: + if not getattr(tensor_event_value, field): + continue + + if PluginNameEnum.METADATA.value == field and not tensor_event_value.metadata.label: + continue + + tensor_value = None + if field == PluginNameEnum.IMAGE_ID.value: + tensor_value = _ExplainParser._add_image_data(tensor_event_value) + elif field == PluginNameEnum.BENCHMARK.value: + tensor_value = _ExplainParser._add_benchmark(tensor_event_value) + elif field == PluginNameEnum.METADATA.value: + tensor_value = _ExplainParser._add_metadata(tensor_event_value) + logger.debug("Event generated, label is %s, step is %s.", field, event.step) + field_list.append(field) + tensor_value_list.append(tensor_value) + return field_list, tensor_value_list + + + @staticmethod + def _add_image_data(tensor_event_value): + """ + Parse image data based on image_id in Explain message + + Args: + tensor_event_value: the object of Explain message + """ + image_data = ImageDataContainer( + image_id=tensor_event_value.image_id, + image_data=tensor_event_value.image_data, + ground_truth_label=tensor_event_value.ground_truth_label, + inference=tensor_event_value.inference, + explanation=tensor_event_value.explanation, + status=tensor_event_value.status + ) + return image_data + + + @staticmethod + def _add_benchmark(tensor_event_value): + """ + Parse benchmark data from Explain message. + + Args: + tensor_event_value: the object of Explain message + + Returns: + benchmark_data: An object containing benchmark. + """ + benchmark_data = BenchmarkContainer( + benchmark=tensor_event_value.benchmark, + status=tensor_event_value.status + ) + + return benchmark_data + + @staticmethod + def _add_metadata(tensor_event_value): + """ + Parse metadata from Explain message. + + Args: + tensor_event_value: the object of Explain message + + Returns: + benchmark_data: An object containing metadata. + """ + metadata_value = MetadataContainer( + metadata=tensor_event_value.metadata, + status=tensor_event_value.status + ) + + return metadata_value