环境:
tensorflow 2.4.3
keras 2.4.0
python 3.8.3
numpy 1.19.2
skimage 0.19
代码:
import os
import matplotlib.pyplot as plt
from mrcnn.config import Config
from mrcnn import model as modellib, utils
from mrcnn.visualize import display_instances
# 设置数据路径
data_dir = 'fruits-360'
train_dir = os.path.join(data_dir, 'Training')
validation_dir = os.path.join(data_dir, 'Validation')
# 配置Mask R-CNN模型
class MyConfig(Config):
NAME = "my_config"
GPU_COUNT = 1
IMAGES_PER_GPU = 1
NUM_CLASSES = 6
IMAGE_MIN_DIM = 640
IMAGE_MAX_DIM = 640
config = MyConfig()
# 创建Mask R-CNN模型
model = modellib.MaskRCNN(mode="inference", config=config, model_dir=os.getcwd())
# 加载预训练权重文件
weights_path = "mask_rcnn_balloon.h5"
model.load_weights(weights_path, by_name=True)
# 准备要识别的图片
image_paths = ['fruits-360/Test/Apple Golden 1/63_100.jpg', 'fruits-360/Test/Apple Golden 1/72_100.jpg'] # 替换为您要识别的图片路径
class_names = ['Apple Golden 1', 'Apple Golden 2', 'Apple Golden 3', 'Apple Red 1', 'Apple Red 2', 'Apple Red 3']
# 进行目标识别
for image_path in image_paths:
image = plt.imread(image_path)
results = model.detect([image], verbose=1)
# 可视化识别结果
r = results[0]
display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
plt.show()
在使用mrcnn模型时出现了许多问题,我也在stackflow上找到了一个解决方案,但按解决方案的步骤去做后,仍然会出现问题。
查询的解决方案如下:
Installing tensorflow with version as following
pip uninstall tensorflow -y
pip uninstall keras -y
pip install tensorflow==2.4.3
pip install keras==2.4.0
After above, some errors will arise. You could solve them by following steps.
@Error: [module 'tensorflow' has no attribute XXXXXXXX]
In the model.py or your code, resolving some api with tf.compat.v1, e.g. tf.compat.v1.Session or import tensorflow.compat.v1 as tf
@Error: [ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.]
mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
replace with this this if-else code block:
if s[1]==None:
mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
else:
mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
@Error: [ValueError: None values not supported.]
indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
replace with
indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
@Error: [AttributeError: module 'keras.engine.saving' has no attribute 'load_weights_from_hdf5_group_by_name']
from keras import saving
replace with
from tensorflow.python.keras.saving import hdf5_format
and
saving.load_weights_from_hdf5_group(f, layers)
saving.load_weights_from_hdf5_group_by_name(f, layers)
replace with
hdf5_format.load_weights_from_hdf5_group(f, layers)
hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
做完最后一步之后,出现问题:
File "D:/HomeWork/deep_learning/yolox.py", line 31, in
model.load_weights(weights_path, by_name=True)
File "C:\Program Files\Python38\lib\site-packages\mrcnn\model.py", line 2122, in load_weights
hdf5_format.load_weights_from_hdf5_group(f, layers)
File "C:\Users\86138\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 702, in load_weights_from_hdf5_group
raise ValueError('Layer #' + str(k) + ' (named "' + layer.name +
ValueError: Layer #222 (named "mrcnn_bbox_fc" in the current model) was found to correspond to layer mrcnn_class_bn1 in the save file. However the new layer mrcnn_bbox_fc expects 2 weights, but the saved weights have 4 elements.