segmentation_models icon indicating copy to clipboard operation
segmentation_models copied to clipboard

AttributeError: 'Functional' object has no attribute 'loss_weights'

Open Crispy13 opened this issue 4 years ago • 6 comments

The code i used:

    model = P['model'](P['backbone'], encoder_weights='imagenet', encoder_freeze = True)#, gn_groups = P['gn_groups'])
    
    if P['use_reg']:
        model = set_regularization(model, kernel_regularizer=P['reg_class'](P['reg_lambda']), 
                                           bias_regularizer = P['reg_class'](P['reg_lambda'])
                              )
    
    dice = DiceCoefficient()

    if P['mixed_precision']:
        opt = keras.mixed_precision.LossScaleOptimizer(
                                                            keras.optimizers.Adam(learning_rate=P['LR']),
                                                    )
    else:
        opt = keras.optimizers.Adam(learning_rate=P['LR'])
        
    loss = HybridLoss()
    
    model.compile(optimizer = opt,
                          loss = loss, #tf.keras.losses.BinaryCrossentropy(),#'focal_tversky',
                          metrics=[dice, 'accuracy'])
    
    model.fit() ...
    
   set_trainable(model)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-27-271bafab9920> in <module>
    109 
    110     # unfreeze model layers
--> 111     set_trainable(model)
    112 
    113     ## Start training

~/software/anaconda3/envs/hk2/lib/python3.8/site-packages/segmentation_models/utils.py in set_trainable(model, recompile, **kwargs)
     32             loss=model.loss,
     33             metrics=model.metrics,
---> 34             loss_weights=model.loss_weights,
     35             sample_weight_mode=model.sample_weight_mode,
     36             weighted_metrics=model.weighted_metrics,

AttributeError: 'Functional' object has no attribute 'loss_weights'

Crispy13 avatar Jan 11 '21 04:01 Crispy13

same problem here. Any solution?

thedevstone avatar Mar 30 '21 09:03 thedevstone

same problem

yokoponzoo avatar May 10 '21 12:05 yokoponzoo

same problem. A quick fix is to pass recompile=False, it's not ideal tho...

diogosilva30 avatar Aug 03 '21 13:08 diogosilva30

same problem.

zahrasalarian avatar Aug 31 '21 18:08 zahrasalarian

The following seems to run, but is it recommended?

set_trainable(model, recompile=False)
model.compile(
    model.optimizer,
    loss=model.loss,
    metrics=model.metrics,
    loss_weights=None,
    sample_weight_mode=None,
    weighted_metrics=None,
)

pace577 avatar Sep 17 '21 13:09 pace577

@pace577 Yes. If you check the code of the set_trainable method, that's what it does.

diogosilva30 avatar Sep 17 '21 13:09 diogosilva30