예제 #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
예제 #2
0
    def classifier(self, base_layers, trainable=True):
        out_roi_pool = RoiPoolingConv(
            self.pooling_region, self.num_rois)([base_layers, self.roi_input])

        out = TimeDistributed(Flatten(name='flatten'))(out_roi_pool)
        out = TimeDistributed(
            Dense(4096, activation='relu', name='fc1',
                  trainable=trainable))(out)
        out = TimeDistributed(Dropout(0.5))(out)
        out = TimeDistributed(
            Dense(4096, activation='relu', name='fc2',
                  trainable=trainable))(out)
        out = TimeDistributed(Dropout(0.5))(out)

        out_class = TimeDistributed(Dense(self.num_cls,
                                          activation='softmax',
                                          kernel_initializer='zero',
                                          trainable=trainable),
                                    name='dense_class_{}'.format(
                                        self.num_cls))(out)
        # note: no regression target for bg class
        out_regr = TimeDistributed(Dense(4 * (self.num_cls - 1),
                                         activation='linear',
                                         kernel_initializer='zero',
                                         trainable=trainable),
                                   name='dense_regress_{}'.format(
                                       self.num_cls))(out)

        return [out_class, out_regr]
예제 #3
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]
예제 #4
0
def get_ROIout(base_layers,
               input_rois,
               num_rois,
               nb_classes=21,
               trainable=False):  #input_rois (None,4)若干个建议框
    pooling_regions = 14
    out_roi_pool = RoiPoolingConv(pooling_regions, num_rois)(
        [base_layers, input_rois])  #输入featuremap和建议框 返回num_rois个14*14的roi
    return out_roi_pool
예제 #5
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]
예제 #6
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]
예제 #7
0
파일: frcnn.py 프로젝트: yblir/faster-RCNN
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]