Esempio n. 1
0
    def forward(self, inputs, batch_norm_decay=0.9, weight_decay=0.0005, isTrain=True, reuse=False):
        # set batch norm params
        batch_norm_params = {
            'decay': batch_norm_decay,
            'epsilon': 1e-05,
            'scale': True,
            'is_training': isTrain,
            'fused': None,  # Use fused batch norm if possible.
        }

        with slim.arg_scope([slim.conv2d, slim.batch_norm], reuse=reuse):
            # darknet53 特征
            # [N, 19, 19, 512], [N, 38, 38, 256], [N, 76, 76, 128]
            route_1, route_2, route_3 = module.extraction_feature(inputs, batch_norm_params, weight_decay)
            
            with slim.arg_scope([slim.conv2d], 
                                normalizer_fn=slim.batch_norm,
                                normalizer_params=batch_norm_params,
                                biases_initializer=None,
                                activation_fn=lambda x: tf.nn.leaky_relu(x, alpha=0.1),
                                weights_regularizer=slim.l2_regularizer(weight_decay)):
                with tf.variable_scope('yolo'):
                    # 计算 y1 特征
                    # [N, 76, 76, 128] => [N, 76, 76, 256]
                    net = module.conv(route_1, 256)
                    # [N, 76, 76, 256] => [N, 76, 76, 255]
                    net = slim.conv2d(net, 3*(4+1+self.class_num), 1,
                                                        stride=1, normalizer_fn=None,
                                                        activation_fn=None, biases_initializer=tf.zeros_initializer())
                    feature_y3 = net

                    # 计算 y2 特征
                    # [N, 76, 76, 128] => [N, 38, 38, 256]
                    net = module.conv(route_1, 256, stride=2)
                    # [N, 38, 38, 512]
                    net = tf.concat([net, route_2], -1)
                    net = module.yolo_conv_block(net, 512, 2, 1)
                    route_147 = net
                    # [N, 38, 38, 256] => [N, 38, 38, 512]
                    net = module.conv(net, 512)
                    # [N, 38, 38, 512] => [N, 38, 38, 255]
                    net = slim.conv2d(net, 3*(4+1+self.class_num), 1,
                                                        stride=1, normalizer_fn=None,
                                                        activation_fn=None, biases_initializer=tf.zeros_initializer())
                    feature_y2 = net

                    # 计算 y3 特征
                    # [N, 38, 38, 256] => [N, 19, 19, 512]
                    net = module.conv(route_147, 512, stride=2)
                    net = tf.concat([net, route_3], -1)
                    net = module.yolo_conv_block(net, 1024, 3, 0)
                    # [N, 19, 19, 1024] => [N, 19, 19, 255]
                    net = slim.conv2d(net, 3*(4+1+self.class_num), 1,
                                                        stride=1, normalizer_fn=None,
                                                        activation_fn=None, biases_initializer=tf.zeros_initializer())
                    feature_y1 = net

        return feature_y1, feature_y2, feature_y3
Esempio n. 2
0
    def forward(self, inputs, isTrain=True):
        # darknet53 特征
        # [N, 19, 19, 512], [N, 38, 38, 256], [N, 76, 76, 128]
        route_1, route_2, route_3 = module.darknet53(inputs, isTrain=isTrain)
        # 计算 y1 特征
        # [N, 76, 76, 128] => [N, 76, 76, 256]
        net = module.conv(route_3, 256, isTrain=isTrain)
        # [N, 76, 76, 256] => [N, 76, 76, 255]
        feature_y3 = module.conv(net,
                                 3 * (self.class_num + 4 + 1),
                                 kernel_size=1,
                                 bn=False,
                                 activation=None,
                                 isTrain=isTrain)

        # 计算 y2 特征
        # [N, 76, 76, 128] => [N, 38, 38, 256]
        net = module.conv(route_3, 256, stride=2, isTrain=isTrain)
        # [N, 38, 38, 512]
        net = tf.concat([net, route_2], -1)
        net = module.yolo_conv_block(net, 512, 2, 1, isTrain=isTrain)
        route_147 = net
        # [N, 38, 38, 256] => [N, 38, 38, 512]
        net = module.conv(net, 512, isTrain=isTrain)
        feature_y2 = module.conv(net,
                                 3 * (self.class_num + 4 + 1),
                                 kernel_size=1,
                                 bn=False,
                                 activation=None,
                                 isTrain=isTrain)

        # 计算 y3 特征
        # [N, 38, 38, 256] => [N, 19, 19, 512]
        net = module.conv(route_147, 512, stride=2, isTrain=isTrain)
        net = tf.concat([net, route_1], -1)
        net = module.yolo_conv_block(net, 1024, 3, 0, isTrain=isTrain)
        # [N, 19, 19, 1024] => [N, 19, 19, 255]
        feature_y1 = module.conv(net,
                                 3 * (self.class_num + 4 + 1),
                                 kernel_size=1,
                                 bn=False,
                                 activation=None,
                                 isTrain=isTrain)

        return feature_y1, feature_y2, feature_y3