Keras: AttributeError: 'Model' object has no attribute 'stateful_metric_names'

Created on 15 Feb 2018  路  8Comments  路  Source: keras-team/keras

Hi,

I just upgrade my Keras to latest version and getting a error related to keras/engine/training on the code that used to work fine beforehand (my previous version was 2.1.0). I am not sure if it's some kind of deprectation or etc.

Traceback (most recent call last):
  File "template_model_1.py", line 118, in <module>
    acc, ll = test_MC_dropout(test_model, X_test, Y_test)
  File "/data/home/shekoofeh/Project/Dropout-BB/BBalpha-TeUS/BBalpha_dropout.py", line 72, in test_MC_dropout
    pred = model.predict(X, verbose=1)  # N x K x D
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1842, in predict
    verbose=verbose, steps=steps)
  File "/usr/local/lib/python2.7/dist-packages/keras/engine/training.py", line 1292, in _predict_loop
    stateful_metrics=self.stateful_metric_names)
AttributeError: 'Model' object has no attribute 'stateful_metric_names'

It's happening on a brach of Dropout + BB-alpha for detecting adversarial examples. Unfortunately, I coudn't share my own version due to disclusure of the project. Thanks for being understanding.

I found Add Stateful (Global) Metrics #9200, but I couldn't track down changes. Any idea how to solve the issue?

Thanks,
Shek

  • [checked] Check that you are up-to-date with the master branch of Keras. You can update with:
    pip install git+git://github.com/keras-team/keras.git --upgrade --no-deps

  • [checked] If running on TensorFlow, check that you are up-to-date with the latest version. The installation instructions can be found here.

Most helpful comment

Quick fix for both of you and I'll do something more permanent
just do a dummy model.compile

loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(MODEL_WEIGHTS)
loaded_model.compile('sgd','mse')

This won't affect your results for prediction in any way.

All 8 comments

Have you compiled your model?
The field is initialized there : https://github.com/keras-team/keras/blob/master/keras/engine/training.py#L855

@Dref360 Thanks for your answer. Yes.
I used the exact same code before updating my Keras version, and It was working fine. Nothing has changed in the code.

I'm also seeing this error on Keras v2.1.4 only.

For more information, I'm trying to load the model from files:

```{python}
from keras.models import model_from_json

load json and create model

json_file = open(LSTM_MODEL, 'r')
loaded_model_json = json_file.read()
loaded_model = model_from_json(loaded_model_json)

load weights into new model

loaded_model.load_weights(MODEL_WEIGHTS)

The error occurs in the predict_proba call, which should not need compiling.

score = loaded_model.predict_proba(seq_array,verbose=1)
print(score.shape)

print(score)

AttributeError Traceback (most recent call last)
in ()
----> 1 score = loaded_model.predict_proba(seq_array,verbose=1)
2 print(score.shape)
3 print(score)

~/lib/conda/envs/azureml_7753abb5a4b4d3655662bae7f7e3b6fe/lib/python3.5/site-packages/keras/models.py in predict_proba(self, x, batch_size, verbose, steps)
1110 A Numpy array of probability predictions.
1111 """
-> 1112 preds = self.predict(x, batch_size, verbose, steps=steps)
1113 if preds.min() < 0. or preds.max() > 1.:
1114 warnings.warn('Network returning invalid probability values. '

~/lib/conda/envs/azureml_7753abb5a4b4d3655662bae7f7e3b6fe/lib/python3.5/site-packages/keras/models.py in predict(self, x, batch_size, verbose, steps)
1023 self.build()
1024 return self.model.predict(x, batch_size=batch_size, verbose=verbose,
-> 1025 steps=steps)
1026
1027 def predict_on_batch(self, x):

~/lib/conda/envs/azureml_7753abb5a4b4d3655662bae7f7e3b6fe/lib/python3.5/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
1840 f = self.predict_function
1841 return self._predict_loop(f, ins, batch_size=batch_size,
-> 1842 verbose=verbose, steps=steps)
1843
1844 def train_on_batch(self, x, y,

~/lib/conda/envs/azureml_7753abb5a4b4d3655662bae7f7e3b6fe/lib/python3.5/site-packages/keras/engine/training.py in _predict_loop(self, f, ins, batch_size, verbose, steps)
1290 else:
1291 progbar = Progbar(target=num_samples,
-> 1292 stateful_metrics=self.stateful_metric_names)
1293
1294 indices_for_conversion_to_dense = []

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

On Keras 2.1.3, this returned the expected output.

score = loaded_model.predict_proba(seq_array,verbose=1)
print(score.shape)
print(score)
15631/15631 [==============================] - 11s 698us/step
(15631, 1)
[[3.7404552e-05]
[3.7972197e-05]
[3.8670321e-05]
...
[9.9917173e-01]
[9.9919158e-01]
[9.9921858e-01]]

```

Quick fix for both of you and I'll do something more permanent
just do a dummy model.compile

loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights(MODEL_WEIGHTS)
loaded_model.compile('sgd','mse')

This won't affect your results for prediction in any way.

works for me. Thanks.

@Dref360 thank you very much. It seems, it solved my issue as well.

@Dref360 thanks. so nice.

this is my code

  • `
  1. from keras.preprocessing.image import load_img
    from keras.preprocessing.image import ImageDataGenerator
    import csv, re, os
    import numpy as np
    from keras.preprocessing.image import img_to_array
    from keras.applications.imagenet_utils import decode_predictions
    import matplotlib.pyplot as plt
    import cv2
    import keras
    from keras import Model
    from keras.layers import Dense, Dropout
    import numpy as np
    from keras.utils import plot_model
    from keras.applications import inception_v3
    os.environ['CUDA_VISIBLE_DEVICES'] = '0'
    from utils import layer_utils

densenet_model = keras.applications.densenet.DenseNet201(weights=None, include_top=False, input_shape=(None,None,3))

all_layers = layer_utils.get_layer_names_bw_2_layers(densenet_model, 'input_1', 'pool 1')

input_img = densenet_model.get_layer('input_1').output
encoded = densenet_model.get_layer('pool1')(input_img)
decoded = densenet_model.get_layer('input_1')(encoded)

model = Model(input_img, decoded)

model.compile(loss="binary_crossentropy", optimizer='adadelta')

mypath = 'images/'

encoder = Model(input_img, encoded)
encoded_input = keras.layers.Input(shape=(1024,1024,3))
decoder_layer = model.layers[-1]
decoder = Model(encoded_input, decoder_layer(encoded_input))
train_datagen = ImageDataGenerator(rescale=1. / 255,
fill_mode='constant', cval=0.,
zoom_range=0,
rotation_range=0,
width_shift_range=0,
height_shift_range=0,
shear_range=0,
validation_split=0.1
)
train_generator = train_datagen.flow_from_directory( 'organized_data22/',batch_size=128, class_mode='input', subset='training')
test_generator = train_datagen.flow_from_directory( 'organized_data22/', batch_size=128, class_mode='input', subset='validation')

for s in range(1000):
w = s+1
print('Iteration - '+str(w))
model.fit_generator(train_generator,
epochs=1,
validation_data=test_generator,
)

    model.save('models/inception_animal_classify_new_test_train_'+str(i)+'.hf5')
    encoded_imgs = encoder.predict(x_test)
    decoded_imgs = decoder.predict(encoded_imgs)
    for i in range(n):
            # display original
            ax = plt.subplot(2, n, i + 1)

plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

            # display reconstruction
            ax = plt.subplot(2, n, i + 1 + n)
            plt.imshow(decoded_imgs[i].reshape(28, 28))
            plt.gray()
            ax.get_xaxis().set_visible(False)
            ax.get_yaxis().set_visible(False)
    plt.show()

showing error
Traceback (most recent call last):
File "densenet_autoencoder.py", line 28, in
model = Model(input_img, decoded)
File "/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/network.py", line 91, in __init__
self._init_graph_network(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/keras/engine/network.py", line 183, in _init_graph_network
'The tensor that caused the issue was: ' +
AttributeError: 'Model' object has no attribute 'name'

Was this page helpful?
0 / 5 - 0 ratings