python - 在 Tensorflow 的 AlexNet 实现中查找输出节点
问题描述
我正在使用Pre-Trained Alexnet的这种实现。我已经用我自己的训练数据重新训练了它,并通过这样做我收到了重新训练的AlexNet的检查点。
我现在想用它来分类图像。据我了解,我需要为分类提供两件事:
- 带有我要分类的图像的输入节点
- 结果的输出节点
我收到所有节点的列表,使用
for op in graph.get_operations():
print(str(op.name), op.outputs)
但是,我无法以这种方式找出输出节点。所有节点的列表可以在这里看到。
我的方法错了吗?我对tensorflow不是很有经验,我很感激任何帮助。非常感谢。
解决方案
调用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>
推荐阅读
- soot - 在 Soot 中为 java 代码使用类和方法
- java - 如何在 Java 的 Native HTTP Client 上使用代理密码认证
- java - 如何使 addSnapshotListener 不启动新活动?适用于 Java 但不适用于 Kotlin
- r - 获取R中两个元素的组合
- cuda - NVCC 不会在 /usr/lib/x86_64-linux-gnu 中查找库 - 为什么?
- youtube-api - Youtube API inStream 参数为空
- javascript - 如果所有项目具有相同的 Id,如何使用单击事件删除单击的项目
- visual-studio-code - VS Code:当我关闭子文件夹并返回文件时,子文件夹会重新打开
- html - 单选按钮不会出现在网站上
- python - 我想向列表中添加一个值并用它做一些事情