Пример #1
0
        public static Functional GetBilibiliModelV1(int width, int height, int labelNameLenght)
        {
            tf.enable_eager_execution();

            // 宽*高*通道数
            var inputss = keras.Input((height, width, 3));

            var inputs = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputss);

            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);
            inputs = layers.Conv2D(64, (3, 3), activation: keras.activations.Relu).Apply(inputs);
            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);
            inputs = layers.Conv2D(64, (3, 3), activation: keras.activations.Relu).Apply(inputs);
            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);
            inputs = layers.Conv2D(64, (3, 3), activation: keras.activations.Relu).Apply(inputs);
            inputs = layers.MaxPooling2D((1, 1)).Apply(inputs);
            inputs = layers.Conv2D(64, (2, 2), activation: keras.activations.Relu).Apply(inputs);
            inputs = layers.MaxPooling2D((1, 1)).Apply(inputs);

            inputs = layers.Flatten().Apply(inputs);

            inputs = layers.Dense(64, keras.activations.Relu).Apply(inputs);
            var outputs = layers.Dense(labelNameLenght).Apply(inputs);

            var model = keras.Model(inputss, outputs, "bilibili");

            model.summary();

            model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                          optimizer: keras.optimizers.Adam(),
                          metrics: new[] { "accuracy" });

            return(model);
        }
Пример #2
0
        static void Main(string[] args)
        {
            //MemoryLeakTest.Test();
            //return;
            //var x = new string[] { "", "", "", "", "", "" };
            //var y = x[1..];

            Console.WriteLine("Hello World!");
            var b      = new bilibili_cnn();
            var config = b.ReadDataSets();

            Console.WriteLine(JsonConvert.SerializeObject(config, Formatting.Indented));
            Console.WriteLine("Image.Shape={0}", config.Images.Shape);
            Console.WriteLine("Label.Shape={0}", config.Lables.Shape);

            tf.enable_eager_execution();

            // 宽*高*通道数
            var inputss = keras.Input((config.Height, config.Width, 3));

            var inputs = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputss);

            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);
            inputs = layers.Conv2D(64, (3, 3), activation: keras.activations.Relu).Apply(inputs);
            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);
            inputs = layers.Conv2D(64, (3, 3), activation: keras.activations.Relu).Apply(inputs);
            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);
            inputs = layers.Conv2D(64, (3, 3), activation: keras.activations.Relu).Apply(inputs);

            inputs = layers.Flatten().Apply(inputs);
            Console.WriteLine(inputs.shape);

            inputs = layers.Dense(64, keras.activations.Relu).Apply(inputs);
            var outputs = layers.Dense(config.LabNames.Length).Apply(inputs);

            var model = keras.Model(inputss, outputs, "bilibili");

            model.summary();

            model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                          optimizer: keras.optimizers.RMSprop(),
                          metrics: new[] { "accuracy" });

            model.fit(config.Images, config.Lables, epochs: 40);
        }
Пример #3
0
            public void Main()
            {
                // Step-1. Prepare Data
                (x_train, y_train, x_test, y_test) = keras.datasets.mnist.load_data();
                x_train = x_train.reshape(60000, 28, 28, 1) / 255f;
                x_test  = x_test.reshape(10000, 28, 28, 1) / 255f;

                // Step-2. Build CNN Model with Keras Functional
                // input layer
                var inputs = keras.Input(shape: (28, 28, 1));
                // 1st convolution layer
                var outputs = layers.Conv2D(32, kernel_size: 5, activation: keras.activations.Relu).Apply(inputs);

                // 2nd maxpooling layer
                outputs = layers.MaxPooling2D(2, strides: 2).Apply(outputs);
                // 3nd convolution layer
                outputs = layers.Conv2D(64, kernel_size: 3, activation: keras.activations.Relu).Apply(outputs);
                // 4nd maxpooling layer
                outputs = layers.MaxPooling2D(2, strides: 2).Apply(outputs);
                // 5nd flatten layer
                outputs = layers.Flatten().Apply(outputs);
                // 6nd dense layer
                outputs = layers.Dense(1024).Apply(outputs);
                // 7nd dropout layer
                outputs = layers.Dropout(rate: 0.5f).Apply(outputs);
                // output layer
                outputs = layers.Dense(10).Apply(outputs);
                // build keras model
                model = keras.Model(inputs, outputs, name: "mnist_model");
                // show model summary
                model.summary();
                // compile keras model into tensorflow's static graph
                model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                              optimizer: keras.optimizers.Adam(learning_rate: 0.001f),
                              metrics: new[] { "accuracy" });

                // Step-3. Train Model
                // train model by feeding data and labels.
                model.fit(x_train, y_train, batch_size: 64, epochs: 2, validation_split: 0.2f);
                // evluate the model
                model.evaluate(x_test, y_test, verbose: 2);
            }
Пример #4
0
        public void Run()
        {
            // tf.debugging.set_log_device_placement(true);
            tf.Context.Config.GpuOptions.AllowGrowth = true;

            int num = 50, width = 64, height = 64;
            // if width = 128, height = 128, the exception occurs faster

            var bytes       = new byte[num * width * height * 3];
            var inputImages = np.array(bytes) / 255.0f;

            // inputImages = inputImages.reshape((num, height, width, 3));

            bytes = new byte[num];
            var outLables = np.array(bytes);

            Console.WriteLine("Image.Shape={0}", inputImages.dims);
            Console.WriteLine("Label.Shape={0}", outLables.dims);

            tf.enable_eager_execution();

            var inputs = keras.Input((height, width, 3));

            var layer = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputs);

            layer = layers.MaxPooling2D((2, 2)).Apply(layer);

            layer = layers.Flatten().Apply(layer);

            var outputs = layers.Dense(10).Apply(layer);

            var model = keras.Model(inputs, outputs, "gpuleak");

            model.summary();

            model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                          optimizer: keras.optimizers.RMSprop(),
                          metrics: new[] { "accuracy" });

            model.fit(inputImages, outLables, batch_size: 32, epochs: 200);

            keras.backend.clear_session();
        }
Пример #5
0
        public static void Test()
        {
            int num = 50, width = 128, height = 128;

            var bytes       = new byte[num * width * height * 3];
            var inputImages = np.array(bytes) / 255.0f;

            inputImages = inputImages.reshape(num, height, width, 3);

            bytes = new byte[num];
            var outLables = np.array(bytes);

            Console.WriteLine("Image.Shape={0}", inputImages.Shape);
            Console.WriteLine("Label.Shape={0}", outLables.Shape);

            tf.enable_eager_execution();

            // 宽*高*通道数
            var inputss = keras.Input((height, width, 3));

            var inputs = layers.Conv2D(32, (3, 3), activation: keras.activations.Relu).Apply(inputss);

            inputs = layers.MaxPooling2D((2, 2)).Apply(inputs);

            inputs = layers.Flatten().Apply(inputs);

            var outputs = layers.Dense(10).Apply(inputs);

            var model = keras.Model(inputss, outputs, "gpuleak");

            model.summary();

            model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                          optimizer: keras.optimizers.RMSprop(),
                          metrics: new[] { "accuracy" });

            model.fit(inputImages, outLables, epochs: 200);
        }