Exemple #1
0
            public void Main()
            {
                //1. prepare data
                (x_train, y_train, x_test, y_test) = keras.datasets.mnist.load_data();
                x_train = x_train.reshape(60000, 784) / 255f;
                x_test  = x_test.reshape(10000, 784) / 255f;

                //2. buid model
                var inputs  = keras.Input(shape: 784);                                            // input layer
                var outputs = layers.Dense(64, activation: keras.activations.Relu).Apply(inputs); // 1st dense layer

                outputs = layers.Dense(64, activation: keras.activations.Relu).Apply(outputs);    // 2nd dense layer
                outputs = layers.Dense(10).Apply(outputs);                                        // output layer
                model   = keras.Model(inputs, outputs, name: "mnist_model");                      // build keras model
                model.summary();                                                                  // show model summary
                model.compile(loss: keras.losses.SparseCategoricalCrossentropy(from_logits: true),
                              optimizer: keras.optimizers.RMSprop(),
                              metrics: new[] { "accuracy" });// compile keras model into tensorflow's static graph

                //3. train model by feeding data and labels.
                model.fit(x_train, y_train, batch_size: 64, epochs: 2, validation_split: 0.2f);

                //4. evluate the model
                model.evaluate(x_test, y_test, verbose: 2);

                //5. save and serialize model
                model.save("mnist_model");

                // reload the exact same model purely from the file:
                // model = keras.models.load_model("path_to_my_model");

                Console.ReadKey();
            }
Exemple #2
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);
        }
        public static void Test()
        {
            tf.enable_eager_execution();

            var fileNames = new string[]
            {
                "train-labels-idx1-ubyte.gz", "train-images-idx3-ubyte.gz",
                "t10k-labels-idx1-ubyte.gz", "t10k-images-idx3-ubyte.gz"
            };

            var train_labels = np.array(ReadBytes(fileNames[0], 8));
            var train_images = np.array(ReadBytes(fileNames[1], 16));
            var test_labels  = np.array(ReadBytes(fileNames[2], 8));
            var test_images  = np.array(ReadBytes(fileNames[3], 16));

            // 转为单通道 28*28 的图片数据
            train_images = train_images.reshape(train_labels.shape[0], 28 * 28);
            test_images  = test_images.reshape(test_labels.shape[0], 28 * 28);

            Console.WriteLine(train_labels.Shape);
            Console.WriteLine(train_images.Shape);;

            train_images = train_images / 255.0f;
            test_images  = test_images / 255.0f;

            var inputs = keras.Input(shape: 28 * 28);

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

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

            var model = keras.Model(inputs, outputs, name: "fmms");

            model.summary();

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

            model.fit(train_images, train_labels, epochs: 2);

            Console.WriteLine("finish");

            // model.evaluate(test_images, test_labels);

            //model.save("fmm_model");
            //var p1 = model.predict(test_images[0]);
            //Console.WriteLine(p1.shape);
        }
Exemple #4
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);
        }
            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);
            }
        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();
        }
        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);
        }
        static void Main(string[] args)
        {
            // Configuration parameters for the whole setup
            var seed  = 42;
            var gamma = 0.99;  // Discount factor for past rewards
            var max_steps_per_episode = 10000;
            // var env = gym.make("CartPole-v0");  // Create the environment
            CartPoleEnv env = new CartPoleEnv(WinFormEnvViewer.Factory);  // Create the environment

            env.Seed(seed);
            // var eps = np.finfo(np.float32).eps.item();  // Smallest number such that 1.0 + eps != 1.0
            var eps = 1e-5;  // Smallest number such that 1.0 + eps != 1.0

            /*/
             * //// Implement Actor Critic network
             *
             * This network learns two functions:
             *
             * 1. Actor: This takes as input the state of our environment and returns a
             * probability value for each action in its action space.
             * 2. Critic: This takes as input the state of our environment and returns
             * an estimate of total rewards in the future.
             *
             * In our implementation, they share the initial layer.
             * /*/

            var     num_inputs  = 4;
            NDArray num_actions = 2;
            var     num_hidden  = 128;

            LayersApi layers = new LayersApi();
            var       inputs = layers.Input(shape: (num_inputs));
            var       common = layers.Dense(num_hidden, activation: "relu").Apply(inputs);
            var       action = layers.Dense(num_actions, activation: "softmax").Apply(common);
            var       critic = layers.Dense(1).Apply(common);

            Model model = keras.Model(inputs: inputs, outputs: (action, critic));

            /*/
             * //// Train
             * /*/

            var    optimizer            = keras.optimizers.Adam(learning_rate: (float)0.01);
            var    huber_loss           = keras.losses.Huber();
            var    action_probs_history = new List <double>();
            var    critic_value_history = new List <dynamic>();
            var    rewards_history      = new List <double>();
            double running_reward       = 0;
            var    episode_count        = 0;

            while (true)  // Run until solved
            {
                Program.state = env.Reset();
                double episode_reward = 0;
                using (var tape = tf.GradientTape())
                {
                    for (int timestep = 1; timestep < max_steps_per_episode; timestep++)
                    {
                        //env.Render(); // Adding this line would show the attempts
                        // of the agent in a pop up window.

                        Program.state = tf.convert_to_tensor(Program.state);
                        Program.state = tf.expand_dims(Program.state, 0);

                        // Predict action probabilities and estimated future rewards
                        // from environment state
                        // var (action_probs, critic_value) = model.Apply(Program.state);
                        var pred_result = model.Apply(tf.cast(Program.state, tf.float32));

                        var action_probs = pred_result[0][0];
                        var critic_value = pred_result[1][0][0];

                        critic_value_history.Add(critic_value);

                        Tensor probabilities = np.squeeze(action_probs);
                        Console.WriteLine(probabilities);
                        // Sample action from action probability distribution
                        NDArray chosen_action = np.random.choice(num_actions, probabilities: probabilities.);
                        action_probs_history.Add(tf.math.log(action_probs[0, chosen_action]));

                        // Apply the sampled action in our environment
                        var(state, reward, done, _) = env.Step(chosen_action);
                        rewards_history.Add(reward);
                        episode_reward += reward;

                        if (done)
                        {
                            break;
                        }
                    }
                    // Update running reward to check condition for solving
                    running_reward = 0.05 * episode_reward + (1 - 0.05) * running_reward;

                    // Calculate expected value from rewards
                    // - At each timestep what was the total reward received after that timestep
                    // - Rewards in the past are discounted by multiplying them with gamma
                    // - These are the labels for our critic
                    dynamic returns        = new List <double>();
                    double  discounted_sum = 0;

                    var reverse_rewards_history = rewards_history;
                    reverse_rewards_history.Reverse();
                    foreach (double r in reverse_rewards_history)
                    {
                        discounted_sum = r + gamma * discounted_sum;
                        returns.Insert(0, discounted_sum);
                    }

                    // Normalize
                    returns = np.array(returns.ToArray());
                    returns = (returns - np.mean(returns)) / (np.std(returns) + eps);
                    returns = returns.ToList();

                    // Calculating loss values to update our network
                    var history       = zip(action_probs_history, critic_value_history, returns);
                    var actor_losses  = new List <double>();
                    var critic_losses = new List <double>();
                    foreach (double[] item in history)
                    {
                        var     log_prob = item[0];
                        dynamic value    = item[1];
                        dynamic ret      = item[2];
                        // At this point in history, the critic estimated that we would get a
                        // total reward = `value` in the future. We took an action with log probability
                        // of `log_prob` and ended up recieving a total reward = `ret`.
                        // The actor must be updated so that it predicts an action that leads to
                        // high rewards (compared to critic's estimate) with high probability.
                        var diff = ret - value;
                        actor_losses.Add(-log_prob * diff);  // actor loss

                        // The critic must be updated so that it predicts a better estimate of
                        // the future rewards.
                        critic_losses.Add(
                            huber_loss.Call(tf.expand_dims(value, 0), tf.expand_dims(ret, 0))
                            );
                    }

                    // Backpropagation
                    dynamic loss_value = actor_losses.Sum(x => Convert.ToDouble(x)) + critic_losses.Sum(x => Convert.ToDouble(x));
                    var     grads      = tape.gradient(loss_value, model.trainable_variables);
                    optimizer.apply_gradients(zip(grads, model.trainable_variables));

                    // Clear the loss and reward history
                    action_probs_history.Clear();
                    critic_value_history.Clear();
                    rewards_history.Clear();
                }
                // Log details
                episode_count += 1;
                if (episode_count % 10 == 0)
                {
                    var template = String.Format("running reward: {0} at episode {1}", running_reward, episode_count);
                    Console.WriteLine(template);
                }

                if (running_reward > 195)  // Condition to consider the task solved
                {
                    Console.WriteLine(String.Format("Solved at episode {0}!", episode_count));
                    break;
                }
            }

            /*/
             * //// Visualizations
             * In early stages of training:
             * ![Imgur](https://i.imgur.com/5gCs5kH.gif)
             *
             * In later stages of training:
             * ![Imgur](https://i.imgur.com/5ziiZUD.gif)
             * /*/
        }