如何在Keras中将密集层更改为conv2D层?

时间:2019-01-15 15:13:22

标签: tensorflow keras classification

我想对具有不同输入大小的图片进行分类。我想使用以下论文思路。

“用于语义分割的全卷积网络” https://www.cv-foundation.org/openaccess/content_cvpr_2015/papers/Long_Fully_Convolutional_Networks_2015_CVPR_paper.pdf

我确实将密集层更改为conv2D层。

def FullyCNN(input_shape, n_classes):

inputs = Input(shape=(None, None, 1))

first_layer = Conv2D(filters=16, kernel_size=(12,16), strides=1, activation='relu', kernel_initializer='he_normal', name='conv1')(inputs)
first_layer = BatchNormalization()(first_layer)
first_layer = MaxPooling2D(pool_size=2)(first_layer)

second_layer = Conv2D(filters=24, kernel_size=(8,12), strides=1, activation='relu', kernel_initializer='he_normal', name='conv2')(first_layer)
second_layer = BatchNormalization()(second_layer)
second_layer = MaxPooling2D(pool_size=2)(second_layer)

third_layer = Conv2D(filters=32, kernel_size=(5,7), strides=1, activation='relu', kernel_initializer='he_normal', name='conv3')(first_layer)
third_layer = BatchNormalization()(third_layer)
third_layer = MaxPooling2D(pool_size=2)(third_layer)

fully_layer = Conv2D(64, kernel_size=8, activation='relu', kernel_initializer='he_normal')(third_layer)
fully_layer = BatchNormalization()(fully_layer)
fully_layer = Dropout(0.5)(fully_layer)
fully_layer = Conv2D(n_classes, kernel_size=1)(fully_layer)
output = Conv2DTranspose(n_classes, kernel_size=1, activation='softmax')(fully_layer)
model = Model(inputs=inputs, outputs=output)
return model

然后我制作了使用fit_generator()的生成器。

def data_generator(x_train, y_train):
      while True:

          index = np.asscalar(np.random.choice(len(x_train),1))
          feature = np.expand_dims(x_train[index],-1)
          feature = np.resize(feature,(-1,feature.shape))
          feature = np.expand_dims(feature,0) # make (1,input_height,input_width,1) 
          label = y_train[index]
          yield (feature,label)

这些是关于我的数据的图像。

y_train_onehot_img

y_train_img

但是,关于尺寸存在一些问题。 由于输出层必须具有4个与原始CNN模型不同的尺寸,因此尺寸不适合标签。

模型摘要:

My model summary_img

CNN原始模型摘要:

Original CNN model summary img

如何处理此问题?我试图通过扩展尺寸来更改标签的尺寸。

label = np.expand_dims(label,0)
label = np.expand_dims(label,0)
label = np.expand_dims(label,0)

我认为有更好的方法,我想知道是否需要conv2DTranspose吗?批量大小应该为1吗?

0 个答案:

没有答案