コード例 #1
0
        // 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);
        }
コード例 #2
0
ファイル: tf.layers.cs プロジェクト: zjg1007/TensorFlow.NET
            /// <summary>
            /// Max pooling layer for 2D inputs (e.g. images).
            /// </summary>
            /// <param name="inputs">The tensor over which to pool. Must have rank 4.</param>
            /// <param name="pool_size"></param>
            /// <param name="strides"></param>
            /// <param name="padding"></param>
            /// <param name="data_format"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public Tensor max_pooling2d(Tensor inputs,
                                        int[] pool_size,
                                        int[] strides,
                                        string padding     = "valid",
                                        string data_format = "channels_last",
                                        string name        = null)
            {
                var layer = new MaxPooling2D(pool_size: pool_size,
                                             strides: strides,
                                             padding: padding,
                                             data_format: data_format,
                                             name: name);

                return(layer.apply(inputs).Item1);
            }
コード例 #3
0
        public ILayer CreateProduct(IKernelDescriptor descriptor)
        {
            if (descriptor is MaxPooling2D)
            {
                MaxPooling2D pool = descriptor as MaxPooling2D;

                ILayer layer = new MaxPool2DLayer(pool.PaddingVertical, pool.PaddingHorizontal,
                                                  pool.StrideVertical, pool.StrideHorizontal,
                                                  pool.KernelHeight, pool.KernelWidth);

                return(layer);
            }

            return(null);
        }
コード例 #4
0
            /// <summary>
            /// Max pooling layer for 2D inputs (e.g. images).
            /// </summary>
            /// <param name="inputs">The tensor over which to pool. Must have rank 4.</param>
            /// <param name="pool_size"></param>
            /// <param name="strides"></param>
            /// <param name="padding"></param>
            /// <param name="data_format"></param>
            /// <param name="name"></param>
            /// <returns></returns>
            public Tensor max_pooling2d(Tensor inputs,
                                        int[] pool_size,
                                        int[] strides,
                                        string padding     = "valid",
                                        string data_format = "channels_last",
                                        string name        = null)
            {
                var layer = new MaxPooling2D(new MaxPooling2DArgs
                {
                    PoolSize   = pool_size,
                    Strides    = strides,
                    Padding    = padding,
                    DataFormat = data_format,
                    Name       = name
                });

                return(layer.Apply(inputs));
            }
コード例 #5
0
ファイル: SingleBenchmark.cs プロジェクト: ohisama/KelpNet
        public static void Run()
        {
            Stopwatch sw = new Stopwatch();

            NdArray inputArrayCpu = new NdArray(Initializer.GetRealArray(INPUT_SIZE));
            NdArray inputArrayGpu = new NdArray(Initializer.GetRealArray(INPUT_SIZE));

            //Linear
            Linear linear = new Linear(INPUT_SIZE, OUTPUT_SIZE);

            Console.WriteLine("◆" + linear.Name);

            sw.Restart();
            NdArray[] gradArrayCpu = linear.Forward(inputArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradArrayCpu[0].Grad = gradArrayCpu[0].Data; //DataをGradとして使用

            sw.Restart();
            linear.Backward(gradArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (linear.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradArrayGpu = linear.Forward(inputArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradArrayGpu[0].Grad = gradArrayGpu[0].Data;

                sw.Restart();
                linear.Backward(gradArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }


            //Tanh
            TanhActivation tanh = new TanhActivation();

            Console.WriteLine("\n◆" + tanh.Name);

            sw.Restart();
            gradArrayCpu = tanh.Forward(inputArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradArrayCpu[0].Grad = gradArrayCpu[0].Data;

            sw.Restart();
            tanh.Backward(gradArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (tanh.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradArrayGpu = tanh.Forward(inputArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradArrayGpu[0].Grad = gradArrayGpu[0].Data;

                sw.Restart();
                tanh.Backward(gradArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }


            //Sigmoid
            Sigmoid sigmoid = new Sigmoid();

            Console.WriteLine("\n◆" + sigmoid.Name);

            sw.Restart();
            gradArrayCpu = sigmoid.Forward(inputArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradArrayCpu[0].Grad = gradArrayCpu[0].Data;

            sw.Restart();
            sigmoid.Backward(gradArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (sigmoid.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradArrayGpu = sigmoid.Forward(inputArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradArrayGpu[0].Grad = gradArrayGpu[0].Data;

                sw.Restart();
                sigmoid.Backward(gradArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }


            //ReLU
            ReLU relu = new ReLU();

            Console.WriteLine("\n◆" + relu.Name);

            sw.Restart();
            gradArrayCpu = relu.Forward(inputArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradArrayCpu[0].Grad = gradArrayCpu[0].Data;

            sw.Restart();
            relu.Backward(gradArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (relu.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradArrayGpu = relu.Forward(inputArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradArrayGpu[0].Grad = gradArrayGpu[0].Data;

                sw.Restart();
                relu.Backward(gradArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }


            //LeakyReLU
            LeakyReLU leakyRelu = new LeakyReLU();

            Console.WriteLine("\n◆" + leakyRelu.Name);

            sw.Restart();
            gradArrayCpu = leakyRelu.Forward(inputArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradArrayCpu[0].Grad = gradArrayCpu[0].Data;

            sw.Restart();
            leakyRelu.Backward(gradArrayCpu);
            sw.Stop();

            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (leakyRelu.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradArrayGpu = leakyRelu.Forward(inputArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradArrayGpu[0].Grad = gradArrayGpu[0].Data;

                sw.Restart();
                leakyRelu.Backward(gradArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }


            NdArray inputImageArrayGpu = new NdArray(Initializer.GetRealArray(3 * 256 * 256 * 5), new[] { 3, 256, 256 }, 5);
            NdArray inputImageArrayCpu = new NdArray(Initializer.GetRealArray(3 * 256 * 256 * 5), new[] { 3, 256, 256 }, 5);


            //MaxPooling
            MaxPooling2D maxPooling2D = new MaxPooling2D(3);

            Console.WriteLine("\n◆" + maxPooling2D.Name);

            sw.Restart();
            NdArray[] gradImageArrayCpu = maxPooling2D.Forward(inputImageArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradImageArrayCpu[0].Grad = gradImageArrayCpu[0].Data;

            sw.Restart();
            maxPooling2D.Backward(gradImageArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (maxPooling2D.SetGpuEnable(true))
            {
                sw.Restart();
                maxPooling2D.Forward(inputImageArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                //メモリ転送のみのため実装がない
                Console.WriteLine("Backward[Gpu] : None");
            }


            //Conv2D
            Convolution2D conv2d = new Convolution2D(3, 3, 3);

            Console.WriteLine("\n◆" + conv2d.Name);

            sw.Restart();
            gradImageArrayCpu = conv2d.Forward(inputImageArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradImageArrayCpu[0].Grad = gradImageArrayCpu[0].Data;

            sw.Restart();
            conv2d.Backward(gradImageArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (conv2d.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradImageArrayGpu = conv2d.Forward(inputImageArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradImageArrayGpu[0].Grad = gradImageArrayGpu[0].Data;

                sw.Restart();
                conv2d.Backward(gradImageArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }


            //Deconv2D
            Deconvolution2D deconv2d = new Deconvolution2D(3, 3, 3);

            Console.WriteLine("\n◆" + deconv2d.Name);

            sw.Restart();
            gradImageArrayCpu = deconv2d.Forward(inputImageArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradImageArrayCpu[0].Grad = gradImageArrayCpu[0].Data;

            sw.Restart();
            deconv2d.Backward(gradImageArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (deconv2d.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradImageArrayGpu = deconv2d.Forward(inputImageArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradImageArrayGpu[0].Grad = gradImageArrayGpu[0].Data;

                sw.Restart();
                deconv2d.Backward(gradImageArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }

            //Dropout
            Dropout dropout = new Dropout();

            Console.WriteLine("\n◆" + dropout.Name);

            sw.Restart();
            gradArrayCpu = dropout.Forward(inputArrayCpu);
            sw.Stop();
            Console.WriteLine("Forward [Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            gradArrayCpu[0].Grad = gradArrayCpu[0].Data;

            sw.Restart();
            dropout.Backward(gradArrayCpu);
            sw.Stop();
            Console.WriteLine("Backward[Cpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

            if (dropout.SetGpuEnable(true))
            {
                sw.Restart();
                NdArray[] gradArrayGpu = dropout.Forward(inputArrayGpu);
                sw.Stop();
                Console.WriteLine("Forward [Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                gradArrayGpu[0].Grad = gradArrayGpu[0].Data;

                sw.Restart();
                dropout.Backward(gradArrayGpu);
                sw.Stop();
                Console.WriteLine("Backward[Gpu] : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");
            }
        }
コード例 #6
0
        private List <IKernelDescriptor> ReadDescriptors(JObject model)
        {
            List <IKernelDescriptor> dscps = model.SelectToken("descriptors").Select(layer => {
                IKernelDescriptor descriptor = null;

                String layerName = (String)layer.SelectToken("layer");

                switch (layerName)
                {
                case "AvgPooling1D":
                    descriptor = new AvgPooling1D(
                        (int)layer.SelectToken("padding"),
                        (int)layer.SelectToken("stride"),
                        (int)layer.SelectToken("kernel_size"));
                    break;

                case "GlobalAveragePooling1D":
                    descriptor = new GlobalAvgPooling1D();
                    break;

                case "AvgPooling2D":
                    descriptor = new AvgPooling2D((int)layer.SelectToken("padding_vl"), (int)layer.SelectToken("padding_hz"),
                                                  (int)layer.SelectToken("stride_vl"), (int)layer.SelectToken("stride_hz"),
                                                  (int)layer.SelectToken("kernel_height"), (int)layer.SelectToken("kernel_width"));
                    break;

                case "GlobalAveragePooling2D":
                    descriptor = new GlobalAvgPooling2D();
                    break;

                case "BatchNormalization":
                    descriptor = new BatchNormalization(
                        (int)layer.SelectToken("epsilon"));
                    break;

                case "Cropping1D":
                    descriptor = new Cropping1D(
                        (int)layer.SelectToken("trimBegin"),
                        (int)layer.SelectToken("trimEnd"));
                    break;

                case "Cropping2D":
                    descriptor = new Cropping2D(
                        (int)layer.SelectToken("topTrim"),
                        (int)layer.SelectToken("bottomTrim"),
                        (int)layer.SelectToken("leftTrim"),
                        (int)layer.SelectToken("rightTrim"));
                    break;

                case "MaxPooling1D":
                    descriptor = new MaxPooling1D(
                        (int)layer.SelectToken("padding"),
                        (int)layer.SelectToken("stride"),
                        (int)layer.SelectToken("kernel_size"));
                    break;

                case "GlobalMaxPooling1D":
                    descriptor = new GlobalMaxPooling1D();
                    break;

                case "MaxPooling2D":
                    descriptor = new MaxPooling2D((int)layer.SelectToken("padding_vl"), (int)layer.SelectToken("padding_hz"),
                                                  (int)layer.SelectToken("stride_vl"), (int)layer.SelectToken("stride_hz"),
                                                  (int)layer.SelectToken("kernel_height"), (int)layer.SelectToken("kernel_width"));
                    break;

                case "GlobalMaxPooling2D":
                    descriptor = new GlobalMaxPooling2D();
                    break;

                case "Convolution1D":
                    descriptor = new Convolution1D(
                        (int)layer.SelectToken("padding"),
                        (int)layer.SelectToken("stride"),
                        (int)layer.SelectToken("kernel_size"),
                        (int)layer.SelectToken("kernel_num"));
                    break;

                case "Convolution2D":
                    descriptor = new Convolution2D((int)layer.SelectToken("padding_vl"), (int)layer.SelectToken("padding_hz"),
                                                   (int)layer.SelectToken("stride_vl"), (int)layer.SelectToken("stride_hz"),
                                                   (int)layer.SelectToken("kernel_height"), (int)layer.SelectToken("kernel_width"),
                                                   (int)layer.SelectToken("kernel_num"));
                    break;

                case "Dense2D":
                    descriptor = new Dense2D((int)layer.SelectToken("units"));
                    break;

                case "Input2D":
                    descriptor = new Input2D((int)layer.SelectToken("height"), (int)layer.SelectToken("width"),
                                             (int)layer.SelectToken("channel"), (int)layer.SelectToken("batch"));
                    break;

                case "Bias2D":
                    descriptor = new Bias2D();
                    break;

                case "Permute":
                    descriptor = new Permute(
                        (int)layer.SelectToken("dim1"),
                        (int)layer.SelectToken("dim2"),
                        (int)layer.SelectToken("dim3"));
                    break;

                case "Reshape":
                    descriptor = new Reshape2D(
                        (int)layer.SelectToken("height"),
                        (int)layer.SelectToken("width"),
                        (int)layer.SelectToken("channel"),
                        1);
                    break;

                case "RepeatVector":
                    descriptor = new RepeatVector(
                        (int)layer.SelectToken("num"));
                    break;

                case "SimpleRNN":
                    descriptor = new SimpleRNN(
                        (int)layer.SelectToken("units"),
                        (int)layer.SelectToken("input_dim"),
                        ANR((string)layer.SelectToken("activation")));
                    break;

                case "LSTM":
                    descriptor = new LSTM(
                        (int)layer.SelectToken("units"),
                        (int)layer.SelectToken("input_dim"),
                        ANR((string)layer.SelectToken("activation")),
                        ANR((string)layer.SelectToken("rec_act")));
                    break;

                case "GRU":
                    descriptor = new GRU(
                        (int)layer.SelectToken("units"),
                        (int)layer.SelectToken("input_dim"),
                        ANR((string)layer.SelectToken("activation")),
                        ANR((string)layer.SelectToken("rec_act")));
                    break;

                case "ELu":
                    descriptor = new ELu(1);
                    break;

                case "HardSigmoid":
                    descriptor = new HardSigmoid();
                    break;

                case "ReLu":
                    descriptor = new ReLu();
                    break;

                case "Sigmoid":
                    descriptor = new Sigmoid();
                    break;

                case "Flatten":
                    descriptor = new Flatten();
                    break;

                case "Softmax":
                    descriptor = new Softmax();
                    break;

                case "SoftPlus":
                    descriptor = new SoftPlus();
                    break;

                case "SoftSign":
                    descriptor = new Softsign();
                    break;

                case "TanH":
                    descriptor = new TanH();
                    break;

                default:
                    throw new Exception("Unknown layer type!");
                }

                return(descriptor);
            }).ToList();

            return(dscps);
        }