Java Code Examples for java.nio.LongBuffer#array()

The following examples show how to use java.nio.LongBuffer#array() . 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: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * ByteChannelからlong配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static long[] readLongArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 8).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 8) throw new IOException();
	buf.clear();
	final LongBuffer result = buf.asLongBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final long[] b = new long[n];
		result.get(b);
		return b;
	}
}
 
Example 2
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 3
Source File: TensorUtil.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
static
public long[] toLongArray(Tensor tensor){
	LongBuffer longBuffer = LongBuffer.allocate(tensor.numElements());

	tensor.writeTo(longBuffer);

	return longBuffer.array();
}
 
Example 4
Source File: LongPointer.java    From tapir with MIT License 5 votes vote down vote up
/**
 * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers
 * backed with an array, allocates enough memory for the array and copies it.
 *
 * @param buffer the Buffer to reference or copy
 * @see #put(long[])
 */
public LongPointer(LongBuffer buffer) {
    super(buffer);
    if (buffer != null && buffer.hasArray()) {
        long[] array = buffer.array();
        allocateArray(array.length);
        put(array);
        position(buffer.position());
        limit(buffer.limit());
    }
}
 
Example 5
Source File: LongTensorTypeSupport.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Override
public List<Field> createListField(Tensor<Long> tensor, LongBuffer longBuffer) {
  List<Field> fields = new ArrayList<>();
  tensor.writeTo(longBuffer);
  long[] longs = longBuffer.array();
  for (long aLong : longs) {
    fields.add(Field.create(aLong));
  }
  return fields;
}
 
Example 6
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;
}