Mon*_*ist 4 python neural-network keras tensorflow
所以我认为tensorflow.keras和独立的keras包有冲突,我无法加载我通过迁移学习制作的模型。在 CNN ipynb 中导入:
!pip install tensorflow-gpu==2.0.0b1
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
Run Code Online (Sandbox Code Playgroud)
加载这个预训练模型
base_model = keras.applications.xception.Xception(weights="imagenet",
include_top=False)
avg = keras.layers.GlobalAveragePooling2D()(base_model.output)
output = keras.layers.Dense(n_classes, activation="softmax")(avg)
model = keras.models.Model(inputs=base_model.input, outputs=output)
Run Code Online (Sandbox Code Playgroud)
保存方式:
model.save('Leavesnet Model 2.h5')
Run Code Online (Sandbox Code Playgroud)
然后在已训练模型的新 ipynb 中(导入与 CNN ipynb 中的相同:
from keras.models import load_model
model =load_model('Leavesnet Model.h5')
Run Code Online (Sandbox Code Playgroud)
我收到错误:
AttributeError Traceback (most recent call last)
<ipython-input-4-77ca5a1f5f24> in <module>()
2 from keras.models import load_model
3
----> 4 model =load_model('Leavesnet Model.h5')
13 frames
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in placeholder(shape, ndim, dtype, sparse, name)
539 x = tf.sparse_placeholder(dtype, shape=shape, name=name)
540 else:
--> 541 x = tf.placeholder(dtype, shape=shape, name=name)
542 x._keras_shape = shape
543 x._uses_learning_phase = False
AttributeError: module 'tensorflow' has no attribute 'placeholder'
Run Code Online (Sandbox Code Playgroud)
我认为 tf.keras 和独立的 keras 之间可能存在冲突,有人可以帮助我吗?
tf.keras是的,包之间存在冲突keras,您使用包训练了模型tf.keras,但随后使用包加载了它keras。这是不受支持的,您应该只使用此软件包的一个版本。
具体问题是您使用的是 TensorFlow 2.0,但独立keras包尚不支持 TensorFlow 2.0。