diff --git a/src/main/java/org/bioimageanalysis/icy/deeplearning/tensorflow/v1/Tensorflow1InterfaceJAvaCPP.java b/src/main/java/org/bioimageanalysis/icy/deeplearning/tensorflow/v1/Tensorflow1InterfaceJAvaCPP.java deleted file mode 100644 index f06aaede4a5ad103225cfc05b3cd9ba6dde89205..0000000000000000000000000000000000000000 --- a/src/main/java/org/bioimageanalysis/icy/deeplearning/tensorflow/v1/Tensorflow1InterfaceJAvaCPP.java +++ /dev/null @@ -1,268 +0,0 @@ -package org.bioimageanalysis.icy.deeplearning.tensorflow.v1; - -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.io.RandomAccessFile; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.URL; -import java.net.URLClassLoader; -import java.nio.ByteBuffer; -import java.nio.FloatBuffer; -import java.nio.MappedByteBuffer; -import java.nio.channels.FileChannel; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.security.CodeSource; -import java.security.ProtectionDomain; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.stream.Collectors; - -import org.bioimageanalysis.icy.deeplearning.engine.DeepLearningEngineInterface; -import org.bioimageanalysis.icy.deeplearning.exceptions.LoadModelException; -import org.bioimageanalysis.icy.deeplearning.exceptions.RunModelException; -import org.bioimageanalysis.icy.deeplearning.tensor.Tensor; -import org.bioimageanalysis.icy.deeplearning.tensorflow.v1.tensor.MappedBufferToImgLib2; -import org.bioimageanalysis.icy.deeplearning.tensorflow.v1.tensor.MappedFileBuilder; -import org.bioimageanalysis.icy.deeplearning.tensorflow.v1.tensor.TensorBuilder; -import org.bytedeco.javacpp.BytePointer; -import org.bytedeco.javacpp.Pointer; -import org.bytedeco.javacpp.indexer.Indexer; -import org.bytedeco.tensorflow.RunOptions; -import org.bytedeco.tensorflow.SessionOptions; -import org.bytedeco.tensorflow.StringTensorPairVector; -import org.bytedeco.tensorflow.StringUnorderedSet; -import org.bytedeco.tensorflow.StringVector; -import org.bytedeco.tensorflow.TensorShape; -import org.bytedeco.tensorflow.TensorVector; - - -/** - * This plugin includes the libraries to convert back and forth TensorFlow 1 to Sequences and IcyBufferedImages. - * - * @see IcyBufferedImageBuilder IcyBufferedImageBuilder: Create images from tensors. - * @see Nd4fBuilder SequenceBuilder: Create sequences from tensors. - * @see TensorBuilder TensorBuilder: Create tensors from images and sequences. - * @author Carlos Garcia Lopez de Haro and Daniel Felipe Gonzalez Obando - */ -public class Tensorflow1InterfaceJAvaCPP implements DeepLearningEngineInterface -{ - private static final String[] MODEL_TAGS = {"serve", "inference", "train", "eval", "gpu", "tpu"}; - - private static final String[] TF_MODEL_TAGS = {"tf.saved_model.tag_constants.SERVING", - "tf.saved_model.tag_constants.INFERENCE", "tf.saved_model.tag_constants.TRAINING", - "tf.saved_model.tag_constants.EVAL", "tf.saved_model.tag_constants.GPU", - "tf.saved_model.tag_constants.TPU"}; - - private static final String[] SIGNATURE_CONSTANTS = {"serving_default", "inputs", "tensorflow/serving/classify", - "classes", "scores", "inputs", "tensorflow/serving/predict", "outputs", "inputs", - "tensorflow/serving/regress", "outputs", "train", "eval", "tensorflow/supervised/training", - "tensorflow/supervised/eval"}; - - private static final String[] TF_SIGNATURE_CONSTANTS = { - "tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY", - "tf.saved_model.signature_constants.CLASSIFY_INPUTS", - "tf.saved_model.signature_constants.CLASSIFY_METHOD_NAME", - "tf.saved_model.signature_constants.CLASSIFY_OUTPUT_CLASSES", - "tf.saved_model.signature_constants.CLASSIFY_OUTPUT_SCORES", - "tf.saved_model.signature_constants.PREDICT_INPUTS", - "tf.saved_model.signature_constants.PREDICT_METHOD_NAME", - "tf.saved_model.signature_constants.PREDICT_OUTPUTS", "tf.saved_model.signature_constants.REGRESS_INPUTS", - "tf.saved_model.signature_constants.REGRESS_METHOD_NAME", - "tf.saved_model.signature_constants.REGRESS_OUTPUTS", - "tf.saved_model.signature_constants.DEFAULT_TRAIN_SIGNATURE_DEF_KEY", - "tf.saved_model.signature_constants.DEFAULT_EVAL_SIGNATURE_DEF_KEY", - "tf.saved_model.signature_constants.SUPERVISED_TRAIN_METHOD_NAME", - "tf.saved_model.signature_constants.SUPERVISED_EVAL_METHOD_NAME"}; - /** - * Number of bytes typically used to represent the datatype in the memory mapped file - */ - final private static int DATATYPE_KEY_LENGTH = 2; - /** - * Number of bytes typically used to represent an identification header in the memory mapped file - */ - final private static int MAGIC_WORD_LENGTH = 2; - /** - * Number of bytes typically used to represent the shape in the memory mapped file. - * A typical shape (on the bigger side) would be: [1, 3, 32, 1024, 1024]. - * Add a factor 2 to the length as a security margin - */ - final private static int TYPICAL_SHAPE_LENTH = 2; - - private String tmpDir; - - private String modelFolder; - - - public Tensorflow1InterfaceJAvaCPP() throws IOException - { - this.tmpDir = getTemporaryDir(); - } - - public static void main(String[] args) throws LoadModelException, RunModelException, IOException { - // Unpack the args needed - if (args.length < 4) - throw new IllegalArgumentException("Error exectuting Tensorflow 1, " - + "at least arguments are required:" + System.lineSeparator() - + " - Folder where the model is located" + System.lineSeparator() - + " - Temporary dir where the memory mapped files are located" + System.lineSeparator() - + " - Name of the model input followed by the String + '_model_input'" + System.lineSeparator() - + " - Name of the second model input (if it exists) followed by the String + '_model_input'" + System.lineSeparator() - + " - ...." + System.lineSeparator() - + " - Name of the nth model input (if it exists) followed by the String + '_model_input'" + System.lineSeparator() - + " - Name of the model output followed by the String + '_model_output'" + System.lineSeparator() - + " - Name of the second model output (if it exists) followed by the String + '_model_output'" + System.lineSeparator() - + " - ...." + System.lineSeparator() - + " - Name of the nth model output (if it exists) followed by the String + '_model_output'" + System.lineSeparator() - ); - String modelFolder = args[0]; - if (new File(modelFolder).isDirectory()) { - throw new IllegalArgumentException("Argument 0 of the main method, '" + modelFolder + "' " - + "should be an existing directory containing a Tensorflow 1 model."); - } - - Tensorflow1InterfaceJAvaCPP tfInterface = new Tensorflow1InterfaceJAvaCPP(); - tfInterface.tmpDir = args[1]; - if (new File(args[1]).isDirectory()) { - throw new IllegalArgumentException("Argument 1 of the main method, '" + args[1] + "' " - + "should be an existing directory."); - } - - tfInterface.loadModel(modelFolder, modelFolder); - - HashMap<String, List<String>> map = getInputTensorsFileNames(args); - List<String> inputNames = map.get(INPUTS_MAP_KEY); - List<String> outputNames = map.get(OUTPUTS_MAP_KEY); - - for (String inName : inputNames) { - try (RandomAccessFile rd = - new RandomAccessFile(tfInterface.tmpDir + File.separator + inName, "r"); - FileChannel fc = rd.getChannel();) { - int initialLenghToRead = inName.length() + DATATYPE_KEY_LENGTH + MAGIC_WORD_LENGTH + TYPICAL_SHAPE_LENTH; - MappedByteBuffer mem = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()); - } - } - - - - - - - System.out.println(""); - System.out.println(System.getProperty("java.library.path")); - String dir = args[0]; - org.bytedeco.tensorflow.SavedModelBundle model = new org.bytedeco.tensorflow.SavedModelBundle(); - StringUnorderedSet ss = new StringUnorderedSet(); - ss.insert(new BytePointer("serve")); - SessionOptions sesOp = new SessionOptions(); - RunOptions runOp = new RunOptions(); - org.bytedeco.tensorflow.global.tensorflow.LoadSavedModel(sesOp, runOp, dir, ss, model); - - TensorShape shape = new TensorShape(1, 512, 512, 1); - org.bytedeco.tensorflow.Tensor x = - new org.bytedeco.tensorflow.Tensor(org.bytedeco.tensorflow.global.tensorflow.DT_FLOAT, - shape); - FloatBuffer x_flat = x.createBuffer(); - float[] arr = new float[512 * 512]; - x_flat.put(arr); - - TensorVector outputV = new TensorVector(); - //outputV.resize(1); - org.bytedeco.tensorflow.Session session = model.session(); - StringTensorPairVector pair = new StringTensorPairVector(new String[] {"input_2_4:0"}, new org.bytedeco.tensorflow.Tensor[] {x}); - StringVector sVec = new StringVector("conv2d_33_4/Sigmoid:0"); - StringVector svec2 = new StringVector("conv2d_33_4/Sigmoid:0"); - session.Run(pair, sVec, svec2, outputV); - /* - session.Run(new RunOptions(), - new StringTensorPairVector(new String[] {"input_2_4:0"}, new org.bytedeco.tensorflow.Tensor[] {x}), - new StringVector("conv2d_33_4/Sigmoid:0"), new StringVector("conv2d_33_4/Sigmoid:0"), - out, new RunMetadata()); - */ - - - //org.bytedeco.tensorflow.Tensor y = outputV.get(0); - - Indexer aa = outputV.get(0).createIndexer(); - Indexer bb = x.createIndexer(); - //Indexer cc = out.get(0).createIndexer(); - FloatBuffer y_flat = outputV.get(0).createBuffer(); - float sss = 0; - for (int i = 0; i < y_flat.capacity(); i ++) { - sss += y_flat.get(i); - } - float mean = sss / y_flat.capacity(); - x.close(); - outputV.close(); - session.Close(); - session.close(); - model.close(); - aa.close(); - bb.close(); - ss.close(); - sesOp.close(); - runOp.close(); - shape.Clear(); - shape.close(); - pair.clear(); - pair.close(); - sVec.clear(); - sVec.close(); - svec2.clear(); - svec2.close(); - - - x.deallocate(); - outputV.deallocate(); - session.deallocate(); - session.deallocate(); - model.deallocate(); - ss.deallocate(); - sesOp.deallocate(); - runOp.deallocate(); - shape.deallocate(); - pair.deallocate(); - sVec.deallocate(); - svec2.deallocate(); - System.out.println(mean); - } - - @Override - public void loadModel(String modelFolder, String modelSource) throws LoadModelException { - - } - - @Override - public void run(List<Tensor<?>> inputTensors, List<Tensor<?>> outputTensors) throws RunModelException { - createTensorsForInterprocessing(inputTensors); - List<String> args = getProcessCommandsWithoutArgs(); - args.add(modelFolder); - args.add(this.tmpDir); - for (Tensor tensor : inputTensors) {args.add(tensor.getName() + INPUT_FILE_TERMINATION);} - for (Tensor tensor : outputTensors) {args.add(tensor.getName() + OUTPUT_FILE_TERMINATION);} - - ProcessBuilder builder = new ProcessBuilder(args); - Process process; - try { - process = builder.inheritIO().start(); - if (process.waitFor() != 0) - throw new RunModelException("Error executing the Tensorflow 1 model in" - + " a separate process. The process was not terminated correctly."); - } catch (RunModelException e) { - throw e; - } catch (Exception e) { - throw new RunModelException(e.getCause().toString()); - } - - retrieveInterprocessingTensors(outputTensors); - } - - @Override - public void closeModel() { - Pointer.interruptDeallocatorThread(); - } -}