Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 层权重的keras形状与保存的权重形状不匹配_Python_Tensorflow_Machine Learning_Keras - Fatal编程技术网

Python 层权重的keras形状与保存的权重形状不匹配

Python 层权重的keras形状与保存的权重形状不匹配,python,tensorflow,machine-learning,keras,Python,Tensorflow,Machine Learning,Keras,我尝试使用matterport Mask R-CNN实现来识别图像中的项目。我使用coco数据集作为起点,并对其进行了训练,以识别其他类型的对象。效果很好。它能够在场景中识别出该类型对象的存在,并取得了一些成功。 我最近完成了第二批培训,现在当我试图检查同一场景中是否存在相同的项目时,它抛出了一个错误 调用此代码时: model = modellib.MaskRCNN(mode="inference", config=config, model_dir=LOGS_AND_MO

我尝试使用matterport Mask R-CNN实现来识别图像中的项目。我使用coco数据集作为起点,并对其进行了训练,以识别其他类型的对象。效果很好。它能够在场景中识别出该类型对象的存在,并取得了一些成功。 我最近完成了第二批培训,现在当我试图检查同一场景中是否存在相同的项目时,它抛出了一个错误

调用此代码时:

model = modellib.MaskRCNN(mode="inference", config=config, model_dir=LOGS_AND_MODEL_DIR)
weights = args["weights"] if args["weights"] \
            else model.find_last()
model.load_weights(weights, by_name=True)
我的程序现在遇到以下错误:

Traceback (most recent call last):
  File "mask_training.py", line 185, in <module>
    model.load_weights(weights, by_name=True)
  File "/usr/local/lib/python3.6/site-packages/mask_rcnn-2.1-py3.6.egg/mrcnn/model.py", line 2132, in load_weights
  File "/usr/local/lib/python3.6/site-packages/keras/engine/saving.py", line 1018, in load_weights_from_hdf5_group_by_name
    str(weight_values[i].shape) + '.')
ValueError: Layer #389 (named "mrcnn_bbox_fc"), weight <tf.Variable 'mrcnn_bbox_fc/kernel:0' shape=(1024, 8) dtype=float32_ref> has shape (1024, 8), but the saved weight has shape (1024, 324).

您应该排除一些层,例如
mrcnn\u bbox\u fc
mrcnn\u class\u logits
(在
load\u weights
方法中填写层的名称),然后开始微调

可以排除这些图层,而不是:

model.load\u权重(自定义模型路径,按名称=True)
使用:

model.load\u重量(
COCO_模型_路径,通过_name=True,
排除=[“mrcnn\u类登录”、“mrcnn\u bbox\u fc”、“mrcnn\u bbox”、“mrcnn\u掩码”、“rpn\u模型”])

此修复对我有效,但我不确定原因。当涉及到推理模式时,为什么我们要排除这些层?马特波特原始回购协议中的演示笔记本中没有提到这个问题的原因可能是什么?
idxs = list(range(0, len(IMAGE_PATHS)))
random.seed(42)
random.shuffle(idxs)
i = int(len(idxs) * TRAINING_SPLIT) 
trainIdxs = idxs[:i]
valIdxs = idxs[i:]
trainDataset = ImageBoundaryDataset(IMAGE_PATHS, CLASS_NAMES)
        trainDataset.load_images(trainIdxs)
        trainDataset.prepare()
        valDataset = ImageBoundaryDataset(IMAGE_PATHS, CLASS_NAMES)
        valDataset.load_images(valIdxs)
        valDataset.prepare()
        config = ImageBoundaryConfig()
        config.display()

    #image augmentation, flip or rotate image
    aug = iaa.SomeOf((0, 2), [
        iaa.Fliplr(0.5),
        iaa.Flipud(0.5),
        iaa.Affine(rotate=(-10, 10))
    ])

model = modellib.MaskRCNN(mode="training", config=config, model_dir=LOGS_AND_MODEL_DIR)
        model.load_weights(COCO_PATH, by_name=True,
                            exclude=["mrcnn_class_logits", "mrcnn_bbox_fc",
                                    "mrcnn_bbox", "mrcnn_mask"])

        print("loaded weights, about to train layer heads")
        #train layer heads
        model.train(trainDataset, valDataset, learning_rate=config.LEARNING_RATE, epochs=5,
            layers="heads", 
            augmentation=aug)
        print("trained layer heads, about to train model")

        #train all layers
        model.train(trainDataset, valDataset, epochs=10, #usually 40 for serious training
            layers="all", learning_rate=config.LEARNING_RATE / 10, augmentation=aug)