matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.63k stars 11.7k forks source link

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 6 years ago

694376965 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

BelhalK commented 6 years ago

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 commented 6 years ago

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

zhaoyucong commented 6 years ago

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

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

zhaoyucong commented 6 years ago

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

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

citlag commented 6 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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

csjunxu commented 5 years ago

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

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

eyildiz-ugoe commented 5 years ago

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 commented 5 years ago

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

AlbertoMCS commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

I get the same error while training it.

Has anyone resolved this issue?

zdforient commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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

how you solve this

angyee commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

oj8k

kimile599 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Switched to YoloV4

sharifim commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

Okay so what worked for me after trying different solutions was a hit a trial thing that I figured out and that is, if you have been using the mask_rcnn_balloon.h5 file while running the program, there are three versions of this file on the release page try to download the mask_rcnn 1.0 coco.h5 file or the mask_rcnn 2.0 coco.h5 file. It worked for me

MatchaCookies commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

howhy1 commented 2 years ago

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 commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。

Tejasw2002 commented 1 year ago

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 commented 1 year ago

这是来自QQ邮箱的假期自动回复邮件。你好,我最近正在休假中,无法亲自回复你的邮件。我将在假期结束后,尽快给你回复。