windows平台tensorflow2+python环境配置mask-rcnn

首先参考了文献1的资料进行配置和转换,基本按照步骤可以实现。但是在调试过程中发现了些新问题,记录如下:

1. 问题 module 'tensorflow' has no attribute 'placeholder'

方案:将

import tensorflow as tf
  • 替换为:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

2. 问题 Tried to convert 'shape' to a tensor and failed

方案:https://github.com/matterport/Mask_RCNN/issues/1820

还是要多看GitHub的issue

3.问题:AttributeError: ‘str‘ object has no attribute ‘decode‘解决方法
前面添加 .encode('utf8').decode('utf8')

有点混乱,什么时候再整理下。

参考资料:

【1】windows+tensorflow2+python3环境配置mask-rcnn_v2.1详解 https://blog.csdn.net/u013085021/article/details/105471398

转载 如下:

windows+tensorflow2+python3环境配置mask-rcnn_v2.1详解


tf2改版后有了较大的改动,以前很多的经典算法都建立在tf1的版本上,需要做相应的修改才能在tf2上运行。本文详细记录了在win+tf2+python3环境下配置mask-rcnn_v2.1气球检测版本的过程以及遇到的问题。本机具体版本为windows10,python3.7,tensorflow2.1.0。

 

Github下载源码

下载GIthub源码与数据

首先搜索适用于win+tf的mask-rcnn源码,选择一个星星比较多的版本,很明显,我们选第一个。
搜索mask-rcnn
进入第一个项目,先大概浏览一下,着重阅读Installation内容,大致了解项目的配置使用流程。
项目Installation
点击releases page,进入发布的新版本以及预训练模型参数页面。
mask2.1
依次下载气球数据集balloon_dataset.zip、预训练模型参数mask_rcnn_balloon.h5(也可以下载coco数据集上的预训练模型参数mask_rcnn_coco.h5做fine-tuning,本文下载了后者fune-tune模型)、源代码Source code(zip)Source code(tar.gz)均可。
下载后的文件如图:
下载内容

解压文件放置到对应目录

1.先解压v2.1文件,随便命名为自己想要的名字,解压后文件夹内的内容如图所示,v2.1核心代码还是mask-rcnn的东西,只添加了一个自定义数据集:
2.1解压
2.解压balloon_dataset.zip放置在v2.1文件夹内:
解压balloon
balloon_dataset.zip文件夹解压后还有一个__MACOSX文件夹,压缩包是苹果打包的,win解压后就多了一个文件夹,删除即可,然后v2.1文件夹内是这样式的:
删除_MACOSX
3.放置预训练权重参数,将mask_rcnn_coco.h5模型参数在项目文件夹与.samples/balloon目录均复制,注意,是两个目录下均复制预训练模型(其实weights_path在路径中是设置在根目录下,源代码是从linux系统中直接拷贝过来的,win系统路径为反斜杠,不识别,所以在运行中权重路径会设置在balloon.py所在路径,但为了避免有的系统可以识别,故偷个懒,在根目录下和balloon目录下均拷贝预训练模型)。
根目录:
预训练模型根目录
balloon目录:
balloon目录预训练模型
4.最后将根目录内所有.py文件均复制到./samples/balloon文件夹内。
复制py文件
到此,下载的数据和代码都放置在了相应的位置。

安装支持库

根据mask-rcnn Requirement要求,安装所需版本的支持库,本机具体为python3.7(Anaconda 4.7.12),tensorflow 2.1.0.
Requirements

设置运行参数

v2.1模型主要运行balloon.py文件,设置运行参数

train --dataset=../../balloon --weights=coco
  • 1

本机用spyder运行,设置方法如图,其他IDE根据自己的环境配置。
配置运行参数
至此,准备工作完成!!开始运行balloon.py。当然肯定会报错了orz,出现什么问题解决什么一步步来。

调试代码

1. No module named ‘keras’

第一次运行,提示

ModuleNotFoundError: No module named 'keras'
  • 1

这个问题很简单,缺少keras,pip安装即可。

pip install keras
  • 1

2. ‘tensorflow’ has no attribute ‘random_shuffle’

再次运行,提示错误

AttributeError: module 'tensorflow' has no attribute 'random_shuffle'
  • 1

显示tf没有random_shuffle属性,mask-rcnn源码为tf 1.3版本,本机是tensorflow 2.1.0环境,tf2弃用了tf1很多函数,在model.py、utils.py等文件(所有需要import tensorflow的文件中均做此修改)中,将

import tensorflow as tf
  • 1

修改为:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
  • 1
  • 2

以兼容模式运行tf1.修改后代码部分:
修改兼容tf1

3. no ‘load_weights_from_hdf5_group_by_name’

  File "E:\_detection\Mask_RCNN-2.1-tf2-Shawn\samples\balloon\model.py", line 2039, in load_weights
    topology.load_weights_from_hdf5_group_by_name(f, layers)

AttributeError: module 'keras.engine.topology' has no attribute 'load_weights_from_hdf5_group_by_name'
  • 1
  • 2
  • 3
  • 4

该错误已在mask R-CNN的issue中有提到,由farzadzare提出:
原因是keras的版本问题,新的版本没有这个函数了
解决的方案使用saving来取代代码中的topology
model.py文件
2017行

from keras.engine import topology
  • 1

修改为

from keras.engine import saving
  • 1

代码中topology全部替换为saving

4. no attribute ‘metrics_tensors’

继续运行balloon.py,报错:

  File "E:\_detection\Mask_RCNN-2.1-tf2-Shawn\samples\balloon\model.py", line 2100, in compile
    self.keras_model.metrics_tensors.append(tf.reduce_mean(

AttributeError: 'Model' object has no attribute 'metrics_tensors'
  • 1
  • 2
  • 3
  • 4

原因为keras本地版本过高,本机keras版本为2.3.1.三种解决办法,任选其一:
1)降低本地版本到2.2.5以下,当然要高于2.0.8
2)修改代码,将
self.keras_model.metrics_tensors.append(loss)
改成
self.keras_model.add_metric(loss, name)
3.初始化
self.keras_model.metrics_tensors = []
选择第二种办法,该方法是masrk rcnn master版本的代码
在2.1中稍有不同,修改办法为
将该错误定位到的model.py中的语句

self.keras_model.metrics_tensors.append(tf.reduce_mean(layer.output, keep_dims=True))
  • 1

改为

self.keras_model.add_metric(tf.reduce_mean(layer.output,keep_dims=True),name)
  • 1

修改后代码:
修改代码4

5. no attribute ‘imresize’

报错

  File "E:\_detection\Mask_RCNN-2.1-tf2-Shawn\samples\balloon\utils.py", line 465, in minimize_mask
    m = scipy.misc.imresize(m.astype(float), mini_shape, interp='bilinear')

AttributeError: module 'scipy.misc' has no attribute 'imresize'
  • 1
  • 2
  • 3
  • 4

该错误原因为scipy版本过高,本机为1.4.1,降级到scipy==1.2.1就可以解决,但是不想降级的话,找个别的函数代替该函数功能即可。
错误定位到的utils.py文件中原函数

m = scipy.misc.imresize(m.astype(float), mini_shape, interp='bilinear')
  • 1

该函数格式为

scipy.misc.imresize( arr, size, interp='bilinear', mode=None)
  • 1

解决方案: 使用skimage.transform.resize()替换代码中出现的所有imresize方法。
将错误定位语句
scipy.misc.imresize(m.astype(float), (h, w), interp=‘bilinear’)
修改为
skimage.transform.resize(m.astype(float), (h,w), order=1, mode=“constant”)

具体修改方法

  1. 先在utils.py文件中添加
import skimage.transform
  • 1
  1. 将错误定位语句
m = scipy.misc.imresize(m.astype(float), mini_shape, interp='bilinear')
  • 1

修改为

m = skimage.transform.resize(m.astype(float), mini_shape, order=1, mode="constant")
  • 1

修改后代码为:
在这里插入图片描述
3) utils.py文件中resize_image、expand_mask与unmold_mask函数中同样使用了该函数。
resize_image函数中

image = scipy.misc.imresize(
            image, (round(h * scale), round(w * scale)))
  • 1
  • 2

修改为:

image = skimage.transform.resize(
            image, (round(h * scale), round(w * scale)),
            order=1, mode="constant", preserve_range=True)
  • 1
  • 2
  • 3

expand_mask函数中

m = scipy.misc.imresize(m.astype(float), (h, w), interp='bilinear')
  • 1

修改为

m = skimage.transform.resize(m.astype(float), (h, w), order=1, mode="constant")
  • 1

unmold_mask函数中语句

mask = scipy.misc.imresize(
        mask, (y2 - y1, x2 - x1), interp='bilinear').astype(np.float32) / 255.0
  • 1
  • 2

修改为

    mask = skimage.transform.resize(mask, (y2 - y1, x2 - x1), order=1, mode="constant")
  • 1

注意:这里经测试不需要除以255.0
修改后代码为:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6. no attribute ‘_get_distribution_strategy’

报错

  File "C:\Anaconda3\lib\site-packages\tensorflow_core\python\keras\callbacks.py", line 1532, in set_model
    self.log_dir, self.model._get_distribution_strategy())  # pylint: disable=protected-access

AttributeError: 'Model' object has no attribute '_get_distribution_strategy'
  • 1
  • 2
  • 3
  • 4

该问题最后出错是因为keras模块调用了keras\callbacks\tensorboard_v2.py文件中的函数,在最开始修改代码的时候我们已经通过以下修改以tf1兼容形式运行。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
  • 1
  • 2

而keras.model模块也是按照tf1模式创建了Model,在后续调用中使用了tensorboard_v2.py文件,本文尝试在balloon.py文件中也加入tf1兼容代码,但无效,最后就只好采用如下修改方法
在Ancaonda文件夹中定位到\Lib\site-packages\keras\callbacks目录中,在tensorboard_v2.py文件中添加了

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
  • 1
  • 2

以tf1兼容模式运行。tensorboard_v2.py代码修改结果:
在这里插入图片描述
则问题解决,注意该问题修复方法修改了keras库文件,故在运行其他程序时记得修改回去

Finally,至此我们就完成了,mask-rcnn 2.1的版本修改,可以在win+tf2的环境下运行了,GPU比较差只想做下demo的小伙伴可以在balloon.py中加入以下语句,在cpu下运行(是真的慢。。。),有GPU的话不用做此修改。

import os
#only execute in cpu
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"  
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
  • 1
  • 2
  • 3
  • 4

运行截图如下,也可以直接在github下载已经修改好的代码,再下载完需要的预训练模型和数据集后,只需要修改第六点错误就可以运行。

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值