DL Params to XML

Write DL parameter to XML output (for Tethys). Needed a bit of messing with .equals in some of the objects in the XML writer class. Not really providing the information we want in the XML, so will need to revisit this in the future.
This commit is contained in:
Douglas Gillespie 2024-05-07 17:11:06 +01:00
parent e9bf8dd602
commit 475e7c42cd
8 changed files with 127 additions and 5 deletions

View File

@ -584,6 +584,28 @@ public class PamguardXMLWriter implements PamSettings {
return el;
}
/**
* Need to use a modified function here since some of Jamies DL params
* classes cast to their own type before the have checked type in the
* equals functoins he's written.
* @param objectHierarchy
* @return
*/
private boolean hasData(ArrayList<Object> objectHierarchy, Object object) {
if (objectHierarchy == null) {
return false;
}
for (Object o : objectHierarchy) {
if (object.getClass() != o.getClass()) {
return false;
}
if (o.equals(object)) {
return true;
}
}
return false;
}
public Element writeObjectData(Document doc, Element el, Object data, ArrayList<Object> objectHierarchy) {
if (data == null) {
return null;
@ -591,7 +613,7 @@ public class PamguardXMLWriter implements PamSettings {
if (objectHierarchy == null) {
objectHierarchy = new ArrayList<>();
}
if (objectHierarchy.contains(data)) {
if (hasData(objectHierarchy, data)) {
// just write the reference, but nothing else or we'll end up in an infinite loop of objects.
Element e = doc.createElement("Object");
e.setAttribute("Class", data.getClass().getName());
@ -794,6 +816,8 @@ public class PamguardXMLWriter implements PamSettings {
}
catch (Exception e) {
System.out.println("Error in PamguardXMLWriter.writeObjectArray: " + e.getMessage());
e.printStackTrace();
}
return null;
}

View File

@ -346,6 +346,7 @@ public class DLControl extends PamControlledUnit implements PamSettings {
this.segmenterProcess.setupSegmenter();
this.dlClassifyProcess.setupProcess();
this.checkModelParams();
// this is a bit of a hack. Annotations are added to data units but the
// datablock knows nothing about them
@ -512,6 +513,34 @@ public class DLControl extends PamControlledUnit implements PamSettings {
public void setParams(RawDLParams newParams) {
this.rawDLParmas = newParams;
checkModelParams();
}
/**
* Called when setParams is called, which should have new model params
* after the dialog was closed. Puts these into dlParams so they get serialised
* with rest of XML.
*/
public void checkModelParams() {
RawDLParams dlParams = getDLParams();
DLClassiferModel model = getDLModel();
Serializable modelParams = null;
if (model != null) {
modelParams = model.getDLModelSettings();
}
dlParams.setModelParameters(modelParams);
// see what else we can find in the model in terms of metadata.
// if (model == null) {
// return;
// }
// try {
// String modelName = model.getName();
// System.out.println("Model name: " + modelName);
// }
// catch (Exception e) {
// e.printStackTrace();
// }
}
/**

View File

@ -100,6 +100,8 @@ public class RawDLParams implements Serializable, Cloneable {
*/
public short classNameIndex = 0;
private Serializable modelParameters;
@Override
public RawDLParams clone() {
RawDLParams newParams = null;
@ -118,4 +120,22 @@ public class RawDLParams implements Serializable, Cloneable {
return newParams;
}
/**
* Set the model parameters. These aren't really needed in here are aren't really
* used except when the parameters are serialized to XML for book keeping.
* @param modelParameters
*/
public Serializable getModelParameters() {
return modelParameters;
}
/**
* Set the model parameters. These aren't really needed in here are aren't really
* used except when the parameters are serialized to XML for book keeping.
* @param modelParameters the modelParameters to set
*/
public void setModelParameters(Serializable modelParameters) {
this.modelParameters = modelParameters;
}
}

View File

@ -223,7 +223,7 @@ public class DLClassifyProcess extends PamInstantProcess {
//run the deep learning algorithm
ArrayList<GroupedRawData> classificationBufferTemp = (ArrayList<GroupedRawData>) classificationBuffer.clone();
ArrayList<? extends PredictionResult> modelResults = this.dlControl.getDLModel().runModel(classificationBuffer);
ArrayList<? extends PredictionResult> modelResults = this.dlControl.getDLModel().runModel(classificationBufferTemp);
if (modelResults==null) {
return; //there has been a problem

View File

@ -3,6 +3,7 @@ package rawDeepLearningClassifier.dlClassification;
import PamView.GroupedDataSource;
import PamView.GroupedSourceParameters;
import PamguardMVC.AcousticDataBlock;
import rawDeepLearningClassifier.DLControl;
import rawDeepLearningClassifier.tethys.DLSpeciesManager;
import rawDeepLearningClassifier.tethys.DLTethysDataProvider;
import tethys.TethysControl;
@ -20,10 +21,12 @@ public class DLDetectionDataBlock extends AcousticDataBlock<DLDetection> impleme
private DLClassifyProcess dlClassifyProcess;
private DLTethysDataProvider dlTethysDataProvider;
private DLSpeciesManager dlSpeciesManager;
private DLControl dlControl;
public DLDetectionDataBlock(String dataName, DLClassifyProcess parentProcess, int channelMap) {
super(DLDetection.class, dataName, parentProcess, channelMap);
this.dlClassifyProcess = parentProcess;
dlControl = dlClassifyProcess.getDLControl();
}
@Override
@ -34,7 +37,7 @@ public class DLDetectionDataBlock extends AcousticDataBlock<DLDetection> impleme
@Override
public TethysDataProvider getTethysDataProvider(TethysControl tethysControl) {
if (dlTethysDataProvider == null) {
dlTethysDataProvider = new DLTethysDataProvider(tethysControl, this);
dlTethysDataProvider = new DLTethysDataProvider(tethysControl, dlControl, this);
}
return dlTethysDataProvider;
}

View File

@ -89,6 +89,14 @@ public class GenericModelParams extends StandardModelParams implements Cloneable
@Override
public boolean equals(Object o) {
if (o instanceof GenericModelParams == false) {
/*
* have to add this since the equals function is used in a list comparason
* in the XML output and that list contains objects of different types
* so need to get out HERE or get a classcastexception at the next line
*/
return false;
}
GenericModelParams params = (GenericModelParams) o;

View File

@ -1,20 +1,30 @@
package rawDeepLearningClassifier.tethys;
import java.io.Serializable;
import javax.xml.bind.JAXBException;
import PamguardMVC.PamDataBlock;
import PamguardMVC.PamDataUnit;
import nilus.Detection;
import nilus.Detection.Parameters;
import rawDeepLearningClassifier.DLControl;
import rawDeepLearningClassifier.RawDLParams;
import rawDeepLearningClassifier.dlClassification.DLClassiferModel;
import rawDeepLearningClassifier.dlClassification.DLDetection;
import tethys.TethysControl;
import tethys.output.StreamExportParams;
import tethys.output.TethysExportParams;
import tethys.pamdata.AutoTethysProvider;
import tethys.pamdata.TethysParameterPacker;
public class DLTethysDataProvider extends AutoTethysProvider {
public DLTethysDataProvider(TethysControl tethysControl, PamDataBlock pamDataBlock) {
private DLControl dlControl;
public DLTethysDataProvider(TethysControl tethysControl, DLControl dlControl, PamDataBlock pamDataBlock) {
super(tethysControl, pamDataBlock);
// TODO Auto-generated constructor stub
this.dlControl = dlControl;
}
@Override
@ -35,4 +45,25 @@ public class DLTethysDataProvider extends AutoTethysProvider {
return detection;
}
@Override
public nilus.AlgorithmType.Parameters getAlgorithmParameters() {
/**
* Add the model parameters to the main dlControl parameters so that they get
* correctly serialized to the XML output
// */
// RawDLParams dlParams = dlControl.getDLParams();
// DLClassiferModel model = dlControl.getDLModel();
// Serializable modelParams = null;
// if (model != null) {
// modelParams = model.getDLModelSettings();
// }
// dlParams.setModelParameters(modelParams);
dlControl.checkModelParams();
nilus.AlgorithmType.Parameters parameters = super.getAlgorithmParameters();
return parameters;
}
}

View File

@ -498,5 +498,12 @@ abstract public class AutoTethysProvider implements TethysDataProvider {
return true;
}
/**
* @return the tethysControl
*/
public TethysControl getTethysControl() {
return tethysControl;
}
}