示例#1
0
        public static void Run()
        {
            var input = new Input(new Keras.Shape(32, 32));
            //var a = new CuDNNLSTM(32).Set(input);
            var a = new Dense(32, activation: "sigmoid").Set(input);
            //a.Set(input);
            var output = new Dense(1, activation: "sigmoid").Set(a);
            //output.Set(a);

            var model = new Keras.Models.Model(new Input[] { input }, new BaseLayer[] { output });

            //Load train data
            Numpy.NDarray x = np.array(new float[, ] {
                { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 }
            });
            NDarray y = np.array(new float[] { 0, 1, 1, 0 });

            var input1   = new Input(new Shape(32, 32, 3));
            var conv1    = new Conv2D(32, (4, 4).ToTuple(), activation: "relu").Set(input1);
            var pool1    = new MaxPooling2D((2, 2).ToTuple()).Set(conv1);
            var flatten1 = new Flatten().Set(pool1);

            var input2   = new Input(new Shape(32, 32, 3));
            var conv2    = new Conv2D(16, (8, 8).ToTuple(), activation: "relu").Set(input2);
            var pool2    = new MaxPooling2D((2, 2).ToTuple()).Set(conv2);
            var flatten2 = new Flatten().Set(pool2);

            var merge = new Concatenate(flatten1, flatten2);
        }
示例#2
0
        public MobileNetV2(float multiplier = 1, int classes = 1000, string prefix = null, ParameterDict @params = null)
            : base(prefix, @params)
        {
            Features = new HybridSequential("");
            MobileNet.AddConv(Features, Convert.ToInt32(32 * multiplier), 3, pad: 1, stride: 2);
            for (var i = 0; i < in_channels_group.Length; i++)
            {
                var in_c = Convert.ToInt32(multiplier * in_channels_group[i]);
                var c    = Convert.ToInt32(multiplier * channels_group[i]);
                var s    = strides[i];
                var t    = ts[i];

                Features.Add(new LinearBottleneck(in_c, c, t, s));
            }

            var last_channel = multiplier > 1 ? Convert.ToInt32(1280 * multiplier) : 1280;

            MobileNet.AddConv(Features, last_channel, relu6: true);

            Features.Add(new GlobalAvgPool2D());

            Output = new HybridSequential("output_");
            Output.Add(new Conv2D(classes, (1, 1), use_bias: false, prefix: "pred_"));
            Output.Add(new Flatten());
        }
示例#3
0
        // Performs convolutional neural network model training:
        // Incorporated parameters include relu and softmax
        // Adds fixed preprocessing layers and pooling: could use further development with exposed parameters
        private static Sequential ProcessCnnModel(Shape input_shape, NDarray x_train, NDarray y_train, NDarray x_test, NDarray y_test,
                                                  int num_classes, string logname, Config config)
        {
            // Build CNN model
            Sequential model = new Sequential();

            model.Add(new Conv2D(16, kernel_size: (3, 3).ToTuple(), activation: "relu", input_shape: input_shape));
            model.Add(new Conv2D(32, (3, 3).ToTuple(), activation: "relu"));
            model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            model.Add(new Flatten());

            Callback[] callbacks = GetCallbacks(config.isEarlyStop, logname);

            AddNodes(model, config);

            model.Add(new Dense(num_classes, activation: "softmax"));

            // Compile with loss, metrics and optimizer
            model.Compile(loss: "categorical_crossentropy",
                          optimizer: new Adam(lr: (float)config.LearnRate, decay: (float)config.LearnDecay), metrics: new[] { "accuracy" });

            // Train the model
            model.Fit(x_train, y_train, batch_size: config.Batch, epochs: config.Epochs, verbose: 1,
                      validation_data: new[] { x_test, y_test }, callbacks: callbacks);

            return(model);
        }
示例#4
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");
        }
示例#5
0
 public DUC(int planes, int upscale_factor = 2, string prefix = null, ParameterDict @params = null) : base(prefix, @params)
 {
     this.conv          = new Conv2D(planes, kernel_size: (3, 3), padding: (1, 1), use_bias: false);
     this.bn            = new BatchNormCudnnOff(gamma_initializer: "ones", beta_initializer: "zeros");
     this.relu          = new Activation(ActivationType.Relu);
     this.pixel_shuffle = new PixelShuffle2D((upscale_factor, upscale_factor));
 }
示例#6
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;
            }
        }
示例#7
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");
        }
示例#8
0
        public SqueezeNet(string version, int classes = 1000, string prefix = "", ParameterDict @params = null) :
            base(prefix, @params)
        {
            if (version != "1.0" && version != "1.1")
            {
                throw new NotSupportedException("Unsupported version");
            }

            Features = new HybridSequential();
            if (version == "1.0")
            {
                Features.Add(new Conv2D(96, (7, 7), (2, 2)));
                Features.Add(new Activation(ActivationType.Relu));
                Features.Add(new MaxPool2D((3, 3), (2, 2), ceil_mode: true));
                Features.Add(MakeFire(16, 64, 64));
                Features.Add(MakeFire(16, 64, 64));
                Features.Add(MakeFire(32, 128, 128));
                Features.Add(new MaxPool2D((3, 3), (2, 2), ceil_mode: true));
                Features.Add(MakeFire(32, 128, 128));
                Features.Add(MakeFire(48, 192, 192));
                Features.Add(MakeFire(48, 192, 192));
                Features.Add(MakeFire(64, 256, 256));
                Features.Add(new MaxPool2D((3, 3), (2, 2), ceil_mode: true));
                Features.Add(MakeFire(64, 256, 256));
            }
            else if (version == "1.1")
            {
                Features.Add(new Conv2D(64, (3, 3), (2, 2)));
                Features.Add(new Activation(ActivationType.Relu));
                Features.Add(new MaxPool2D((3, 3), (2, 2), ceil_mode: true));
                Features.Add(MakeFire(16, 64, 64));
                Features.Add(MakeFire(16, 64, 64));
                Features.Add(new MaxPool2D((3, 3), (2, 2), ceil_mode: true));
                Features.Add(MakeFire(32, 128, 128));
                Features.Add(MakeFire(32, 128, 128));
                Features.Add(new MaxPool2D((3, 3), (2, 2), ceil_mode: true));
                Features.Add(MakeFire(48, 192, 192));
                Features.Add(MakeFire(48, 192, 192));
                Features.Add(MakeFire(64, 256, 256));
                Features.Add(MakeFire(64, 256, 256));
            }

            Features.Add(new Dropout(0.5f));

            RegisterChild(Features, "features");

            Output = new HybridSequential();
            Output.Add(new Conv2D(classes, (1, 1)));
            Output.Add(new Activation(ActivationType.Relu));
            Output.Add(new AvgPool2D((13, 13)));
            Output.Add(new Flatten());

            RegisterChild(Output, "output");
        }
示例#9
0
    protected ValueTuple <Tensor, List <Tensor> > CreateVisualEncoder(Tensor visualInput, List <SimpleDenseLayerDef> denseLayers, string scope)
    {
        //use the same encoder as in UnityML's python codes
        Tensor        temp;
        List <Tensor> returnWeights = new List <Tensor>();

        using (Current.K.name_scope(scope))
        {
            var conv1 = new Conv2D(16, new int[] { 8, 8 }, new int[] { 4, 4 }, use_bias: visualEncoderBias, kernel_initializer: new GlorotUniform(scale: visualEncoderInitialScale), activation: new ELU());
            var conv2 = new Conv2D(32, new int[] { 4, 4 }, new int[] { 2, 2 }, use_bias: visualEncoderBias, kernel_initializer: new GlorotUniform(scale: visualEncoderInitialScale), activation: new ELU());

            temp = conv1.Call(visualInput)[0];
            temp = conv2.Call(temp)[0];

            var flatten = new Flatten();
            //temp = Current.K.batch_flatten(temp);
            temp = flatten.Call(temp)[0];
            returnWeights.AddRange(conv1.weights);
            returnWeights.AddRange(conv2.weights);
        }

        var output     = BuildSequentialLayers(denseLayers, temp, scope);
        var hiddenFlat = output.Item1;

        returnWeights.AddRange(output.Item2);


        return(ValueTuple.Create(hiddenFlat, returnWeights));
    }
示例#10
0
        public static void LoadWeight(this Conv2D layer, KerasLayerWeightJson[] weightsKernel)
        {
            Vector4 WeightShape = new Vector4(weightsKernel[0].shape[0],
                                              weightsKernel[0].shape[1],
                                              weightsKernel[0].shape[2],
                                              weightsKernel[0].shape[3]);

            layer.WeightShape = WeightShape;
            int kernel_weight_length = (int)(WeightShape.x * WeightShape.y * WeightShape.z * WeightShape.w);
            int bias_weight_length   = (int)WeightShape.w;

            layer.weightcache = new float[kernel_weight_length + bias_weight_length];
            for (int i = 0; i < WeightShape.x; i++)
            {
                for (int j = 0; j < WeightShape.y; j++)
                {
                    for (int k = 0; k < WeightShape.z; k++)
                    {
                        for (int w = 0; w < WeightShape.w; w++)
                        {
                            float arrayindex = i * WeightShape.y * WeightShape.z * WeightShape.w +
                                               j * WeightShape.z * WeightShape.w +
                                               k * WeightShape.w +
                                               w;
                            layer.weightcache[(int)arrayindex] = weightsKernel[0].kernelweight[i, j, k, w];
                        }
                    }
                }
            }
            Array.Copy(weightsKernel[1].arrayweight, 0, layer.weightcache, kernel_weight_length, bias_weight_length);
        }
        // https://github.com/keras-team/keras/blob/49f5b931410bc2e56378f20a15e8ac919e0efb88/keras/applications/vgg16.py
        // WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5'


        /// <summary>
        /// モデル作って返します。
        /// </summary>
        /// <returns></returns>
        public static Model CreateModel(string weightFile)
        {
            var img_input = Python.Input(new int?[] { 224, 224, 3 });
            var x         = new Conv2D(64, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(img_input);

            x = new Conv2D(64, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new MaxPooling2D(new int[] { 2, 2 }).Call(x);

            x = new Conv2D(128, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(128, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new MaxPooling2D(new int[] { 2, 2 }).Call(x);

            x = new Conv2D(256, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(256, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(256, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new MaxPooling2D(new int[] { 2, 2 }).Call(x);

            x = new Conv2D(512, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(512, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(512, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new MaxPooling2D(new int[] { 2, 2 }).Call(x);

            x = new Conv2D(512, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(512, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new Conv2D(512, new[] { 3, 3 }, activation: new ReLU(), padding: PaddingType.Same).Call(x);
            x = new MaxPooling2D(new int[] { 2, 2 }).Call(x);


            var model = new Model(img_input, x, "vgg16");

            model.LoadWeightsH5(weightFile);

            return(model);
        }
        public static Model CreateModel(int classCount)
        {
            var       activation  = tf.keras.activations.elu_fn;
            const int filterCount = 8;

            int[] resNetFilters = { filterCount, filterCount, filterCount };
            return(new Sequential(new Layer[] {
                new Dropout(rate: 0.05),
                Conv2D.NewDyn(filters: filterCount, kernel_size: 5, padding: "same"),
                Activation.NewDyn(activation),
                new MaxPool2D(pool_size: 2),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new MaxPool2D(),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new MaxPool2D(),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new ResNetBlock(kernelSize: 3, filters: resNetFilters, activation: activation),
                new AvgPool2D(pool_size: 2),
                new Flatten(),
                new Dense(units: classCount, activation: tf.nn.softmax_fn),
            }));
        }
示例#13
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;
     }
 }
示例#14
0
            public static Tensor conv2d(Tensor inputs,
                                        int filters,
                                        int[] kernel_size,
                                        int[] strides                   = null,
                                        string padding                  = "valid",
                                        string data_format              = "channels_last",
                                        int[] dilation_rate             = null,
                                        bool use_bias                   = true,
                                        IActivation activation          = null,
                                        IInitializer kernel_initializer = null)
            {
                if (strides == null)
                {
                    strides = new int[] { 1, 1 }
                }
                ;
                if (dilation_rate == null)
                {
                    dilation_rate = new int[] { 1, 1 }
                }
                ;

                var layer = new Conv2D(filters, kernel_size);

                return(layer.apply(inputs));
            }
        }
    }
示例#15
0
        public static BaseModel GenerateModel()
        {
            var model = new Sequential();

            model.Add(new Conv2D(64, kernel_size: (3, 3).ToTuple(), activation: "relu", padding: "same", input_shape: (9, 9, 1)));
            model.Add(new BatchNormalization());
            model.Add(new Conv2D(64, kernel_size: (3, 3).ToTuple(), activation: "relu", padding: "same"));
            model.Add(new BatchNormalization());
            model.Add(new Conv2D(128, kernel_size: (1, 1).ToTuple(), activation: "relu", padding: "same"));

            model.Add(new Flatten());
            model.Add(new Dense(81 * 9));
            model.Add(new Reshape((-1, 9)));
            model.Add(new Activation("softmax"));

            return(model);
        }
示例#16
0
 public ResNetBlock(int kernelSize, int[] filters)
 {
     for (int part = 0; part < PartCount; part++)
     {
         this.convs.Add(this.Track(part == 1
             ? Conv2D.NewDyn(filters[part], kernel_size: kernelSize, padding: "same")
             : Conv2D.NewDyn(filters[part], kernel_size: (1, 1))));
         this.batchNorms.Add(this.Track(new BatchNormalization()));
     }
 }
示例#17
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;
     }
 }
        public void Train()
        {
            // Build CNN model
            _model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(),
                                  padding: Settings.PaddingMode,
                                  input_shape: new Shape(Settings.ImgWidth, Settings.ImgHeight, Settings.Channels)));
            _model.Add(new Activation(Settings.ActivationFunction));
            _model.Add(new Conv2D(32, (3, 3).ToTuple()));
            _model.Add(new Activation(Settings.ActivationFunction));
            _model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            _model.Add(new Dropout(0.25));

            _model.Add(new Conv2D(64, kernel_size: (3, 3).ToTuple(),
                                  padding: Settings.PaddingMode));
            _model.Add(new Activation(Settings.ActivationFunction));
            _model.Add(new Conv2D(64, (3, 3).ToTuple()));
            _model.Add(new Activation(Settings.ActivationFunction));
            _model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            _model.Add(new Dropout(0.25));

            _model.Add(new Flatten());
            _model.Add(new Dense(Settings.FullyConnectedNodes));
            _model.Add(new Activation(Settings.ActivationFunction));
            _model.Add(new Dropout(0.5));
            _model.Add(new Dense(_dataset.NumberClasses));
            _model.Add(new Softmax());

            _model.Compile(loss: Settings.LossFunction,
                           optimizer: Settings.Optimizer,
                           metrics: new string[] { Settings.Accuracy });

            _model.Fit(_dataset.TrainX, _dataset.TrainY,
                       batch_size: Settings.BatchSize,
                       epochs: Settings.Epochs,
                       validation_data: new NDarray[] { _dataset.ValidationX, _dataset.ValidationY });

            var score = _model.Evaluate(_dataset.ValidationX, _dataset.ValidationY, verbose: 0);

            Console.WriteLine("Test loss:" + score[0]);
            Console.WriteLine("Test accuracy:" + score[1]);
        }
示例#19
0
        public ResNetBlock(int kernelSize, int[] filters, PythonFunctionContainer?activation = null)
        {
            this.activation = activation ?? tf.keras.activations.relu_fn;
            for (int part = 0; part < PartCount; part++)
            {
                this.convs.Add(this.Track(part == 1
                    ? Conv2D.NewDyn(filters: filters[part], kernel_size: kernelSize, padding: "same")
                    : Conv2D.NewDyn(filters[part], kernel_size: (1, 1))));
                this.batchNorms.Add(this.Track(new BatchNormalization()));
            }

            this.outputChannels = filters[PartCount - 1];
        }
示例#20
0
        public AlexNet(int classes = 1000, string prefix = "", ParameterDict @params = null) : base(prefix, @params)
        {
            Features = new HybridSequential(prefix);
            Features.Add(new Conv2D(64, (11, 11), (4, 4), (2, 2), activation: ActivationType.Relu));
            Features.Add(new MaxPool2D((3, 3), (2, 2)));

            Features.Add(new Conv2D(192, (5, 5), padding: (2, 2), activation: ActivationType.Relu));
            Features.Add(new MaxPool2D((3, 3), (2, 2)));

            Features.Add(new Conv2D(384, (3, 3), padding: (1, 1), activation: ActivationType.Relu));
            Features.Add(new Conv2D(256, (3, 3), padding: (1, 1), activation: ActivationType.Relu));
            Features.Add(new Conv2D(256, (3, 3), padding: (1, 1), activation: ActivationType.Relu));
            Features.Add(new MaxPool2D((3, 3), (2, 2)));
            Features.Add(new Flatten());
            Features.Add(new Dense(4096, ActivationType.Relu));
            Features.Add(new Dropout(0.5f));
            Features.Add(new Dense(4096, ActivationType.Relu));
            Features.Add(new Dropout(0.5f));

            Output = new Dense(classes);

            RegisterChild(Features);
            RegisterChild(Output);
        }
示例#21
0
        static void identity_block(input_tensor, kernel_size, filters, stage, block) {
            //KerasSharp.Backends.Current.Switch("KerasSharp.Backends.TensorFlowBackend");
            //
            var bn_axis = 3;
            var filters1, filters2, filters3 = filters;
            var conv_name_base = "res" + str(stage) + block + "_branch";
            var bn_name_base = "bn" + str(stage) + block + "_branch";

            var x = new Conv2D(filters1, (1,1), name:conv_name_base + "2a")(input_tensor);
            x = x.add(new BatchNormalization(axis:bn_axis, name:bn_name_base + "2a")(x));
            x = x.add(new activation: ReLU())(x));

            x = x.add(new Conv2D(filters2, kernel_size, padding:"same", name:conv_name_base + "2b")(x));
            x = x.add(new BatchNormalization(axis:bn_axis, name:bn_name_base + "2b")(x));
            x = x.add(new activation: ReLU())(x));


            x = x.add(new Conv2D(filters3, (1,1), name:conv_name_base + "2c")(x));
            x = x.add(new BatchNormalization(axis:bn_axis, name:bn_name_base + "2c")(x));

            x = x.add(activation: new ReLU())(x));
  
            return x;
        }
示例#22
0
            public Tensor conv2d(Tensor inputs,
                                 int filters,
                                 int[] kernel_size,
                                 int[] strides                   = null,
                                 string padding                  = "valid",
                                 string data_format              = "channels_last",
                                 int[] dilation_rate             = null,
                                 bool use_bias                   = true,
                                 Activation activation           = null,
                                 IInitializer kernel_initializer = null,
                                 IInitializer bias_initializer   = null,
                                 bool trainable                  = true,
                                 string name = null)
            {
                if (strides == null)
                {
                    strides = new int[] { 1, 1 }
                }
                ;
                if (dilation_rate == null)
                {
                    dilation_rate = new int[] { 1, 1 }
                }
                ;
                if (bias_initializer == null)
                {
                    bias_initializer = tf.zeros_initializer;
                }

                var layer = new Conv2D(new Conv2DArgs
                {
                    Filters           = filters,
                    KernelSize        = kernel_size,
                    Strides           = strides,
                    Padding           = padding,
                    DataFormat        = data_format,
                    DilationRate      = dilation_rate,
                    Activation        = activation,
                    UseBias           = use_bias,
                    KernelInitializer = kernel_initializer,
                    BiasInitializer   = bias_initializer,
                    Trainable         = trainable,
                    Name = name
                });

                return(layer.Apply(inputs));
            }
示例#23
0
            public static Tensor conv2d(Tensor inputs,
                                        int filters,
                                        int[] kernel_size,
                                        int[] strides                   = null,
                                        string padding                  = "valid",
                                        string data_format              = "channels_last",
                                        int[] dilation_rate             = null,
                                        bool use_bias                   = true,
                                        IActivation activation          = null,
                                        IInitializer kernel_initializer = null,
                                        IInitializer bias_initializer   = null,
                                        bool trainable                  = true,
                                        string name = null)
            {
                if (strides == null)
                {
                    strides = new int[] { 1, 1 }
                }
                ;
                if (dilation_rate == null)
                {
                    dilation_rate = new int[] { 1, 1 }
                }
                ;
                if (bias_initializer == null)
                {
                    bias_initializer = tf.zeros_initializer;
                }

                var layer = new Conv2D(filters,
                                       kernel_size: kernel_size,
                                       strides: strides,
                                       padding: padding,
                                       data_format: data_format,
                                       dilation_rate: dilation_rate,
                                       activation: activation,
                                       use_bias: use_bias,
                                       kernel_initializer: kernel_initializer,
                                       bias_initializer: bias_initializer,
                                       trainable: trainable,
                                       name: name);

                return(layer.apply(inputs));
            }
        }
    }
示例#24
0
    public void TestConv2D()
    {
        var inputLayer = UnityTFUtils.Input(shape: new int?[] { 32, 32, 3 });

        var conv1 = new Conv2D(16, new int[] { 3, 3 }, padding: PaddingType.Same, activation: new ReLU());
        var conv2 = new Conv2D(3, new int[] { 3, 3 }, padding: PaddingType.Same, activation: new ReLU());

        var target = UnityTFUtils.Input(shape: new int?[] { 32, 32, 3 });


        var pred  = conv2.Call(conv1.Call(inputLayer[0])[0])[0];
        var lossM = new MeanSquareError();

        lossM.Call(target[0], pred);


        ((UnityTFBackend)K).ExportGraphDef("SavedGraph/convLayer.pb");
    }
示例#25
0
        public static void Run()
        {
            int batch_size  = 128;
            int num_classes = 10;
            int epochs      = 100;

            // the data, split between train and test sets
            var((x_train, y_train), (x_test, y_test)) = Cifar10.LoadData();

            Console.WriteLine("x_train shape: " + x_train.shape);
            Console.WriteLine(x_train.shape[0] + " train samples");
            Console.WriteLine(x_test.shape[0] + " test samples");

            // convert class vectors to binary class matrices
            y_train = Util.ToCategorical(y_train, num_classes);
            y_test  = Util.ToCategorical(y_test, num_classes);

            // Build CNN model
            var model = new Sequential();

            model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(),
                                 padding: "same",
                                 input_shape: new Shape(32, 32, 3)));
            model.Add(new Activation("relu"));
            model.Add(new Conv2D(32, (3, 3).ToTuple()));
            model.Add(new Activation("relu"));
            model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            model.Add(new Dropout(0.25));

            model.Add(new Conv2D(64, kernel_size: (3, 3).ToTuple(),
                                 padding: "same"));
            model.Add(new Activation("relu"));
            model.Add(new Conv2D(64, (3, 3).ToTuple()));
            model.Add(new Activation("relu"));
            model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            model.Add(new Dropout(0.25));

            model.Add(new Flatten());
            model.Add(new Dense(512));
            model.Add(new Activation("relu"));
            model.Add(new Dropout(0.5));
            model.Add(new Dense(num_classes));
            model.Add(new Activation("softmax"));

            model.Compile(loss: "categorical_crossentropy",
                          optimizer: new RMSprop(lr: 0.0001f, decay: 1e-6f), metrics: new string[] { "accuracy" });

            x_train  = x_train.astype(np.float32);
            x_test   = x_test.astype(np.float32);
            x_train /= 255;
            x_test  /= 255;

            model.Fit(x_train, y_train,
                      batch_size: batch_size,
                      epochs: epochs,
                      verbose: 1,
                      validation_data: new NDarray[] { x_test, y_test },
                      shuffle: true);

            //Save model and weights
            //string model_path = "./model.json";
            //string weight_path = "./weights.h5";
            //string json = model.ToJson();
            //File.WriteAllText(model_path, json);
            //model.SaveWeight(weight_path);
            model.Save("model.h5");
            model.SaveTensorflowJSFormat("./");

            //Score trained model.
            var score = model.Evaluate(x_test, y_test, verbose: 0);

            Console.WriteLine("Test loss:" + score[0]);
            Console.WriteLine("Test accuracy:" + score[1]);
        }
示例#26
0
        public static void RunConv()
        {
            var mnist      = TestUtils.GetMNIST();
            var batch_size = 128;
            var train_data = new NDArrayIter(mnist["train_data"], mnist["train_label"], batch_size, true);
            var val_data   = new NDArrayIter(mnist["test_data"], mnist["test_label"], batch_size);

            var net = new Sequential();

            net.Add(new Conv2D(20, kernel_size: (5, 5), activation: ActivationType.Tanh));
            net.Add(new MaxPool2D(pool_size: (2, 2), strides: (2, 2)));
            net.Add(new Conv2D(50, kernel_size: (5, 5), activation: ActivationType.Tanh));
            net.Add(new MaxPool2D(pool_size: (2, 2), strides: (2, 2)));
            net.Add(new Flatten());
            net.Add(new Dense(500, ActivationType.Tanh));
            net.Add(new Dense(10));

            var gpus = TestUtils.ListGpus();
            var ctx  = gpus.Count > 0 ? gpus.Select(x => Context.Gpu(x)).ToArray() : new[] { Context.Cpu(0) };

            net.Initialize(new Xavier(magnitude: 2.24f), ctx);
            var trainer = new Trainer(net.CollectParams(), new SGD(learning_rate: 0.02f));

            var   epoch  = 10;
            var   metric = new Accuracy();
            var   softmax_cross_entropy_loss = new SoftmaxCELoss();
            float lossVal = 0;

            for (var iter = 0; iter < epoch; iter++)
            {
                var tic = DateTime.Now;
                train_data.Reset();
                lossVal = 0;
                while (!train_data.End())
                {
                    var batch = train_data.Next();
                    var data  = Utils.SplitAndLoad(batch.Data[0], ctx, batch_axis: 0);
                    var label = Utils.SplitAndLoad(batch.Label[0], ctx, batch_axis: 0);

                    var outputs = new NDArrayList();
                    using (var ag = Autograd.Record())
                    {
                        for (var i = 0; i < data.Length; i++)
                        {
                            var x = data[i];
                            var y = label[i];

                            var     z    = net.Call(x);
                            NDArray loss = softmax_cross_entropy_loss.Call(z, y);
                            loss.Backward();
                            lossVal += loss.Mean();
                            outputs.Add(z);
                        }

                        //outputs = Enumerable.Zip(data, label, (x, y) =>
                        //{
                        //    var z = net.Call(x);
                        //    NDArray loss = softmax_cross_entropy_loss.Call(z, y);
                        //    loss.Backward();
                        //    lossVal += loss.Mean();
                        //    return z;
                        //}).ToList();
                    }

                    metric.Update(label, outputs.ToArray());
                    trainer.Step(batch.Data[0].Shape[0]);
                }

                var toc = DateTime.Now;

                var(name, acc) = metric.Get();
                metric.Reset();
                Console.Write($"Loss: {lossVal} ");
                Console.WriteLine($"Training acc at epoch {iter}: {name}={(acc * 100).ToString("0.##")}%, Duration: {(toc - tic).TotalSeconds.ToString("0.#")}s");
            }
        }
示例#27
0
 public static void LoadConfig(this Conv2D layer, KerasLayerConfigJson config)
 {
     layer.Filters    = config.filters;
     layer.KernalSize = new Vector2Int(config.kernel_size[0], config.kernel_size[1]);
     layer.Stride     = new Vector2Int(config.strides[0], config.strides[1]);
 }
示例#28
0
        public static void Run()
        {
            int batch_size  = 128;
            int num_classes = 10;
            int epochs      = 12;

            // input image dimensions
            int img_rows = 28, img_cols = 28;

            Shape input_shape = null;

            // the data, split between train and test sets
            var((x_train, y_train), (x_test, y_test)) = MNIST.LoadData();

            if (K.ImageDataFormat() == "channels_first")
            {
                x_train     = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols);
                x_test      = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols);
                input_shape = (1, img_rows, img_cols);
            }
            else
            {
                x_train     = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1);
                x_test      = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1);
                input_shape = (img_rows, img_cols, 1);
            }

            x_train  = x_train.astype(np.float32);
            x_test   = x_test.astype(np.float32);
            x_train /= 255;
            x_test  /= 255;
            Console.WriteLine("x_train shape: " + x_train.shape);
            Console.WriteLine(x_train.shape[0] + " train samples");
            Console.WriteLine(x_test.shape[0] + " test samples");

            // convert class vectors to binary class matrices
            y_train = Util.ToCategorical(y_train, num_classes);
            y_test  = Util.ToCategorical(y_test, num_classes);

            // Build CNN model
            var model = new Sequential();

            model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(),
                                 activation: "relu",
                                 input_shape: input_shape));
            model.Add(new Conv2D(64, (3, 3).ToTuple(), activation: "relu"));
            model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            model.Add(new Dropout(0.25));
            model.Add(new Flatten());
            model.Add(new Dense(128, activation: "relu"));
            model.Add(new Dropout(0.5));
            model.Add(new Dense(num_classes, activation: "softmax"));

            model.Compile(loss: "categorical_crossentropy",
                          optimizer: new Adadelta(), metrics: new string[] { "accuracy" });

            model.Fit(x_train, y_train,
                      batch_size: batch_size,
                      epochs: epochs,
                      verbose: 1,
                      validation_data: new NDarray[] { x_test, y_test });


            var score = model.Evaluate(x_test, y_test, verbose: 0);

            Console.WriteLine("Test loss:" + score[0]);
            Console.WriteLine("Test accuracy:" + score[1]);
        }
示例#29
0
        static void Main(string[] args)
        {
            int batch_size  = 128; //Training batch size
            int num_classes = 10;  //No. of classes
            int epochs      = 12;  //No. of epoches we will train

            // input image dimensions
            int img_rows = 28, img_cols = 28;

            // Declare the input shape for the network
            Shape input_shape = null;

            // Load the MNIST dataset into Numpy array
            var((x_train, y_train), (x_test, y_test)) = MNIST.LoadData();

            //Check if its channel fist or last and rearrange the dataset accordingly
            if (K.ImageDataFormat() == "channels_first")
            {
                x_train     = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols);
                x_test      = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols);
                input_shape = (1, img_rows, img_cols);
            }
            else
            {
                x_train     = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1);
                x_test      = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1);
                input_shape = (img_rows, img_cols, 1);
            }

            //Normalize the input data
            x_train  = x_train.astype(np.float32);
            x_test   = x_test.astype(np.float32);
            x_train /= 255;
            x_test  /= 255;
            Console.WriteLine("x_train shape: " + x_train.shape);
            Console.WriteLine(x_train.shape[0] + " train samples");
            Console.WriteLine(x_test.shape[0] + " test samples");

            // Convert class vectors to binary class matrices
            y_train = Util.ToCategorical(y_train, num_classes);
            y_test  = Util.ToCategorical(y_test, num_classes);

            // Build CNN model
            var model = new Sequential();

            model.Add(new Conv2D(32, kernel_size: (3, 3).ToTuple(),
                                 activation: "relu",
                                 input_shape: input_shape));
            model.Add(new Conv2D(64, (3, 3).ToTuple(), activation: "relu"));
            model.Add(new MaxPooling2D(pool_size: (2, 2).ToTuple()));
            model.Add(new Dropout(0.25));
            model.Add(new Flatten());
            model.Add(new Dense(128, activation: "relu"));
            model.Add(new Dropout(0.5));
            model.Add(new Dense(num_classes, activation: "softmax"));

            //Compile with loss, metrics and optimizer
            model.Compile(loss: "categorical_crossentropy",
                          optimizer: new Adadelta(), metrics: new string[] { "accuracy" });

            //Train the model
            model.Fit(x_train, y_train,
                      batch_size: batch_size,
                      epochs: epochs,
                      verbose: 1,
                      validation_data: new NDarray[] { x_test, y_test });


            //Score the model for performance
            var score = model.Evaluate(x_test, y_test, verbose: 0);

            Console.WriteLine("Test loss:" + score[0]);
            Console.WriteLine("Test accuracy:" + score[1]);

            // Save the model to HDF5 format which can be loaded later or ported to other application
            model.Save("model.h5");
            // Save it to Tensorflow JS format and we will test it in browser.
            var v = K.Instance;

            //model.SaveTensorflowJSFormat(@"C:\_temp\");
            //model.SaveOnnx(@"C:\_temp\");
            Console.ReadLine();
        }
示例#30
0
        private static void LoadModel(KerasLayersJson layersJson, List <NNLayerBase> Layers)
        {
            foreach (var layer in layersJson.layers)
            {
                switch (layer.class_name)
                {
                case "InputLayer":
                    var Input = new InputLayer();
                    Input.LoadConfig(layer.config);
                    Layers.Add(Input);
                    break;

                case "Activation":
                    if (layer.config.activation == "relu")
                    {
                        Layers.Add(new ReLU());
                    }

                    if (layer.config.activation == "tanh")
                    {
                        Layers.Add(new Tanh());
                    }

                    break;

                case "Conv2D":
                    var Conv2DLayer = new Conv2D();
                    Conv2DLayer.LoadConfig(layer.config);
                    Layers.Add(Conv2DLayer);
                    if (layer.config.activation == "relu")
                    {
                        Layers.Add(new ReLU());
                    }

                    if (layer.config.activation == "tanh")
                    {
                        Layers.Add(new Tanh());
                    }

                    break;

                case "LeakyReLU":
                    var LeakyReLULayer = new LeakyReLU();
                    LeakyReLULayer.LoadConfig(layer.config);
                    Layers.Add(LeakyReLULayer);
                    break;

                case "BatchNormalization":
                    Layers.Add(new BatchNormalization());
                    break;

                case "UpSampling2D":
                    var UpSampling2DLayer = new UpSampling2D();
                    UpSampling2DLayer.LoadConfig(layer.config);
                    Layers.Add(UpSampling2DLayer);
                    break;

                case "Concatenate":
                    var    thislayer            = new Concatenate();
                    string alternativeLayerName = layer.inbound_nodes[0][1][0] as string;
                    thislayer.AlternativeInputId =
                        Layers.FindIndex(ly => string.Compare(ly.Name, alternativeLayerName) == 0);
                    Layers.Add(thislayer);
                    break;

                case "Add":
                    var addlayer   = new Add();
                    int alterinput = layer.inbound_nodes[0].FindIndex(node =>
                                                                      string.Compare(node[0] as string, Layers[Layers.Count - 1].Name) != 0);
                    string addalternativeLayerName = layer.inbound_nodes[0][alterinput][0] as string;
                    addlayer.AlternativeInputId =
                        Layers.FindIndex(ly => string.Compare(ly.Name, addalternativeLayerName) == 0);
                    Layers.Add(addlayer);
                    break;
                }

                Layers[Layers.Count - 1].Name = layer.name;
            }
        }