Mask R-CNN:<IPython.core.display.HTML object>

项目场景:

运行Mask-RCNN的源码,在Pycharm中运行inspect_weights.py文件时候,后台报出问题:<IPython.core.display.HTML object>

分析

  1. 报错信息描述:
    在这里插入图片描述

  2. 分析
    仔细观察一下是哪里的数据没有输出:
    发现追踪到代码中的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)

运行,发现数据显示无误。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值