【目标检测算法实现系列】Keras实现Faster R-CNN算法(一)
上篇文章中,我们主要实现了相关数据的解析,预处理等准备工作,这次我们来搭建相关网络模块
一、共享网络模块搭建
我们使用VGG16网络模型的卷积模块(去掉最后一个池化层)作为共享网络,用来进行提取feature map。具体代码如下:
from keras.layers import Input, Conv2D, MaxPool2D, Flatten, Dense
from keras import backend as K
from keras_faster_rcnn import RoiPoolingConv
def base_net_vgg(input_tensor):
if input_tensor is None:
input_tensor = Input(shape=(None,None,3))
else:
if not K.is_keras_tensor(input_tensor):
input_tensor = Input(tensor=input_tensor, shape=(None,None,3))
#开始构造基础模型(VGG16的卷积模块),到block5_conv3层,用来提取feature map
# Block 1
X = Conv2D(filters=64, kernel_size=(3,3), activation="relu",
padding="same", name="block1_conv1")(input_tensor)
X = Conv2D(filters=64, kernel_size=(3, 3), activation="relu",
padding="same", name="block1_conv2")(X)
X = MaxPool2D(pool_size=(2,2), strides=(2,2), name="block1_pool")(X)
# Block 2
X = Conv2D(filters=128, kernel_size=(3, 3), activation="relu",
padding="same", name="block2_conv1")(X)
X = Conv2D(filters=128, kernel_size=(3, 3), activation="relu",
padding="same", name="block2_conv2")(X)
X = MaxPool2D(pool_size=(2, 2), strides=(2, 2), name="block2_pool")(X)
# Block 3
X = Conv2D(filters=256, kernel_size=(3, 3), activation="relu",
padding="same", name="block3_conv1")(X)
X = Conv2D(filters=256, kernel_size=(3, 3), activation="relu",
padding="same", name="block3_conv2")(X)
X = Conv2D(filters=256, kernel_size=(3, 3), activation="relu",
padding="same", name="block3_conv3")(X)
X = MaxPool2D(pool_size=(2, 2), strides=(2, 2), name="block3_pool")(X)
# Block 4
X = Conv2D(filters=512, kernel_size=(3, 3), activation="relu",
padding="same", name="block4_conv1")(X)
X = Conv2D(filters=512, kernel_size=(3, 3), activation="relu",
padding="same", name="block4_conv2")(X)
X