使用Keras的模型类将代码从TensorFlow 1.x迁移到Tensorflow 2.x

问题描述

我有一个运行“自定义”模型的代码,看来它是使用“急切模式”构造的。当我尝试运行model.predict()函数时,出现以下错误

文件“ /home/jptalledo/.local/lib/python3.6/site-packages/tensorflow/python/keras/utils/version_utils.py”,第122行,在disallow_legacy_graph中 引发ValueError(error_msg) ValueError:构建Model.predict实例并启用了eager模式时,不支持以图形方式调用Model。请在图形模式下构建Model实例,或在启用快速模式的情况下调用Model.predict

python代码如下:

  def nn_predict(self,img):
        """Run model prediction to classify image as EV and return its probability"""
        
        img = cv2.resize(cv2.cvtColor(img,cv2.COLOR_BGR2RGB),self.target_image_size).astype(np.float32) / 255.0
        img = np.expand_dims(img,axis=0)
    
        with self.tf_graph.as_default():
            predictions = self.nn_model.predict(img)
        return predictions

问题所在的位置:predictions = self.nn_model.predict(img)

任何建议如何启用急切模式?

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)