Java Code Examples for org.tensorflow.Tensor#copyTo()

The following examples show how to use org.tensorflow.Tensor#copyTo() . 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: TensorTupleConverterTest.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 6 votes vote down vote up
@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);

	Tuple tuple = TensorTupleConverter.toTuple(inTensor);
	Tensor outTensor = TensorTupleConverter.toTensor(tuple);

	long[][] outLongArray = new long[2][2];
	outLongArray = outTensor.copyTo(outLongArray);

	compareTensors(inTensor, outTensor);
	assertArrayEquals(inLongArray, outLongArray);
}
 
Example 2
Source File: TwitterSentimentTensorflowOutputConverter.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 6 votes vote down vote up
@Override
public String convert(Tensor tensor, Map<String, Object> processorContext) {
	// Read Tensor's value into float[][] matrix
	float[][] resultMatrix = new float[12][2];
	tensor.copyTo(resultMatrix);
	String sentimentString = Sentiment.get(resultMatrix[0][1]).toString();

	// Prepare teh output map
	Map inputJsonMap = (Map) processorContext.get(PROCESSOR_CONTEXT_TWEET_JSON_MAP);

	Map<String, Object> outputJsonMap = new HashMap<>();
	outputJsonMap.put(SENTIMENT_TAG, sentimentString);
	outputJsonMap.put(TEXT_TAG, inputJsonMap.get(TEXT_TAG));
	outputJsonMap.put(ID_TAG, inputJsonMap.get(ID_TAG));
	outputJsonMap.put(LANG_TAG, inputJsonMap.get(LANG_TAG));

	try {
		return objectMapper.writeValueAsString(outputJsonMap);
	}
	catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to generate JSON output", e);
	}
}
 
Example 3
Source File: TensorJsonConverterTests.java    From tensorflow with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: TwitterSentimentTensorflowOutputConverter.java    From tensorflow with Apache License 2.0 6 votes vote down vote up
@Override
public String convert(Map<String, Tensor<?>> tensorMap, Map<String, Object> processorContext) {
	Tensor tensor = tensorMap.entrySet().iterator().next().getValue();
	// Read Tensor's value into float[][] matrix
	float[][] resultMatrix = new float[12][2];
	tensor.copyTo(resultMatrix);
	String sentimentString = Sentiment.get(resultMatrix[0][1]).toString();

	// Prepare teh output map
	Map inputJsonMap = (Map) processorContext.get(
			TwitterSentimentProcessorConfiguration.PROCESSOR_CONTEXT_TWEET_JSON_MAP);

	Map<String, Object> outputJsonMap = new HashMap<>();
	outputJsonMap.put(SENTIMENT_TAG, sentimentString);
	outputJsonMap.put(TEXT_TAG, inputJsonMap.get(TEXT_TAG));
	outputJsonMap.put(ID_TAG, inputJsonMap.get(ID_TAG));
	outputJsonMap.put(LANG_TAG, inputJsonMap.get(LANG_TAG));

	try {
		return objectMapper.writeValueAsString(outputJsonMap);
	}
	catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to generate JSON output", e);
	}
}
 
Example 5
Source File: TensorFlow.java    From ssj with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 6
Source File: MobilenetV2Classifier.java    From tensorboot with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Recognition> convertToResult(Tensor<Float> output) {
    int maxObjects = (int) output.shape()[1];
    float[] result;
    result = output.copyTo(new float[1][maxObjects])[0];
    return convertToRecognitions(result);
}
 
Example 7
Source File: FaceRecognizer.java    From server_face_recognition with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Running neural network
 *
 * @param image cropped, centralized face
 * @return describing of a face based on 128 float features
 */
private FaceFeatures passImageThroughNeuralNetwork(BufferedImage image, int faceType) {
    FaceFeatures features;

    try (Session session = new Session(graph)) {
        Tensor<Float> feedImage = Tensors.create(imageToMultiDimensionalArray(image));

        long timeResponse = System.currentTimeMillis();

        Tensor<Float> response = session.runner()
                .feed("input", feedImage)
                .feed("phase_train", Tensor.create(false))
                .fetch("embeddings")
                .run().get(0)
                .expect(Float.class);

        FileUtils.timeSpent(timeResponse, "RESPONSE");

        final long[] shape = response.shape();

        //first dimension should return 1 as for image with normal size
        //second dimension should give 128 characteristics of face
        if (shape[0] != 1 || shape[1] != 128) {
            throw new IllegalStateException("illegal output values: 1 = " + shape[0] + " 2 = " + shape[1]);
        }

        float[][] featuresHolder = new float[1][128];
        response.copyTo(featuresHolder);

        features = new FaceFeatures(featuresHolder[0], faceType);

        response.close();
    }

    return features;
}
 
Example 8
Source File: LabelImageTensorflowOutputConverter.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(Tensor tensor, Map<String, Object> processorContext) {
	final long[] rshape = tensor.shape();
	if (tensor.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];
	float[] labelProbabilities = tensor.copyTo(new float[1][nlabels])[0];

	int mostProbableLabelIndex = maxProbabilityIndex(labelProbabilities);

	Map<String, Object> outputJsonMap = new HashMap<>();
	outputJsonMap.put("label", labels.get(mostProbableLabelIndex));


	if (alternativesLength > 0) {
		List<Integer> top10Probabilities = topKProbabilities(labelProbabilities, alternativesLength);

		Map[] alternatives = new Map[top10Probabilities.size()];
		for (int i = 0; i < top10Probabilities.size(); i++) {
			int probabilityInidex = top10Probabilities.get(i);
			alternatives[i] = toMap(labels.get(probabilityInidex), labelProbabilities[probabilityInidex]);
		}
		outputJsonMap.put("alternatives", alternatives);
	}

	try {
		return objectMapper.writeValueAsString(outputJsonMap);
	}
	catch (JsonProcessingException e) {
		throw new RuntimeException("Failed to generate JSON output", e);
	}
}
 
Example 9
Source File: LinearRegressionTensorflowProcessorIntegrationTests.java    From tensorflow-spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Bean
public TensorflowOutputConverter tensorflowOutputConverter() {
	return new TensorflowOutputConverter<Object>() {
		@Override
		public Object convert(Tensor tensor, Map<String, Object> processorContext) {
			float[] outputValue = new float[1];
			tensor.copyTo(outputValue);
			return outputValue[0];
		}
	};
}
 
Example 10
Source File: ImageRecognitionTensorflowOutputConverter.java    From tensorflow with Apache License 2.0 5 votes vote down vote up
@Override
public String convert(Map<String, Tensor<?>> tensorMap, Map<String, Object> processorContext) {
	Tensor tensor = tensorMap.entrySet().iterator().next().getValue();
	final long[] rshape = tensor.shape();
	if (tensor.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 labelsCount = (int) rshape[1];

	float[][] resultMatrix = new float[1][labelsCount];

	float[] labelProbabilities = ((float[][])tensor.copyTo(resultMatrix))[0];

	List<String> entries = new ArrayList<>();
	if (responseSize == 1) {
		int maxProbabilityIndex = maxProbabilityIndex(labelProbabilities);
		entries.add(String.format("{\"%s\":%s}", labels.get(maxProbabilityIndex), labelProbabilities[maxProbabilityIndex]));
	}
	else {
		List<Integer> topKProbabilities = indexesOfTopKProbabilities(labelProbabilities, responseSize);

		for (int i = 0; i < topKProbabilities.size(); i++) {
			int probabilityIndex = topKProbabilities.get(i);
			entries.add(String.format("{\"%s\":%s}", labels.get(probabilityIndex), labelProbabilities[probabilityIndex]));
		}
	}

	return String.format("{\"labels\":%s}", Arrays.toString(entries.toArray(new String[entries.size()]) ));
}
 
Example 11
Source File: LinearRegressionTensorflowProcessorIntegrationTests.java    From tensorflow with Apache License 2.0 5 votes vote down vote up
@Bean
@Primary
public TensorflowOutputConverter tensorflowOutputConverterX() {
	return (TensorflowOutputConverter<Object>) (tensorMap, processorContext) -> {
		Tensor tensor = tensorMap.entrySet().iterator().next().getValue();
		float[] outputValue = new float[1];
		tensor.copyTo(outputValue);
		return outputValue[0];
	};
}
 
Example 12
Source File: DLSegment.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
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 13
Source File: JTensor.java    From zoltar with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new {@link JTensor} instance by extracting data from the underlying {@link Tensor} and
 * closing it afterwards.
 */
public static JTensor create(final Tensor<?> tensor) {
  final JTensor jt;
  try {
    switch (tensor.dataType()) {
      case STRING:
        if (tensor.numDimensions() == 0) {
          final String value = new String(tensor.bytesValue(), UTF_8);
          jt =
              new AutoValue_JTensor(
                  tensor.dataType(), tensor.numDimensions(), tensor.shape(), value);
        } else {
          final int[] dimensions = toIntExact(tensor.shape());
          final Object byteArray =
              tensor.copyTo(Array.newInstance(byte[].class, toIntExact(tensor.shape())));
          jt =
              new AutoValue_JTensor(
                  tensor.dataType(),
                  tensor.numDimensions(),
                  tensor.shape(),
                  toStringArray(byteArray, tensor.numElements(), dimensions));
        }
        break;
      case INT32:
        final IntBuffer intBuf = IntBuffer.allocate(tensor.numElements());
        tensor.writeTo(intBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), intBuf.array());
        break;
      case INT64:
        final LongBuffer longBuf = LongBuffer.allocate(tensor.numElements());
        tensor.writeTo(longBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), longBuf.array());
        break;
      case FLOAT:
        final FloatBuffer floatBuf = FloatBuffer.allocate(tensor.numElements());
        tensor.writeTo(floatBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), floatBuf.array());
        break;
      case DOUBLE:
        final DoubleBuffer doubleBuf = DoubleBuffer.allocate(tensor.numElements());
        tensor.writeTo(doubleBuf);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), doubleBuf.array());
        break;
      case BOOL:
        final boolean[] array = new boolean[tensor.numElements()];
        tensor.copyTo(array);
        jt =
            new AutoValue_JTensor(
                tensor.dataType(), tensor.numDimensions(), tensor.shape(), array);
        break;
      default:
        throw new IllegalStateException("Unsupported data type " + tensor.dataType());
    }
  } finally {
    tensor.close();
  }

  return jt;
}
 
Example 14
Source File: TFPredictor.java    From JFoolNLTK with Apache License 2.0 4 votes vote down vote up
public List<List<Integer>> predict(List<List<Integer>> sents) {
    List<List<Integer>> path = new ArrayList();

    int sentLength = sents.size();
    int maxSentLength = 0;
    int[] sentLengths = new int[sentLength];

    for (int i = 0; i < sentLength; i++) {
        int s = sents.get(i).size();
        sentLengths[i] = s;
        if (maxSentLength < s) {
            maxSentLength = s;
        }
    }

    Tensor inputx = TFUtil.createTensor(sents, maxSentLength);
    Tensor lengths = Tensor.create(sentLengths);
    Tensor logits = session.runner()
            .feed("char_inputs", inputx)
            .feed("dropout", dropT)
            .feed("lengths", lengths)
            .fetch("project/logits").run().get(0);

    if (tranValue == null){
        tranValue = new float[numClass + 1][numClass + 1];
        Tensor trans = session.runner()
                .feed("char_inputs", inputx)
                .feed("dropout", dropT)
                .feed("lengths", lengths)
                .fetch("crf_loss/transitions").run().get(0);
        trans.copyTo(tranValue);
    }


    float[][][] flog = new float[sentLength][maxSentLength][this.numClass];

    logits.copyTo(flog);

    for (int i = 0; i < flog.length; i++) {
        List<Integer> sentPath = decode(flog[i], tranValue, sentLengths[i]);
        path.add(sentPath);
    }
    return path;
}
 
Example 15
Source File: DLHelpers.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public static RawDetections executeInceptionGraph(final Session s, final Tensor<Float> input, final int inputWidth, final int inputHeight, final int maxDetections, final int maskWidth, final int maskHeight) {
    // image metas
    //  meta = np.array(
    //        [image_id] +                  # size=1
    //        list(original_image_shape) +  # size=3
    //        list(image_shape) +           # size=3
    //        list(window) +                # size=4 (y1, x1, y2, x2) in image cooredinates
    //        [scale[0]] +                     # size=1 NO LONGER, I dont have time to correct this properly so take only the first element
    //        list(active_class_ids)        # size=num_classes
    //    )
    final FloatBuffer metas = FloatBuffer.wrap(new float[]{
            0,
            inputWidth,inputHeight,3,
            inputWidth,inputHeight,3,
            0,0,inputWidth,inputHeight,
            1,
            0,0
    });
    final Tensor<Float> meta_data = Tensor.create(new long[]{1,14},metas);

    List<Tensor<?>> res = s
            .runner()
            .feed("input_image", input)
            .feed("input_image_meta", meta_data)
            .feed("input_anchors", getAnchors(inputWidth))  // dtype float and shape [?,?,4]
            .fetch("mrcnn_detection/Reshape_1")   // mrcnn_mask/Reshape_1   mrcnn_detection/Reshape_1    mrcnn_bbox/Reshape     mrcnn_class/Reshape_1
            .fetch("mrcnn_mask/Reshape_1")
            .run();

    float[][][] res_detection = new float[1][maxDetections][6];   // mrcnn_detection/Reshape_1   -> y1,x1,y2,x2,class_id,probability (ordered desc)
    float[][][][][] res_mask = new float[1][maxDetections][maskHeight][maskWidth][2];   // mrcnn_mask/Reshape_1
    Tensor<Float> mrcnn_detection = res.get(0).expect(Float.class);
    Tensor<Float> mrcnn_mask = res.get(1).expect(Float.class);
    mrcnn_detection.copyTo(res_detection);
    mrcnn_mask.copyTo(res_mask);

    RawDetections rawDetections = new RawDetections();
    rawDetections.objectBB = res_detection;
    rawDetections.masks = res_mask;
    return rawDetections;
}
 
Example 16
Source File: InstSegMaskRCNN.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public RawDetections executeInceptionGraph(Session s, Tensor<Float> input) {
        // image metas
        //  meta = np.array(
        //        [image_id] +                  # size=1
        //        list(original_image_shape) +  # size=3
        //        list(image_shape) +           # size=3
        //        list(window) +                # size=4 (y1, x1, y2, x2) in image cooredinates
        //        [scale[0]] +                     # size=1 NO LONGER, I dont have time to correct this properly so take only the first element
        //        list(active_class_ids)        # size=num_classes
        //    )
        final FloatBuffer metas = FloatBuffer.wrap(new float[]{
                0,
                512,512,3,
                512,512,3,
                0,0,512,512,
                1,
                0,0
        });
        final Tensor<Float> meta_data = Tensor.create(new long[]{1,14},metas);

            List<Tensor<?>> res = s
            .runner()
            .feed("input_image", input)
            .feed("input_image_meta", meta_data)
            .feed("input_anchors", getAnchors())  // dtype float and shape [?,?,4]
            .fetch("mrcnn_detection/Reshape_1")   // mrcnn_mask/Reshape_1   mrcnn_detection/Reshape_1    mrcnn_bbox/Reshape     mrcnn_class/Reshape_1
            .fetch("mrcnn_mask/Reshape_1")
            .run();

            float[][][] res_detection = new float[1][512][6];   // mrcnn_detection/Reshape_1   -> y1,x1,y2,x2,class_id,probability (ordered desc)
            float[][][][][] res_mask = new float[1][512][28][28][2];   // mrcnn_mask/Reshape_1
            Tensor<Float> mrcnn_detection = res.get(0).expect(Float.class);
            Tensor<Float> mrcnn_mask = res.get(1).expect(Float.class);
            mrcnn_detection.copyTo(res_detection);
            mrcnn_mask.copyTo(res_mask);

            RawDetections rawDetections = new RawDetections();
            rawDetections.objectBB = res_detection;
            rawDetections.masks = res_mask;
            return rawDetections;
}