Harsh Patel
Harsh Patel

Reputation: 1

AttributeError: 'Sequential' object has no attribute '_make_predict_function'

I am working on Python 3.6, macOS and Coursera's Guided Project of Facial Expression Recognition.

This is model.py code.

from tensorflow.keras.models import model_from_json

import numpy as np

import tensorflow as tf

config = tf.compat.v1.ConfigProto()

config.gpu_options.per_process_gpu_memory_fraction = 0.15

session = tf.compat.v1.Session(config=config)


class FacialExpressionModel(object):

    EMOTIONS_LIST = ["Angry", "Disgust",
                     "Fear", "Happy",
                     "Neutral", "Sad",
                     "Surprise"]

    def __init__(self, model_json_file, model_weights_file):
        # load model from JSON file
        with open(model_json_file, "r") as json_file:
            loaded_model_json = json_file.read()
            self.loaded_model = model_from_json(loaded_model_json)

        # load weights into the new model
        self.loaded_model.load_weights(model_weights_file)
        self.loaded_model._make_predict_function()

    def predict_emotion(self, img):
        self.preds = self.loaded_model.predict(img)
        return FacialExpressionModel.EMOTIONS_LIST[np.argmax(self.preds)]

The error for this is as follows:

/usr/local/bin/python3.6 
/Users/apple/Downloads/Facial_Expression_Recognition/camera.py

2020-09-25 11:03:09.532649: I 
tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN)to use the following CPU instructions in performance-critical operations:  AVX2 FMA

To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.

2020-09-25 11:03:09.546790: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fd8577eb710 initialized for platform Host (this does not guarantee that XLA will be used). Devices:

2020-09-25 11:03:09.546804: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version

Traceback (most recent call last):
  File "/Users/apple/Downloads/Facial_Expression_Recognition/camera.py", line 6, in <module>

    model = FacialExpressionModel("model.json", "model_weights.h5")

  File "/Users/apple/Downloads/Facial_Expression_Recognition/model.py", line 28, in __init__

    self.loaded_model._make_predict_function()

AttributeError: 'Sequential' object has no attribute '_make_predict_function'

Process finished with exit code 1

Upvotes: 0

Views: 4323

Answers (1)

Jakub Vonšovsk&#253;
Jakub Vonšovsk&#253;

Reputation: 501

_make_predict_function() is from old version of Keras. Try to install Tensorflow 1.15 (with Keras 2.2.4-tf) instead of 2.x.x as you are trying to call old function on new model_from_json.

Upvotes: 1

Related Questions