Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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) #849

Open
694376965 opened this issue Aug 10, 2018 · 50 comments

Comments

@694376965
Copy link

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

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
<HDF5 file "mask_rcnn_coco.h5" (mode r)>
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 <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).

@Alexlastname
Copy link

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.

@694376965
Copy link
Author

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

@BelhalK
Copy link

BelhalK commented Sep 2, 2018

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

@zhaoyucong
Copy link

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

@zhaoyucong
Copy link

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

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

@zhaoyucong
Copy link

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

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

@citlag
Copy link

citlag commented Oct 23, 2018

@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

@LeBronChen1
Copy link

@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.

@ZhouHuang23
Copy link

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

@csjunxu
Copy link

csjunxu commented Dec 16, 2018

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

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

@eyildiz-ugoe
Copy link

eyildiz-ugoe commented Feb 8, 2019

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)

@abdelDebug
Copy link

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

@AlbertoMCS
Copy link

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

@KanchanIIT
Copy link

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.

@ApoorvaSuresh
Copy link

I get the same error while training it.

Has anyone resolved this issue?

@zdforient
Copy link

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.

@angepocalypse
Copy link

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.

@LIMU2
Copy link

LIMU2 commented May 2, 2019

@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~

@angyee
Copy link

angyee commented May 27, 2019

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?

@akkshita
Copy link

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

how you solve this

@angyee
Copy link

angyee commented May 28, 2019

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

@angyee
Copy link

angyee commented Jun 3, 2019

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

@lbq779660843
Copy link

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.

@guanpingxiao
Copy link

@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'

@manojkumar621
Copy link

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

@bernardcaldas
Copy link

@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!

@DeveloperRachit
Copy link

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

ValueError: Layer #24 (named "res2b_branch2a"), weight <tf.Variable 'res2b_branch2a/kernel:0' shape=(1, 1, 512, 64) dtype=float32> has shape (1, 1, 512, 64), but the saved weight has shape (64, 256, 1, 1).

@HirataYurina
Copy link

oj8k

@kimile599
Copy link

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

ValueError: Layer #24 (named "res2b_branch2a"), weight <tf.Variable 'res2b_branch2a/kernel:0' shape=(1, 1, 512, 64) dtype=float32> 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

@mikechen66
Copy link

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)

@zekeriyyaa
Copy link

zekeriyyaa commented Jan 3, 2021

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.

You can exclude these layers as given below:

instead of model.load_weights(CUSTOM_MODEL_PATH, by_name=True)

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

@asanc199
Copy link

Hi!
I stuck still stucked with this issue, i'm writing down on my model.py:
keras_model.load_weights(filepath, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc","mrcnn_bbox", "mrcnn_mask"])

but the following error pops up:

TypeError: load_weights() got an unexpected keyword argument 'exclude'

Has someone achieved to solve this issue?
@zhaoyucong @eyildiz-ugoe @csjunxu @guanpingxiao @zdforient

@technisekai
Copy link

hello!
i have same error here. im adding exclude it seem the error is solved but when im upload an image the result is *** No instances to display *** . what should i do? thank you before

@jchen706
Copy link

I have the same error, use exclude will affect the result because on the other laptop (windows, it worked fine without exclude and with exclude my results are messed up), but on Ubuntu Linux, it get this error

@abanoub-sman
Copy link

abanoub-sman commented Apr 23, 2021

I was having the same problem after changing the name in my extended configuration to be name=" coco", and then used the default load weights model.load_weights(weights_path, by_name=True) (without excluding anything). It worked correctly. I hope that is helpful.

@pvhdhxd
Copy link

pvhdhxd commented Jun 4, 2021

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.

Hi, I followed this guidance. It has worked, however the result was not correct.
Could anyone help ?

@jchen706
Copy link

jchen706 commented Jun 5, 2021

Switched to YoloV4

@sharifim
Copy link

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.

Hi, I followed this guidance. It has worked, however the result was not correct.
Could anyone help ?

I have the same issue here. Were you able to resolve this problem?

@deeptox
Copy link

deeptox commented Aug 4, 2021

Refer to # #2252

Actually I changed
"from keras import saving"
to "from tensorflow.python.keras.saving import hdf5_format as saving"
in model.py
It worked perfectly for my case.
Good luck!

@ota-sts
Copy link

ota-sts commented Aug 25, 2021

I have a question. Excluding some layers, Are these layers not able to training ?
And for people who can't excluding these layers, try to rewrite "--weights = coco" not "--weights = 'mask_rcnn_coco.h5'" when choosing a weight you'll use in terminal . It's possible to run your program successfully.

@shepherd25
Copy link

shepherd25 commented Sep 14, 2021 via email

@MatchaCookies
Copy link

MatchaCookies commented Oct 26, 2021

@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

But in my case, crack detection, it made the crack detection itself producing incorrect output compared to the demo

@MatchaCookies
Copy link

@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.

did you solve it? how?

@rajathslr
Copy link

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.

For me this was the exact problem, the trick is to choose the model.h5 file from ""C:\Users\Username\Desktop\Python Projects\Curveball\MaskRcnn\logs\object20220131T0046\mask_rcnn_object_0020.h5"" and not the saved model as entered by you.

@mahaveer220
Copy link

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 Layer #389 error.

Anyone was able to solve it?

kindly shed some light.

@Alexlastname
Copy link

Alexlastname commented May 6, 2022 via email

@howhy1
Copy link

howhy1 commented Jun 10, 2022

If you are loading the weights before re-training, then for sure you need to have exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"]
However, if you are loading the weights for inference, then you may want to check the NUM_CLASSES in your config file. It should match the value of your config for the re-training. (Yes, I made the same mistake. The default value for coco is 80+1.)

@Alexlastname
Copy link

Alexlastname commented Jun 10, 2022 via email

@Tejasw2002
Copy link

If you are loading the weights before re-training, then for sure you need to have exclude=["mrcnn_class_logits", "mrcnn_bbox_fc", "mrcnn_bbox", "mrcnn_mask"] However, if you are loading the weights for inference, then you may want to check the NUM_CLASSES in your config file. It should match the value of your config for the re-training. (Yes, I made the same mistake. The default value for coco is 80+1.)

This worked!

@Alexlastname
Copy link

Alexlastname commented Aug 18, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests