All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.actelion.research.orbit.imageAnalysis.deeplearning.GraphBuilder Maven / Gradle / Ivy

Go to download

Orbit, a versatile image analysis software for biological image-based quantification

There is a newer version: 3.15
Show newest version
/*
 *     Orbit, a versatile image analysis software for biological image-based quantification.
 *     Copyright (C) 2009 - 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91, CH-4123 Allschwil, Switzerland.
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see .
 *
 */

package com.actelion.research.orbit.imageAnalysis.deeplearning;

import org.tensorflow.DataType;
import org.tensorflow.Graph;
import org.tensorflow.Output;
import org.tensorflow.Tensor;
import org.tensorflow.types.UInt8;

public class GraphBuilder {

	// In the fullness of time, equivalents of the methods of this class should
	// be auto-generated from
	// the OpDefs linked into libtensorflow_jni.so. That would match what is
	// done in other languages
	// like Python, C++ and Go.

	private Graph g;

	GraphBuilder(Graph g) {
		this.g = g;
	}

	Output div(Output x, Output y) {
		return binaryOp("Div", x, y);
	}

	 Output sub(Output x, Output y) {
		return binaryOp("Sub", x, y);
	}

	 Output resizeBilinear(Output images, Output size) {
		return binaryOp3("ResizeBilinear", images, size);
	}

	 Output expandDims(Output input, Output dim) {
		return binaryOp3("ExpandDims", input, dim);
	}

	 Output cast(Output value, Class type) {
		DataType dtype = DataType.fromClass(type);
		return g.opBuilder("Cast", "Cast").addInput(value)
				.setAttr("DstT", dtype).build(). output(0);
	}

	Output decodeJpeg(Output contents, long channels) {
		return g.opBuilder("DecodeJpeg", "DecodeJpeg").addInput(contents)
				.setAttr("channels", channels).build(). output(0);
	}

	 Output constant(String name, Object value, Class type) {
		try (Tensor t = Tensor. create(value, type)) {
			return g.opBuilder("Const", name)
					.setAttr("dtype", DataType.fromClass(type))
					.setAttr("value", t).build(). output(0);
		}
	}

	Output constant(String name, byte[] value) {
		return this.constant(name, value, String.class);
	}

	Output constant(String name, int value) {
		return this.constant(name, value, Integer.class);
	}
	
	Output constant(String name, float[] value) {
		return this.constant(name, value, Float.class);
	}

	Output constant(String name, int[] value) {
		return this.constant(name, value, Integer.class);
	}

	Output constant(String name, float value) {
		return this.constant(name, value, Float.class);
	}

	private  Output binaryOp(String type, Output in1, Output in2) {
		return g.opBuilder(type, type).addInput(in1).addInput(in2).build()
				. output(0);
	}

	private  Output binaryOp3(String type, Output in1,
			Output in2) {
		return g.opBuilder(type, type).addInput(in1).addInput(in2).build()
				. output(0);
	}
}