keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 913 forks source link

AttributeError: 'Model' object has no attribute 'metrics_tensors' running mask r-cnn #145

Open jiangjiangjianggx opened 4 years ago

jiangjiangjianggx commented 4 years ago

Hi, I am trying to run a mask r-cnn code for dental segmentation images training based on the coco and Mask_RCNN, the code should work perfectly, but since my Keras is 2.3.0 and it seems not having the attribute metrics_tensor.

the code:


model.train(dataset_train, dataset_train,
learning_rate=config_train.LEARNING_RATE,
epochs=10,
layers='heads')

Training - Stage 2

Finetune layers from ResNet stage 4 and up

print("Fine tune Resnet stage 4 and up") model.train(dataset_train, dataset_train, learning_rate=config_train.LEARNING_RATE, epochs=50, layers='4+')

Training - Stage 3

Fine tune all layers

print("Fine tune all layers") model.train(dataset_train, dataset_train, learning_rate=config_train.LEARNING_RATE / 10, epochs=100, layers='all'



> here is the error:

AttributeError                            Traceback (most recent call last)
<ipython-input-28-fb0a244ef101> in <module>
      4             learning_rate=config_train.LEARNING_RATE,
      5             epochs=10,
----> 6             layers='heads')
      7 
      8 # Training - Stage 2

/usr/local/lib/python3.7/site-packages/mrcnn/model.py in train(self, train_dataset, val_dataset, learning_rate, epochs, layers, augmentation)
   2330         log("Checkpoint Path: {}".format(self.checkpoint_path))
   2331         self.set_trainable(layers)
-> 2332         self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
   2333 
   2334         # Work-around for Windows: Keras fails on Windows when using

/usr/local/lib/python3.7/site-packages/mrcnn/model.py in compile(self, learning_rate, momentum)
   2186                 tf.reduce_mean(layer.output, keepdims=True)
   2187                 * self.config.LOSS_WEIGHTS.get(name, 1.))
-> 2188             self.keras_model.metrics_tensors.append(loss)
   2189 
   2190     def set_trainable(self, layer_regex, keras_model=None, indent=0, verbose=1):

AttributeError: 'Model' object has no attribute 'metrics_tensors'

how can I fix this error? should I downgrade my Keras to lower version?
atahadd commented 4 years ago

I have the same issue with keras=1.3.1

DataStunner commented 4 years ago

It is metrics instead of metrics_tensors. I'm using Tensorflow 1.14.0 and Keras 2.3.0. Updating the model.py (2190 - 2199) worked for me.

Add metrics for losses

    for name in loss_names:
        if name in self.keras_model.metrics_names:
            continue
        layer = self.keras_model.get_layer(name)
        self.keras_model.metrics_names.append(name)
        loss = (
            tf.reduce_mean(layer.output, keepdims=True)
            * self.config.LOSS_WEIGHTS.get(name, 1.))
        self.keras_model.metrics.append(loss)
dlllll-q commented 4 years ago

It is metrics instead of metrics_tensors. I'm using Tensorflow 1.14.0 and Keras 2.3.0. Updating the model.py (2190 - 2199) worked for me.

Add metrics for losses

    for name in loss_names:
        if name in self.keras_model.metrics_names:
            continue
        layer = self.keras_model.get_layer(name)
        self.keras_model.metrics_names.append(name)
        loss = (
            tf.reduce_mean(layer.output, keepdims=True)
            * self.config.LOSS_WEIGHTS.get(name, 1.))
        self.keras_model.metrics.append(loss)

Hi, firstly, thanks for your reply, this works for me. But there is one problem, in tf.1.12, I use metrics_tensor, which can print loc_loss, class_loss except the total loss. When i change metrics_tensor to metrics in tf1.14, only total loss can be printed in training process. How can I solve this problem?

pipun-gif commented 4 years ago

It is metrics instead of metrics_tensors. I'm using Tensorflow 1.14.0 and Keras 2.3.0. Updating the model.py (2190 - 2199) worked for me.

Add metrics for losses

    for name in loss_names:
        if name in self.keras_model.metrics_names:
            continue
        layer = self.keras_model.get_layer(name)
        self.keras_model.metrics_names.append(name)
        loss = (
            tf.reduce_mean(layer.output, keepdims=True)
            * self.config.LOSS_WEIGHTS.get(name, 1.))
        self.keras_model.metrics.append(loss)

Hi, firstly, thanks for your reply, this works for me. But there is one problem, in tf.1.12, I use metrics_tensor, which can print loc_loss, class_loss except the total loss. When i change metrics_tensor to metrics in tf1.14, only total loss can be printed in training process. How can I solve this problem?

hi excuse me @dlllll-q how to edit model.py inside egg file? i can open it by rename the .egg into .zip and modify all file inside that egg, but unfortunately i don't know package it back as a python egg file after my modification. thank you

rsalunga29 commented 4 years ago

It is metrics instead of metrics_tensors. I'm using Tensorflow 1.14.0 and Keras 2.3.0. Updating the model.py (2190 - 2199) worked for me.

Add metrics for losses

    for name in loss_names:
        if name in self.keras_model.metrics_names:
            continue
        layer = self.keras_model.get_layer(name)
        self.keras_model.metrics_names.append(name)
        loss = (
            tf.reduce_mean(layer.output, keepdims=True)
            * self.config.LOSS_WEIGHTS.get(name, 1.))
        self.keras_model.metrics.append(loss)

do we have a pull request for this so it can be fixed for everyone running keras 2.3.x?

rrm003 commented 4 years ago

It is metrics instead of metrics_tensors. I'm using Tensorflow 1.14.0 and Keras 2.3.0. Updating the model.py (2190 - 2199) worked for me.

Add metrics for losses

    for name in loss_names:
        if name in self.keras_model.metrics_names:
            continue
        layer = self.keras_model.get_layer(name)
        self.keras_model.metrics_names.append(name)
        loss = (
            tf.reduce_mean(layer.output, keepdims=True)
            * self.config.LOSS_WEIGHTS.get(name, 1.))
        self.keras_model.metrics.append(loss)

I am getting AttributeError: 'NoneType' object has no attribute 'append' for self.keras_model.metrics.append(loss)

weishanlee commented 3 years ago

It is metrics instead of metrics_tensors. I'm using Tensorflow 1.14.0 and Keras 2.3.0. Updating the model.py (2190 - 2199) worked for me.

Add metrics for losses

    for name in loss_names:
        if name in self.keras_model.metrics_names:
            continue
        layer = self.keras_model.get_layer(name)
        self.keras_model.metrics_names.append(name)
        loss = (
            tf.reduce_mean(layer.output, keepdims=True)
            * self.config.LOSS_WEIGHTS.get(name, 1.))
        self.keras_model.metrics.append(loss)

I am getting AttributeError: 'NoneType' object has no attribute 'append' for self.keras_model.metrics.append(loss)

Try to restart all over again to run the script. It should be ok.