conv2d(
filter=特征图的个数,也就是卷积核的个数,也就是神经网络中需要提取特征的数量。
kernal_size=卷积核的大小
strip=步长
padding='valid' or 'same'
input_shape=
activate='relu'
)
举个例子1:直接创建一个可用的卷积神经网络
创建CNN应该包含以下7步骤。
"""
@author:fuzekun
@file:cnn.py
@time:2022/11/16
@description: 卷积神经网络模型
"""
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Activation,Dense,Flatten,Conv2D,MaxPooling2D
from keras.utils import np_utils
# 1.初始化
model = Sequential()
# 2.卷积层
model.add(Conv2D(filters=32, kernel_size=(3, 3), padding='same', input_shape=(28,28,1), activation='relu'))
# 3.池化层
model.add(MaxPooling2D(pool_size=(2,2)))
# 4. 图片拉平成向量
model.add(Flatten())
# 5. 全连接
model.add(Dense(n_hidden_1, activation='relu'))
# 6. 输出
model.add(Dense(n_classes, activation='softmax'))
# 7. 编译
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
举个例子2,创建三张图片作为输入的的的卷积神经网络
def create_model(input_shape):
"""
这里没有参数,应该有
input_shape:
kernal_size:
pool_size:
activation:
optimizer:
"""
def create_single_model() :
# 1.建立卷积神经网络
model = Sequential()
# 两个卷积层
model.add(Conv2D(filters=64, kernel_size=kernal_size, padding='same', input_shape=input_shape, activation='relu'))
model.add(Conv2D(filters=128,kernel_size=kernal_size, padding='same', input_shape=input_shape, activation='relu'))
# model.add(Conv2D(filters=64, kernel_size=kernal_size, padding='same', input_shape=input_shape, activation='relu'))
# model.add(Conv2D(filters=128, kernel_size=kernal_size, padding='same', input_shape=input_shape, activation='relu'))
# 池化层采用最大值池化
model.add(MaxPooling2D(pool_size=(2,2)))
# 再来一个卷积层
model.add(Conv2D(filters=256, kernel_size=kernal_size, padding='same', input_shape=input_shape, activation='relu'))
# model.add(Dense(n_hidden_1, activation='relu'))
# model.add(Dense(n_classes, activation='softmax'))
# model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
return model
out_rri = create_single_model()
out_edr = create_single_model()
out_amp = create_single_model()
# 2. 进行模型融合
# print(out_rri.output)
combined = concatenate([out_rri.output, out_edr.output, out_amp.output]) #(None, 7, 7, 768)
# print(combined)
# 2.1融合输入
x = Dense(n_hidden_1, activation='relu')(combined)
x = Flatten()(x)
# 2.2最后输出
x = Dense(n_classes, activation='softmax')(x)
# 2.3模型定义完成
model = Model(inputs = [out_rri.input, out_edr.input, out_amp.input], outputs = x)
# model.summary()
plot_model(model, to_file=base_floder_path + 'model.png', show_shapes=True)
return model
创建之后后的绘制如下: