コード例 #1
0
    def base(self, images, is_training):
        self.images = images
        keep_prob = tf.cond(is_training, lambda: tf.constant(0.5),
                            lambda: tf.constant(1.0))

        self.conv_1 = conv2d("conv_1",
                             images,
                             filters=64,
                             kernel_size=7,
                             strides=2,
                             activation=self.leaky_relu)
        self.pool_2 = max_pooling2d("pool_2",
                                    self.conv_1,
                                    pool_size=2,
                                    strides=2)
        self.conv_3 = conv2d("conv_3",
                             self.pool_2,
                             filters=192,
                             kernel_size=3,
                             activation=self.leaky_relu)
        self.pool_4 = max_pooling2d("pool_4",
                                    self.conv_3,
                                    pool_size=2,
                                    strides=2)
        self.conv_5 = conv2d("conv_5",
                             self.pool_4,
                             filters=128,
                             kernel_size=1,
                             activation=self.leaky_relu)
        self.conv_6 = conv2d("conv_6",
                             self.conv_5,
                             filters=256,
                             kernel_size=3,
                             activation=self.leaky_relu)
        self.conv_7 = conv2d("conv_7",
                             self.conv_6,
                             filters=256,
                             kernel_size=1,
                             activation=self.leaky_relu)
        self.conv_8 = conv2d("conv_8",
                             self.conv_7,
                             filters=512,
                             kernel_size=3,
                             activation=self.leaky_relu)
        self.pool_9 = max_pooling2d("pool_9",
                                    self.conv_8,
                                    pool_size=2,
                                    strides=2)
        self.conv_10 = conv2d("conv_10",
                              self.pool_9,
                              filters=256,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_11 = conv2d("conv_11",
                              self.conv_10,
                              filters=512,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_12 = conv2d("conv_12",
                              self.conv_11,
                              filters=256,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_13 = conv2d("conv_13",
                              self.conv_12,
                              filters=512,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_14 = conv2d("conv_14",
                              self.conv_13,
                              filters=256,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_15 = conv2d("conv_15",
                              self.conv_14,
                              filters=512,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_16 = conv2d("conv_16",
                              self.conv_15,
                              filters=256,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_17 = conv2d("conv_17",
                              self.conv_16,
                              filters=512,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_18 = conv2d("conv_18",
                              self.conv_17,
                              filters=512,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_19 = conv2d("conv_19",
                              self.conv_18,
                              filters=1024,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.pool_20 = max_pooling2d("pool_20",
                                     self.conv_19,
                                     pool_size=2,
                                     strides=2)
        self.conv_21 = conv2d("conv_21",
                              self.pool_20,
                              filters=512,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_22 = conv2d("conv_22",
                              self.conv_21,
                              filters=1024,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_23 = conv2d("conv_23",
                              self.conv_22,
                              filters=512,
                              kernel_size=1,
                              activation=self.leaky_relu)
        self.conv_24 = conv2d("conv_24",
                              self.conv_23,
                              filters=1024,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_25 = conv2d("conv_25",
                              self.conv_24,
                              filters=1024,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_26 = conv2d("conv_26",
                              self.conv_25,
                              filters=1024,
                              kernel_size=3,
                              strides=2,
                              activation=self.leaky_relu)
        self.conv_27 = conv2d("conv_27",
                              self.conv_26,
                              filters=1024,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.conv_28 = conv2d("conv_28",
                              self.conv_27,
                              filters=1024,
                              kernel_size=3,
                              activation=self.leaky_relu)
        self.fc_29 = fully_connected("fc_29",
                                     self.conv_28,
                                     filters=512,
                                     activation=self.leaky_relu)
        self.fc_30 = fully_connected("fc_30",
                                     self.fc_29,
                                     filters=4096,
                                     activation=self.leaky_relu)

        self.dropout_31 = tf.nn.dropout(self.fc_30, keep_prob)

        output_size = (self.cell_size * self.cell_size) * (
            self.num_classes + self.boxes_per_cell * 5)
        self.fc_32 = fully_connected("fc_32",
                                     self.dropout_31,
                                     filters=output_size,
                                     activation=None)

        return self.fc_32
コード例 #2
0
ファイル: darknet.py プロジェクト: yasumura-lm/blueoil
    def base(self, images, is_training):
        if self.data_format == "NCHW":
            channel_data_format = "channels_first"
        elif self.data_format == "NHWC":
            channel_data_format = "channels_last"
        else:
            raise RuntimeError(
                "data format {} shodul be in ['NCHW', 'NHWC]'.".format(
                    self.data_format))

        self.inputs = self.images = images

        self.block_1 = darknet_block(
            "block_1",
            self.inputs,
            filters=32,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.pool_1 = max_pooling2d("pool_1",
                                    self.block_1,
                                    pool_size=2,
                                    strides=2,
                                    data_format=channel_data_format)

        self.block_2 = darknet_block(
            "block_2",
            self.pool_1,
            filters=64,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.pool_2 = max_pooling2d("pool_2",
                                    self.block_2,
                                    pool_size=2,
                                    strides=2,
                                    data_format=channel_data_format)

        self.block_3 = darknet_block(
            "block_3",
            self.pool_2,
            filters=128,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_4 = darknet_block(
            "block_4",
            self.block_3,
            filters=64,
            kernel_size=1,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_5 = darknet_block(
            "block_5",
            self.block_4,
            filters=128,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.pool_3 = max_pooling2d("pool_3",
                                    self.block_5,
                                    pool_size=2,
                                    strides=2,
                                    data_format=channel_data_format)

        self.block_6 = darknet_block(
            "block_6",
            self.pool_3,
            filters=256,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_7 = darknet_block(
            "block_7",
            self.block_6,
            filters=128,
            kernel_size=1,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_8 = darknet_block(
            "block_8",
            self.block_7,
            filters=256,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.pool_4 = max_pooling2d("pool_4",
                                    self.block_8,
                                    pool_size=2,
                                    strides=2,
                                    data_format=channel_data_format)

        self.block_9 = darknet_block(
            "block_9",
            self.pool_4,
            filters=512,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_10 = darknet_block(
            "block_10",
            self.block_9,
            filters=256,
            kernel_size=1,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_11 = darknet_block(
            "block_11",
            self.block_10,
            filters=512,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_12 = darknet_block(
            "block_12",
            self.block_11,
            filters=256,
            kernel_size=1,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_13 = darknet_block(
            "block_13",
            self.block_12,
            filters=512,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.pool_5 = max_pooling2d("pool_5",
                                    self.block_13,
                                    pool_size=2,
                                    strides=2,
                                    data_format=channel_data_format)

        self.block_14 = darknet_block(
            "block_14",
            self.pool_5,
            filters=1024,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_15 = darknet_block(
            "block_15",
            self.block_14,
            filters=512,
            kernel_size=1,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_16 = darknet_block(
            "block_16",
            self.block_15,
            filters=1024,
            kernel_size=3,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_17 = darknet_block(
            "block_17",
            self.block_16,
            filters=512,
            kernel_size=1,
            is_training=is_training,
            activation=self.activation,
            data_format=self.data_format,
        )

        self.block_18 = darknet_block(
            "block_18",
            self.block_17,
            filters=1024,
            kernel_size=3,
            is_training=is_training,
            activation=self.before_last_activation,
            data_format=self.data_format,
        )

        kernel_initializer = tf.random_normal_initializer(mean=0.0,
                                                          stddev=0.01)

        self.conv_19 = conv2d(
            "conv_19",
            self.block_18,
            filters=self.num_classes,
            kernel_size=1,
            activation=None,
            use_bias=True,
            is_debug=self.is_debug,
            kernel_initializer=kernel_initializer,
            data_format=channel_data_format,
        )

        if self.is_debug:
            self._heatmap_layer = self.conv_19

        if self.data_format == "NCHW":
            axis = [2, 3]
        if self.data_format == "NHWC":
            axis = [1, 2]
        # TODO(wakisaka): global average pooling should use tf.reduce_mean()

        self.pool_6 = tf.reduce_mean(self.conv_19,
                                     axis=axis,
                                     name="global_average_pool_6")
        self.base_output = tf.reshape(self.pool_6, [-1, self.num_classes],
                                      name="pool6_reshape")

        return self.base_output