gpt4 book ai didi

neural-network - 如何在 keras 中拥有并行卷积层?

转载 作者:行者123 更新时间:2023-12-03 21:16:40 25 4
gpt4 key购买 nike

我对神经网络和 keras 有点陌生。我有一些大小为 6*7 的图像,过滤器的大小为 15。我想有几个过滤器并在每个过滤器上分别训练一个卷积层,然后将它们组合起来。我在这里看过一个例子:

model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten(input_shape=input_shape))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('tanh'))

该模型适用于一个过滤器。谁能给我一些关于如何修改模型以使用并行卷积层的提示。

谢谢

最佳答案

我的方法是创建其他模型来定义所有并行卷积和拉操作,并将所有并行结果张量连接到单个输出张量。现在您可以在顺序模型中添加此并行模型图,就像层一样。这是我的解决方案,希望它能解决你的问题。

# variable initialization 
from keras import Input, Model, Sequential
from keras.layers import Conv2D, MaxPooling2D, Concatenate, Activation, Dropout, Flatten, Dense

nb_filters =100
kernel_size= {}
kernel_size[0]= [3,3]
kernel_size[1]= [4,4]
kernel_size[2]= [5,5]
input_shape=(32, 32, 3)
pool_size = (2,2)
nb_classes =2
no_parallel_filters = 3

# create seperate model graph for parallel processing with different filter sizes
# apply 'same' padding so that ll produce o/p tensor of same size for concatination
# cancat all paralle output

inp = Input(shape=input_shape)
convs = []
for k_no in range(len(kernel_size)):
conv = Conv2D(nb_filters, kernel_size[k_no][0], kernel_size[k_no][1],
border_mode='same',
activation='relu',
input_shape=input_shape)(inp)
pool = MaxPooling2D(pool_size=pool_size)(conv)
convs.append(pool)

if len(kernel_size) > 1:
out = Concatenate()(convs)
else:
out = convs[0]

conv_model = Model(input=inp, output=out)

# add created model grapg in sequential model

model = Sequential()
model.add(conv_model) # add model just like layer
model.add(Conv2D(nb_filters, kernel_size[1][0], kernel_size[1][0]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten(input_shape=input_shape))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('tanh'))

有关更多信息,请参阅类似问题: Combining the outputs of multiple models into one model

关于neural-network - 如何在 keras 中拥有并行卷积层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43151775/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com