Exemplo n.º 1
0
def get_classifier(base_layers,
                   input_rois,
                   num_rois,
                   nb_classes=21,
                   trainable=False):
    ''' roi --> cls+reg'''
    pooling_regions = 14
    input_shape = (num_rois, 14, 14, 1024)
    # base_layers[38,38,1024], input_rois[num_prior,4]  num_prior=32
    # 1 roiPooling,base_layers[38,38,1024], input_rois[-,4] ,pooling_regions=14, num_rois=32
    out_roi_pool = RoiPoolingConv(pooling_regions,
                                  num_rois)([base_layers,
                                             input_rois])  # input_rois是建议框
    # 2
    out = classifier_layers(out_roi_pool,
                            input_shape=input_shape,
                            trainable=True)
    out = TimeDistributed(Flatten())(out)
    out_class = TimeDistributed(Dense(nb_classes,
                                      activation='softmax',
                                      kernel_initializer='zero'),
                                name='dense_class_{}'.format(nb_classes))(out)
    out_regr = TimeDistributed(Dense(4 * (nb_classes - 1),
                                     activation='linear',
                                     kernel_initializer='zero'),
                               name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr]  # 21, 20*4
Exemplo n.º 2
0
def get_classifier(base_layers,
                   input_rois,
                   num_rois,
                   nb_classes=21,
                   trainable=False):
    # 设定池化大小
    pooling_regions = 14
    # 设定输入大小(roi数量,池化大小,池化大小,固定长度)
    input_shape = (num_rois, 14, 14, 1024)
    # base_layers -> [38,38,1024] 当输入为[600,600,3] 的图片的时候
    #input_rois -> [None, 4],None一般每次处理为32个建议框,在config.py中的num.rois中设置,
    # 获取RoiPoolingConv层输出的公共特征层
    out_roi_pool = RoiPoolingConv(pooling_regions,
                                  num_rois)([base_layers, input_rois])
    # 获取分类层的输出结果
    out = classifier_layers(out_roi_pool,
                            input_shape=input_shape,
                            trainable=True)
    # 进行flatten操作,准备进行全连接输出分类结果
    out = TimeDistributed(Flatten())(out)
    # 进行全连接输出分类结果
    out_class = TimeDistributed(Dense(nb_classes,
                                      activation="softmax",
                                      kernel_initializer="zero"),
                                name="dense_class_{}".format(nb_classes))(out)
    # 进行全连接输出调整后的框位置
    out_regr = TimeDistributed(Dense(4 * (nb_classes - 1),
                                     activation="linear",
                                     kernel_initializer="zero"),
                               name="dense_regress_{}".format(nb_classes))(out)
    # 返回
    return [out_class, out_regr]
Exemplo n.º 3
0
def get_classifier(base_layers, input_rois, nb_classes=21, pooling_regions=14):
    # num_rois, 38, 38, 1024 -> num_rois, 14, 14, 2048
    out_roi_pool = RoiPoolingConv(pooling_regions)([base_layers, input_rois])

    # num_rois, 14, 14, 1024 -> num_rois, 1, 1, 2048
    out = classifier_layers(out_roi_pool)

    # num_rois, 1, 1, 1024 -> num_rois, 2048
    out = TimeDistributed(Flatten())(out)

    # num_rois, 1, 1, 1024 -> num_rois, nb_classes
    out_class = TimeDistributed(Dense(nb_classes,
                                      activation='softmax',
                                      kernel_initializer='zero'),
                                name='dense_class_{}'.format(nb_classes))(out)
    # num_rois, 1, 1, 1024 -> num_rois, 4 * (nb_classes-1)
    out_regr = TimeDistributed(Dense(4 * (nb_classes - 1),
                                     activation='linear',
                                     kernel_initializer='zero'),
                               name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr]
Exemplo n.º 4
0
def get_classifier(base_layers,
                   input_rois,
                   num_rois,
                   nb_classes=21,
                   trainable=False):
    pooling_regions = 14
    input_shape = (num_rois, 14, 14, 1024)
    out_roi_pool = RoiPoolingConv(pooling_regions,
                                  num_rois)([base_layers, input_rois])
    out = classifier_layers(out_roi_pool,
                            input_shape=input_shape,
                            trainable=True)
    out = TimeDistributed(Flatten())(out)
    out_class = TimeDistributed(Dense(nb_classes,
                                      activation='softmax',
                                      kernel_initializer='zero'),
                                name='dense_class_{}'.format(nb_classes))(out)
    out_regr = TimeDistributed(Dense(4 * (nb_classes - 1),
                                     activation='linear',
                                     kernel_initializer='zero'),
                               name='dense_regress_{}'.format(nb_classes))(out)
    return [out_class, out_regr]
Exemplo n.º 5
0
def get_classifier(base_layers, input_rois, nb_classes=21, pooling_regions=14):
    '''
    Faster-RCNN网络模型
    Parameters
    ----------
    base_layers: resnet50输出的特征层(None,38,38,1024)
    input_rois:
    nb_classes
    pooling_regions

    Returns
    -------

    '''
    # num_rois:一张图片中建议框数量
    # num_rois, 38, 38, 1024 -> num_rois, 14, 14, 2048
    out_roi_pool = RoiPoolingConv()([base_layers, input_rois, pooling_regions])
    # out_roi_pool = RoiPoolingConv(pooling_regions)([base_layers, input_rois])
    # num_rois, 14, 14, 1024 -> num_rois, 1, 1, 2048
    out = classifier_layers(out_roi_pool)
    # TimeDistributed: 对batch_size中的每一个单独做处理
    # num_rois, 1, 1, 1024 -> num_rois, 2048
    out = TimeDistributed(Flatten())(out)

    # num_rois, 1, 1, 1024 -> num_rois, nb_classes
    # (batch_size,num_rois,nb_classes),None:batch_size,num_rois:每张图片的建议框数量,nb_classes:每个建议框预测的类别
    proposal_boxes_class = TimeDistributed(
        Dense(nb_classes, activation='softmax', kernel_initializer='zero'),
        name=f'dense_class_{nb_classes}')(out)
    # num_rois, 1, 1, 1024 -> num_rois, 4 * (nb_classes-1)
    # (batch_size,num_rois,4*(nb_classes-1)), 4*(nb_classes-1):每个建议框预测的所有类别的建议框变化量. 这个变化量+建议框=预测框
    proposal_boxes_offset = TimeDistributed(
        Dense(4 * (nb_classes - 1),
              activation='linear',
              kernel_initializer='zero'),
        name=f'dense_regress_{nb_classes}')(out)
    return [proposal_boxes_class, proposal_boxes_offset]