Tensorflow + Keras 运行Mask-RCNN

源码地址:https://github.com/matterport/Mask_RCNN

实验环境:Ubuntu + tensorflow1.4 + cuda8.0 + python3.5 + Keras2.1

 

前几天在tensorflow上测试了MaskRCNN,见https://blog.csdn.net/BockSong/article/details/82690343

但这个repo已经较长时间不维护了,效果也不是很理想。于是选用了github上star数最多的MaskRCNN项目,在keras和tensorflow上再次进行实验。

相关的博客资料已经较为丰富,配置过程中主要参考了:

https://blog.csdn.net/chenmoran0928/article/details/79999073

https://blog.csdn.net/wlnvgu/article/details/78489346

这里对配置中遇到的问题做一下记录。

1、关于notebook

在还没配完环境的时候提前打开了notebook,装完后后测试时报错,后来发现是notebook环境没有更新。需要在命令行里重启jupyter notebook,再次从头运行demo文件,即可顺利载入模块。

2、关于pycocotools的安装

首先下载项目源码 https://github.com/waleedka/coco (所在路径无要求)

根据另一篇博客所述,如果是Ubuntu系统,PythonAPI编译时需先用文本编辑器打开makefile,把python改成python3

然后执行如下指令(权限不够的话加上sudo)

make
make install
python setup.py install

3、生成model时提示:TypeError: softmax() got an unexpected keyword argument 'axis'

查找后发现是Keras新版和repo中的旧版语法冲突了,新版Keras去掉了这一参数。因此回退Keras版本到2.1。新版Keras附带了preprocessing和applications两个工具,为了卸载干净我先单独卸掉这两个工具,再执行如下命令重装旧版本。

pip install keras==2.1

之后demo即可顺利运行。

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用CIOU损失函数的MASK-RCNN的代码示例: ```python import tensorflow as tf from tensorflow.keras.losses import Loss def compute_ciou(box1, box2): # 计算两个边界框的IOU x1, y1, w1, h1 = box1[:, 0], box1[:, 1], box1[:, 2], box1[:, 3] x2, y2, w2, h2 = box2[:, 0], box2[:, 1], box2[:, 2], box2[:, 3] area1, area2 = w1 * h1, w2 * h2 x_min, y_min, x_max, y_max = tf.maximum(x1 - w1 / 2, x2 - w2 / 2), \ tf.maximum(y1 - h1 / 2, y2 - h2 / 2), \ tf.minimum(x1 + w1 / 2, x2 + w2 / 2), \ tf.minimum(y1 + h1 / 2, y2 + h2 / 2) intersection, union = tf.maximum((x_max - x_min), 0) * tf.maximum((y_max - y_min), 0), \ tf.maximum((area1 + area2 - intersection), 1e-7) iou = intersection / union # 计算两个边界框的中心点距离 center_distance = tf.square(x1 - x2) + tf.square(y1 - y2) # 计算两个边界框的对角线长度平方 box1_diag, box2_diag = tf.square(w1) + tf.square(h1), tf.square(w2) + tf.square(h2) # 计算CIOU v = 4 * tf.square(tf.math.atan2(w2, h2) - tf.math.atan2(w1, h1)) / (tf.math.pi ** 2) with tf.device('/cpu:0'): alpha = v / (1 - iou + v) ciou = iou - (center_distance / box2_diag) - alpha * (1 - union / (area1 + area2 - union)) return ciou class CIOULoss(Loss): def __init__(self, weight=1.0, **kwargs): super(CIOULoss, self).__init__(**kwargs) self.weight = weight def call(self, y_true, y_pred): box1, box2 = y_true, y_pred ciou = tf.clip_by_value(compute_ciou(box1, box2), clip_value_min=-1.0, clip_value_max=1.0) loss = 1 - ciou loss = tf.reduce_mean(loss) * self.weight return loss ``` 在这里,我们定义了一个`compute_ciou`函数来计算两个边界框的CIOU值,并在`CIOULoss`类中使用该函数来计算CIOU损失。最后,我们将损失乘以权重作为最终的损失。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值