@-@991 2023-10-08 16:00 采纳率: 0%
浏览 9

ValueError: Layer #222

环境:
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.

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2023-10-09 06:32
    关注

    引自强大而免费的微信小程序:皆我百晓生。


    这个错误是由于保存的模型权重与加载的模型结构不匹配导致的。具体来说,模型中的第222个层被命名为"mrcnn_bbox_fc",但是保存的权重中对应的层被命名为"mrcnn_class_bn1"。

    要解决这个问题,你可以尝试以下方法之一:

    1. 使用相同的模型结构重新训练并保存权重:重新训练模型,并确保在保存模型权重时使用相同的命名方式。然后使用重新训练的权重进行预测。

    2. 修改加载权重的代码:你可以在加载权重之前检查模型中每个层的名称和顺序,然后根据需要重命名加载的权重。例如,你可以使用model.summary()查看模型结构,并在加载权重之前通过修改代码中的层名称来对齐。

    也可以尝试更新mrcnn库的版本来解决该问题。请尝试更新mrcnn库的版本,或查看是否有其他版本兼容的解决方案可用。

    希望这些信息对你有帮助,如果还有其他问题,请随时询问。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月8日

悬赏问题

  • ¥15 有可能用平板通过拓展坞来烧录程序吗(keil5的那种)
  • ¥15 网络分析设施点无法识别
  • ¥15 状态图的并发态问题咨询
  • ¥15 PFC3D,plot
  • ¥15 VAE模型编程报错无法解决
  • ¥100 基于SVM的信息粒化时序回归预测,有偿求解!
  • ¥15 物体组批优化问题-数学建模求解答
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题