我正在使用预训练Alexnet的实现。 我用我自己的训练数据重新训练了它,通过这样做,我收到了重新训练的AlexNet的检查点。
我现在想用它来对图像进行分类。 据我了解,我需要为分类提供两件事:
- 包含我要分类的图像的输入节点
- 结果的输出节点
我收到所有节点的列表
for op in graph.get_operations():
print(str(op.name), op.outputs)
但是,我无法以这种方式找出输出节点。 可以在此处查看所有节点的列表。
我的方法错了吗?我对张量流不是很有经验,我感谢任何帮助。 谢谢。
调用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>