我正在尝试在一组图像(2D数组)上实现ConvNet二进制分类器。我意识到第一个卷积层对于特征提取至关重要。但是,我还有其他有助于分类的输入参数。这个想法是将附加节点附加到第一个完全连接的层,以便我可以使用前馈神经网络进行最终分类。无论如何,keras API都能做到吗?我还想知道是否有一种方法可以通过顺序模型体系结构提取中间层的输出。以下代码定义了模型:
new = Sequential([
Conv2D(8, [3,3], activation='relu', padding ='same'),
MaxPool2D([2,2], 2, padding='valid'),
Conv2D(16, [3,3], activation='relu', padding='same'),
MaxPool2D([2,2], 2, padding='valid'),
Flatten(),
Dense(256),
Dense(64),
Dense(1, activation='sigmoid')
])
答案 0 :(得分:0)
我意识到第一卷积层对于特征提取至关重要。但是,我还有其他有助于分类的输入参数。这个想法是将额外的节点附加到第一个完全连接的层,以便我可以使用前馈神经网络进行最终分类。无论如何,keras API都能做到吗?
是的,请使用Keras Functional API在同一网络中的中间输入引用示例代码
from keras.layers import Dense, Input, Conv2D, MaxPooling2D, Flatten, Dense, concatenate
from keras.models import Model
# feature extraction from gray scale image
inputs = Input(shape = (28,28,1))
conv1 = Conv2D(16, (3,3), activation = 'relu', padding = "SAME")(inputs)
pool1 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv1)
conv2 = Conv2D(32, (3,3), activation = 'relu', padding = "SAME")(pool1)
pool2 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv2)
flat_1 = Flatten()(pool2)
# feature extraction from RGB image
inputs_2 = Input(shape = (28,28,3))
conv1_2 = Conv2D(16, (3,3), activation = 'relu', padding = "SAME")(inputs_2)
pool1_2 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv1_2)
conv2_2 = Conv2D(32, (3,3), activation = 'relu', padding = "SAME")(pool1_2)
pool2_2 = MaxPooling2D(pool_size = (2,2), strides = 2)(conv2_2)
flat_2 = Flatten()(pool2_2)
# concatenate both feature layers and define output layer after some dense layers
concat = concatenate([flat_1,flat_2])
dense1 = Dense(512, activation = 'relu')(concat)
dense2 = Dense(128, activation = 'relu')(dense1)
dense3 = Dense(32, activation = 'relu')(dense2)
output = Dense(10, activation = 'softmax')(dense3)
# create model with two inputs
model = Model([inputs,inputs_2], output)
我也想知道是否有一种方法可以提取 顺序模型架构的中间层。
是的,对于要在Keras模型层上进行的任何操作,首先,我们需要访问模型所保存的keras.layers
对象的列表
model_layers = model.layers
此列表中的每个Layer对象都有其自己的input
和output
张量(如果您使用的是TensorFlow后端)
input_tensor = model.layers[ layer_index ].input
output_tensor = model.layers[ layer_index ].output
下面显示了如何从顺序网络中提取中间层的输出
model = Sequential()
model.add(Conv2D(8, [3,3], input_shape=(28,28,1), activation='relu', padding ='same'))
model.add(MaxPool2D([2,2], 2, padding='valid'))
model.add(Conv2D(16, [3,3], activation='relu', padding='same'))
model.add(MaxPool2D([2,2], 2, padding='valid'))
model.add(Flatten())
model.add(Dense(256))
model.add(Dense(64))
model.add(Dense(1, activation='sigmoid'))
model.summary()
要提取第4层的输出,
output_tensor = model.layers[3].output
print(output_tensor)
输出:
Model: "sequential_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 28, 28, 8) 80
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 8) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 14, 14, 16) 1168
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 7, 7, 16) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 784) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 200960
_________________________________________________________________
dense_2 (Dense) (None, 64) 16448
_________________________________________________________________
dense_3 (Dense) (None, 1) 65
=================================================================
Total params: 218,721
Trainable params: 218,721
Non-trainable params: 0
_________________________________________________________________
Tensor("max_pooling2d_2/MaxPool:0", shape=(None, 7, 7, 16), dtype=float32)