org.tensorflow.Tensor Java Examples
The following examples show how to use
org.tensorflow.Tensor.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TensorUtil.java From jpmml-tensorflow with GNU Affero General Public License v3.0 | 7 votes |
static public List<?> getValues(Tensor tensor){ DataType dataType = tensor.dataType(); switch(dataType){ case FLOAT: return Floats.asList(TensorUtil.toFloatArray(tensor)); case DOUBLE: return Doubles.asList(TensorUtil.toDoubleArray(tensor)); case INT32: return Ints.asList(TensorUtil.toIntArray(tensor)); case INT64: return Longs.asList(TensorUtil.toLongArray(tensor)); case STRING: return Arrays.asList(TensorUtil.toStringArray(tensor)); case BOOL: return Booleans.asList(TensorUtil.toBooleanArray(tensor)); default: throw new IllegalArgumentException(); } }
Example #2
Source File: TfTest.java From orbit-image-analysis with GNU General Public License v3.0 | 6 votes |
public static void main(String[] args) throws Exception { try (Graph g = new Graph()) { final String value = "Hello from " + TensorFlow.version(); // Construct the computation graph with a single operation, a constant // named "MyConst" with a value "value". try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) { // The Java API doesn't yet include convenience functions for adding operations. g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build(); } // Execute the "MyConst" operation in a Session. try (Session s = new Session(g); Tensor output = s.runner().fetch("MyConst").run().get(0)) { System.out.println(new String(output.bytesValue(), "UTF-8")); } } }
Example #3
Source File: TensorFlow.java From ssj with GNU General Public License v3.0 | 6 votes |
/** * Makes prediction about the given image data. * * @param floatValues RGB float data. * @return Probability array. */ private float[] makePrediction(float[] floatValues) { Tensor input = Tensor.create(options.shape.get(), FloatBuffer.wrap(floatValues)); Tensor result = session.runner() .feed(options.inputNode.get(), input) .fetch(options.outputNode.get()) .run().get(0); long[] rshape = result.shape(); if (result.numDimensions() != 2 || rshape[0] != 1) { throw new RuntimeException( String.format( "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s", Arrays.toString(rshape))); } int nlabels = (int) rshape[1]; return result.copyTo(new float[1][nlabels])[0]; }
Example #4
Source File: Bert.java From easy-bert with MIT License | 6 votes |
/** * Gets a pooled BERT embedding for a single sequence. Sequences are usually individual sentences, but don't have to be. * * @param sequence * the sequence to embed * @return the pooled embedding for the sequence * @since 1.0.3 */ public float[] embedSequence(final String sequence) { try(Inputs inputs = getInputs(sequence)) { final List<Tensor<?>> output = bundle.session().runner() .feed(model.inputIds, inputs.inputIds) .feed(model.inputMask, inputs.inputMask) .feed(model.segmentIds, inputs.segmentIds) .fetch(model.pooledOutput) .run(); try(Tensor<?> embedding = output.get(0)) { final float[][] converted = new float[1][(int)embedding.shape()[1]]; embedding.copyTo(converted); return converted[0]; } } }
Example #5
Source File: ConstantTest.java From java with Apache License 2.0 | 6 votes |
@Test public void createLongs() { LongDataBuffer buffer = DataBuffers.of(1L, 2L, 3L, 4L); Shape shape = Shape.of(4); LongNdArray array = NdArrays.wrap(shape, buffer); try (Graph g = new Graph(); Session sess = new Session(g)) { Scope scope = new Scope(g); Constant<TInt64> op1 = Constant.tensorOf(scope, shape, buffer); Constant<TInt64> op2 = Constant.tensorOf(scope, array); try (AutoCloseableList<Tensor<?>> t = new AutoCloseableList<>(sess.runner().fetch(op1).fetch(op2).run())) { assertEquals(array, t.get(0).expect(TInt64.DTYPE).data()); assertEquals(array, t.get(1).expect(TInt64.DTYPE).data()); } } }
Example #6
Source File: GeneratedOperationsTest.java From java with Apache License 2.0 | 6 votes |
/** * Test for Ops.withControlDependencies. * * <p>Creates an add node with a control dependency to an assign node. In other words, the assign * node is a control input to the add node. When the add node is run, the assign node is expected * to have run beforehand due to the control dependency. */ @Test public void testControlDependencies() { try (Graph g = new Graph(); Session sess = new Session(g)) { Ops ops = Ops.create(g); Operand<TInt32> variable = ops.variable(Shape.scalar(), TInt32.DTYPE); Operand<?> initVariable = ops.assign(variable, ops.constant(0)); ArrayList<Op> controls = new ArrayList<>(); controls.add(ops.assign(variable, ops.constant(3))); Operand<TInt32> x = ops.withControlDependencies(controls).math.add(variable, ops.constant(0)); sess.runner().addTarget(initVariable).run(); try (Tensor<TInt32> result = sess.runner().fetch(x).run().get(0).expect(TInt32.DTYPE)) { assertEquals(3, result.data().getInt()); } } }
Example #7
Source File: TensorJsonConverterTests.java From tensorflow with Apache License 2.0 | 6 votes |
@Test public void longArray() { long[][] inLongArray = new long[2][2]; inLongArray[0][0] = 0; inLongArray[0][1] = 1; inLongArray[1][0] = 2; inLongArray[1][1] = 3; Tensor inTensor = Tensor.create(inLongArray); String jsonTensor = TensorJsonConverter.toJson(inTensor); Tensor outTensor = TensorJsonConverter.toTensor(jsonTensor); long[][] outLongArray = new long[2][2]; outLongArray = (long[][]) outTensor.copyTo(outLongArray); compareTensors(inTensor, outTensor); assertArrayEquals(inLongArray, outLongArray); }
Example #8
Source File: TfSymbolBlock.java From djl with Apache License 2.0 | 6 votes |
/** {@inheritDoc} */ @Override public NDList forward( ParameterStore parameterStore, NDList inputs, boolean training, PairList<String, Object> params) { Session.Runner runner = session.runner(); PairList<String, Shape> inputDescriptions = describeInput(); PairList<String, Shape> outputDescriptions = describeOutput(); for (int i = 0; i < inputDescriptions.size(); i++) { runner.feed(inputDescriptions.get(i).getKey(), ((TfNDArray) inputs.get(i)).getTensor()); } for (int i = 0; i < outputDescriptions.size(); i++) { runner.fetch(outputDescriptions.get(i).getKey()); } List<Tensor<?>> result = runner.run(); NDList resultNDList = new NDList(); TfNDManager tfNDManager = (TfNDManager) inputs.head().getManager(); for (Tensor<?> tensor : result) { resultNDList.add(tfNDManager.create(tensor)); } return resultNDList; }
Example #9
Source File: MRCNNBrainDetector.java From orbit-image-analysis with GNU General Public License v3.0 | 6 votes |
public List<DetectorResult> detectBrains(final BufferedImage smallImage, BufferedImage image512) throws IOException { List<DetectorResult> resList = new ArrayList<>(); Tensor<Float> input = DLHelpers.convertBufferedImageToTensor(image512,512,512); if (input != null) { RawDetections rawDetections = DLHelpers.executeInceptionGraph(s, input, 512, 512, MAX_DETECTIONS,28,28); Detections detections = processDetections(512,512,rawDetections); double scaleW = smallImage.getWidth()/(double)image512.getWidth(); double scaleH = smallImage.getHeight()/(double)image512.getHeight(); Rectangle bb = detections.getContours().get(0).getBounds(); int pad = 20; bb = new Rectangle(bb.x-pad,bb.y-pad, bb.width+pad*2, bb.height+pad*2); Rectangle bbScaled = new Rectangle((int) (bb.x * scaleW), (int) (bb.y * scaleH), (int) (bb.width * scaleW), (int) (bb.height * scaleH)); bbScaled = new Rectangle(smallImage.getMinX(),smallImage.getMinY(),smallImage.getWidth(),smallImage.getHeight()).intersection(bbScaled); BufferedImage roiImage = smallImage.getSubimage(bbScaled.x,bbScaled.y,(int)bbScaled.getWidth(),(int)bbScaled.getHeight()); // roiImage = DLHelpers.resize(roiImage,DLHelpers.DESIRED_SIZE,DLHelpers.DESIRED_SIZE); roiImage = DLHelpers.resize(roiImage,size,size); DetectorResult result = new DetectorResult(roiImage,bb.x,bb.y,bb.x+bb.width, bb.y+bb.height); resList.add(result); } return resList; }
Example #10
Source File: TFDetectTest.java From orbit-image-analysis with GNU General Public License v3.0 | 6 votes |
public static void main(String[] args) throws IOException { Date startDate = new Date(); byte[] graphDef = readAllBytesOrExit(Paths.get(MODEL_DIR, MODEL_NAME)); /* * Create Tensor with Tensorflow byte[] image = readAllBytesOrExit(Paths.get(INPUT_IMAGE)); Tensor<Float> input = constructAndExecuteGraphToNormalizeImage(image); */ /* Create Tensor from BufferedImage */ BufferedImage bufferdImage = ImageIO.read(new File(INPUT_IMAGE)); Tensor<Float> input = convertBufferedImageToTensor(bufferdImage); if(input != null) { long[] mask = executeInceptionGraph(graphDef, input); BufferedImage bufferedImage = decodeLabels(mask); ImageIO.write(bufferedImage, "jpg", new File(OUTPUT_IMAGE)); } long elapsedTimeInSec = (new Date().getTime() - startDate.getTime()) / 1000; System.out.println(String.format("Ended in %ds .", elapsedTimeInSec)); }
Example #11
Source File: ShapeOpsTest.java From java with Apache License 2.0 | 6 votes |
/** * Test of size method, of class ShapeOps. */ @Test public void testSize_Shape() { try (Graph g = new Graph(); Session session = new Session(g)) { Scope scope = new Scope(g); Operand operand = Constant.arrayOf(scope, new float[]{1, 2, 3, 4, 5, 6, 7, 8}); Operand actual = Reshape.create(scope, operand, Constant.vectorOf(scope, new long[]{4, 2, 1})); Shape<TInt64> tfshape = Shape.create(scope, actual, TInt64.DTYPE); Operand<TInt64> size = ShapeOps.size(scope, tfshape, TInt64.DTYPE); AtomicInteger index = new AtomicInteger(); try (Tensor<TInt64> result1 = session.runner().fetch(size.asOutput()).run().get(0).expect(TInt64.DTYPE)) { result1.data().scalars().forEach(s -> assertEquals(8, s.getLong())); } } }
Example #12
Source File: GradientsTest.java From java with Apache License 2.0 | 6 votes |
@Test public void createGradientsWithInitialValues() { try (Graph g = new Graph(); Session sess = new Session(g)) { Ops tf = Ops.create(g); Output<TFloat32> x = tf.placeholder(TFloat32.DTYPE).output(); Output<TFloat32> y0 = tf.math.square(x).y(); Output<TFloat32> y1 = tf.math.square(y0).y(); Gradients grads0 = Gradients.create(tf.scope(), y1, Arrays.asList(y0)); Gradients grads1 = Gradients.create(tf.scope(), y0, Arrays.asList(x), Gradients.dx(grads0.dy())); assertNotNull(grads1); assertNotNull(grads1.dy()); assertEquals(1, grads1.dy().size()); try (Tensor<TFloat32> c = TFloat32.scalarOf(3.0f); AutoCloseableList<Tensor<?>> outputs = new AutoCloseableList<>( sess.runner().feed(x, c).fetch(grads1.dy(0)).run())) { assertEquals(108.0f, outputs.get(0).expect(TFloat32.DTYPE).data().getFloat(), 0.0f); } } }
Example #13
Source File: ObjectDetectionTensorflowInputConverter.java From tensorflow with Apache License 2.0 | 6 votes |
private static Tensor<UInt8> makeImageTensor(byte[] imageBytes) throws IOException { ByteArrayInputStream is = new ByteArrayInputStream(imageBytes); BufferedImage img = ImageIO.read(is); if (img.getType() != BufferedImage.TYPE_3BYTE_BGR) { throw new IllegalArgumentException( String.format("Expected 3-byte BGR encoding in BufferedImage, found %d", img.getType())); } byte[] data = ((DataBufferByte) img.getData().getDataBuffer()).getData(); // ImageIO.read produces BGR-encoded images, while the model expects RGB. bgrToRgb(data); //Expand dimensions since the model expects images to have shape: [1, None, None, 3] long[] shape = new long[] { BATCH_SIZE, img.getHeight(), img.getWidth(), CHANNELS }; return Tensor.create(UInt8.class, shape, ByteBuffer.wrap(data)); }
Example #14
Source File: ShapeOpsTest.java From java with Apache License 2.0 | 6 votes |
/** * Test of squeeze method, of class ShapeOps. */ @Test public void testSqueeze() { try (Graph g = new Graph(); Session session = new Session(g)) { Scope scope = new Scope(g); Operand operand = Constant.arrayOf(scope, new float[]{1, 2, 3, 4, 5, 6, 7, 8}); Operand actual = Reshape.create(scope, operand, Constant.vectorOf(scope, new long[]{4, 1, 2, 1})); Shape<TInt32> tfshape = Shape.create(scope, actual); Operand<TInt32> squeezed = ShapeOps.squeeze(scope, tfshape); AtomicInteger index = new AtomicInteger(); int[] expected = {4, 2}; try (Tensor<TInt32> result = session.runner().fetch(squeezed.asOutput()).run().get(0).expect(TInt32.DTYPE)) { result.data().scalars().forEach(s -> { assertEquals(expected[index.getAndIncrement()], s.getInt()); }); } assertEquals(expected.length, index.get()); } }
Example #15
Source File: TakeDatasetTest.java From java with Apache License 2.0 | 6 votes |
@Test public void testEagerTakeDataset() { Ops tf = Ops.create(); Dataset dataset = Dataset.fromTensorSlices( tf, Arrays.asList(tf.constant(testMatrix1), tf.constant(testMatrix2)), Arrays.asList(TInt32.DTYPE, TInt32.DTYPE)) .take(4); int count = 0; for (List<Operand<?>> components : dataset) { try (Tensor<TInt32> batch1 = components.get(0).asTensor().expect(TInt32.DTYPE); Tensor<TInt32> batch2 = components.get(1).asTensor().expect(TInt32.DTYPE); ) { assertEquals(testMatrix1.get(count), batch1.data()); assertEquals(testMatrix2.get(count), batch2.data()); count++; } } assertEquals(4, count); }
Example #16
Source File: RNTensorflowInference.java From react-native-tensorflow with Apache License 2.0 | 6 votes |
public void run(String[] outputNames, boolean enableStats) { if(tfContext != null) { for (String outputName : outputNames) { tfContext.runner.fetch(outputName); } List<Tensor> tensors = tfContext.runner.run(); tfContext.outputTensors.clear(); for (int i = 0; i < outputNames.length; i++) { tfContext.outputTensors.put(outputNames[i], tensors.get(i)); } } else { throw new IllegalStateException("Could not find inference for id"); } }
Example #17
Source File: DLSegment.java From orbit-image-analysis with GNU General Public License v3.0 | 6 votes |
public static void main3(String[] args) throws IOException { Session s = DLSegment.buildSession(DEMO_MODEL_NAME); BufferedImage sourceImage = ImageIO.read(new File(INPUT_IMAGE)); BufferedImage bi1 = new BufferedImage(1024,1024,BufferedImage.TYPE_INT_RGB); bi1.getGraphics().drawImage(sourceImage,0,0,null); bi1.getGraphics().drawImage(sourceImage,512,0,null); bi1.getGraphics().drawImage(sourceImage,0,512,null); bi1.getGraphics().drawImage(sourceImage,512,512,null); BufferedImage bi2 = new BufferedImage(512,512,BufferedImage.TYPE_INT_RGB); bi2.getGraphics().drawImage(bi1,0,0,512,512,null); sourceImage = bi2; Tensor<Float> inputTensor = convertBufferedImageToTensor(sourceImage); BufferedImage bufferedImage = DLSegment.segmentInput(inputTensor,s, Color.black, Color.green); s.close(); BufferedImage bi3 = new BufferedImage(512,512,BufferedImage.TYPE_INT_RGB); bi3.getGraphics().drawImage(bufferedImage,0,0,1024,1024,null); ImageIO.write(bi3, "jpg", new File(DEMO_OUTPUT_IMAGE)); }
Example #18
Source File: Kafka_Streams_TensorFlow_Image_Recognition_Example.java From kafka-streams-machine-learning-examples with Apache License 2.0 | 5 votes |
private static Tensor constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) { // Graph construction: using the OperationBuilder class to construct a graph to // decode, resize and normalize a JPEG image. try (Graph g = new Graph()) { GraphBuilder b = new GraphBuilder(g); // Some constants specific to the pre-trained model at: // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip // // - The model was trained with images scaled to 224x224 pixels. // - The colors, represented as R, G, B in 1-byte each were // converted to // float using (value - Mean)/Scale. final int H = 224; final int W = 224; final float mean = 117f; final float scale = 1f; // Since the graph is being constructed once per execution here, we // can use a constant for the // input image. If the graph were to be re-used for multiple input // images, a placeholder would // have been more appropriate. final Output input = b.constant("input", imageBytes); final Output output = b .div(b.sub( b.resizeBilinear(b.expandDims(b.cast(b.decodeJpeg(input, 3), DataType.FLOAT), b.constant("make_batch", 0)), b.constant("size", new int[] { H, W })), b.constant("mean", mean)), b.constant("scale", scale)); try (Session s = new Session(g)) { return s.runner().fetch(output.op().name()).run().get(0); } } }
Example #19
Source File: JTensorTest.java From zoltar with Apache License 2.0 | 5 votes |
@Test public void floatTensorSerializable() throws IOException { final float[] floatValue = {1, 2, 3, 4, 5}; final Tensor<Float> tensor = Tensors.create(floatValue); final JTensor jt = JTensor.create(tensor); new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(jt); }
Example #20
Source File: DLSegment.java From orbit-image-analysis with GNU General Public License v3.0 | 5 votes |
public static BufferedImage segmentInput(final Tensor<Float> inputTensor, Session s, Color bg, Color fg) { Tensor<Long> outputTensor = s .runner() .feed("image_batch", inputTensor) .fetch("predictions") .run().get(0).expect(Long.class); long[] mask = outputTensor.copyTo(new long[outputTensor.numElements()]); BufferedImage bufferedImage = decodeLabels(mask,bg,fg); return bufferedImage; }
Example #21
Source File: YOLO.java From cineast with MIT License | 5 votes |
/** * Executes graph on the given preprocessed image * * @param image preprocessed image * @return output tensor returned by tensorFlow */ private float[] executeYOLOGraph(final Tensor<Float> image) { Tensor<Float> result = yoloSession.runner().feed("input", image).fetch("output").run().get(0) .expect(Float.class); float[] outputTensor = new float[getOutputSizeByShape(result)]; FloatBuffer floatBuffer = FloatBuffer.wrap(outputTensor); result.writeTo(floatBuffer); result.close(); return outputTensor; }
Example #22
Source File: JTensorTest.java From zoltar with Apache License 2.0 | 5 votes |
@Test public void multidimensionalStringTensorSerializable() throws IOException { final byte[][][][] byteArray = toByteArray(STRING_ARRAY_3DIMENSIONS); final Tensor<String> tensor = Tensors.create(byteArray); final JTensor jt = JTensor.create(tensor); new ObjectOutputStream(new ByteArrayOutputStream()).writeObject(jt); }
Example #23
Source File: ScopeTest.java From java with Apache License 2.0 | 5 votes |
static <T extends TType> Const<T> create(Scope s, Tensor<T> value) { return new Const<>( s.env() .opBuilder("Const", s.makeOpName("Const")) .setAttr("dtype", value.dataType()) .setAttr("value", value) .build() .output(0)); }
Example #24
Source File: Constant.java From java with Apache License 2.0 | 5 votes |
/** * Creates a rank-6 constant of {@code String} elements, using default UTF-8 encoding. * * @param scope is a scope used to add the underlying operation. * @param data An array containing the values to put into the new constant. * @return a {@link TString} constant matrix */ public static Constant<TString> tensorOf(Scope scope, String[][][][][][] data) { NdArray<String> src = NdArrays.ofObjects(String.class, StdArrays.shapeOf(data)); StdArrays.copyTo(src, data); try (Tensor<TString> value = TString.tensorOf(src)) { return create(scope, value); } }
Example #25
Source File: GeneratedOperationsTest.java From java with Apache License 2.0 | 5 votes |
@Test public void testListInputTensorOutput() { try (Graph g = new Graph(); Session sess = new Session(g)) { Ops ops = Ops.create(g); ArrayList<Operand<TInt32>> inputs = new ArrayList<>(); inputs.add(ops.constant(1)); inputs.add(ops.constant(2)); inputs.add(ops.constant(3)); Operand<TInt32> x = ops.math.addN(inputs); try (Tensor<TInt32> result = sess.runner().fetch(x).run().get(0).expect(TInt32.DTYPE)) { assertEquals(6, result.data().getInt()); } } }
Example #26
Source File: TensorFlowPredictFn.java From zoltar with Apache License 2.0 | 5 votes |
/** * TensorFlow Example prediction function. * * @deprecated Use {@link #example(Function, String...)} * @param outTensorExtractor Function to extract the output value from JTensor's * @param fetchOps operations to fetch. */ @Deprecated static <InputT, ValueT> TensorFlowPredictFn<InputT, List<Example>, ValueT> exampleBatch( final Function<Map<String, JTensor>, ValueT> outTensorExtractor, final String... fetchOps) { final BiFunction<TensorFlowModel, List<Example>, ValueT> predictFn = (model, examples) -> { final byte[][] bytes = examples.stream().map(Example::toByteArray).toArray(byte[][]::new); try (final Tensor<String> t = Tensors.create(bytes)) { final Session.Runner runner = model.instance().session().runner().feed("input_example_tensor", t); final Map<String, JTensor> result = TensorFlowExtras.runAndExtract(runner, fetchOps); return outTensorExtractor.apply(result); } }; return (model, vectors) -> { final List<CompletableFuture<Prediction<InputT, ValueT>>> predictions = vectors .stream() .map( vector -> CompletableFuture.supplyAsync(() -> predictFn.apply(model, vector.value())) .thenApply(v -> Prediction.create(vector.input(), v))) .collect(Collectors.toList()); return CompletableFutures.allAsList(predictions); }; }
Example #27
Source File: TensorUtil.java From jpmml-tensorflow with GNU Affero General Public License v3.0 | 5 votes |
static public int[] toIntArray(Tensor tensor){ IntBuffer intBuffer = IntBuffer.allocate(tensor.numElements()); tensor.writeTo(intBuffer); return intBuffer.array(); }
Example #28
Source File: TensorFlowConverters.java From konduit-serving with Apache License 2.0 | 5 votes |
@Override public <U> U convert(NDArray from, Class<U> to) { Preconditions.checkState(canConvert(from, to), "Unable to convert NDArray to %s", to); Tensor<?> t = (Tensor<?>) from.get(); SerializedNDArray arr = convert(t); return (U)arr; }
Example #29
Source File: TStringTest.java From java with Apache License 2.0 | 5 votes |
@Test public void createVector() { Tensor<TString> tensor = TString.vectorOf("Pretty", "vacant"); assertNotNull(tensor); TString data = tensor.data(); assertNotNull(data); assertEquals(Shape.of(2), data.shape()); assertEquals("Pretty", data.getObject(0)); assertEquals("vacant", data.getObject(1)); }
Example #30
Source File: JTensorTest.java From zoltar with Apache License 2.0 | 5 votes |
@Test public void testStringTensor3Dimensions() { final byte[][][][] byteArray = toByteArray(STRING_ARRAY_3DIMENSIONS); final Tensor<String> tensor = Tensors.create(byteArray); final JTensor jt = JTensor.create(tensor); testMultidimensionalStringTensor(jt, STRING_ARRAY_3DIMENSIONS, new long[] {3, 3, 3}); }