Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
AziziShekoofeh opened this issue Feb 15, 2018 · 8 comments
Closed

Comments

@AziziShekoofeh
Copy link

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.

@Dref360
Copy link
Contributor

Dref360 commented Feb 15, 2018

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

@AziziShekoofeh
Copy link
Author

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

@ehrlinger
Copy link

ehrlinger commented Feb 16, 2018

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

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

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)
<ipython-input-23-e9fff1ef7d52> in <module>()
----> 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]]


@Dref360
Copy link
Contributor

Dref360 commented Feb 16, 2018

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.

@ehrlinger
Copy link

ehrlinger commented Feb 16, 2018

works for me. Thanks.

@AziziShekoofeh
Copy link
Author

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

weiji14 added a commit to weiji14/nz_convnet that referenced this issue Feb 28, 2018
Improvements to keras model again by training on more data. This time using 0.1m resolution imagery from Waikato. Tuakau was chosen as it was close to Auckland, and had a good amount of greenhouses (tile bb32_4735) to decrease bias and increase detection by a bit on those types of shiny buildings.

Model trained on 100 epochs, resuming from the previous checkpoint (Wigram and Hastings), bringing total training to 300 epochs. Mean_iou accuracy metric is now >90%. TQDM progress bars added in the data processing step.

predict.py script now accepts a threshold value, so one can run something like `python predict.py 512 50` to get a building detector of size 512*512 and probabilty >50%.

DNN libraries updated to CUDA 9, CUDNN 7.0.5, Keras 2.1.4, Tensorflow 1.5 (from https://github.com/mind/wheels). There is a known issue keras-team/keras#9394 in having to compile a loaded model before predict can work...
@alyato
Copy link

alyato commented May 14, 2018

@Dref360 thanks. so nice.

@tavishm
Copy link

tavishm commented Aug 12, 2018

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants