示例#1
0
def alexnet(image, label, trainable=True):
    conv1 = _conv2d_layer(
        "conv1",
        image,
        filters=64,
        kernel_size=11,
        strides=4,
        padding="VALID",
    )
    pool1 = flow.nn.avg_pool2d(conv1, 3, 2, "VALID", "NCHW", name="pool1")
    conv2 = _conv2d_layer("conv2", pool1, filters=192, kernel_size=5)
    pool2 = flow.nn.avg_pool2d(conv2, 3, 2, "VALID", "NCHW", name="pool2")
    conv3 = _conv2d_layer("conv3", pool2, filters=384)
    conv4 = _conv2d_layer("conv4", conv3, filters=384)
    conv5 = _conv2d_layer("conv5", conv4, filters=256)
    pool5 = flow.nn.avg_pool2d(conv5, 3, 2, "VALID", "NCHW", name="pool5")

    if len(pool5.shape) > 2:
        pool5 = flow.flatten(pool5, start_dim=1, end_dim=-1)

    initializer = flow.truncated_normal_initializer(stddev=0.816496580927726)

    fc1 = flow.layers.dense(
        inputs=pool5,
        units=4096,
        activation=flow.math.relu,
        use_bias=False,
        kernel_initializer=initializer,
        bias_initializer=False,
        trainable=trainable,
        name="fc1",
    )

    dropout1 = fc1
    fc2 = flow.layers.dense(
        inputs=dropout1,
        units=4096,
        activation=flow.math.relu,
        use_bias=False,
        kernel_initializer=initializer,
        bias_initializer=False,
        trainable=trainable,
        name="fc2",
    )

    dropout2 = fc2
    fc3 = flow.layers.dense(
        inputs=dropout2,
        units=1001,
        activation=None,
        use_bias=False,
        kernel_initializer=initializer,
        bias_initializer=False,
        trainable=trainable,
        name="fc3",
    )

    loss = flow.nn.sparse_softmax_cross_entropy_with_logits(
        label, fc3, name="softmax_loss")
    return loss
示例#2
0
def _trunc_normal_(
    self,
    mean=0.0,
    std=1.0,
    a=-2.0,
    b=2.0,
):
    initializer_conf = flow.truncated_normal_initializer(mean=mean, stddev=std)
    res = _init_by_initializer_conf(self, initializer_conf)
    res = flow.clamp(res, min=a, max=b)
    return res
示例#3
0
    def _build_dense(self, x, unit, name="dense_"):
        """
        Build Dense Layer
        :param x:
        :return:
        """
        self.init_range = 0.2
        self.init = flow.truncated_normal_initializer(self.init_range)
        self.reg = flow.regularizers.l2(0.01)

        return flow.layers.dense(x,
                                 units=unit,
                                 kernel_initializer=self.init,
                                 kernel_regularizer=self.reg,
                                 use_bias=self.use_bias,
                                 name=name + "w")
示例#4
0
    def test_float_initializer(test_case):
        initializers = [
            flow.random_normal_initializer(mean=3, stddev=4),
            flow.random_uniform_initializer(minval=-6, maxval=18),
            flow.truncated_normal_initializer(mean=-5, stddev=8),
            flow.xavier_uniform_initializer(data_format="NCHW"),
            flow.xavier_uniform_initializer(data_format="NHWC"),
            flow.xavier_normal_initializer(data_format="NCHW"),
            flow.xavier_normal_initializer(data_format="NHWC"),
            flow.constant_initializer(value=4),
            flow.ones_initializer(),
            flow.zeros_initializer(),
        ]

        kaiming_args = GenArgDict(
            OrderedDict(
                shape=[SHAPE],
                mode=["fan_in", "fan_out", "fan_avg"],
                distribution=["random_normal", "random_uniform"],
                data_format=["NCHW", "NHWC"],
                negative_slope=[0.5],
            ))
        vs_args = GenArgDict(
            OrderedDict(
                scale=[3.4],
                mode=["fan_in", "fan_out", "fan_avg"],
                distribution=[
                    "truncated_normal", "random_normal", "random_uniform"
                ],
                data_format=["NCHW", "NHWC"],
            ))
        for args in kaiming_args:
            initializers.append(flow.kaiming_initializer(**args))

        for args in vs_args:
            initializers.append(flow.variance_scaling_initializer(**args))

        for initializer in initializers:
            CompareTwoDistribution(test_case, flow.float32, initializer)