我想知道如何连接卷积层以形成残差。这是我的VGG16:
#Initialising the CNN
cls=Sequential()
#adding 1st Convolution2D layer
cls.add(Convolution2D(64,(3,3),strides=1,border_mode='same',activation='relu',input_shape=(120,120,1)))
cls.add(Convolution2D(64,(3,3),strides=1,border_mode='same',activation='relu'))
#adding 1st pooling layer
cls.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))
#adding 2nd Convolution2D layer
cls.add(Convolution2D(128,(3,3),strides=1,border_mode='same',activation='relu'))
cls.add(Convolution2D(128,(3,3),strides=1,border_mode='same',activation='relu'))
#adding 2nd pooling layer
cls.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))
#adding 3rd Convolution2D layer
cls.add(Convolution2D(256,(3,3),strides=1,border_mode='same',activation='relu'))
cls.add(Convolution2D(256,(3,3),strides=1,border_mode='same',activation='relu'))
cls.add(Convolution2D(256,(3,3),strides=1,border_mode='same',activation='relu'))
#adding 3rd pooling layer
cls.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid')) #15
#adding 4th Convolution2D layer
########################connection start#########################
cls.add(Convolution2D(512,(3,3),strides=1,border_mode='same',activation='relu'))
cls.add(Convolution2D(512,(3,3),strides=1,border_mode='same',activation='relu'))
#########################connection end#########################
cls.add(Convolution2D(512,(3,3),strides=1,border_mode='same',activation='relu'))
#adding 4th pooling layer
cls.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))
#adding 5th Convolution2D layer
cls.add(Convolution2D(512,(3,3),strides=1,border_mode='same',activation='relu'))
cls.add(Convolution2D(512,(3,3),strides=1,border_mode='same',activation='relu'))
cls.add(Convolution2D(512,(3,3),strides=1,border_mode='same',activation='relu'))
#adding 5th pooling layer
cls.add(MaxPooling2D(pool_size=(2, 2), strides=2, padding='valid'))
#Flattening
cls.add(Flatten())
#Full connection1
cls.add(Dense(output_dim=2704,activation='relu'))
cls.add(Dropout(0.2))
#Full connection1
cls.add(Dense(output_dim=2000,activation='relu'))
cls.add(Dropout(0.2))
#Final Layer
cls.add(Dense(output_dim=10,activation='softmax'))
#Compiling CNN
cls.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])#'adam'
我要按代码所示连接两层-#connection开始和结束
答案 0 :(得分:0)
Keras的Sequential
模型-顾名思义-具有严格的线性流动。为了使代码正常工作,您必须使用Functional API。
简而言之,您将必须定义一个(稍微复杂一些,但仍可管理的工作流程),在其中将中间结果分配给变量,然后可以将它们组合起来用作以后的层中的输入,从而创建剩余层。
还有一个nice gist,展示了如何实现诸如残差层之类的东西。