fuwiak
fuwiak

Reputation: 741

TypeError: __init__() missing 2 required positional arguments

I have Keras model in format .h5. I want to read it with this code:

#model.h5 is name of mode, tf and modellib is read

custom_objects={'tf': tf,'BatchNorm':modellib.BatchNorm,'ProposalLayer':
                modellib.ProposalLayer} 
model=tf.keras.models.load_model("model.h5")

But I getting an error

TypeError: init() missing 2 required positional arguments: 'proposal_count' and 'nms_threshold'

I have the newest version of TensorFlow (2.2). Changing the version of TensorFlow is not helping.

Upvotes: 1

Views: 3236

Answers (1)

SvMax
SvMax

Reputation: 198

Based on the error and the custom objects i think you are tring to save the model of matterport/MaskRcnn, I ran in the same issue. The problem was that the custom layer ProposalLayer isn't correctly defined.

The parameters of the custom layer init should be initialized by default, otherwise an error may be reported:

TypeError: init() missing 2 required positional arguments: 'proposal_count' and 'nms_threshold'

I solved the issue in this way:

first of all i load the standard configuration in this way:

from mrcnn import config as config_std

thene i modify the init of the custom layer "ProposalLayer"

    def __init__(self,  proposal_count = config_std.Config.POST_NMS_ROIS_TRAINING, 
                    nms_threshold = config_std.Config.RPN_NMS_THRESHOLD, 
                    config=None, **kwargs):

I simply put the standard config passed in build function of the framework directly as default argument as keras need.

Upvotes: 1

Related Questions