Esempio n. 1
0
        public ResNetV1(string block, int[] layers, int[] channels, int classes = 1000, bool thumbnail = false,
                        string prefix = "", ParameterDict @params = null) : base(prefix, @params)
        {
            if (layers.Length != channels.Length - 1)
            {
                throw new Exception("layers.length should be equal to channels.length - 1");
            }

            Features = new HybridSequential();
            if (thumbnail)
            {
                Features.Add(ResNet.Conv3x3(channels[0], 1, 0));
            }
            else
            {
                Features.Add(new Conv2D(channels[0], (7, 7), (2, 2), (3, 3), use_bias: false));
                Features.Add(new BatchNorm());
                Features.Add(new Activation(ActivationType.Relu));
                Features.Add(new MaxPool2D((3, 3), (2, 2), (1, 1)));
            }

            for (var i = 0; i < layers.Length; i++)
            {
                var stride    = i == 0 ? 1 : 2;
                var num_layer = layers[i];
                Features.Add(MakeLayer(block, num_layer, channels[i + 1], stride, i + 1, channels[i]));
            }

            Features.Add(new GlobalAvgPool2D());

            Output = new Dense(classes, in_units: channels.Last());

            RegisterChild(Features, "features");
            RegisterChild(Output, "output");
        }
Esempio n. 2
0
        public BottleneckV2(int channels, int stride, bool downsample = false, int in_channels = 0,
                            string prefix = "", ParameterDict @params = null) : base(prefix, @params)
        {
            var channel_one_fourth = Convert.ToInt32(channels / 4);

            bn1   = new BatchNorm();
            conv1 = new Conv2D(channel_one_fourth, (1, 1), (1, 1), use_bias: false);
            bn2   = new BatchNorm();
            conv2 = ResNet.Conv3x3(channel_one_fourth, stride, channel_one_fourth);
            bn3   = new BatchNorm();
            conv3 = new Conv2D(channels, (1, 1), (1, 1), use_bias: false);
            RegisterChild(bn1, "bn1");
            RegisterChild(conv1, "conv1");
            RegisterChild(bn2, "bn2");
            RegisterChild(conv2, "conv2");
            RegisterChild(bn3, "bn3");
            RegisterChild(conv3, "conv3");

            if (downsample)
            {
                ds = new Conv2D(channels, (1, 1), (stride, stride), use_bias: false, in_channels: in_channels);
                RegisterChild(ds, "downsample");
            }
            else
            {
                ds = null;
            }
        }
Esempio n. 3
0
        public BottleneckV1(int channels, int stride, bool downsample = false, int in_channels = 0,
                            string prefix = "", ParameterDict @params = null) : base(prefix, @params)
        {
            var channel_one_fourth = Convert.ToInt32(channels / 4);

            body = new HybridSequential("");
            body.Add(new Conv2D(channel_one_fourth, (1, 1), (stride, stride)));
            body.Add(new BatchNorm());
            body.Add(new Activation(ActivationType.Relu));
            body.Add(ResNet.Conv3x3(channel_one_fourth, 1, channel_one_fourth));
            body.Add(new BatchNorm());
            body.Add(new Activation(ActivationType.Relu));
            body.Add(new Conv2D(channels, (1, 1), (1, 1)));
            body.Add(new BatchNorm());

            if (downsample)
            {
                ds = new HybridSequential();
                ds.Add(new Conv2D(channels, (1, 1), (stride, stride), use_bias: false, in_channels: in_channels));
                ds.Add(new BatchNorm());
                RegisterChild(ds, "downsample");
            }
            else
            {
                ds = null;
            }

            RegisterChild(body, "body");
        }
Esempio n. 4
0
 public BasicBlockV2(int channels, int stride, bool downsample = false, int in_channels = 0,
                     string prefix = null, ParameterDict @params = null) : base(prefix, @params)
 {
     bn1   = new BatchNorm();
     conv1 = ResNet.Conv3x3(channels, stride, in_channels);
     bn2   = new BatchNorm();
     conv2 = ResNet.Conv3x3(channels, 1, in_channels);
     if (downsample)
     {
         ds = new Conv2D(channels, (1, 1), (stride, stride), use_bias: false, in_channels: in_channels);
     }
     else
     {
         ds = null;
     }
 }
Esempio n. 5
0
 public BasicBlockV1(int channels, int stride, bool downsample = false, int in_channels = 0,
                     string prefix = null, ParameterDict @params = null) : base(prefix, @params)
 {
     body = new HybridSequential("");
     body.Add(ResNet.Conv3x3(channels, stride, in_channels));
     body.Add(new BatchNorm());
     body.Add(new Activation(ActivationType.Relu));
     body.Add(ResNet.Conv3x3(channels, 1, in_channels));
     body.Add(new BatchNorm());
     if (downsample)
     {
         ds = new HybridSequential();
         ds.Add(new Conv2D(channels, (1, 1), (stride, stride), use_bias: false, in_channels: in_channels));
         ds.Add(new BatchNorm());
     }
     else
     {
         ds = null;
     }
 }