public void HashBinNeuralHasherConstructorTest()
 {
     const int Id = 0;
     const long HashBin = 0;
     const int HashTable = 0;
     const int TrackId = -1;
     HashBinNeuralHasher target = new HashBinNeuralHasher(Id, HashBin, HashTable, TrackId);
     Assert.AreEqual(Id, target.Id);
     Assert.AreEqual(HashBin, target.Hashbin);
     Assert.AreEqual(HashTable, target.HashTable);
     Assert.AreEqual(TrackId, target.TrackId);
 }
        /// <summary>
        ///   Get a command to insert a HashBin created by Neural Hasher
        /// </summary>
        /// <param name = "hashBinNeuralHasher">Hash to be inserted</param>
        /// <param name = "connection">Connection used in insertion operation</param>
        /// <returns>Command to be executed</returns>
        public DbCommand GetInsertHashBinNeuralHasherCommand(HashBinNeuralHasher hashBinNeuralHasher, DbConnection connection)
        {
            DbCommand command = GetStoredProcedureCommand(SP_INSERT_NEURALHASHER_HASHBIN, connection);
            DbParameter param = command.CreateParameter();
            param.ParameterName = PARAM_HASHBIN_NEURALHASHER_HASHBIN;
            param.DbType = DbType.Int64;
            param.Value = hashBinNeuralHasher.Hashbin;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = PARAM_HASHBIN_NEURALHASHER_HASHTABLE;
            param.DbType = DbType.Int32;
            param.Value = hashBinNeuralHasher.HashTable;
            command.Parameters.Add(param);

            param = command.CreateParameter();
            param.ParameterName = PARAM_HASHBIN_NEURALHASHER_TRACK_ID;
            param.DbType = DbType.Int32;
            param.Value = hashBinNeuralHasher.TrackId;
            command.Parameters.Add(param);

            return command;
        }
Пример #3
0
        /// <summary>
        ///   Compute hashes using neural network ensemble
        /// </summary>
        private void ComputeHashesUsingNeuralHasher()
        {
            string connectionString = null;
            _cmbValidationConnectionStringNeuralHasher.Invoke(new Action(() => { connectionString = _cmbValidationConnectionStringNeuralHasher.SelectedItem.ToString(); }));

            if (String.IsNullOrEmpty(connectionString))
            {
                MessageBox.Show(Resources.SelectValidationDB);
                return;
            }

            _dalManager.SetConnectionString(connectionString);
            string path = _tbStoredEnsembleFilename.Text;
            if (String.IsNullOrEmpty(path))
            {
                MessageBox.Show(Resources.SelectPathToSerializedEnsemble, Resources.SelectFile, MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                return;
            }

            NNEnsemble ensemble = NNEnsemble.Load(path); /*Load the serialized ensemble used to create hashes*/
            List<Track> tracks = _dalManager.ReadTracks(); /*Read all tracks from the database for which the ensemble will create hashes*/
            _pbProgress.Invoke(new Action(() =>
                                          {
                                              _pbProgress.Minimum = 1;
                                              _pbProgress.Maximum = tracks.Count;
                                              _pbProgress.Value = 1;
                                              _pbProgress.Step = 1;
                                          }));

            /*Create hashes for each fingerprint in the database*/
            for (int index = 0; index < tracks.Count; index++)
            {
                Track track = tracks[index];
                List<Fingerprint> fingerprints;
                try
                {
                    fingerprints = _dalManager.ReadFingerprintsByTrackId(track.Id, 0);
                    if (fingerprints == null)
                        continue;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                List<HashBinNeuralHasher> listToInsert = new List<HashBinNeuralHasher>();
                foreach (Fingerprint fingerprint in fingerprints) /*For each track's fingerprint create hash*/
                {
                    ensemble.ComputeHash(FingerprintManager.DecodeFingerprint(fingerprint.Signature));
                    long[] hashbins = ensemble.ExtractHashBins(); /*Extract hash bin / hash table*/
                    for (int i = 0; i < hashbins.Length; i++)
                    {
                        HashBinNeuralHasher hash = new HashBinNeuralHasher(i, hashbins[i], i, track.Id);
                        listToInsert.Add(hash);
                    }
                }
                _dalManager.InsertHashBin(listToInsert);
                _pbProgress.Invoke(new Action(() => _pbProgress.PerformStep()));
            }
        }
        public void GetInsertHashBinNeuralHasherCommandTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;
            DaoStoredProcedureBuilder target = new DaoStoredProcedureBuilder();
            HashBinNeuralHasher hashBinNeuralHasher = new HashBinNeuralHasher(0, 45, 12, 0);

            DbConnection connection = Dbf.CreateConnection();
            IDbCommand actual = target.GetInsertHashBinNeuralHasherCommand(hashBinNeuralHasher, connection);

            Assert.AreEqual(SP_INSERT_NEURALHASHER_HASHBIN, actual.CommandText);
            Assert.AreEqual(CommandType.StoredProcedure, actual.CommandType);
            Assert.IsNotNull(actual.Connection);
            Assert.AreEqual(ConnectionState.Closed, actual.Connection.State);
            Assert.AreEqual(3, actual.Parameters.Count);
        }
Пример #5
0
 /// <summary>
 ///   Hash Fingerprints using Neural hasher component for solving k-nearest neighbor problem
 /// </summary>
 /// <param name = "listOfFingerprintsToHash">List of fingerprints already inserted in the database</param>
 /// <param name = "track">Track of the corresponding fingerprints</param>
 private void HashFingerprintsUsingNeuralHasher(IEnumerable<Fingerprint> listOfFingerprintsToHash, Track track)
 {
     List<HashBinNeuralHasher> listToInsert = new List<HashBinNeuralHasher>();
     foreach (Fingerprint fingerprint in listOfFingerprintsToHash)
     {
         _ensemble.ComputeHash(FingerprintManager.DecodeFingerprint(fingerprint.Signature));
         long[] hashbins = _ensemble.ExtractHashBins();
         for (int i = 0; i < hashbins.Length; i++)
         {
             HashBinNeuralHasher hash = new HashBinNeuralHasher(i, hashbins[i], i, track.Id);
             listToInsert.Add(hash);
         }
     }
     _dalManager.InsertHashBin(listToInsert);
 }
        public void InsertReadHashBinNeuralHashesTest()
        {
            string name = MethodBase.GetCurrentMethod().Name;
            Album album = dalManager.ReadUnknownAlbum();
            Track track = new Track(0, name, name, album.Id);
            dalManager.InsertTrack(track);
            Fingerprint finger = new Fingerprint(0, GenericFingerprint, track.Id, 0);
            dalManager.InsertFingerprint(finger);
            const long Hashbin = 100000;
            const int Hashtable = 20;
            List<HashBinNeuralHasher> list = new List<HashBinNeuralHasher>();
            const int Count = 20;
            for (int i = 0; i < Count; i++)
            {
                HashBinNeuralHasher hashbinminhash = new HashBinNeuralHasher(0, Hashbin, Hashtable, track.Id);
                list.Add(hashbinminhash);
            }

            dalManager.InsertHashBin(list);
            List<int> result = dalManager.ReadTrackIdByHashBinAndHashTableNeuralHasher(Hashbin, Hashtable);
            Assert.IsNotNull(result);
            Assert.AreEqual(Count, result.Count);
            dalManager.DeleteTrack(track.Id);
        }