コード例 #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
ファイル: 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);
            }
        }