/// <summary>
        ///   Start button clicked. Assembling started.
        /// </summary>
        private void BtnStartClick(object sender, EventArgs e)
        {
            Invoke(new Action(() =>
                              {
                                  _btnStart.Enabled = false;
                                  _btnInsert.Enabled = false;
                                  _nudNumberOfGroupsNeuralHasher.Enabled = false;
                                  _nudNumberOfHashesPerKeyNeuralHasher.Enabled = false;
                                  _btnSaveEnsamble.Enabled = false;
                                  _tbSaveToEnsembleFilename.ReadOnly = true;
                              }));

            /*Ensemble*/
            Action action = () =>
                            {
                                _dalManager.SetConnectionString(_connectionStringNeuralHasher);
                                List<Network> networks = new List<Network>();
                                /*Load all the networks*/
                                foreach (KeyValuePair<string, bool> item in _dictionaryPathToNetworks)
                                {
                                    if (item.Value)
                                    {
                                        networks.Add((Network) SerializeObject.Load(item.Key));
                                        if (networks[networks.Count - 1].MedianResponces == null)
                                            networks.RemoveAt(networks.Count - 1);
                                    }
                                }
                                if ((networks.Count*10 /*Number of network outputs*/) < (_numberofgroupsneuralhasher*_numberofhashesperkeyneuralhasher))
                                {
                                    MessageBox.Show(string.Format("Not enough networks to create an ensemble of such size {0} {1}", _numberofgroupsneuralhasher, _numberofhashesperkeyneuralhasher));
                                    return;
                                }

                                /*Load a network trainer*/
                                IActivationFunction function = networks[0].GetActivation(0);
                                NetTrainer netTrainer = new NetTrainer(_dalManager);
                                double[][] inputs = null, outputs = null; /*Normalized input/output pairs*/
                                Dictionary<Int32, List<BasicMLData>> trackIdFingerprints = netTrainer.GetNormalizedTrackFingerprints(function, 10, 10);
                                double[][] binaryCodes = netTrainer.GetNormalizedBinaryCodes(function, 10);
                                Tuple<double[][], double[][]> tuple = netTrainer.FillStandardInputsOutputs(trackIdFingerprints, binaryCodes);
                                inputs = tuple.Item1;
                                outputs = tuple.Item2;

                                /*Construct outputs using median response*/
                                int samplesCount = inputs.Length; /*10240*/
                                NNEnsemble nNEnsembe = new NNEnsemble(networks.ToArray()); /*40 networks*/
                                for (int i = 0; i < samplesCount /*1024 * 10*/; i++)
                                {
                                    byte[] outputVec = nNEnsembe.ComputeHash(inputs[i]); /*Hash the inputs, returns 10*40 = 400*/
                                    outputs[i] = new double[outputVec.Length]; /*400*/
                                    for (int j = 0; j < outputVec.Length; j++)
                                    {
                                        outputs[i][j] = outputVec[j]; /*10240x400 matrix*/
                                    }
                                }

                                /*At this point we have a the 10240 hash vectors [0, 400] which represent the outputs for the actual input*/
                                /*Calculate minimal mutual information between those outputs*/
                                MinimalMutualInfoPattern mmiPattern = new MinimalMutualInfoPattern(_numberofgroupsneuralhasher, _numberofhashesperkeyneuralhasher);
                                mmiPattern.CreatePattern(outputs);
                                nNEnsembe.HashPattern = mmiPattern;
                                nNEnsembe.Save(_pathToEnsemble);
                            };

            action.BeginInvoke((result) =>
                               {
                                   /*Ensemble ended*/
                                   action.EndInvoke(result);
                                   Invoke(new Action(() =>
                                                     {
                                                         _btnStart.Enabled = true;
                                                         _btnInsert.Enabled = true;
                                                         _nudNumberOfGroupsNeuralHasher.Enabled = true;
                                                         _nudNumberOfHashesPerKeyNeuralHasher.Enabled = true;
                                                         _btnSaveEnsamble.Enabled = true;
                                                         _tbSaveToEnsembleFilename.ReadOnly = false;
                                                     }));
                                   MessageBox.Show(Resources.EnsemblingEnded, Resources.Success, MessageBoxButtons.OK, MessageBoxIcon.Information);
                               }, action);
        }
 /// <summary>
 ///   Public constructor for NN algorithm
 /// </summary>
 /// <param name = "connectionString">Connection string</param>
 /// <param name = "secondsToAnalyze">Number of fingerprints to analyze</param>
 /// <param name = "startSeconds">Starting seconds</param>
 /// <param name = "stride">Query stride</param>
 /// <param name = "topWavelets">Number of top wavelets</param>
 /// <param name = "fileList">File list to analyze</param>
 /// <param name = "pathToEnsemble">Path to ensemble</param>
 public WinQueryResults(string connectionString, int secondsToAnalyze, int startSeconds, IStride stride, int topWavelets, List<string> fileList, string pathToEnsemble)
     : this(connectionString, secondsToAnalyze, startSeconds, stride, topWavelets, fileList)
 {
     _ensemble = NNEnsemble.Load(pathToEnsemble);
     _dgvResults.Columns.Add(COL_HIT, "Number of hits");
     _dgvResults.Columns[COL_HIT].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
     Action action = ExtractCandidatesWithNeuralHasher;
     action.BeginInvoke((result) => action.EndInvoke(result), action);
 }
Exemple #3
0
        /// <summary>
        ///   Start inserting into the database
        /// </summary>
        private void BtnStartClick(object sender, EventArgs e)
        {
            string connectionString = _cmbDBFillerConnectionString.SelectedItem.ToString(); //Set Connection String
            try
            {
                _dalManager.SetConnectionString(connectionString); //Try Connection String
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                FadeAllControls(false);
                return;
            }
            if (!String.IsNullOrEmpty(_tbRootFolder.Text) || !String.IsNullOrEmpty(_tbSingleFile.Text) && _fileList == null)
            {
                _fileList = new List<string>();
                if (!String.IsNullOrEmpty(_tbRootFolder.Text))
                    TbRootFolderTextChanged(this, null);
                if (!String.IsNullOrEmpty(_tbSingleFile.Text))
                    TbSingleFileTextChanged(this, null);
            }
            if (_fileList == null || _fileList.Count == 0)
            {
                MessageBox.Show(Resources.FileListEmpty, Resources.FileListEmptyCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            FadeAllControls(true); //Fade all controls

            int rest = _fileList.Count%THREADS;
            int filesPerThread = _fileList.Count/THREADS;

            _listOfAllAlbums = _dalManager.ReadAlbums(); //Get all albums
            _unknownAlbum = _dalManager.ReadUnknownAlbum(); //Read unknown album
            int topWavelets = (int) _nudTopWav.Value;
            _fingerManager = new FingerprintManager {TopWavelets = topWavelets};
            switch (_hashAlgorithm)
            {
                case HashAlgorithm.LSH:
                    _hashTables = (int) _nudHashTables.Value; //If LSH is used # of Hash tables
                    _hashKeys = (int) _nudHashKeys.Value; //If LSH is used # of keys per table
                    break;
                case HashAlgorithm.NeuralHasher:
                    if (String.IsNullOrEmpty(_tbPathToEnsemble.Text)) //Check if the path to ensemble is specified
                    {
                        MessageBox.Show(Resources.SpecifyPathToNetworkEnsemble, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        FadeAllControls(false);
                        return;
                    }
                    try
                    {
                        _ensemble = NNEnsemble.Load(_tbPathToEnsemble.Text); //Load the ensemble
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        FadeAllControls(false);

                        return;
                    }
                    break;
                case HashAlgorithm.None:
                    break;
            }
            BeginInvoke(new Action(() => { }), null);

            ResetControls();
            int runningThreads = THREADS;
            for (int i = 0; i < THREADS; i++) //Start asynchronous operation
            {
                int start = i*filesPerThread; //Define start and end indexes
                int end = (i == THREADS - 1) ? i*filesPerThread + filesPerThread + rest : i*filesPerThread + filesPerThread;
                Action<int, int> action = InsertInDatabase;
                action.BeginInvoke(start, end,
                    (result) =>
                    {
                        //End Asynchronous operation
                        Action<int, int> item = (Action<int, int>) result.AsyncState;
                        item.EndInvoke(result);
                        Interlocked.Decrement(ref runningThreads);
                        if (runningThreads == 0)
                        {
                            /********* END OF INSERTION PROCESS HERE!********/

                            Invoke(new Action(() =>
                                              {
                                                  _pbTotalSongs.Visible = false;
                                                  FadeAllControls(false);
                                                  _tbRootFolder.Text = null;
                                                  _tbSingleFile.Text = null;
                                              }));
                            MessageBox.Show(Resources.InsertionEnded, Resources.End, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }, action);
            }
        }