Mask_RCNN icon indicating copy to clipboard operation
Mask_RCNN copied to clipboard

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)

Open 694376965 opened this issue 5 years ago • 50 comments

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

694376965 avatar Aug 10 '18 06:08 694376965

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 avatar Aug 10 '18 07:08 Alexlastname

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

694376965 avatar Aug 13 '18 06:08 694376965

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

BelhalK avatar Sep 02 '18 08:09 BelhalK

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

zhaoyucong avatar Oct 10 '18 08:10 zhaoyucong

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

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

zhaoyucong avatar Oct 15 '18 07:10 zhaoyucong

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

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

zhaoyucong avatar Oct 15 '18 07:10 zhaoyucong

@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 avatar Oct 23 '18 09:10 citlag

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

LeBronChen1 avatar Nov 26 '18 13:11 LeBronChen1

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

ZhouHuang23 avatar Nov 29 '18 12:11 ZhouHuang23

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

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

csjunxu avatar Dec 16 '18 11:12 csjunxu

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 avatar Feb 08 '19 07:02 eyildiz-ugoe

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

abdelDebug avatar Feb 14 '19 10:02 abdelDebug

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

AlbertoMCS avatar Mar 23 '19 21:03 AlbertoMCS

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.

KanchanIIT avatar Mar 25 '19 11:03 KanchanIIT

I get the same error while training it.

Has anyone resolved this issue?

ApoorvaSuresh avatar Apr 02 '19 17:04 ApoorvaSuresh

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.

zdforient avatar Apr 03 '19 15:04 zdforient

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.

angepocalypse avatar Apr 05 '19 04:04 angepocalypse

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

LIMU2 avatar May 02 '19 09:05 LIMU2

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?

angyee avatar May 27 '19 08:05 angyee

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

how you solve this

akkshita avatar May 28 '19 11:05 akkshita

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 avatar May 28 '19 13:05 angyee

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

angyee avatar Jun 03 '19 07:06 angyee

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.

lbq779660843 avatar Jul 12 '19 03:07 lbq779660843

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

guanpingxiao avatar Jul 30 '19 04:07 guanpingxiao

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

manojkumar621 avatar Oct 14 '19 09:10 manojkumar621

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

bernardcaldas avatar Dec 08 '19 22:12 bernardcaldas

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

DeveloperRachit avatar Jan 21 '20 12:01 DeveloperRachit

oj8k

HirataYurina avatar Apr 26 '20 14:04 HirataYurina

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

kimile599 avatar Jul 01 '20 22:07 kimile599

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)

mikechen66 avatar Sep 17 '20 09:09 mikechen66