【问题标题】:Find the output node in a AlexNet implementation in Tensorflow在 Tensorflow 的 AlexNet 实现中查找输出节点
【发布时间】:2019-12-06 10:32:15
【问题描述】:

我正在使用 this 实现预训练的 Alexnet。 我使用自己的训练数据对其进行了重新训练,并且通过这样做,我收到了重新训练的 AlexNet 的检查点。

我现在想用它来分类图像。 据我了解,我需要为分类提供两件事:

  1. 带有我要分类的图像的输入节点
  2. 结果的输出节点

我正在使用

接收所有节点的列表
for op in graph.get_operations():
    print(str(op.name), op.outputs)

但是,我无法通过这种方式找出输出节点。 所有节点列表可见here

我的方法错了吗?我对tensorflow不是很有经验,我很感激任何帮助。 非常感谢。

【问题讨论】:

  • 您可以使用Netron 来可视化图表。

标签: python tensorflow neural-network conv-neural-network


【解决方案1】:

致电model.summary() 打印一份有用的模型摘要,其中包括:

  • 模型中所有层的名称和类型。
  • 每层的输出形状。
  • 每层的权重参数数量。
  • 每层接收的输入
  • 模型的可训练和不可训练参数的总数。

另外,您可以使用model.layers[] 打印图层信息。

示例:我在这里定义了一个简单的模型,并使用model.summary() 显示其摘要,并使用model.layers[] 显示层信息。

import tensorflow as tf
from tensorflow.python.keras import Sequential
from tensorflow.keras.layers import MaxPooling2D, Conv2D, Dense, Dropout, Flatten, BatchNormalization
from tensorflow.keras.models import load_model


# Add the layers 
model = Sequential()
model.add(Conv2D(64,(3,3), input_shape=(424,424,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dense(32, activation='relu'))
model.add(Conv2D(64,(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dense(64, activation='relu'))
model.add(Dropout(.3))#test
model.add(Conv2D(64,(3,3), activation='relu'))
model.add(MaxPooling2D(pool_size=(3,3)))
model.add(Dropout(.3))
model.add(Flatten(input_shape=(424,424,3)))
model.add(BatchNormalization())
model.add(Dense(2, activation='softmax'))

# Model summary 
model.summary()

输出 -

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_2 (Conv2D)            (None, 422, 422, 64)      1792      
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 140, 140, 64)      0         
_________________________________________________________________
dense_1 (Dense)              (None, 140, 140, 32)      2080      
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 138, 138, 64)      18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 46, 46, 64)        0         
_________________________________________________________________
dense_2 (Dense)              (None, 46, 46, 64)        4160      
_________________________________________________________________
dropout (Dropout)            (None, 46, 46, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 44, 44, 64)        36928     
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 14, 14, 64)        0         
_________________________________________________________________
dropout_1 (Dropout)          (None, 14, 14, 64)        0         
_________________________________________________________________
flatten (Flatten)            (None, 12544)             0         
_________________________________________________________________
batch_normalization (BatchNo (None, 12544)             50176     
_________________________________________________________________
dense_3 (Dense)              (None, 2)                 25090     
=================================================================
Total params: 138,722
Trainable params: 113,634
Non-trainable params: 25,088
_________________________________________________________________

建立模型后打印层信息-

# To print all the layers of the Model
print("All the Layers of the Model:")
for layers in model.layers:
    print(layers)
print("\n")

# To print first layer OR Input layer of the Model
print("Input Layer of the Model:","\n",model.layers[0],"\n")

# To print last layer OR Output layer of the Model
print("Output Layer of the Model:","\n",model.layers[-1])

输出 -

All the Layers of the Model:
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7faa09294550>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7faa09294cf8>
<tensorflow.python.keras.layers.core.Dense object at 0x7faa09294d30>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7faa0044e780>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7faa0046fda0>
<tensorflow.python.keras.layers.core.Dense object at 0x7faa09294f98>
<tensorflow.python.keras.layers.core.Dropout object at 0x7faa00477da0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7faa0046ff60>
<tensorflow.python.keras.layers.pooling.MaxPooling2D object at 0x7faa004412b0>
<tensorflow.python.keras.layers.core.Dropout object at 0x7faa00477f60>
<tensorflow.python.keras.layers.core.Flatten object at 0x7faa004802b0>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7faa003d0b00>
<tensorflow.python.keras.layers.core.Dense object at 0x7faa00441588>


Input Layer of the Model: 
 <tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7faa09294550> 

Output Layer of the Model: 
 <tensorflow.python.keras.layers.core.Dense object at 0x7faa00441588>

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 1970-01-01
    • 2022-06-30
    • 2018-04-26
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多