예제 #1
0
파일: resnet.py 프로젝트: ytfksw/blueoil
    def base(self, images, is_training):
        use_bias = False

        self.images = self.input = images

        with tf.compat.v1.variable_scope("init"):
            self.conv1 = conv2d(
                "conv1",
                self.images,
                filters=16,
                kernel_size=3,
                activation=None,
                use_bias=use_bias,
                is_debug=self.is_debug,
            )

            self.bn1 = batch_norm("bn1", self.conv1, is_training=is_training)
            with tf.compat.v1.variable_scope("relu1"):
                self.relu1 = tf.nn.relu(self.bn1)

        for i in range(0, self.num_residual):
            with tf.compat.v1.variable_scope("unit1_{}".format(i)):
                if i == 0:
                    out = self._residual(self.relu1, in_filters=16, out_filters=16, strides=1, is_training=is_training)
                else:
                    out = self._residual(out, in_filters=16, out_filters=16, strides=1, is_training=is_training)

        for i in range(0, self.num_residual):
            with tf.compat.v1.variable_scope("unit2_{}".format(i)):
                if i == 0:
                    out = self._residual(out, in_filters=16, out_filters=32, strides=2, is_training=is_training)
                else:
                    out = self._residual(out, in_filters=32, out_filters=32, strides=1, is_training=is_training)

        for i in range(0, self.num_residual):
            with tf.compat.v1.variable_scope("unit3_{}".format(i)):
                if i == 0:
                    out = self._residual(out, in_filters=32, out_filters=64, strides=2, is_training=is_training)
                else:
                    out = self._residual(out, in_filters=64, out_filters=64, strides=1, is_training=is_training)

        with tf.compat.v1.variable_scope("unit4_0"):
            self.bn4 = batch_norm("bn4", out, is_training=is_training, activation=tf.nn.relu)

        # global average pooling
        h = self.bn4.get_shape()[1].value
        w = self.bn4.get_shape()[2].value
        self.global_average_pool = average_pooling2d(
            "global_average_pool", self.bn4, pool_size=[h, w], padding="VALID", is_debug=self.is_debug,)

        self._heatmap_layer = None
        self.fc = fully_connected("fc", self.global_average_pool, filters=self.num_classes, activation=None)

        return self.fc
예제 #2
0
파일: resnet.py 프로젝트: ytfksw/blueoil
    def _residual(self, inputs, in_filters, out_filters, strides, is_training):
        use_bias = False

        with tf.compat.v1.variable_scope('sub1'):
            bn1 = batch_norm("bn1", inputs, is_training=is_training)

            with tf.compat.v1.variable_scope('relu1'):
                relu1 = tf.nn.relu(bn1)
            conv1 = conv2d(
                "conv1",
                relu1,
                filters=out_filters,
                kernel_size=3,
                activation=None,
                use_bias=use_bias,
                strides=strides,
                is_debug=self.is_debug,
            )

        with tf.compat.v1.variable_scope('sub2'):
            bn2 = batch_norm("bn2", conv1, is_training=is_training)

            with tf.compat.v1.variable_scope('relu2'):
                relu2 = tf.nn.relu(bn2)

            conv2 = conv2d(
                "conv2",
                relu2,
                filters=out_filters,
                kernel_size=3,
                activation=None,
                use_bias=use_bias,
                strides=1,
                is_debug=self.is_debug,
            )

        with tf.compat.v1.variable_scope('sub_add'):
            if in_filters != out_filters:
                inputs = tf.nn.avg_pool(
                    inputs,
                    ksize=[1, strides, strides, 1],
                    strides=[1, strides, strides, 1],
                    padding='VALID'
                )
                inputs = tf.pad(
                    inputs,
                    [[0, 0], [0, 0], [0, 0], [(out_filters - in_filters)//2, (out_filters - in_filters)//2]]
                )

        output = conv2 + inputs

        return output
예제 #3
0
    def base(self, images, is_training):
        assert self.data_format == "NHWC"
        channel_data_format = "channels_last"

        self.inputs = self.images = images

        with tf.compat.v1.variable_scope("block_1"):
            conv = conv2d("conv", self.inputs, filters=32, kernel_size=3,
                          activation=None, use_bias=False, data_format=channel_data_format,
                          kernel_initializer=tf.compat.v1.keras.initializers.VarianceScaling(scale=2.0))
            batch_normed = batch_norm("bn", conv, is_training=is_training, decay=0.99, scale=True, center=True,
                                      data_format=self.data_format)
            self.block_1 = self.activation(batch_normed)

        self.block_last = conv2d("block_last", self.block_1, filters=self.num_classes, kernel_size=1,
                                 activation=None, use_bias=True, is_debug=self.is_debug,
                                 kernel_initializer=tf.compat.v1.random_normal_initializer(mean=0.0, stddev=0.01),
                                 data_format=channel_data_format)

        h = self.block_last.get_shape()[1].value
        w = self.block_last.get_shape()[2].value
        self.pool = tf.compat.v1.layers.average_pooling2d(name='global_average_pool', inputs=self.block_last,
                                                          pool_size=[h, w], padding='VALID', strides=1,
                                                          data_format=channel_data_format)
        self.base_output = tf.reshape(self.pool, [-1, self.num_classes], name="pool_reshape")

        return self.base_output
예제 #4
0
def darknet(name,
            inputs,
            filters,
            kernel_size,
            is_training=tf.constant(False),
            activation=None,
            data_format="NHWC"):
    """Darknet19 block.

    Do convolution, batch_norm, bias, leaky_relu activation.
    Ref: https://arxiv.org/pdf/1612.08242.pdf
         https://github.com/pjreddie/darknet/blob/3bf2f342c03b0ad22efd799d5be9990c9d792354/cfg/darknet19.cfg
         https://github.com/pjreddie/darknet/blob/8215a8864d4ad07e058acafd75b2c6ff6600b9e8/cfg/yolo.2.0.cfg
    """
    if data_format == "NCHW":
        channel_data_format = "channels_first"
    elif data_format == "NHWC":
        channel_data_format = "channels_last"
    else:
        raise ValueError(
            "data format must be 'NCHW' or 'NHWC'. got {}.".format(
                data_format))

    with tf.compat.v1.variable_scope(name):
        if activation is None:

            def activation(x):
                return tf.nn.leaky_relu(x, alpha=0.1, name="leaky_relu")

        conv = conv2d(
            "conv",
            inputs,
            filters=filters,
            kernel_size=kernel_size,
            activation=None,
            use_bias=False,
            data_format=channel_data_format,
            kernel_initializer=tf.contrib.layers.variance_scaling_initializer(
            ),
        )  # he initializer

        # TODO(wakisaka): Should be the same as darknet batch norm.
        # https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/contrib/layers/python/layers/layers.py
        # https://github.com/pjreddie/darknet/blob/8215a8864d4ad07e058acafd75b2c6ff6600b9e8/src/batchnorm_layer.c#L135
        batch_normed = batch_norm("bn",
                                  conv,
                                  is_training=is_training,
                                  decay=0.99,
                                  scale=True,
                                  center=True,
                                  data_format=data_format)
        tf.compat.v1.summary.histogram("batch_normed", batch_normed)

        output = activation(batch_normed)
        tf.compat.v1.summary.histogram("output", output)

        return output