from keras.layers import Input, Conv2D, MaxPooling2D, concatenate, AveragePooling2D, Dropout, Flatten, Dense
from keras.models import Model
def inception_module(x, filters):
branch1x1 = Conv2D(filters[0], (1, 1), padding='same', activation='relu')(x)
branch3x3 = Conv2D(filters[1], (1, 1), padding='same', activation='relu')(x)
branch3x3 = Conv2D(filters[2], (3, 3), padding='same', activation='relu')(branch3x3)
branch5x5 = Conv2D(filters[3], (1, 1), padding='same', activation='relu')(x)
branch5x5 = Conv2D(filters[4], (5, 5), padding='same', activation='relu')(branch5x5)
branch_pool = MaxPooling2D((3, 3), strides=(1, 1), padding='same')(x)
branch_pool = Conv2D(filters[5], (1, 1), padding='same', activation='relu')(branch_pool)
output = concatenate([branch1x1, branch3x3, branch5x5, branch_pool], axis=3)
return output
input_layer = Input(shape=(224, 224, 3))
x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', activation='relu')(input_layer)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = Conv2D(64, (1, 1), padding='same', activation='relu')(x)
x = Conv2D(192, (3, 3), padding='same', activation='relu')(x)
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = inception_module(x, [64, 96, 128, 16, 32, 32])
x = inception_module(x, [128, 128, 192, 32, 96, 64])
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = inception_module(x, [192, 96, 208, 16, 48, 64])
x = inception_module(x, [160, 112, 224, 24, 64, 64])
x = inception_module(x, [128, 128, 256, 24, 64, 64])
x = inception_module(x, [112, 144, 288, 32, 64, 64])
x = inception_module(x, [256, 160, 320, 32, 128, 128])
x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)
x = inception_module(x, [256, 160, 320, 32, 128, 128])
x = inception_module(x, [384, 192, 384, 48, 128, 128])
x = AveragePooling2D((7, 7))(x) x = Dropout(0.4)(x)
x = Flatten()(x)
output_layer = Dense(1000, activation='softmax')(x)
model = Model(input_layer, output_layer)
这段代码定义了一个GoogLeNet模型,包括多个inception模块和全连接层。输入层的大小为224x224x3,输出层为1000个类别的softmax激活函数。