コード例 #1
0
ファイル: Sample15.cs プロジェクト: ShTair/ConvolutionChecker
        public static void Run(VGGModel modelType)
        {
            OpenFileDialog ofd = new OpenFileDialog {
                Filter = "画像ファイル(*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp|すべてのファイル(*.*)|*.*"
            };

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                int vggId = (int)modelType;

                Console.WriteLine("Model Loading.");
                string modelFilePath = InternetFileDownloader.Donwload(Urls[vggId], FileNames[vggId], Hashes[vggId]);

                List <Function> vggNet = CaffemodelDataLoader.ModelLoad(modelFilePath);

                string[] classList = File.ReadAllLines(CLASS_LIST_PATH);

                //GPUを初期化
                for (int i = 0; i < vggNet.Count - 1; i++)
                {
                    if (vggNet[i] is CPU.Convolution2D || vggNet[i] is CPU.Linear || vggNet[i] is CPU.MaxPooling2D)
                    {
                        vggNet[i] = (Function)CLConverter.Convert(vggNet[i]);
                    }
                }

                FunctionStack nn = new FunctionStack(vggNet.ToArray());

                //層を圧縮
                nn.Compress();

                Console.WriteLine("Model Loading done.");

                do
                {
                    //ネットワークへ入力する前に解像度を 224px x 224px x 3ch にしておく
                    Bitmap   baseImage   = new Bitmap(ofd.FileName);
                    Bitmap   resultImage = new Bitmap(224, 224, PixelFormat.Format24bppRgb);
                    Graphics g           = Graphics.FromImage(resultImage);
                    g.DrawImage(baseImage, 0, 0, 224, 224);
                    g.Dispose();

                    Real[]  bias       = { -123.68, -116.779, -103.939 }; //補正値のチャンネル順は入力画像に従う(標準的なBitmapならRGB)
                    NdArray imageArray = BitmapConverter.Image2NdArray(resultImage, false, true, bias);

                    Console.WriteLine("Start predict.");
                    Stopwatch sw     = Stopwatch.StartNew();
                    NdArray   result = nn.Predict(imageArray)[0];
                    sw.Stop();

                    Console.WriteLine("Result Time : " +
                                      (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") +
                                      "μs");

                    int maxIndex = Array.IndexOf(result.Data, result.Data.Max());
                    Console.WriteLine("[" + result.Data[maxIndex] + "] : " + classList[maxIndex]);
                } while (ofd.ShowDialog() == DialogResult.OK);
            }
        }
コード例 #2
0
        public static void Run()
        {
            OpenFileDialog ofd = new OpenFileDialog
            {
                Filter = "画像ファイル(*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp|すべてのファイル(*.*)|*.*"
            };

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                Console.WriteLine("Model Loading.");
                string modelFilePath            = InternetFileDownloader.Donwload(DOWNLOAD_URL, MODEL_FILE, MODEL_FILE_HASH);
                List <Function <Real> > alexNet = CaffemodelDataLoader.ModelLoad <Real>(modelFilePath);
                string[] classList = File.ReadAllLines(CLASS_LIST_PATH);

                //GPUを初期化
                for (int i = 0; i < alexNet.Count - 1; i++)
                {
                    if (alexNet[i] is CPU.Convolution2D <Real> || alexNet[i] is CPU.Linear <Real> || alexNet[i] is CPU.MaxPooling2D <Real> )
                    {
                        alexNet[i] = (Function <Real>)CLConverter.Convert(alexNet[i]);
                    }
                }

                FunctionStack <Real> nn = new FunctionStack <Real>(alexNet.ToArray());

                //層を圧縮
                nn.Compress();

                Console.WriteLine("Model Loading done.");

                do
                {
                    //ネットワークへ入力する前に解像度を 224px x 224px x 3ch にしておく
                    Bitmap   baseImage   = new Bitmap(ofd.FileName);
                    Bitmap   resultImage = new Bitmap(227, 227, PixelFormat.Format24bppRgb);
                    Graphics g           = Graphics.FromImage(resultImage);
                    g.DrawImage(baseImage, 0, 0, 227, 227);
                    g.Dispose();

                    Real[]         bias       = new Real[] { -123.68f, -116.779f, -103.939f }; //補正値のチャンネル順は入力画像に従う
                    NdArray <Real> imageArray = BitmapConverter.Image2NdArray(resultImage, false, true, bias);

                    Console.WriteLine("Start predict.");
                    Stopwatch      sw     = Stopwatch.StartNew();
                    NdArray <Real> result = nn.Predict(imageArray)[0];
                    sw.Stop();

                    Console.WriteLine("Result Time : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                    int maxIndex = Array.IndexOf(result.Data, result.Data.Max());
                    Console.WriteLine("[" + result.Data[maxIndex] + "] : " + classList[maxIndex]);
                } while (ofd.ShowDialog() == DialogResult.OK);
            }
        }
コード例 #3
0
ファイル: Sample17.cs プロジェクト: ziichuan/KelpNet
        static void SwitchGPU(CPU.FunctionStack <Real> functionStack)
        {
            for (int i = 0; i < functionStack.Functions.Length; i++)
            {
                if (functionStack.Functions[i] is CPU.Convolution2D <Real> || functionStack.Functions[i] is CPU.Linear <Real> || functionStack.Functions[i] is CPU.MaxPooling2D <Real> )
                {
                    functionStack.Functions[i] = (Function <Real>)CLConverter.Convert(functionStack.Functions[i]);
                }

                if (functionStack.Functions[i] is SplitFunction <Real> splitFunction)
                {
                    for (int j = 0; j < splitFunction.SplitedFunctions.Length; j++)
                    {
                        SwitchGPU(splitFunction.SplitedFunctions[j]);
                    }
                }
            }

            //ブロック単位で層の圧縮を実行
            functionStack.Compress();
        }
コード例 #4
0
ファイル: Sample20.cs プロジェクト: ziichuan/KelpNet
        public static void Run(VGGModel modelType)
        {
            OpenFileDialog ofd = new OpenFileDialog {
                Filter = "画像ファイル(*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp|すべてのファイル(*.*)|*.*"
            };

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                int vggId = (int)modelType;

                Console.WriteLine("Model Loading.");
                string modelFilePath = InternetFileDownloader.Donwload(Urls[vggId], FileNames[vggId], Hashes[vggId]);

                List <Function <Real> > vggNet = OnnxmodelDataLoader.LoadNetWork <Real>(modelFilePath);

                string[] classList = File.ReadAllLines(CLASS_LIST_PATH);

                //GPUを初期化
                for (int i = 0; i < vggNet.Count - 1; i++)
                {
                    if (vggNet[i] is CPU.Convolution2D <Real> || vggNet[i] is CPU.Linear <Real> || vggNet[i] is CPU.MaxPooling2D <Real> )
                    {
                        vggNet[i] = (Function <Real>)CLConverter.Convert(vggNet[i]);
                    }
                }

                FunctionStack <Real> nn = new FunctionStack <Real>(vggNet.ToArray());

                //層を圧縮
                nn.Compress();

                Console.WriteLine("Model Loading done.");

                do
                {
                    //ネットワークへ入力する前に解像度を 224px x 224px x 3ch にしておく
                    Bitmap   baseImage   = new Bitmap(ofd.FileName);
                    Bitmap   resultImage = new Bitmap(224, 224, PixelFormat.Format24bppRgb);
                    Graphics g           = Graphics.FromImage(resultImage);
                    g.DrawImage(baseImage, 0, 0, 224, 224);
                    g.Dispose();

                    Real[] mean = new Real[] { 0.485f, 0.456f, 0.406f };
                    Real[] std  = new Real[] { 0.229f, 0.224f, 0.225f };

                    NdArray <Real> imageArray = BitmapConverter.Image2NdArray <Real>(resultImage);
                    int            dataSize   = imageArray.Shape[1] * imageArray.Shape[2];
                    for (int ch = 0; ch < imageArray.Shape[0]; ch++)
                    {
                        for (int i = 0; i < dataSize; i++)
                        {
                            imageArray.Data[ch * dataSize + i] = (imageArray.Data[ch * dataSize + i] - mean[ch]) / std[ch];
                        }
                    }

                    Console.WriteLine("Start predict.");
                    Stopwatch      sw     = Stopwatch.StartNew();
                    NdArray <Real> result = nn.Predict(imageArray)[0];
                    sw.Stop();

                    Console.WriteLine("Result Time : " + (sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L))).ToString("n0") + "μs");

                    int maxIndex = Array.IndexOf(result.Data, result.Data.Max());

                    Console.WriteLine("[" + result.Data[maxIndex] + "] : " + classList[maxIndex]);
                } while (ofd.ShowDialog() == DialogResult.OK);
            }
        }