简体   繁体   English

通过 Keras 2.4.3 和 Tensorflow 2.2 从 SavedModel 中提取特征

[英]Extracting Features from a SavedModel via Keras 2.4.3 and Tensorflow 2.2

I would like to extract the features from the final dense layer in my CNN model.我想从我的 CNN 模型中的最终密集层中提取特征。 However, I am very conflicted with all of the google research I have done.但是,我对我所做的所有谷歌研究都非常矛盾。 There are so many different methods with Tensorflow and I am struggling to get something to work. Tensorflow 有很多不同的方法,我正在努力让一些东西工作。

I have sucessfully trained a model on CIFAR10.我已经成功地在 CIFAR10 上训练了一个模型。 I have saved the model to a directory and have a saved_model.pb file.我已将模型保存到一个目录并有一个 saved_model.pb 文件。 I have visualized the model via tensorboard but not entirely sure of the name of my final layer.我已经通过 tensorboard 可视化了模型,但并不完全确定我最后一层的名称。 The visualization seems a bit confusing.可视化似乎有点令人困惑。

How can I proceed to extract these features?我该如何继续提取这些特征? I want to use them for a t-SNE analysis.我想将它们用于 t-SNE 分析。

I am trying to load the pb graph with gfile but not sure if this is the correct approach.Thank you.我正在尝试使用 gfile 加载 pb 图,但不确定这是否是正确的方法。谢谢。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.python.platform import gfile


pb_graph_file = '../data/processed/saved_models/saved_model.pb'

f = gfile.GFile(pb_graph_file, 'rb')
graph_def = tf.GraphDef()
f.close()

My Keras Sequential model looks like:我的 Keras Sequential 模型如下所示:

    """
    This is the CNN model's architecture
    """
    weight_decay = 1e-4
    model = Sequential()
    model.add(Conv2D(32, (3, 3), activation = 'relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay), padding = 'same', input_shape = (32, 32, 3)))
    model.add(BatchNormalization())
    model.add(Conv2D(32, (3, 3), activation = 'relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay), padding = 'same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2)))
    model.add(Dropout(0.2))

    model.add(Conv2D(64, (3, 3), activation = 'relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay), padding='same'))
    model.add(BatchNormalization())
    model.add(Conv2D(64, (3, 3), activation = 'relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay), padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2)))
    model.add(Dropout(0.3))

    model.add(Conv2D(128, (3, 3), activation = 'relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay), padding='same'))
    model.add(BatchNormalization())
    model.add(Conv2D(128, (3, 3), activation = 'relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay), padding='same'))
    model.add(BatchNormalization())
    model.add(MaxPooling2D((2, 2)))
    model.add(Dropout(0.4))

    # model.add(Conv2D(256, (3, 3), activation = 'relu', kernel_initializer = 'he_uniform', kernel_regularizer = l2(weight_decay), padding='same'))
    # model.add(Conv2D(256, (3, 3), activation = 'relu', kernel_initializer = 'he_uniform', kernel_regularizer = l2(weight_decay), padding='same'))
    # model.add(MaxPooling2D((2, 2)))

    model.add(Flatten())
    # model.add(Dense(128, acti vation='relu', kernel_initializer = 'he_normal', kernel_regularizer = l2(weight_decay)))
    # model.add(BatchNormalization())
    # model.add(Dropout(0.5))
    # output layer
    model.add(Dense(10, activation = 'softmax'))

    # optimize and compile model
    opt = Adam(learning_rate = 1e-3)
    model.compile(optimizer = opt, loss = 'categorical_crossentropy', metrics = ['accuracy'])

    return model

First get name of your desired layer using model.summary() .首先使用model.summary()获取所需层的名称。

Then use the name of that layer in place of desired_layer in below given code:然后在下面给定的代码中使用该层的名称代替 desired_layer:

from keras.models import Model
extractor = Model(inputs=model.inputs, outputs=model.get_layer(desired_layer).output)
features = extractor.predict(x)

Here x is data from which you want to extract features.这里x是您要从中提取特征的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 EfficientNet Tensorflow 中提取特征 - Extracting features from EfficientNet Tensorflow Savedmodel 的预测(Tensorflow 2) - Predictions from Savedmodel (Tensorflow 2) Keras VGGFace提取功能 - Keras VGGFace extracting features TensorFlow:如何从SavedModel进行预测? - TensorFlow: How to predict from a SavedModel? 如何解决 ImportError:Keras 需要 TensorFlow 2.2 或更高版本。 通过“pip install tensorflow”安装 TensorFlow? - How to solve ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`? 导入错误:Keras 需要 TensorFlow 2.2 或更高版本。 在 Windows 上通过 `pip install tensorflow` 安装 TensorFlow - ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow` on Windows ImportError:Keras 需要 TensorFlow 2.2 或更高版本。 通过 `pip install tensorflow` 安装 TensorFlow - ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow` 从 TensorFlow 2.x 中的特定层移除层/提取特征 - Removing layers/extracting features from specific layers in TensorFlow 2.x 使用预训练的Inceptionv3提取瓶颈特征 - Keras的实现和Native Tensorflow实现之间的差异 - extracting Bottleneck features using pretrained Inceptionv3 - differences between Keras' implementation and Native Tensorflow implementation Tensorflow:提取训练模型的特征 - Tensorflow: Extracting the features of a trained model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM