mirror of
https://github.com/PAMGuard/PAMGuard.git
synced 2024-11-22 07:02:29 +00:00
Updates to DelphinID
This commit is contained in:
parent
61033b38ca
commit
822832f179
@ -6,7 +6,7 @@
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</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>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
2
pom.xml
2
pom.xml
@ -310,7 +310,7 @@
|
||||
<dependency>
|
||||
<groupId>io.github.macster110</groupId>
|
||||
<artifactId>jdl4pam</artifactId>
|
||||
<version>0.0.98</version>
|
||||
<version>0.0.99</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/gov.nist.math/jama -->
|
||||
|
@ -228,7 +228,7 @@ public class DelphinIDTest {
|
||||
String whistleContourPath = "D:/Dropbox/PAMGuard_dev/Deep_Learning/delphinID/testencounter415/whistle_contours.mat";
|
||||
|
||||
//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
|
||||
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);
|
||||
|
||||
//segment the whistle detections
|
||||
ArrayList<SegmenterDetectionGroup> segments = segmentWhsitleData(whistleContours, dataStartMillis,
|
||||
ArrayList<SegmenterDetectionGroup> segments = segmentWhsitleData(whistleContours, (long) (dataStartMillis+(9.565*1000.)),
|
||||
segLen, segHop);
|
||||
|
||||
for (int i=0; i<segments.size(); i++) {
|
||||
@ -267,7 +267,7 @@ public class DelphinIDTest {
|
||||
float[] output = predicition.get(0).getPrediction();
|
||||
|
||||
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++) {
|
||||
System.out.print(" " + output[j]);
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import org.jamdev.jdl4pam.transforms.DLTransform.DLTransformType;
|
||||
import org.jamdev.jdl4pam.transforms.jsonfile.DLTransformsParser;
|
||||
import org.jamdev.jdl4pam.utils.DLMatFile;
|
||||
import org.jamdev.jdl4pam.utils.DLUtils;
|
||||
import org.jamdev.jpamutils.JamArr;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@ -59,6 +60,7 @@ public class DelphinIDWorker extends ArchiveModelWorker {
|
||||
for (int i=0; i<dlParams.numClasses; i++) {
|
||||
dlParams.binaryClassification[i]=true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -94,6 +96,8 @@ public class DelphinIDWorker extends ArchiveModelWorker {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//something has gone wrong if we get here.
|
||||
return null;
|
||||
}
|
||||
@ -153,6 +157,8 @@ public class DelphinIDWorker extends ArchiveModelWorker {
|
||||
float[][][] transformedDataStack = new float[numChunks][][];
|
||||
|
||||
double[][] transformedData2; //spectrogram data
|
||||
|
||||
|
||||
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());
|
||||
@ -161,7 +167,7 @@ public class DelphinIDWorker extends ArchiveModelWorker {
|
||||
Whistles2Image whistles2Image = new Whistles2Image(whistleGroups.get(j), whistleImageParams);
|
||||
|
||||
//set the spec transform
|
||||
((FreqTransform) this.getModelTransforms().get(0)).setSpecTransfrom(whistles2Image.getSpecTransfrom());
|
||||
((FreqTransform) modelTransforms.get(0)).setSpecTransfrom(whistles2Image.getSpecTransfrom());
|
||||
|
||||
//process all the transforms.
|
||||
DLTransform transform = modelTransforms.get(0);
|
||||
@ -170,6 +176,11 @@ public class DelphinIDWorker extends ArchiveModelWorker {
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// //TEMP
|
||||
|
@ -65,7 +65,7 @@ public class Whistles2Image extends FreqTransform {
|
||||
ArrayList<double[][]> points = whistContours2Points(whistleGroup);
|
||||
|
||||
//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]];
|
||||
|
||||
|
@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.Raster;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
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.utils.DLMatFile;
|
||||
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 ai.djl.MalformedModelException;
|
||||
import rawDeepLearningClassifier.dlClassification.archiveModel.SimpleArchiveModel;
|
||||
import rawDeepLearningClassifier.dlClassification.delphinID.Whistles2Image;
|
||||
import us.hebi.matlab.mat.format.Mat5;
|
||||
import us.hebi.matlab.mat.types.MatFile;
|
||||
@ -24,6 +32,55 @@ import us.hebi.matlab.mat.types.Matrix;
|
||||
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
|
||||
public void whistle2ImageTest() {
|
||||
|
||||
@ -44,22 +101,22 @@ public class DelphinIDTest {
|
||||
//the values for the whistle detector.
|
||||
double[][] whistleValues = DLMatFile.matrix2array(array);
|
||||
|
||||
//the image after compression
|
||||
array = matFile.getArray("image1compressedgrayscale");
|
||||
double[][] compressedWhistleImage = DLMatFile.matrix2array(array);
|
||||
|
||||
//the whistle2Image transform image
|
||||
array = matFile.getArray("image1originalgrayscalenorm");
|
||||
double[][] whislteImage = DLMatFile.matrix2array(array);
|
||||
// //the image after compression
|
||||
// array = matFile.getArray("image1compressedgrayscale");
|
||||
// double[][] compressedWhistleImage = DLMatFile.matrix2array(array);
|
||||
//
|
||||
// //the whistle2Image transform image
|
||||
// array = matFile.getArray("image1originalgrayscalenorm");
|
||||
// double[][] whislteImage = DLMatFile.matrix2array(array);
|
||||
|
||||
//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.};
|
||||
|
||||
ArrayList<double[][]> whistleImageArr = new ArrayList<double[][]>();
|
||||
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]];
|
||||
|
||||
@ -67,33 +124,53 @@ public class DelphinIDTest {
|
||||
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);
|
||||
color = raster.getPixel(i,j, color);
|
||||
imaged[i][j] = (255-color[0])/255.; //normalize
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<DLTransform> transforms = new ArrayList<DLTransform>();
|
||||
transforms.add(new FreqTransform(DLTransformType.SPECRESIZE, new Number[] {Integer.valueOf(64), Integer.valueOf(48)}));
|
||||
//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)}));
|
||||
|
||||
//
|
||||
// //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
|
||||
// Create MAT file with a scalar in a nested struct
|
||||
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
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@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");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Binary file not shown.
Loading…
Reference in New Issue
Block a user