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

The following examples show how to use org.tensorflow.Tensor#shape() . 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: TensorJsonConverter.java    From tensorflow with Apache License 2.0 6 votes vote down vote up
public static String toJson(Tensor tensor) {

		// Retrieve all bytes in the buffer
		ByteBuffer buffer = ByteBuffer.allocate(tensor.numBytes());
		tensor.writeTo(buffer);
		buffer.clear();
		byte[] bytes = new byte[buffer.capacity()];
		buffer.get(bytes, 0, bytes.length);

		long[] shape = tensor.shape();

		String bytesBase64 = Base64.getEncoder().encodeToString(bytes);

		return String.format("{ \"type\": \"%s\", \"shape\": %s, \"value\": \"%s\" }",
				tensor.dataType().name(), Arrays.toString(shape), bytesBase64);
	}
 
Example 2
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 3
Source File: DeepLab.java    From cineast with MIT License 6 votes vote down vote up
public synchronized int[][] processImage(Tensor<UInt8> input) {

    Tensor<Long> result = session.runner().feed("ImageTensor", input)
        .fetch("SemanticPredictions").run().get(0).expect(Long.class);

    int len = result.numElements();
    LongBuffer buf = LongBuffer.allocate(len);
    result.writeTo(buf);
    result.close();

    long[] resultShape = result.shape();
    long[] resultArray = buf.array();

    int w = (int) resultShape[2];
    int h = (int) resultShape[1];

    int[][] resultMatrix = new int[w][h];

    for (int i = 0; i < resultArray.length; ++i) {
      resultMatrix[i % w][i / w] = (int) resultArray[i];
    }

    return resultMatrix;
  }
 
Example 4
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 5
Source File: TensorFlowConverters.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
public SerializedNDArray convert(Tensor<?> from){
    long[] shape = from.shape();
    NDArrayType t = TensorFlowUtil.fromTFType(from.dataType());

    int w = t.width();
    long length = ArrayUtil.prodLong(shape);
    long lengthBytes = w * length;
    ByteBuffer bb = ByteBuffer.allocateDirect((int)lengthBytes).order(ByteOrder.nativeOrder());     //TODO SerializedNDArray should be in little endian...
    from.writeTo(bb);

    return new SerializedNDArray(t, shape, bb);
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: YOLOClassifier.java    From tensorflow-java-examples-spring with Do What The F*ck You Want To Public License 2 votes vote down vote up
/**
 * Gets the number of classes based on the tensor shape
 *
 * @param result - the tensorflow output
 * @return the number of classes
 */
public int getOutputSizeByShape(Tensor<Float> result) {
    return (int) (result.shape()[3] * Math.pow(SIZE,2));
}
 
Example 11
Source File: YOLOClassifier.java    From tensorflow-example-java with Do What The F*ck You Want To Public License 2 votes vote down vote up
/**
 * Gets the number of classes based on the tensor shape
 *
 * @param result - the tensorflow output
 * @return the number of classes
 */
public int getOutputSizeByShape(Tensor<Float> result) {
    return (int) (result.shape()[3] * Math.pow(SIZE,2));
}
 
Example 12
Source File: YOLO.java    From cineast with MIT License 2 votes vote down vote up
/**
 * Gets the number of classes based on the tensor shape
 *
 * @param result - the tensorflow output
 * @return the number of classes
 */
private int getOutputSizeByShape(Tensor<Float> result) {
  return (int) (result.shape()[3] * Math.pow(SIZE, 2));
}