Exemplo n.º 1
0
        public static string Classification(string path)
        {
            string rv = "";

            string imagePath   = Path.GetFullPath(path);
            string modelPath   = Path.GetFullPath("Diyari_IQD.h5");
            string weightsPath = Path.GetFullPath("Diyari_IQD_weights.h5");

            if (File.Exists(imagePath))
            {
                var     img = ImageUtil.LoadImg(path, target_size: new Shape(64, 64));
                NDarray x   = ImageUtil.ImageToArray(img);
                x = x.reshape(1, x.shape[0], x.shape[1], x.shape[2]);
                var model = Sequential.LoadModel(modelPath);
                model.LoadWeight(weightsPath);
                var y = model.Predict(x);
                y = y.argmax();
                int index = y.asscalar <int>();
                rv = labels[index];
            }
            else
            {
                throw (new Exception("No Image found at: " + imagePath));
            }

            return(rv);
        }
Exemplo n.º 2
0
        public static string Predict(string imagePath)
        {
            var     img = ImageUtil.LoadImg(imagePath, target_size: new Shape(32, 32));
            NDarray x   = ImageUtil.ImageToArray(img);

            x = x.reshape(1, x.shape[0], x.shape[1], x.shape[2]);
            var model = Sequential.LoadModel("model.h5");

            model.LoadWeight("weights.h5");
            var y = model.Predict(x);

            y = y.argmax();
            int index = y.asscalar <int>();

            return(labels[index]);
        }
Exemplo n.º 3
0
        public void Predict(string text, Accord.MachineLearning.TFIDF codebook, int max_news_len)
        {
            var    model  = Sequential.LoadModel("best_model_gru.h5");
            string result = "";

            string[] words   = TextUtil.TextToWordSequence(text);
            double[] tokens  = codebook.Transform(words);
            var      newItem = tokens.Where(value => value != 0).ToArray();
            NDarray  x       = np.array(newItem);

            x = x.reshape(1, x.shape[0]);
            x = SequenceUtil.PadSequences(x, maxlen: max_news_len, dtype: "double");

            var y = model.Predict(x);

            Console.WriteLine(y.str);
        }
Exemplo n.º 4
0
        public static void Predict(string text)
        {
            var    model  = Sequential.LoadModel("model.h5");
            string result = "";

            var indexes = IMDB.GetWordIndex();

            string[] words  = TextUtil.TextToWordSequence(text);
            float[]  tokens = words.Select(i => ((float)indexes[i])).ToArray();

            NDarray x = np.array(tokens);

            x = x.reshape(1, x.shape[0]);
            x = SequenceUtil.PadSequences(x, maxlen: 500);
            var y      = model.Predict(x);
            var binary = Math.Round(y[0].asscalar <float>());

            result = binary == 0 ? "Negative" : "Positive";
            Console.WriteLine("Sentiment for \"{0}\": {1}", text, result);
        }
Exemplo n.º 5
0
        public static void Predict(string text)
        {
            var    model  = Sequential.LoadModel("model.h5");
            string result = "";

            float[,] tokens = new float[1, max_words];
            string[] words = TextUtil.TextToWordSequence(text).Take(max_words).ToArray();
            for (int i = 0; i < words.Length; i++)
            {
                tokens[0, i] = indexesByFrequency.ContainsKey(words[i])
                    ? (float)indexesByFrequency[words[i]] : 0f;
            }

            NDarray x      = np.array(tokens);
            var     y      = model.Predict(x);
            var     binary = Math.Round(y[0].asscalar <float>());

            result = binary == 0 ? "Норм, не токсично" : "ТОКСИЧНО";
            Console.WriteLine($"Результат для \"{text}\": {result}, оценка: {y[0].asscalar<float>()}");
        }