Mask_rcnn: 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)

Created on 10 Aug 2018  ·  30Comments  ·  Source: matterport/Mask_RCNN

hi, when I loaded all the files down, I can run the balloon demo to detect balloon correctly.
But, when I want to train a new model with the given balloon data set according to the steps given, there was an error:
Layer #389 (named "mrcnn_bbox_fc"), weight has shape (1024, 8), but the saved weight has shape (1024, 324)

Can some one help me?
I used the mask_rcnn_coco.h5 as the pretrained model.

python3 balloon.py train --dataset=/opt/projects/samples/balloon/balloonImages/datasets/ --weights=/opt/projects/samples/balloon/mask_rcnn_coco.h5
Using TensorFlow backend.
Weights: /opt/projects/samples/balloon/mask_rcnn_coco.h5
Dataset: /opt/projects/samples/balloon/balloonImages/datasets/
Logs: /opt/projects/logs_balloon
<__main__.BalloonConfig object at 0x7efb9d8cc898>

Configurations:
BACKBONE resnet101
BACKBONE_STRIDES [4, 8, 16, 32, 64]
BATCH_SIZE 2
BBOX_STD_DEV [0.1 0.1 0.2 0.2]
COMPUTE_BACKBONE_SHAPE None
DETECTION_MAX_INSTANCES 100
DETECTION_MIN_CONFIDENCE 0.9
DETECTION_NMS_THRESHOLD 0.3
FPN_CLASSIF_FC_LAYERS_SIZE 1024
GPU_COUNT 1
GRADIENT_CLIP_NORM 5.0
IMAGES_PER_GPU 2
IMAGE_MAX_DIM 1024
IMAGE_META_SIZE 14
IMAGE_MIN_DIM 800
IMAGE_MIN_SCALE 0
IMAGE_RESIZE_MODE square
IMAGE_SHAPE [1024 1024 3]
LEARNING_MOMENTUM 0.9
LEARNING_RATE 0.001
LOSS_WEIGHTS {'rpn_class_loss': 1.0, 'rpn_bbox_loss': 1.0, 'mrcnn_class_loss': 1.0, 'mrcnn_bbox_loss': 1.0, 'mrcnn_mask_loss': 1.0}
MASK_POOL_SIZE 14
MASK_SHAPE [28, 28]
MAX_GT_INSTANCES 100
MEAN_PIXEL [123.7 116.8 103.9]
MINI_MASK_SHAPE (56, 56)
NAME balloon
NUM_CLASSES 2
POOL_SIZE 7
POST_NMS_ROIS_INFERENCE 1000
POST_NMS_ROIS_TRAINING 2000
ROI_POSITIVE_RATIO 0.33
RPN_ANCHOR_RATIOS [0.5, 1, 2]
RPN_ANCHOR_SCALES (32, 64, 128, 256, 512)
RPN_ANCHOR_STRIDE 1
RPN_BBOX_STD_DEV [0.1 0.1 0.2 0.2]
RPN_NMS_THRESHOLD 0.7
RPN_TRAIN_ANCHORS_PER_IMAGE 256
STEPS_PER_EPOCH 100
TOP_DOWN_PYRAMID_SIZE 256
TRAIN_BN False
TRAIN_ROIS_PER_IMAGE 200
USE_MINI_MASK True
USE_RPN_ROIS True
VALIDATION_STEPS 50
WEIGHT_DECAY 0.0001

Loading weights /opt/projects/samples/balloon/mask_rcnn_coco.h5
Traceback (most recent call last):
File "balloon.py", line 357, in
model.load_weights(weights_path, by_name=True)
File "/opt/projects/mrcnn/model.py", line 2140, in load_weights
reshape=False)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py", line 1017, in load_weights_from_hdf5_group_by_name
str(weight_values[i].shape) + '.')
ValueError: Layer #389 (named "mrcnn_bbox_fc"), weight has shape (1024, 8), but the saved weight has shape (1024, 324).

Most helpful comment

@zhaoyucong I had the same problem. After searching for a while I found that when you want to fine-tune, you have to specify in "load_weights()" the parameter "by_name=True" in order to be able to use only some common layers (https://keras.io/models/about-keras-models/).

In my case that was not enough, and I added the following:
model.load_weights(filepath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

Hope it helps

All 30 comments

There are 80+1 classes in coco dataset, while you only get 2 classes. So when loading weights you should exclude some layers such as 'mrcnn_bbox_fc','mrcnn_class_logits’(fill the layer's name in the load_weights method), then start fine-tuning.

@Alexlastname, OK, I did it as you told me , the error was resolved, Thank you very much!

Hi,

I trained the model with my dataset (it worked).
but now when I want to test on an image
python3 balloon.py splash --weights=weights/mask_rcnn_coco_trainedV1.h5 --image=customImages/vall/image66.jpg

I get the same error.

How can it work for training but not for predicting?
How could i exclude some layers as you mentionned bove? @Alexlastname

@694376965 load_weights方法在那个文件夹下

coco数据集中有80 + 1个类,而您只有2个类。因此,在加载权重时,您应该排除某些层,例如'mrcnn_bbox_fc','mrcnn_class_logits'(在load_weights方法中填充图层的名称),然后开始微调。

您能详细的说一下么 我还是没有明白

@Alexlastname,好的,我按你告诉我的那样做了,错误已经解决了,非常感谢!

您能说一下你解决的详细步骤么

@zhaoyucong I had the same problem. After searching for a while I found that when you want to fine-tune, you have to specify in "load_weights()" the parameter "by_name=True" in order to be able to use only some common layers (https://keras.io/models/about-keras-models/).

In my case that was not enough, and I added the following:
model.load_weights(filepath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

Hope it helps

@citlag Thanks. I do it as you told me. However, the result isn't which I want to see. The result is many cars which are boxed and named 'person'. It isn't correct cause the class i want to detect is person.

@zhaoyucong 请问你的问题解决了吗,现在我也遇到同样的问题。

@zhaoyucong 请问你的问题解决了吗,现在我也遇到同样的问题。

model.load_weights(filepath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

I trained it and had no problem, but when I want to predict I get this error.

I do have model.load_weights(weights_path, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]) line and I still get this error.

Anyone who was able to solve it? (Only in English please)

@eyildiz-ugoe did you find a solution for this issue ??

There are 80+1 classes in coco dataset, while you only get 2 classes. So when loading weights you should exclude some layers such as 'mrcnn_bbox_fc','mrcnn_class_logits’(fill the layer's name in the load_weights method), then start fine-tuning.

I don't understand well the answer above (I am new on this). Can somebody shed some lights in how to applied that ("fill the layer's name in the load_weights method")

Thanks

the same issue here. while training i did like model.load_weights(weights_path, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]).
But during testing, while loading the model if I exclude ["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"] not producing the correct results. But If I load the model without the exclude layers in testing it showing the same error.

I get the same error while training it.

Has anyone resolved this issue?

Solved;
In your model.py:
find the following lines:
if args.weights.lower() == "coco":
# Exclude the last layers because they require a matching
# number of classes
model.load_weights(weights_path, by_name=True, exclude=[
"mrcnn_class_logits", "mrcnn_bbox_fc",
"mrcnn_bbox", "mrcnn_mask"])
else:
model.load_weights(weights_path, by_name=True)
you can see some layers are excluded if the weight is "coco";
In your case, you just need to add some lines to else{} to remove these layers.

I think I found another possible disconnect for people following the balloon sample README.

Notice the comment on this page:
https://github.com/matterport/Mask_RCNN/releases
"Note: COCO weights are not updated in this release. Continue to use the .h5 file from release 2.0."

These are the training arguments described on the balloon sample page:

python3 balloon.py train --dataset=/path/to/balloon/dataset --weights=coco

But even though the training will succeed, it produces the incorrect result when using the newly trained model to predict.

Instead, you should train by downloading mask_rcnn_coco.h5 from the 2.0 release and changing the arguments to:

python3 balloon.py train --dataset=/path/to/balloon/dataset --weights=/path/to/mask_rcnn_coco.h5

You still need to change the model.load_weights call to exclude the other layers as @zdforient mentioned.

@zhaoyucong 请问你的问题解决了吗,现在我也遇到同样的问题。

model.load_weights(filepath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

thank you, the error was resolved~

the same issue here. while training i did like model.load_weights(weights_path, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]).
But during testing, while loading the model if I exclude ["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"] not producing the correct results. But If I load the model without the exclude layers in testing it showing the same error.

@zhaoyucong @Alexlastname @KanchanIIT

not producing the correct results, you are solved the issue?

@Alexlastname, OK, I did it as you told me , the error was resolved, Thank you very much!

how you solve this

Load weights

print("Loading weights ", weights_path)
if args.weights.lower() == "coco":
    # Exclude the last layers because they require a matching
    # number of classes
    model.load_weights(weights_path, by_name=True, exclude=[
        "mrcnn_class_logits", "mrcnn_bbox_fc",
    "mrcnn_bbox", "mrcnn_mask"])
else:
    model.load_weights(weights_path, by_name=True, exclude=[
        "mrcnn_class_logits", "mrcnn_bbox_fc",
    "mrcnn_bbox", "mrcnn_mask"])

above code is a part of program and because of this running the program but not producing the masked result images. anyone solved the problem of this issue? @Alexlastname @zdforient @BelhalK @zhaoyucong @waleedka @PavlosMelissinos @rymalia @moorage

done exactly same but the result is saving in png image file but no masked image result for a
damaged part on the car

splash_20190531T105807

I have done same as you told solution 1, it is working but the result is in non-masked image for damage part of car, means result is same as input image.

for solution 2, in the code where I have to change name from ""mask_rcnn_coco.h5"" to ""coco"" ?

@akkshita

I meet the same problem just like
Layer #389 (named "mrcnn_bbox_fc"), weight <tf.Variable 'mrcnn_bbox_fc/kernel:0' shape=(1024, 8) dtype=float32_ref> has shape (1024, 96), but the saved weight has shape (1024, 100)
and then I solve it.
I think the reason why it occurs is I used the some region attributes(frankly,the name) in different regions while I annotated some pictures .
so I add 1 class which has never been used in the balloon.py (1 = [100 -96]/4,maybe) just like:
self.add_class("balloon", 24, "blank")
and now the new NUM_CLASSES quotes to NUM_CLASSES + 1.

@zhaoyucong I had the same problem. After searching for a while I found that when you want to fine-tune, you have to specify in "load_weights()" the parameter "by_name=True" in order to be able to use only some common layers (https://keras.io/models/about-keras-models/).

In my case that was not enough, and I added the following:
model.load_weights(filepath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

Hope it helps

Hi, when I use the exclude, it throws the following message:
_TypeError: load_weights() got an unexpected keyword argument 'exclude'_

I still cant figure out a working solution from all the above discussion. Anybody out there to rescue?? Atleast please explain how to append the classes number in model.py

@zhaoyucong I had the same problem. After searching for a while I found that when you want to fine-tune, you have to specify in "load_weights()" the parameter "by_name=True" in order to be able to use only some common layers (https://keras.io/models/about-keras-models/).

In my case that was not enough, and I added the following:
model.load_weights(filepath, by_name=True, exclude=[ "mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"])

Hope it helps

it works thanks!

i am also gettting same error when usin resnet152 how to resolve it..

ValueError: Layer #24 (named "res2b_branch2a"), weight has shape (1, 1, 512, 64), but the saved weight has shape (64, 256, 1, 1).

oj8k

i am also gettting same error when usin resnet152 how to resolve it..

ValueError: Layer #24 (named "res2b_branch2a"), weight has shape (1, 1, 512, 64), but the saved weight has shape (64, 256, 1, 1).

Do you modify the width/height for the anchor

I have solved the kind of issue as follows. Hope the solution would be helpful.

Delete "by_name=True"

# -model.load_weights(weights_path, by_name=True,...)
model.load_weights(weights_path)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

JonathanCMitchell picture JonathanCMitchell  ·  3Comments

Mabinogiysk picture Mabinogiysk  ·  3Comments

LifeBeyondExpectations picture LifeBeyondExpectations  ·  4Comments

techjjun picture techjjun  ·  4Comments

wadmes picture wadmes  ·  4Comments