Updates to DelphinID

This commit is contained in:
Jamie Mac 2024-05-10 08:50:12 +01:00
parent 61033b38ca
commit 822832f179
7 changed files with 195 additions and 35 deletions

View File

@ -6,7 +6,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/Amazon Coretto 21"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk-21.0.2.13-hotspot">
<attributes> <attributes>
<attribute name="module" value="true"/> <attribute name="module" value="true"/>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

View File

@ -310,7 +310,7 @@
<dependency> <dependency>
<groupId>io.github.macster110</groupId> <groupId>io.github.macster110</groupId>
<artifactId>jdl4pam</artifactId> <artifactId>jdl4pam</artifactId>
<version>0.0.98</version> <version>0.0.99</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/gov.nist.math/jama --> <!-- https://mvnrepository.com/artifact/gov.nist.math/jama -->

View File

@ -228,7 +228,7 @@ public class DelphinIDTest {
String whistleContourPath = "D:/Dropbox/PAMGuard_dev/Deep_Learning/delphinID/testencounter415/whistle_contours.mat"; String whistleContourPath = "D:/Dropbox/PAMGuard_dev/Deep_Learning/delphinID/testencounter415/whistle_contours.mat";
//the path to the model //the path to the model
String modelPath = "D:/Dropbox/PAMGuard_dev/Deep_Learning/delphinID/testencounter415/whistle_4s_encounter415.zip"; String modelPath = "D:/Dropbox/PAMGuard_dev/Deep_Learning/delphinID/testencounter415/whistle_model_2/whistle_4s_415.zip";
//the path to the model //the path to the model
String matImageSave = "C:/Users/Jamie Macaulay/MATLAB Drive/MATLAB/PAMGUARD/deep_learning/delphinID/whistleimages.mat"; String matImageSave = "C:/Users/Jamie Macaulay/MATLAB Drive/MATLAB/PAMGUARD/deep_learning/delphinID/whistleimages.mat";
@ -240,7 +240,7 @@ public class DelphinIDTest {
ArrayList<AbstractWhistleDataUnit> whistleContours = getWhistleContoursMAT(whistleContourPath); ArrayList<AbstractWhistleDataUnit> whistleContours = getWhistleContoursMAT(whistleContourPath);
//segment the whistle detections //segment the whistle detections
ArrayList<SegmenterDetectionGroup> segments = segmentWhsitleData(whistleContours, dataStartMillis, ArrayList<SegmenterDetectionGroup> segments = segmentWhsitleData(whistleContours, (long) (dataStartMillis+(9.565*1000.)),
segLen, segHop); segLen, segHop);
for (int i=0; i<segments.size(); i++) { for (int i=0; i<segments.size(); i++) {
@ -267,7 +267,7 @@ public class DelphinIDTest {
float[] output = predicition.get(0).getPrediction(); float[] output = predicition.get(0).getPrediction();
System.out.println(); System.out.println();
System.out.print("Segment: " +(aSegment.get(0).getSegmentStartMillis()-dataStartMillis)/1000.); System.out.print("Segment: " + i + " " + (aSegment.get(0).getSegmentStartMillis()-dataStartMillis)/1000. + "s ");
for (int j=0; j<output.length; j++) { for (int j=0; j<output.length; j++) {
System.out.print(" " + output[j]); System.out.print(" " + output[j]);
} }

View File

@ -12,6 +12,7 @@ import org.jamdev.jdl4pam.transforms.DLTransform.DLTransformType;
import org.jamdev.jdl4pam.transforms.jsonfile.DLTransformsParser; import org.jamdev.jdl4pam.transforms.jsonfile.DLTransformsParser;
import org.jamdev.jdl4pam.utils.DLMatFile; import org.jamdev.jdl4pam.utils.DLMatFile;
import org.jamdev.jdl4pam.utils.DLUtils; import org.jamdev.jdl4pam.utils.DLUtils;
import org.jamdev.jpamutils.JamArr;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -59,6 +60,7 @@ public class DelphinIDWorker extends ArchiveModelWorker {
for (int i=0; i<dlParams.numClasses; i++) { for (int i=0; i<dlParams.numClasses; i++) {
dlParams.binaryClassification[i]=true; dlParams.binaryClassification[i]=true;
} }
} }
@ -94,6 +96,8 @@ public class DelphinIDWorker extends ArchiveModelWorker {
} }
} }
//something has gone wrong if we get here. //something has gone wrong if we get here.
return null; return null;
} }
@ -153,6 +157,8 @@ public class DelphinIDWorker extends ArchiveModelWorker {
float[][][] transformedDataStack = new float[numChunks][][]; float[][][] transformedDataStack = new float[numChunks][][];
double[][] transformedData2; //spectrogram data double[][] transformedData2; //spectrogram data
for (int j=0; j<numChunks; j++) { for (int j=0; j<numChunks; j++) {
// System.out.println("Number of whistle to process: " + whistleGroups.get(j).getStartSecond() + "s " + whistleGroups.get(j).getSubDetectionsCount() + " " + whistleGroups.get(j).getSegmentStartMillis()); // System.out.println("Number of whistle to process: " + whistleGroups.get(j).getStartSecond() + "s " + whistleGroups.get(j).getSubDetectionsCount() + " " + whistleGroups.get(j).getSegmentStartMillis());
@ -161,7 +167,7 @@ public class DelphinIDWorker extends ArchiveModelWorker {
Whistles2Image whistles2Image = new Whistles2Image(whistleGroups.get(j), whistleImageParams); Whistles2Image whistles2Image = new Whistles2Image(whistleGroups.get(j), whistleImageParams);
//set the spec transform //set the spec transform
((FreqTransform) this.getModelTransforms().get(0)).setSpecTransfrom(whistles2Image.getSpecTransfrom()); ((FreqTransform) modelTransforms.get(0)).setSpecTransfrom(whistles2Image.getSpecTransfrom());
//process all the transforms. //process all the transforms.
DLTransform transform = modelTransforms.get(0); DLTransform transform = modelTransforms.get(0);
@ -170,6 +176,11 @@ public class DelphinIDWorker extends ArchiveModelWorker {
} }
transformedData2 = ((FreqTransform) transform).getSpecTransfrom().getTransformedData(); transformedData2 = ((FreqTransform) transform).getSpecTransfrom().getTransformedData();
//a bit ugly but works.
transformedData2 = JamArr.transposeMatrix(transformedData2);
// System.out.println("DelphinID input image: " + transformedData2.length + " x " + transformedData2[0].length );
transformedDataStack[j] = DLUtils.toFloatArray(transformedData2); transformedDataStack[j] = DLUtils.toFloatArray(transformedData2);
// //TEMP // //TEMP

View File

@ -65,7 +65,7 @@ public class Whistles2Image extends FreqTransform {
ArrayList<double[][]> points = whistContours2Points(whistleGroup); ArrayList<double[][]> points = whistContours2Points(whistleGroup);
//does not work becaue it has to be on the AWT thread. //does not work becaue it has to be on the AWT thread.
BufferedImage canvas = makeScatterImage(points, size, new double[]{0, whistleGroup.getSegmentDuration()/1000.}, freqLimits, 5.); BufferedImage canvas = makeScatterImage(points, size, new double[]{0, whistleGroup.getSegmentDuration()/1000.}, freqLimits, 10.);
double[][] imaged = new double[(int) size[0]][(int) size[1]]; double[][] imaged = new double[(int) size[0]][(int) size[1]];

View File

@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.Raster; import java.awt.image.Raster;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -14,8 +15,15 @@ import org.jamdev.jdl4pam.transforms.FreqTransform;
import org.jamdev.jdl4pam.transforms.DLTransform.DLTransformType; import org.jamdev.jdl4pam.transforms.DLTransform.DLTransformType;
import org.jamdev.jdl4pam.utils.DLMatFile; import org.jamdev.jdl4pam.utils.DLMatFile;
import org.jamdev.jdl4pam.utils.DLUtils; import org.jamdev.jdl4pam.utils.DLUtils;
import org.jamdev.jpamutils.JamArr;
import org.jamdev.jpamutils.interpolation.Bicubic;
import org.jamdev.jpamutils.interpolation.Bilinear;
import org.jamdev.jpamutils.interpolation.NearestNeighbor;
import org.jamdev.jpamutils.spectrogram.SpecTransform;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import ai.djl.MalformedModelException;
import rawDeepLearningClassifier.dlClassification.archiveModel.SimpleArchiveModel;
import rawDeepLearningClassifier.dlClassification.delphinID.Whistles2Image; import rawDeepLearningClassifier.dlClassification.delphinID.Whistles2Image;
import us.hebi.matlab.mat.format.Mat5; import us.hebi.matlab.mat.format.Mat5;
import us.hebi.matlab.mat.types.MatFile; import us.hebi.matlab.mat.types.MatFile;
@ -24,6 +32,55 @@ import us.hebi.matlab.mat.types.Matrix;
public class DelphinIDTest { public class DelphinIDTest {
private static double[][] whistleScatter2Image(double[][] whistleValues) {
//now perform the image transform in Java
double[] freqLimits = new double[] {2000., 20000.};
double[] size = new double[] {680., 480.};
ArrayList<double[][]> whistleImageArr = new ArrayList<double[][]>();
whistleImageArr.add(whistleValues);
BufferedImage canvas = Whistles2Image.makeScatterImage(whistleImageArr, size, new double[]{50, 50. + 4.}, freqLimits, 6.);
double[][] imaged = new double[(int) size[0]][(int) size[1]];
float[] color = new float[3];
Raster raster = canvas.getData();
for (int i=0; i<imaged.length; i++) {
for (int j=0; j<imaged[0].length; j++) {
color = raster.getPixel(i,j, color);
imaged[i][j] = (255-color[0])/255.; //normalize
}
}
//create the model transforms
ArrayList<DLTransform> modelTransforms = new ArrayList<DLTransform>();
modelTransforms.add(new FreqTransform(DLTransformType.SPECFLIP));
// modelTransforms.add(new FreqTransform(DLTransformType.SPECNORMALISE_MINIMAX));
modelTransforms.add(new FreqTransform(DLTransformType.SPECRESIZE, new Number[] {Integer.valueOf(60), Integer.valueOf(80), SpecTransform.RESIZE_BICUBIC}));
modelTransforms.add(new FreqTransform(DLTransformType.GAUSSIAN_FILTER, new Number[] {Double.valueOf(0.5)}));
SpecTransform specTransform = new SpecTransform();
specTransform.setSpecData(imaged);
specTransform.setSampleRate((float) (freqLimits[1]*2));
//set the spec transform
((FreqTransform) modelTransforms.get(0)).setSpecTransfrom(specTransform);
//process all the transforms.
DLTransform transform = modelTransforms.get(0);
for (int i =0; i<modelTransforms.size(); i++) {
transform = modelTransforms.get(i).transformData(transform);
}
double[][] transformedData2 = ((FreqTransform) transform).getSpecTransfrom().getTransformedData();
return transformedData2;
}
@Test @Test
public void whistle2ImageTest() { public void whistle2ImageTest() {
@ -44,22 +101,22 @@ public class DelphinIDTest {
//the values for the whistle detector. //the values for the whistle detector.
double[][] whistleValues = DLMatFile.matrix2array(array); double[][] whistleValues = DLMatFile.matrix2array(array);
//the image after compression // //the image after compression
array = matFile.getArray("image1compressedgrayscale"); // array = matFile.getArray("image1compressedgrayscale");
double[][] compressedWhistleImage = DLMatFile.matrix2array(array); // double[][] compressedWhistleImage = DLMatFile.matrix2array(array);
//
//the whistle2Image transform image // //the whistle2Image transform image
array = matFile.getArray("image1originalgrayscalenorm"); // array = matFile.getArray("image1originalgrayscalenorm");
double[][] whislteImage = DLMatFile.matrix2array(array); // double[][] whislteImage = DLMatFile.matrix2array(array);
//now perform the image transform in Java //now perform the image transform in Java
double[] freqLimits = new double[] {0., 20000.}; double[] freqLimits = new double[] {2000., 20000.};
double[] size = new double[] {680., 480.}; double[] size = new double[] {680., 480.};
ArrayList<double[][]> whistleImageArr = new ArrayList<double[][]>(); ArrayList<double[][]> whistleImageArr = new ArrayList<double[][]>();
whistleImageArr.add(whistleValues); whistleImageArr.add(whistleValues);
BufferedImage canvas = Whistles2Image.makeScatterImage(whistleImageArr, size, new double[]{48, 48. + 4.}, freqLimits, 5.); BufferedImage canvas = Whistles2Image.makeScatterImage(whistleImageArr, size, new double[]{50, 50. + 4.}, freqLimits, 10.);
double[][] imaged = new double[(int) size[0]][(int) size[1]]; double[][] imaged = new double[(int) size[0]][(int) size[1]];
@ -67,33 +124,53 @@ public class DelphinIDTest {
Raster raster = canvas.getData(); Raster raster = canvas.getData();
for (int i=0; i<imaged.length; i++) { for (int i=0; i<imaged.length; i++) {
for (int j=0; j<imaged[0].length; j++) { for (int j=0; j<imaged[0].length; j++) {
color = raster.getPixel(i, j, color); color = raster.getPixel(i,j, color);
imaged[i][j] = (255-color[0])/255.; //normalize imaged[i][j] = (255-color[0])/255.; //normalize
} }
} }
ArrayList<DLTransform> transforms = new ArrayList<DLTransform>(); //create the model transforms
transforms.add(new FreqTransform(DLTransformType.SPECRESIZE, new Number[] {Integer.valueOf(64), Integer.valueOf(48)})); ArrayList<DLTransform> modelTransforms = new ArrayList<DLTransform>();
modelTransforms.add(new FreqTransform(DLTransformType.SPECFLIP));
// modelTransforms.add(new FreqTransform(DLTransformType.SPECNORMALISE_MINIMAX));
modelTransforms.add(new FreqTransform(DLTransformType.SPECRESIZE, new Number[] {Integer.valueOf(60), Integer.valueOf(80), SpecTransform.RESIZE_BICUBIC}));
modelTransforms.add(new FreqTransform(DLTransformType.GAUSSIAN_FILTER, new Number[] {Double.valueOf(0.5)}));
//
// //set the spec transform
// ((FreqTransform) transforms.get(0)).setSpecTransfrom(whistles2Image.getSpecTransfrom());
//
// //process all the transforms.
// DLTransform transform = modelTransforms.get(0);
// for (int i =0; i<modelTransforms.size(); i++) {
// transform = modelTransforms.get(i).transformData(transform);
// }
//
// transformedData2 = ((FreqTransform) transform).getSpecTransfrom().getTransformedData();
// transformedDataStack[j] = DLUtils.toFloatArray(transformedData2);
//
SpecTransform specTransform = new SpecTransform();
specTransform.setSpecData(imaged);
specTransform.setSampleRate((float) (freqLimits[1]*2));
//set the spec transform
((FreqTransform) modelTransforms.get(0)).setSpecTransfrom(specTransform);
//process all the transforms.
DLTransform transform = modelTransforms.get(0);
for (int i =0; i<modelTransforms.size(); i++) {
transform = modelTransforms.get(i).transformData(transform);
}
double[][] transformedData2 = ((FreqTransform) transform).getSpecTransfrom().getTransformedData();
// Bilinear interpolation1 = new Bilinear(JamArr.doubleToFloat(transformedData2));
// Bicubic interpolation2 = new Bicubic(JamArr.doubleToFloat(imaged));
//
// System.out.println("Len input: " + imaged.length);
//
// float[][] resizeArr = interpolation2.resize(Integer.valueOf(80), Integer.valueOf(60));
//
// System.out.println("Len resize: " + resizeArr.length);
System.out.println("Size Java: " + transformedData2.length + " x " + transformedData2[0].length);
//now save this image to a MATFILE //now save this image to a MATFILE
// Create MAT file with a scalar in a nested struct // Create MAT file with a scalar in a nested struct
MatFile matFileWrite = Mat5.newMatFile() MatFile matFileWrite = Mat5.newMatFile()
.addArray("image1originalgrayscalenorm",DLMatFile.array2Matrix(imaged)); .addArray("image1originalgrayscalenorm",DLMatFile.array2Matrix(imaged))
.addArray("imagecompressedgrayscalenorm",DLMatFile.array2Matrix(transformedData2));
// .addArray("imagecompressedgrayscalenorm_nearest",DLMatFile.array2Matrix(JamArr.floatToDouble(resizeArr)));
// Serialize to disk using default configurations // Serialize to disk using default configurations
Mat5.writeToFile(matFileWrite, "C:\\Users\\Jamie Macaulay\\MATLAB Drive\\MATLAB\\PAMGUARD\\deep_learning\\delphinID\\whistle_image_example_java.mat"); Mat5.writeToFile(matFileWrite, "C:\\Users\\Jamie Macaulay\\MATLAB Drive\\MATLAB\\PAMGUARD\\deep_learning\\delphinID\\whistle_image_example_java.mat");
@ -105,9 +182,81 @@ public class DelphinIDTest {
assertEquals(false, false); assertEquals(false, false);
} }
}
@Test
public void modelInputTest() {
System.out.println("DelphinID mode test start");
//ttest the model
String modelPath = "D:/Dropbox/PAMGuard_dev/Deep_Learning/delphinID/testencounter415/whistle_model_2/whistle_4s_415.zip";
String relMatPath = "./src/test/resources/rawDeepLearningClassifier/DelphinID/whistle_image_example.mat";
try {
//load the model
SimpleArchiveModel model = new SimpleArchiveModel(new File(modelPath));
Path path = Paths.get(relMatPath);
// Create MAT file with a scalar in a nested struct
MatFile matFile = Mat5.readFromFile(path.toString());
Matrix array = matFile.getArray("tfvalues");
//the values for the whistle detector.
double[][] whistleValues = DLMatFile.matrix2array(array);
//the image after compression
array = matFile.getArray("image1compressedgrayscalenorm");
double[][] compressedWhistleImage = DLMatFile.matrix2array(array);
//the raw whistle frequency time scatter values
array = matFile.getArray("tfvalues");
//the values for the whistle detector.
double[][] pamguardWhistleImage = whistleScatter2Image(whistleValues);
System.out.println("Size python: " + compressedWhistleImage.length + " x " + compressedWhistleImage[0].length);
float[][][] input = new float[1][][];
input[0] = JamArr.doubleToFloat(compressedWhistleImage);
System.out.println("Model output: ");
float[] outputPython = model.runModel(input);
input[0] = JamArr.doubleToFloat(JamArr.transposeMatrix(pamguardWhistleImage));
System.out.println("Size Java: " + input[0].length + " x " + input[0][0].length);
//a bit ugly but works.
// transformedData2 = JamArr.transposeMatrix(transformedData2);
float[] outputJava = model.runModel(input);
for (int i=0; i<outputPython.length; i++) {
System.out.println(String.format("Output Python: %.4f Java: %.4f",outputPython[i],outputJava[i] ));
}
MatFile matFileWrite = Mat5.newMatFile()
.addArray("imagePython",DLMatFile.array2Matrix(compressedWhistleImage))
.addArray("imageJava",DLMatFile.array2Matrix(JamArr.transposeMatrix(pamguardWhistleImage)));
Mat5.writeToFile(matFileWrite, "C:\\Users\\Jamie Macaulay\\MATLAB Drive\\MATLAB\\PAMGUARD\\deep_learning\\delphinID\\whistle_image_inout_test.mat");
// JamArr.printArray(output);
} catch (MalformedModelException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("DelphinID mode test end");
} }
} }