コード例 #1
0
def _conv_block(
    in_blob,
    index,
    filters,
    conv_times,
    data_format="NCHW",
    trainable=True,
    training=True,
):
    conv_block = []
    conv_block.insert(0, in_blob)
    weight_initializer = flow.variance_scaling_initializer(
        2, "fan_out", "random_normal", data_format=data_format)
    for i in range(conv_times):
        conv_i = conv2d_layer(
            name="conv{}".format(index),
            input=conv_block[i],
            filters=filters,
            kernel_size=3,
            strides=1,
            data_format=data_format,
            weight_initializer=weight_initializer,
            trainable=trainable,
            training=training,
            bn=True,
        )

        conv_block.append(conv_i)
        index += 1

    return conv_block
コード例 #2
0
ファイル: test_resnet50.py プロジェクト: zzk0/oneflow
def _conv2d(
        name,
        input,
        filters,
        kernel_size,
        strides=1,
        padding="SAME",
        data_format="NCHW",
        dilations=1,
        weight_initializer=flow.variance_scaling_initializer(),
):
    weight = flow.get_variable(
        name + "-weight",
        shape=(filters, input.shape[1], kernel_size, kernel_size),
        dtype=input.dtype,
        initializer=weight_initializer,
        trainable=g_trainable,
    )
    return flow.nn.conv2d(input,
                          weight,
                          strides,
                          padding,
                          None,
                          data_format,
                          dilations,
                          name=name)
コード例 #3
0
def _get_initializer(model_name):
    if model_name == "weight":
        return flow.variance_scaling_initializer(
            2.0, mode="fan_out", distribution="random_normal", data_format="NCHW"
        )
    elif model_name == "bias":
        return flow.zeros_initializer()
    elif model_name == "gamma":
        return flow.ones_initializer()
    elif model_name == "beta":
        return flow.zeros_initializer()
    elif model_name == "dense_weight":
        return flow.variance_scaling_initializer(
            1 / 3, mode="fan_in", distribution="random_uniform"
        )
    elif model_name == "dense_bias":
        return flow.random_uniform_initializer(0, 0.01)
コード例 #4
0
ファイル: resnet_model.py プロジェクト: zzk0/oneflow
 def __init__(
     self, weight_regularizer, trainable=True, training=True, channel_last=False
 ):
     self.data_format = "NHWC" if channel_last else "NCHW"
     self.weight_initializer = flow.variance_scaling_initializer(
         2, "fan_in", "random_normal", data_format=self.data_format
     )
     self.weight_regularizer = weight_regularizer
     self.trainable = trainable
     self.training = training
コード例 #5
0
def resnet50(images, args, trainable=True, training=True):
    weight_regularizer = (
        flow.regularizers.l2(args.wd) if args.wd > 0.0 and args.wd < 1.0 else None
    )
    builder = ResnetBuilder(
        weight_regularizer,
        trainable,
        training,
        args.channel_last,
        args.fuse_bn_relu,
        args.fuse_bn_add_relu,
    )

    if args.pad_output:
        if args.channel_last:
            paddings = ((0, 0), (0, 0), (0, 0), (0, 1))
        else:
            paddings = ((0, 0), (0, 1), (0, 0), (0, 0))
        images = flow.pad(images, paddings=paddings)
    with flow.scope.namespace("Resnet"):
        stem = builder.resnet_stem(images)
        body = builder.resnet_conv_x_body(stem)
        pool5 = flow.nn.avg_pool2d(
            body,
            ksize=7,
            strides=1,
            padding="VALID",
            data_format=builder.data_format,
            name="pool5",
        )
        fc1001 = flow.layers.dense(
            flow.reshape(pool5, (pool5.shape[0], -1)),
            units=1000,
            use_bias=True,
            kernel_initializer=flow.variance_scaling_initializer(
                2, "fan_in", "random_normal"
            ),
            bias_initializer=flow.zeros_initializer(),
            kernel_regularizer=weight_regularizer,
            bias_regularizer=weight_regularizer,
            trainable=trainable,
            name="fc1001",
        )
    return fc1001
コード例 #6
0
def conv2d_layer(
        name,
        input,
        out_channel,
        kernel_size=3,
        strides=1,
        padding="SAME",
        data_format="NCHW",
        dilation_rate=1,
        use_bias=True,
        weight_initializer=flow.variance_scaling_initializer(
            2, "fan_out", "random_normal", data_format="NCHW"),
        bias_initializer=flow.zeros_initializer(),
        trainable=True,
):
    weight_shape = (out_channel, input.shape[1], kernel_size, kernel_size)
    weight = flow.get_variable(
        name + "_weight",
        shape=weight_shape,
        dtype=input.dtype,
        initializer=weight_initializer,
        trainable=trainable,
    )
    output = flow.nn.conv2d(input,
                            weight,
                            strides,
                            padding,
                            None,
                            data_format,
                            dilation_rate,
                            name=name)
    if use_bias:
        bias = flow.get_variable(
            name + "_bias",
            shape=(out_channel, ),
            dtype=input.dtype,
            initializer=bias_initializer,
            trainable=trainable,
        )
        output = flow.nn.bias_add(output, bias, data_format)
    return output
コード例 #7
0
ファイル: resnet_model.py プロジェクト: zzk0/oneflow
def resnet50(
    images,
    trainable=True,
    need_transpose=False,
    training=True,
    wd=1.0 / 32768,
    channel_last=False,
):
    weight_regularizer = flow.regularizers.l2(wd) if wd > 0.0 and wd < 1.0 else None
    builder = ResnetBuilder(weight_regularizer, trainable, training, channel_last)
    if need_transpose:
        images = flow.transpose(images, name="transpose", perm=[0, 3, 1, 2])
    if channel_last:
        images = flow.transpose(images, name="transpose", perm=[0, 2, 3, 1])
    with flow.scope.namespace("Resnet"):
        stem = builder.resnet_stem(images)
        body = builder.resnet_conv_x_body(stem)
        pool5 = flow.nn.avg_pool2d(
            body,
            ksize=7,
            strides=1,
            padding="VALID",
            data_format=builder.data_format,
            name="pool5",
        )
        fc1001 = flow.layers.dense(
            flow.reshape(pool5, (pool5.shape[0], -1)),
            units=1000,
            use_bias=True,
            kernel_initializer=flow.variance_scaling_initializer(
                2, "fan_in", "random_normal"
            ),
            bias_initializer=flow.zeros_initializer(),
            kernel_regularizer=weight_regularizer,
            bias_regularizer=weight_regularizer,
            trainable=trainable,
            name="fc1001",
        )
    return fc1001
コード例 #8
0
ファイル: test_dqn.py プロジェクト: zzk0/oneflow
def getQNetParams(var_name_prefix: str = "QNet", is_train: bool = True):
    weight_init = flow.variance_scaling_initializer(
        scale=1.0,
        mode="fan_in",
        distribution="truncated_normal",
        data_format="NCHW")
    bias_init = flow.constant_initializer(value=0.0)
    conv_prefix = "_conv1"
    conv1_weight = flow.get_variable(
        var_name_prefix + conv_prefix + "_weight",
        shape=(32, 4, 3, 3),
        dtype=flow.float32,
        initializer=weight_init,
        trainable=is_train,
    )
    conv1_bias = flow.get_variable(
        var_name_prefix + conv_prefix + "_bias",
        shape=(32, ),
        dtype=flow.float32,
        initializer=bias_init,
        trainable=is_train,
    )
    conv_prefix = "_conv2"
    conv2_weight = flow.get_variable(
        var_name_prefix + conv_prefix + "_weight",
        shape=(32, 32, 3, 3),
        dtype=flow.float32,
        initializer=weight_init,
        trainable=is_train,
    )
    conv2_bias = flow.get_variable(
        var_name_prefix + conv_prefix + "_bias",
        shape=(32, ),
        dtype=flow.float32,
        initializer=bias_init,
        trainable=is_train,
    )
    fc_prefix = "_fc1"
    fc1_weight = flow.get_variable(
        var_name_prefix + fc_prefix + "_weight",
        shape=(512, 32 * 16 * 16),
        dtype=flow.float32,
        initializer=weight_init,
        trainable=is_train,
    )
    fc1_bias = flow.get_variable(
        var_name_prefix + fc_prefix + "_bias",
        shape=(512, ),
        dtype=flow.float32,
        initializer=bias_init,
        trainable=is_train,
    )
    fc_prefix = "_fc2"
    fc2_weight = flow.get_variable(
        var_name_prefix + fc_prefix + "_weight",
        shape=(2, 512),
        dtype=flow.float32,
        initializer=weight_init,
        trainable=is_train,
    )
    fc2_bias = flow.get_variable(
        var_name_prefix + fc_prefix + "_bias",
        shape=(2, ),
        dtype=flow.float32,
        initializer=bias_init,
        trainable=is_train,
    )
    return (
        conv1_weight,
        conv1_bias,
        conv2_weight,
        conv2_bias,
        fc1_weight,
        fc1_bias,
        fc2_weight,
        fc2_bias,
    )
コード例 #9
0
def _get_kernel_initializer():
    return flow.variance_scaling_initializer(distribution="random_normal",
                                             data_format="NCHW")
コード例 #10
0
def _get_initializer():
    return flow.variance_scaling_initializer(2.0, "fan_out", "random_normal",
                                             "NCHW")