Skip to content

Commit

Permalink
amended patch sampling approach in semi-supervised clustering process
Browse files Browse the repository at this point in the history
  • Loading branch information
mkholghi committed Jan 8, 2019
1 parent 8f9dac6 commit 44ac920
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 23 deletions.
20 changes: 11 additions & 9 deletions src/AnalysisPrograms/MahnooshSandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,22 +147,23 @@ public override Task<int> Execute(CommandLineApplication app)
//instance.Execute(this);
//GenerateSpectrograms();
//ExtractClusteringFeatures();
BuildSemisupervisedClusters();
BuildSemisupervisedClusteringFeatures();

return this.Ok();
}
}

// output is the cluster centroids that obtained from a semi-supervised feature learning approach.
public static void BuildSemisupervisedClusters()
public static void BuildSemisupervisedClusteringFeatures()
{
LoggedConsole.WriteLine("semi-supervised clustering process...");
var inputDir = @"M:\Postdoc\Liz\Least Bittern\"; //@"D:\Mahnoosh\Liz\Least_Bittern\"; // @"D:\Mahnoosh\Liz\"; //@"C:\Users\kholghim\Mahnoosh\Liz\"; // @"C:\Users\kholghim\Mahnoosh\UnsupervisedFeatureLearning\"; //
var resultDir = Path.Combine(inputDir, "SemisupervisedClusters");
var inputPath = Path.Combine(inputDir, "TrainSet\\one_min_recordings-samples"); //one_min_recordings //PatchSamplingSegments
LoggedConsole.WriteLine("semi-supervised feature learning process...");
var inputDir = @"D:\Mahnoosh\Liz\Least_Bittern\"; //@"M:\Postdoc\Liz\Least Bittern\"; // @"D:\Mahnoosh\Liz\"; //@"C:\Users\kholghim\Mahnoosh\Liz\"; // @"C:\Users\kholghim\Mahnoosh\UnsupervisedFeatureLearning\"; //
var resultDir = Path.Combine(inputDir, "SemisupervisedClusteringFeatures");
var inputPath = Path.Combine(inputDir, "TrainSet\\one_min_recordings"); //one_min_recordings-samples //PatchSamplingSegments

// the infoFile contains the info about the frames of interest for supervised feature learning.
var frameInfoFilePath = @"M:\Postdoc\Liz\Least Bittern\manual-cluster\positive_frames.csv";//Path.Combine(inputDir, "TrainSet\\train_data");
var configPath = @"M:\Postdoc\Liz\Least Bittern\FeatureLearningConfig.yml"; // @"D:\Mahnoosh\Liz\Least_Bittern\FeatureLearningConfig.yml";
var frameInfoFilePath = @"D:\Mahnoosh\Liz\Least_Bittern\TrainSet\positive_frames.csv"; //@"M:\Postdoc\Liz\Least Bittern\manual-cluster\positive_frames.csv";//Path.Combine(inputDir, "TrainSet\\train_data");
var configPath = @"D:\Mahnoosh\Liz\Least_Bittern\FeatureLearningConfig.yml"; //@"M:\Postdoc\Liz\Least Bittern\FeatureLearningConfig.yml"; //

var configFile = configPath.ToFileInfo();

Expand Down Expand Up @@ -227,7 +228,6 @@ public static void BuildSemisupervisedClusters()
}

allBandsCentroids.Add(centroids);
//allClusteringOutput.Add(clusteringOutput.Clusters);

List<double[,]> allCentroids = new List<double[,]>();
for (int k = 0; k < centroids.Length; k++)
Expand Down Expand Up @@ -263,6 +263,8 @@ public static void BuildSemisupervisedClusters()
clusterImage.Save(outputClusteringImage);
}

// extracting features
FeatureExtraction.UnsupervisedFeatureExtraction(configuration, allBandsCentroids, inputPath, resultDir);
LoggedConsole.WriteLine("Done...");
}

Expand Down
18 changes: 8 additions & 10 deletions src/AudioAnalysisTools/DSP/FeatureExtraction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,6 @@ public static void UnsupervisedFeatureExtraction(FeatureLearningSettings config,
var allStds = new List<double[][,]>();
var allSkewness = new List<double[][,]>();


// looping over freq bands
for (int i = 0; i < meanFeatures[0].Count; i++)
{
Expand Down Expand Up @@ -518,7 +517,7 @@ public static void UnsupervisedFeatureExtraction(FeatureLearningSettings config,
// creating the header for CSV file
List<string> header = new List<string>();
header.Add("file name");
/*

for (int j = 0; j < allMins.ToArray()[i][0].GetLength(1); j++)
{
header.Add("min" + j.ToString());
Expand All @@ -533,17 +532,17 @@ public static void UnsupervisedFeatureExtraction(FeatureLearningSettings config,
{
header.Add("max" + j.ToString());
}
*/

for (int j = 0; j < allStds.ToArray()[i][0].GetLength(1); j++)
{
header.Add("std" + j.ToString());
}
/*

for (int j = 0; j < allSkewness.ToArray()[i][0].GetLength(1); j++)
{
header.Add("skewness" + j.ToString());
}
*/

var csv = new StringBuilder();
string content = string.Empty;
foreach (var entry in header.ToArray())
Expand All @@ -558,18 +557,17 @@ public static void UnsupervisedFeatureExtraction(FeatureLearningSettings config,
// looping over files
for (int j = 0; j < allMeans.ToArray()[i].GetLength(0); j++)
{

// concatenating mean, std, and max vector together for the pre-defined resolution
List<double[]> featureVectors = new List<double[]>();
for (int k = 0; k < allMeans.ToArray()[i][j].ToJagged().GetLength(0); k++)
{
List<double[]> featureList = new List<double[]>
{
//allMins.ToArray()[i][j].ToJagged()[k],
//allMeans.ToArray()[i][j].ToJagged()[k],
//allMaxs.ToArray()[i][j].ToJagged()[k],
allMins.ToArray()[i][j].ToJagged()[k],
allMeans.ToArray()[i][j].ToJagged()[k],
allMaxs.ToArray()[i][j].ToJagged()[k],
allStds.ToArray()[i][j].ToJagged()[k],
//allSkewness.ToArray()[i][j].ToJagged()[k],
allSkewness.ToArray()[i][j].ToJagged()[k],
};
double[] featureVector = DataTools.ConcatenateVectors(featureList);
featureVectors.Add(featureVector);
Expand Down
27 changes: 25 additions & 2 deletions src/AudioAnalysisTools/DSP/FeatureLearning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ public static class FeatureLearning

while (count < allNegativeFramesSubmatrices.Count)
{
// select random patches from those semgments that do not contain the call of interest
if (allPositiveFramesSubmatrices.Count != 0)
{
// downsampling the input matrix by a factor of n (MaxPoolingFactor) using max pooling
Expand All @@ -416,13 +417,35 @@ public static class FeatureLearning
(rows / patchHeight) * (columns / patchWidth),
PatchSampling.SamplingMethod.Sequential).ToMatrix());
}
else
{
// downsampling the input matrix by a factor of n (MaxPoolingFactor) using max pooling
double[,] downsampledNegativeMatrix = MaxPooling(allNegativeFramesSubmatrices.ToArray()[count], config.MaxPoolingFactor);
randomPatchLists[$"randomPatch{count.ToString()}"].Add(PatchSampling
.GetPatches(downsampledNegativeMatrix, patchWidth, patchHeight, numRandomPatches,
PatchSampling.SamplingMethod.Random).ToMatrix());
}

/*
We can use this block of code instead of line 422 to 426, of we want to select random patches from negative grames of the segments with call of interest
// downsampling the input matrix by a factor of n (MaxPoolingFactor) using max pooling
double[,] downsampledNegativeMatrix = MaxPooling(allNegativeFramesSubmatrices.ToArray()[count], config.MaxPoolingFactor);

randomPatchLists[$"randomPatch{count.ToString()}"].Add(PatchSampling
if (downsampledNegativeMatrix.GetLength(0) < numRandomPatches)
{
int numR = downsampledNegativeMatrix.GetLength(0);
int numC = downsampledNegativeMatrix.GetLength(1);
randomPatchLists[$"randomPatch{count.ToString()}"].Add(PatchSampling
.GetPatches(downsampledNegativeMatrix, patchWidth, patchHeight,
(numR / patchHeight) * (numC / patchWidth),
PatchSampling.SamplingMethod.Sequential).ToMatrix());
}
else
{
randomPatchLists[$"randomPatch{count.ToString()}"].Add(PatchSampling
.GetPatches(downsampledNegativeMatrix, patchWidth, patchHeight, numRandomPatches,
PatchSampling.SamplingMethod.Random).ToMatrix());
}
*/

count++;
}
Expand Down
2 changes: 0 additions & 2 deletions src/AudioAnalysisTools/DSP/PatchSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace AudioAnalysisTools.DSP
using Accord.Math;
using TowseyLibrary;
using WavTools;
using YamlDotNet.Core.Tokens;

public static class PatchSampling
{
Expand Down Expand Up @@ -299,7 +298,6 @@ public static List<double[,]> GetFreqBandMatrices(double[,] matrix, int numberOf
int minY = matrix.GetLength(1);

// copying the original matrix to a new matrix (row by row)

for (int i = 0; i < minX; ++i)
{
Array.Copy(matrix, i * matrix.GetLength(1), newMatrix, i * matrix.GetLength(1), minY);
Expand Down

0 comments on commit 44ac920

Please sign in to comment.