简体   繁体   中英

How to access layers from a savedModel with tf.keras

Tensorflow 2.0

python 3.7

I trained and saved a model like this using 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:

model.save("model.h5")
image_model = tf.keras.models.load_model('model.h5')

Then your image_model will have layers in it. In your case, image_model.layers produces:

[<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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM