/// <summary> /// Compute hashes using neural network ensemble /// </summary> private void ComputeHashesUsingNeuralHasher() { string connectionString = null; _cmbValidationConnectionStringNeuralHasher.Invoke(new Action(() => { connectionString = _cmbValidationConnectionStringNeuralHasher.SelectedItem.ToString(); })); FingerprintDescriptor descriptor = new FingerprintDescriptor(); if (String.IsNullOrEmpty(connectionString)) { MessageBox.Show(Resources.SelectValidationDB); return; } string path = _tbStoredEnsembleFilename.Text; if (String.IsNullOrEmpty(path)) { MessageBox.Show(Resources.SelectPathToSerializedEnsemble, Resources.SelectFile, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } NeuralNetworkEnsemble ensemble = NeuralNetworkEnsemble.Load(path); /*Load the serialized ensemble used to create hashes*/ IList<Track> tracks = modelService.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]; IList<Fingerprint> fingerprints; try { fingerprints = modelService.ReadFingerprintsByTrackId(track.Id, 0); if (fingerprints == null) continue; } catch (Exception ex) { MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (Fingerprint fingerprint in fingerprints) /*For each track's fingerprint create hash*/ { ensemble.ComputeHash(descriptor.DecodeFingerprint(fingerprint.Signature)); long[] hashbins = ensemble.ExtractHashBins(); /*Extract hash bin / hash table*/ } // modelService.InsertHashBin(listToInsert); _pbProgress.Invoke(new Action(() => _pbProgress.PerformStep())); } }
///<summary> /// Gets tracks/fingerprints from the database ///</summary> ///<param name = "function">Activation function</param> ///<param name = "fingerprintsPerTrack">Fingerprints per track</param> ///<param name = "outputs">Number of outputs from the neural network</param> ///<returns>Dictionary (Int32 - track id, List - list of corresponding fingerprints</returns> public Dictionary<Int32, List<BasicMLData>> GetNormalizedTrackFingerprints(IActivationFunction function, int fingerprintsPerTrack, int outputs) { IList<Track> tracks = modelService.ReadTracks(); IDictionary<int, IList<Fingerprint>> unnormalized = modelService.ReadFingerprintsByMultipleTrackId(tracks, fingerprintsPerTrack); Dictionary<Int32, List<BasicMLData>> retVal = new Dictionary<Int32, List<BasicMLData>>(); int neededTracks = (int) Math.Pow(2, outputs); int count = 0; FingerprintDescriptor descriptor = new FingerprintDescriptor(); foreach (var pair in unnormalized) { retVal.Add(pair.Key, new List<BasicMLData>()); foreach (Fingerprint fingerprint in pair.Value) retVal[pair.Key].Add(new BasicMLData(NormalizeUtils.NormalizeDesiredInputInPlace(function, descriptor.DecodeFingerprint(fingerprint.Signature)))); count++; if (count > neededTracks - 1) break; } return retVal; }