[英]How to access layers from a savedModel with tf.keras
Tensorflow 2.0 TensorFlow 2.0
python 3.7蟒蛇 3.7
I trained and saved a model like this using tf.keras我使用 tf.keras 训练并保存了这样的模型
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, models
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck']
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10))
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
model.save("saved_model_pretrained/")
I want to load the model in order to extract layers from it in another project like this我想加载模型以便在这样的另一个项目中从中提取图层
image_model = tf.keras.models.load_model('pathToFolder')
I am trying to get layer like this :我正在尝试获得这样的图层:
layer_indices = []
for index, layer in enumerate(image_model.layers):
layers_indices.append(index)
However i am getting this error但是我收到这个错误
future: <Task finished coro=<server_task.<locals>.server_work() done, defined at ...\...\xx.py:249> exception=AttributeError("'_UserObject' object has no attribute 'layers'")>
Traceback (most recent call last):
File "...\...\xx.py", line 280, in server_work
image_model, layers_indices = init(model_choice, layers_to_see)
File "...\...\xx.py", line 148, in init
for index, layer in enumerate(image_model.layers):
AttributeError: '_UserObject' object has no attribute 'layers'
Any help is much appreciated任何帮助深表感谢
Firstly, you should be saving your model properly!首先,您应该正确保存模型! Use following syntax to save/load model as (.h5) file - A common format for saving models:使用以下语法将模型保存/加载为 (.h5) 文件 - 保存模型的常用格式:
model.save("model.h5")
image_model = tf.keras.models.load_model('model.h5')
Then your image_model
will have layers
in it.然后你的image_model
会有layers
。 In your case, image_model.layers
produces:在你的情况下, image_model.layers
产生:
[<tensorflow.python.keras.layers.convolutional.Conv2D at 0x1d81b325898>,
<tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x1d81b325f98>,
<tensorflow.python.keras.layers.convolutional.Conv2D at 0x1d82b06b2e8>,
<tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x1d82c1dbe10>,
<tensorflow.python.keras.layers.convolutional.Conv2D at 0x1d82c17d2e8>,
<tensorflow.python.keras.layers.core.Flatten at 0x1d82c17df98>,
<tensorflow.python.keras.layers.core.Dense at 0x1d82c2c8320>,
<tensorflow.python.keras.layers.core.Dense at 0x1d82b016048>]
And和
layers_indices = []
for index, layer in enumerate(image_model.layers):
layers_indices.append(index)
layers_indices
produces:产生:
[0, 1, 2, 3, 4, 5, 6, 7]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.