Esempio n. 1
0
        static void Main(string[] args)
        {
            Console.WriteLine(" ### macademy test console ");
            ComputeDevice selectedDevice = ComputeDeviceFactory.CreateFallbackComputeDevice();

            Generate();

            while (true)
            {
                Console.WriteLine("");
                Console.Write("> ");
                string rawCommand = Console.ReadLine().Trim();
                var    commands   = rawCommand.Split(' ');
                if (commands.Length == 0)
                {
                    continue;
                }

                var nextCommand = commands[0];

                try
                {
                    if (nextCommand == "exit")
                    {
                        break;
                    }
                    else if (nextCommand == "help")
                    {
                        Console.WriteLine("General");
                        Console.WriteLine(" help          - Displays this help message");
                        Console.WriteLine(" exit          - Exits the app");
                        Console.WriteLine("");
                        Console.WriteLine("Device selection");
                        Console.WriteLine(" devices       - Displays available devices");
                        Console.WriteLine(" select (n)    - Selectes the devices with the given id");
                        Console.WriteLine(" info          - Displays information about the selected device");
                        Console.WriteLine(" quicktest     - Performs a quick test on the selected device");
                        Console.WriteLine(" benchmark [n] - Performs a benchmark on the selected device, on the given difficulty (1-10, default: 2)");
                        Console.WriteLine("");
                        Console.WriteLine("Model training and testing");
                        Console.WriteLine(" generate      - Generates a new network");
                        Console.WriteLine(" train         - Trains the network");
                        Console.WriteLine(" eval (i)      - Evaluates the output of the network to the given input");
                        Console.WriteLine(" export [f]    - Exports the current network to the filename provided (default: 'output.json')");
                        Console.WriteLine(" import [f]    - Imports the network from the filename provided (default: 'output.json')");
                    }
                    else if (nextCommand == "devices")
                    {
                        var devices = ComputeDeviceFactory.GetComputeDevices();
                        System.Console.WriteLine(String.Format("Found a total of {0} devices!", devices.Count));
                        int i = 0;
                        foreach (var dev in devices)
                        {
                            Console.WriteLine(String.Format((i++).ToString() + ": [{0}] {1}", dev.GetDeviceAccessType(), dev.GetDeviceName()));
                        }
                    }
                    else if (nextCommand.StartsWith("select"))
                    {
                        if (commands.Length >= 2)
                        {
                            var devices = ComputeDeviceFactory.GetComputeDevices();

                            int selectedDeviceId = 0;
                            if (int.TryParse(commands[1], out selectedDeviceId))
                            {
                                if (selectedDeviceId < 0 || selectedDeviceId >= devices.Count)
                                {
                                    Console.WriteLine("No such device: " + selectedDeviceId);
                                    continue;
                                }

                                selectedDevice = ComputeDeviceFactory.CreateComputeDevice(devices[selectedDeviceId]);
                                Console.WriteLine("Selected device: " + selectedDeviceId + ": " + selectedDevice.GetName());
                            }
                            else
                            {
                                Console.WriteLine("Invalid device id given!");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No device id given!");
                        }
                    }
                    else if (nextCommand == "quicktest")
                    {
                        Console.WriteLine("Testing on device: " + selectedDevice.GetName());
                        TestDevice(selectedDevice);
                    }
                    else if (nextCommand == "train")
                    {
                        int epochs = 1;

                        if (commands.Length >= 2)
                        {
                            int.TryParse(commands[1], out epochs);
                        }

                        Train(selectedDevice, epochs);
                    }
                    else if (nextCommand == "eval")
                    {
                        if (commands.Length < 2)
                        {
                            Console.WriteLine("No input given!");
                            continue;
                        }

                        List <float> input_values = new List <float>();
                        for (int i = 1; i < commands.Length; ++i)
                        {
                            float input = 0;
                            if (!float.TryParse(commands[i], out input))
                            {
                                Console.WriteLine("Invalid input given at index {0}", (i - 1));
                            }

                            input_values.Add(input);
                        }

                        float[] result = Eval(input_values.ToArray(), selectedDevice);

                        Console.WriteLine("Network output: [" + string.Join(", ", result) + "]");
                    }
                    else if (nextCommand == "generate")
                    {
                        Generate();
                        Console.WriteLine("A new network has been generated!");
                    }
                    else if (nextCommand == "benchmark")
                    {
                        int level = 2;

                        if (commands.Length >= 2)
                        {
                            try
                            {
                                level = Math.Max(Math.Min(10, Int32.Parse(commands[1])), 1);
                            }
                            catch (System.Exception)
                            {
                            }
                        }

                        BenchmarkDevice(selectedDevice, level);
                    }
                    else if (nextCommand == "export")
                    {
                        string filename = "output.json";

                        if (commands.Length >= 2)
                        {
                            filename = commands[1];
                        }

                        try
                        {
                            System.IO.File.WriteAllText(filename, target_network.ExportToJSON());
                            Console.WriteLine("Model exported to: '{0}'", filename);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine("Error: " + exc.Message);
                        }
                    }
                    else if (nextCommand == "import")
                    {
                        string filename = "output.json";

                        if (commands.Length >= 2)
                        {
                            filename = commands[1];
                        }

                        try
                        {
                            string json_network = System.IO.File.ReadAllText(filename);
                            target_network = Network.CreateNetworkFromJSON(json_network);
                            Console.WriteLine("Model imported from: '{0}'", filename);
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine("Error: " + exc.Message);
                        }
                    }
                    else if (nextCommand == "info")
                    {
                        if (selectedDevice != null)
                        {
                            Console.WriteLine("Device Name: " + selectedDevice.GetName());
                            Console.WriteLine("Device Access: " + selectedDevice.GetDeviceAccessMode());
                            Console.WriteLine("Core count: " + selectedDevice.GetDeviceCoreCount());
                            Console.WriteLine("Memory: " + selectedDevice.GetDeviceMemorySize());
                        }
                        else
                        {
                            Console.WriteLine("CPU Fallback device is selected!");
                        }
                    }
                }
                catch (System.Exception exc)
                {
                    Console.WriteLine("An error occured when running the command! " + exc.ToString());
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.WriteLine(" ### macademy test console ");
            ComputeDevice selectedDevice = ComputeDeviceFactory.CreateFallbackComputeDevice();

            while (true)
            {
                Console.WriteLine("");
                string rawCommand = Console.ReadLine().Trim();
                var    commands   = rawCommand.Split(' ');
                if (commands.Length == 0)
                {
                    continue;
                }

                var nextCommand = commands[0];

                try
                {
                    if (nextCommand == "exit")
                    {
                        break;
                    }
                    else if (nextCommand == "help")
                    {
                        Console.WriteLine("help       - Displays this help message");
                        Console.WriteLine("devices    - Displays available devices");
                        Console.WriteLine("select (n) - Selectes the devices with the given id");
                        Console.WriteLine("info       - Displays information about the selected device");
                        Console.WriteLine("test       - Performs a quick test on the selected device");
                        Console.WriteLine("exit       - Exits the app");
                    }
                    else if (nextCommand == "devices")
                    {
                        var devices = ComputeDeviceFactory.GetComputeDevices();
                        System.Console.WriteLine(String.Format("Found a total of {0} devices!", devices.Count));
                        int i = 0;
                        foreach (var dev in devices)
                        {
                            Console.WriteLine(String.Format((i++).ToString() + ": [{0}] {1}", dev.GetDeviceAccessType(), dev.GetDeviceName()));
                        }
                    }
                    else if (nextCommand.StartsWith("select"))
                    {
                        if (commands.Length >= 2)
                        {
                            var devices = ComputeDeviceFactory.GetComputeDevices();

                            int selectedDeviceId = 0;
                            if (int.TryParse(commands[1], out selectedDeviceId))
                            {
                                if (selectedDeviceId < 0 || selectedDeviceId >= devices.Count)
                                {
                                    Console.WriteLine("No such device: " + selectedDeviceId);
                                    continue;
                                }

                                selectedDevice = ComputeDeviceFactory.CreateComputeDevice(devices[selectedDeviceId]);
                                Console.WriteLine("Selected device: " + selectedDeviceId + ": " + selectedDevice.GetName());
                            }
                            else
                            {
                                Console.WriteLine("Invalid device id given!");
                            }
                        }
                        else
                        {
                            Console.WriteLine("No device id given!");
                        }
                    }
                    else if (nextCommand == "test")
                    {
                        Console.WriteLine("Testing on device: " + selectedDevice.GetName());
                        TestDevice(selectedDevice);
                    }
                    else if (nextCommand == "info")
                    {
                        if (selectedDevice != null)
                        {
                            Console.WriteLine("Device Name: " + selectedDevice.GetName());
                            Console.WriteLine("Device Access: " + selectedDevice.GetDeviceAccessMode());
                            Console.WriteLine("Core count: " + selectedDevice.GetDeviceCoreCount());
                            Console.WriteLine("Memory: " + selectedDevice.GetDeviceMemorySize());
                        }
                        else
                        {
                            Console.WriteLine("CPU Fallback device is selected!");
                        }
                    }
                }
                catch (System.Exception exc)
                {
                    Console.WriteLine("An error occured when running the command! " + exc.ToString());
                }
            }
        }