物体检测--Mask_RCNN系列:(二)程序运行与演示

从上一篇mast rcnn(1)文章开头处找到项目地址,下载。

《一》配置自己的环境;

Mask-RCNN的官方要求的环境是:

  • Python3.4+
  • Keras>=2.0.8
  • TensorFlow>=1.3.0
  • Jupyter Notebook
  • Numpy,skimage,scipy,Pillow,cython,h5py等,详情见工程下requirements.txt文件,若后面运行有缺的库自己按需安装就行,这里若遇到安装 imgaug库时出错,大部分原因是缺少此库的依赖库,解决办法可以参考我的上上篇文章。

《二》 下载训练好的权重mask_rcnn_coco.h5

《三》coco数据集及配置pycocotools;配置pycocotools不会的百度一下,

是从git clone https://github.com/pdollar/coco下载安装包,再进行编译 cd coco/PythonAPI;make -j8;其实就是对其进行编译成.py脚本,我们项目会用到coco的一些评价指标,参数等内容。

编译好后将生成的pycocotools文件夹拷贝到此项目中,集体地址为:Mask_RCNN/samples/coco/目录下

《四》下载MSCOCO数据集:数据以及5K minval和35K valid-minus-minval的json文件(即annotation),并新建一个coco文件夹,将图片放图coco/train,coco/val,json文件放入coco/annotation,这里测试的话不会用到这些数据集,

《五》用demo.ipynb 演示,这是利用已经训练好的模型来展示对任意图像的检测和分割效果,并作可视化显示。

原demo.ipynb为.ipynb格式文件,这里我利用Jupyter Notebook 打开后将其转为了.py文件(转换后可能有一些变化,自己修改)

运行程序,如果运行出错:

from numpy.lib.arraypad import _validate_lengths
ImportError: cannot import name '_validate_lengths' from 'numpy.lib.arraypad'

是由于numpy等版本太高原因造成的,没关系,在numpy的安装目录下找到arraypad.py脚本;这里我的在python3.7/site-packages/numpy/lib/arraypad.py,打开文件后,在954行后添加以下代码,保存退出,问题解决。

def _normalize_shape(ndarray, shape, cast_to_int=True):
    """
    Private function which does some checks and normalizes the possibly
    much simpler representations of ‘pad_width‘, ‘stat_length‘,
    ‘constant_values‘, ‘end_values‘.

    Parameters
    ----------
    narray : ndarray
        Input ndarray
    shape : {sequence, array_like, float, int}, optional
        The width of padding (pad_width), the number of elements on the
        edge of the narray used for statistics (stat_length), the constant
        value(s) to use when filling padded regions (constant_values), or the
        endpoint target(s) for linear ramps (end_values).
        ((before_1, after_1), ... (before_N, after_N)) unique number of
        elements for each axis where `N` is rank of `narray`.
        ((before, after),) yields same before and after constants for each
        axis.
        (constant,) or val is a shortcut for before = after = constant for
        all axes.
    cast_to_int : bool, optional
        Controls if values in ``shape`` will be rounded and cast to int
        before being returned.

    Returns
    -------
    normalized_shape : tuple of tuples
        val                               => ((val, val), (val, val), ...)
        [[val1, val2], [val3, val4], ...] => ((val1, val2), (val3, val4), ...)
        ((val1, val2), (val3, val4), ...) => no change
        [[val1, val2], ]                  => ((val1, val2), (val1, val2), ...)
        ((val1, val2), )                  => ((val1, val2), (val1, val2), ...)
        [[val ,     ], ]                  => ((val, val), (val, val), ...)
        ((val ,     ), )                  => ((val, val), (val, val), ...)

    """
    ndims = ndarray.ndim

    # Shortcut shape=None
    if shape is None:
        return ((None, None), ) * ndims

    # Convert any input `info` to a NumPy array
    shape_arr = np.asarray(shape)

    try:
        shape_arr = np.broadcast_to(shape_arr, (ndims, 2))
    except ValueError:
        fmt = "Unable to create correctly shaped tuple from %s"
        raise ValueError(fmt % (shape,))

    # Cast if necessary
    if cast_to_int is True:
        shape_arr = np.round(shape_arr).astype(int)

    # Convert list of lists to tuple of tuples
    return tuple(tuple(axis) for axis in shape_arr.tolist())


def _validate_lengths(narray, number_elements):
    """
    Private function which does some checks and reformats pad_width and
    stat_length using _normalize_shape.

    Parameters
    ----------
    narray : ndarray
        Input ndarray
    number_elements : {sequence, int}, optional
        The width of padding (pad_width) or the number of elements on the edge
        of the narray used for statistics (stat_length).
        ((before_1, after_1), ... (before_N, after_N)) unique number of
        elements for each axis.
        ((before, after),) yields same before and after constants for each
        axis.
        (constant,) or int is a shortcut for before = after = constant for all
        axes.

    Returns
    -------
    _validate_lengths : tuple of tuples
        int                               => ((int, int), (int, int), ...)
        [[int1, int2], [int3, int4], ...] => ((int1, int2), (int3, int4), ...)
        ((int1, int2), (int3, int4), ...) => no change
        [[int1, int2], ]                  => ((int1, int2), (int1, int2), ...)
        ((int1, int2), )                  => ((int1, int2), (int1, int2), ...)
        [[int ,     ], ]                  => ((int, int), (int, int), ...)
        ((int ,     ), )                  => ((int, int), (int, int), ...)

    """
    normshp = _normalize_shape(narray, number_elements)
    for i in normshp:
        chk = [1 if x is None else x for x in i]
        chk = [1 if x >= 0 else -1 for x in chk]
        if (chk[0] < 0) or (chk[1] < 0):
            fmt = "%s cannot contain negative values."
            raise ValueError(fmt % (number_elements,))
    return normshp

demo.py中通过coco.py中的CocoConfig类来写入运行需要的参数;再通过创建模型并导入训练好的权重;最后随机从工程自带的images文件夹选取图像并进行检测和分割,也可以换成自己想要检测的图片,把想要检测的图片放图images文件夹并对下面代码稍作修改就好啦。

 

 

 

 

   

你可能感兴趣的:(物体检测--Mask_RCNN系列:(二)程序运行与演示)