项目场景:
运行Mask-RCNN的源码,在Pycharm中运行inspect_weights.py
文件时候,后台报出问题:<IPython.core.display.HTML object>
。
分析
-
报错信息描述:
-
分析
仔细观察一下是哪里的数据没有输出:
发现追踪到代码中的visualize.display_weight_stats(model)
:
进入到display_weight_stats(model)
函数中:def display_weight_stats(model): """Scans all the weights in the model and returns a list of tuples that contain stats about each weight. """ layers = model.get_trainable_layers() table = [["WEIGHT NAME", "SHAPE", "MIN", "MAX", "STD"]] for l in layers: weight_values = l.get_weights() # list of Numpy arrays weight_tensors = l.weights # list of TF tensors for i, w in enumerate(weight_values): weight_name = weight_tensors[i].name # Detect problematic layers. Exclude biases of conv layers. alert = "" if w.min() == w.max() and not (l.__class__.__name__ == "Conv2D" and i == 1): alert += "<span style='color:red'>*** dead?</span>" if np.abs(w.min()) > 1000 or np.abs(w.max()) > 1000: alert += "<span style='color:red'>*** Overflow?</span>" # Add row table.append([ weight_name + alert, str(w.shape), "{:+9.4f}".format(w.min()), "{:+10.4f}".format(w.max()), "{:+9.4f}".format(w.std()), ]) display_table(table)
发现最后一句
display_table(table)
,进入该函数:def display_table(table): """Display values in a table format. table: an iterable of rows, and each row is an iterable of values. """ html = "" for row in table: row_html = "" for col in row: row_html += "<td>{:40}</td>".format(str(col)) html += "<tr>" + row_html + "</tr>" html = "<table>" + html + "</table>" IPython.display.display(IPython.display.HTML(html))
现在找到和报错信息相似的函数了:
IPython.display.display(IPython.display.HTML(html))
看一下Ipython的文档说明,查看IPython.display.display
:
Notes In Python, objects can declare their textual representation using the __repr__ method. IPython expands on this idea and allows objects to declare other, rich representations including: HTML JSON PNG JPEG SVG LaTeX A single object can declare some or all of these representations; all are handled by IPython’s display system. The main idea of the first approach is that you have to implement special display methods when you define your class, one for each representation you want to use. Here is a list of the names of the special methods and the values they must return: _repr_html_: return raw HTML as a string, or a tuple (see below). _repr_json_: return a JSONable dict, or a tuple (see below). _repr_jpeg_: return raw JPEG data, or a tuple (see below). _repr_png_: return raw PNG data, or a tuple (see below). _repr_svg_: return raw SVG data as a string, or a tuple (see below). _repr_latex_: return LaTeX commands in a string surrounded by “$”,or a tuple (see below). _repr_mimebundle_: return a full mimebundle containing the mapping from all mimetypes to data. Use this for any mime-type not listed above. The above functions may also return the object’s metadata alonside the data. If the metadata is available, the functions will return a tuple containing the data and metadata, in that order. If there is no metadata available, then the functions will return the data only. When you are directly writing your own classes, you can adapt them for display in IPython by following the above approach. But in practice, you often need to work with existing classes that you can’t easily modify. You can refer to the documentation on integrating with the display system in order to register custom formatters for already existing types (Rich display). New in version 5.4: display available without import New in version 6.1: display available without import Since IPython 5.4 and 6.1 display() is automatically made available to the user without import. If you are using display in a document that might be used in a pure python context or with older version of IPython, use the following import at the top of your file: from IPython.display import display
意思就是只有Jupyter notebook 能显示此类的信息,python中无法显示。
解决
既然此方法只能在jupyter notebook页面上输出信息,那就自己重新调整一下输出语句。
找到display_weight_stats
的定义,此函数在mrcnn
下的visualize.py
文件中定义。看一下其内容,无非就是将查询的信息存放到一个table表格中,并在html页面中输出。
def display_weight_stats(model):
"""Scans all the weights in the model and returns a list of tuples
that contain stats about each weight.
"""
layers = model.get_trainable_layers()
table = [["WEIGHT NAME", "SHAPE", "MIN", "MAX", "STD"]]
for l in layers:
weight_values = l.get_weights() # list of Numpy arrays
weight_tensors = l.weights # list of TF tensors
for i, w in enumerate(weight_values):
weight_name = weight_tensors[i].name
# Detect problematic layers. Exclude biases of conv layers.
alert = ""
if w.min() == w.max() and not (l.__class__.__name__ == "Conv2D" and i == 1):
alert += "<span style='color:red'>*** dead?</span>"
if np.abs(w.min()) > 1000 or np.abs(w.max()) > 1000:
alert += "<span style='color:red'>*** Overflow?</span>"
# Add row
table.append([
weight_name + alert,
str(w.shape),
"{:+9.4f}".format(w.min()),
"{:+10.4f}".format(w.max()),
"{:+9.4f}".format(w.std()),
])
display_table(table)
def display_table(table):
"""Display values in a table format.
table: an iterable of rows, and each row is an iterable of values.
"""
html = ""
for row in table:
row_html = ""
for col in row:
row_html += "<td>{:40}</td>".format(str(col))
html += "<tr>" + row_html + "</tr>"
html = "<table>" + html + "</table>"
IPython.display.display(IPython.display.HTML(html))
为了不影响jupyter notebook和其他代码文件的运行,所以现在自己写一个打印函数,直接打印查询的信息就行了。
💥💥切记: 不可直接在
display_weight_stats
函数中改动代码。因为可能别的程序也会调用此函数。很可能会引发错误。
由于python不存在函数重载,所以在mrcnn
下的visualize.py
文件中自己定义一个新的函数display_weight_stats_python
:
def display_weight_stats_python(model):
"""Scans all the weights in the model and returns a list of tuples
that contain stats about each weight.
"""
layers = model.get_trainable_layers()
for l in layers:
weight_values = l.get_weights() # list of Numpy arrays
weight_tensors = l.weights # list of TF tensors
for i, w in enumerate(weight_values):
weight_name = weight_tensors[i].name
# Detect problematic layers. Exclude biases of conv layers.
alert = ""
if w.min() == w.max() and not (l.__class__.__name__ == "Conv2D" and i == 1):
alert += "*** dead?"
if np.abs(w.min()) > 1000 or np.abs(w.max()) > 1000:
alert += "*** Overflow?"
print( weight_name+ alert+" "+ str(w.shape)+" "+ "{:+9.4f}".format(w.min())+" "+"{:+10.4f}".format(w.max())+ " "+ "{:+9.4f}".format(w.std()))
将inspect_weights.py
文件中将visualize.display_weight_stats(model)
屏蔽,添加visualize.display_weight_stats_python(model)
。
运行,发现数据显示无误。