原版的 Mask R-CNN: GitHub - matterport/Mask_RCNN: Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
的运行环境是 Tensorflow 1.x,我使用的显卡是 Nvidia 3090Ti,当运行 train.py 训练模型时出错:
failed to run cuBLAS routine: CUBLAS_STATUS_EXECUTION_FAILED
did not memzero GPU location; source: 0x70bc90ffbaf0
did not allocate timer: 0x70bc90ffbb20
出错的原因是 NVidia 3090Ti 显卡与 Tensorflow 1.14/15 不兼容。
为了能够在 Tensorflow 2.4 环境下使用 Mask R-CNN,设置如下:
1. 从原版Mask R-CNN 地址下载其一个分支:
https://github.com/matterport/Mask_RCNN/tree/295c802f0bebbc4a34ec4855f4960a52a701271d
2. 修改 rcnn/model.py,找到代码:
```
if model.uses_learning_phase and not isinstance(K.learning_phase(), int):
```
替换为:
```
if not isinstance(K.learning_phase(), int):
```
3. 在 model.py 中添加:
```
class AnchorsLayer(KL.Layer):
def __init__(self, anchors, name="anchors", **kwargs):
super(AnchorsLayer, self).__init__(name=name, **kwargs)
self.anchors = tf.Variable(anchors)
def call(self, dummy):
return self.anchors
def get_config(self):
config = super(AnchorsLayer, self).get_config()
return config
```
找到代码:
```
anchors = KL.Lambda(lambda x: tf.Variable(anchors), name="anchors")(input_image)
```
替换为:
```
anchors = AnchorsLayer(anchors, name="anchors")(input_image)
```
4. 确保主要python 包的版本:
tensorflow 2.4.0
keras 2.9.0
numpy 1.23.4
scikit-image 0.16.2
5. python 版本 3.8,CUDA 12.2
6. 运行train.py 时观察控制台信息,如发现无法装载某个 CUDA 库,可以设置 LD_LIBRARY_PATH 环境变量指向该库所在的目录
7. 通过命令 nvidia-smi 观察GPU 使用情况,确保 train.py 使用了 Nvidai GPU