3

我想使用Lucid分析我在自己的数据集上使用 tensorflow 对象检测 API 训练的检测模型的特征提取。使用的模型是来自Tensorflow Object Detection Zoo的模型,即faster_rcnn_resnet101.

我按照Lucid 教程导入了我自己的模型,并将模型的冻结图保存/all_class_predictions_with_backgroundoutput_node.

我无法找到图表的输入节点以使 Lucid 在其上运行。

此外,我真的不认为我有正确的方法。也许我应该先提取检测模型的所有分类部分,然后只用这部分冻结一个新图,然后再去 Lucid。

或者也许我应该只导入一个resnet_101分类模型并从检测模型中复制/粘贴正确的权重?

但我真的不知道如何做这些事情。

有人能帮我吗?我真的很想尝试在我的检测网络上运行 Lucid。

4

1 回答 1

0

是的,您应该导出推理(冻结)图以在 Lucid 中使用。

我使用以下脚本从训练检查点文件中导出图表。有关导出文件中节点的有用信息会记录到控制台。

training_model="ssd_mnet_v2_ppn_512x288.config"

model_signature="eb_13_v09_ppmn2_13_256_adam_512x288_tf_1.14_200k"

# the specific checkpoint to export from
checkpoint_path="/TRAIN/models/model/train/model.ckpt-200000"

# directory to export into
output_path="/XYZ/graphs/${model_signature}"

# ensure these graph nodes are exported, and everything in between
additional_output_tensor_names="Preprocessor/sub,concat_1"

# 
python export_inference_graph.py \
   --input_type=image_tensor \
   --pipeline_config_path /TRAIN/models/model/$training_model \
   --trained_checkpoint_prefix=$checkpoint_path \
   --output_directory=$output_path \
   --additional_output_tensor_names=$additional_output_tensor_names

在查看了 Lucid 模型动物园中的示例后,我发现创建自己的 Lucid 模型类很方便。您必须仔细检查您的图表,因为您需要指定输入节点,并提供 Lucid 可以使用的层列表。

from lucid.modelzoo.vision_base import Model, _layers_from_list_of_dicts


# the input node "Preprocessor/sub" is appropriate for image injection
class SSD_Mnet2_PPN( Model ):

    def __init__(self, image_shape=None, graph_path=None, labels_path=None ):        
        self.model_path = graph_path
        self.labels_path = labels_path

        self.image_shape = image_shape
        self.image_value_range = (-1, 1) 
        self.input_name = "Preprocessor/sub"

        super().__init__()

# a hand-crafted list of layers - by inspection of the graph
SSD_Mnet2_PPN.layers = _layers_from_list_of_dicts(SSD_Mnet2_PPN, [
  { 'id':  0, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_2/add', 'depth': 24, 'shape': [ 1, 72, 128, 24 ], 'transform_id': 2 },  
  { 'id':  2, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_5/add', 'depth': 32, 'shape': [ 1, 36, 64, 32 ], 'transform_id': 2 },  
  { 'id':  5, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_9/add', 'depth': 64, 'shape': [ 1, 18, 32, 64 ], 'transform_id': 2 },  
  { 'id':  7, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_12/add', 'depth': 96, 'shape': [ 1, 18, 32, 96 ], 'transform_id': 2 },  
  { 'id':  9, 'tags': ['conv'], 'name': 'FeatureExtractor/MobilenetV2/expanded_conv_15/add', 'depth': 160, 'shape': [ 1, 9, 16, 160 ], 'transform_id': 2 },  
  { 'id': 11, 'tags': ['concat'], 'name': 'concat_1', 'depth': 13, 'shape': [ 1, 1212, 13 ], 'transform_id': 4 },
])


def model_for_version( version=None, path=None ):

    if "320x180" in version:
        return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 320, 180, 3 ] )

    if "480x270" in version:
        return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 480, 270, 3 ] )

    if "512x288" in version:
        return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 512, 288, 3 ] )        

    if "720x405" in version:
        return SSD_Mnet2_PPN( graph_path=path, image_shape=[ 720, 405, 3 ] )

    raise ValueError( "No model for graph_version: {}".format( version ) )

然后你可以编写如下代码:

from lucid.optvis import render

model = model_for_version( 
    version = "eb_13_v09_ppmn2_13_256_adam_512x288_tf_1.14", 
    path = "/XYZ/graphs/eb_13_v09_ppmn2_13_256_adam_512x288_tf_1.14_200k/frozen_inference_graph.pb" 
)

model.load_graphdef()

_ = render.render_vis( model, "FeatureExtractor/MobilenetV2/expanded_conv_15/add:17", thresholds=( 32, 256, 1024 ) )    

不可避免地,一个人必须进行相当多的实验。

于 2019-10-01T14:15:27.120 回答