Upgrade to OPENRNDR 0.3.47, Javacpp 1.5.5, Tensorflow 0.3.1, Gradle 7.0

This commit is contained in:
Edwin Jakobs
2021-04-13 08:52:17 +02:00
parent d2c77a0ba4
commit a43a95b702
3 changed files with 37 additions and 42 deletions

View File

@@ -6,35 +6,38 @@ import org.openrndr.draw.ColorFormat
import org.openrndr.draw.ColorType
import org.openrndr.draw.colorBuffer
import org.openrndr.extra.tensorflow.arrays.*
import org.tensorflow.Output
import org.tensorflow.Tensor
import org.tensorflow.ndarray.StdArrays
import org.tensorflow.ndarray.buffer.DataBuffers
import org.tensorflow.op.math.Add
import org.tensorflow.types.*
import org.tensorflow.types.family.TType
import java.nio.ByteBuffer
import java.nio.ByteOrder
fun ColorBuffer.copyTo(tensor: Tensor<TFloat32>) {
fun ColorBuffer.copyTo(tensor: TFloat32) {
val buffer = ByteBuffer.allocateDirect(effectiveWidth * effectiveHeight * format.componentCount * 4)
buffer.order(ByteOrder.nativeOrder())
this.read(buffer, targetType = ColorType.FLOAT32)
buffer.rewind()
val dataBuffer = DataBuffers.of(buffer.asFloatBuffer())
tensor.data().write(dataBuffer)
tensor.write(dataBuffer)
}
@JvmName("copyToTUint8")
fun ColorBuffer.copyTo(tensor: Tensor<TUint8>) {
fun ColorBuffer.copyTo(tensor: TUint8) {
val buffer = ByteBuffer.allocateDirect(effectiveWidth * effectiveHeight * format.componentCount)
buffer.order(ByteOrder.nativeOrder())
this.read(buffer, targetType = ColorType.UINT8)
buffer.rewind()
val dataBuffer = DataBuffers.of(buffer)
tensor.data().write(dataBuffer)
tensor.write(dataBuffer)
}
fun Tensor<TFloat32>.copyTo(colorBuffer: ColorBuffer) {
fun TFloat32.copyTo(colorBuffer: ColorBuffer) {
val s = shape()
val components = when {
@@ -54,92 +57,84 @@ fun Tensor<TFloat32>.copyTo(colorBuffer: ColorBuffer) {
val buffer = ByteBuffer.allocateDirect(this.numBytes().toInt())
buffer.order(ByteOrder.nativeOrder())
val dataBuffer = DataBuffers.of(buffer.asFloatBuffer())
data().read(dataBuffer)
this.read(dataBuffer)
buffer.rewind()
colorBuffer.write(buffer, sourceFormat = format, sourceType = ColorType.FLOAT32)
}
fun <T : TType> Tensor<T>.summary() {
println("type: ${this.dataType().name()}")
fun TType.summary() {
println("type: ${this.dataType().name}")
println("shape: [${this.shape().asArray().joinToString(", ")}]")
}
fun Tensor<TInt32>.toIntArray(): IntArray {
fun TInt32.toIntArray(): IntArray {
val elementCount = this.numBytes() / 4
val tensorData = data()
val targetArray = IntArray(elementCount.toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TInt64>.toLongArray(): LongArray {
fun TInt64.toLongArray(): LongArray {
val elementCount = this.numBytes() / 8
val tensorData = data()
val targetArray = LongArray(elementCount.toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TUint8>.toByteArray(): ByteArray {
fun TUint8.toByteArray(): ByteArray {
val elementCount = this.numBytes() / 8
val tensorData = data()
val targetArray = ByteArray(elementCount.toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TFloat32>.toFloatArray(): FloatArray {
fun TFloat32.toFloatArray(): FloatArray {
val elementCount = this.numBytes() / 4
val tensorData = data()
val targetArray = FloatArray(elementCount.toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TFloat32>.toFloatArray2D(): FloatArray2D {
fun TFloat32.toFloatArray2D(): FloatArray2D {
val shape = this.shape()
require(shape.numDimensions() == 2) {
"tensor has ${shape.numDimensions()} dimensions, need 2"
}
val tensorData = data()
val targetArray = floatArray2D(shape.size(0).toInt(), shape.size(1).toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TFloat32>.toFloatArray3D(): FloatArray3D {
fun TFloat32.toFloatArray3D(): FloatArray3D {
val shape = this.shape()
require(shape.numDimensions() == 3) {
"tensor has ${shape.numDimensions()} dimensions, need 3"
}
val tensorData = data()
val targetArray = floatArray3D(shape.size(0).toInt(), shape.size(1).toInt(), shape.size(2).toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TFloat32>.toFloatArray4D(): FloatArray4D {
fun TFloat32.toFloatArray4D(): FloatArray4D {
val shape = this.shape()
require(shape.numDimensions() == 4) {
"tensor has ${shape.numDimensions()} dimensions, need 4"
}
val tensorData = data()
val targetArray = floatArray4D(shape.size(0).toInt(), shape.size(1).toInt(), shape.size(2).toInt(), shape.size(3).toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TFloat64>.toDoubleArray(): DoubleArray {
fun TFloat64.toDoubleArray(): DoubleArray {
val elementCount = this.numBytes() / 8
val tensorData = data()
val targetArray = DoubleArray(elementCount.toInt())
StdArrays.copyFrom(tensorData, targetArray)
StdArrays.copyFrom(this, targetArray)
return targetArray
}
fun Tensor<TFloat32>.toColorBuffer(target: ColorBuffer? = null): ColorBuffer {
fun TFloat32.toColorBuffer(target: ColorBuffer? = null): ColorBuffer {
val s = shape()
require(s.numDimensions() == 2 || s.numDimensions() == 3)
@@ -168,7 +163,7 @@ fun Tensor<TFloat32>.toColorBuffer(target: ColorBuffer? = null): ColorBuffer {
@JvmName("toColorBufferTInt8")
fun Tensor<TUint8>.toColorBuffer(target: ColorBuffer? = null): ColorBuffer {
fun TUint8.toColorBuffer(target: ColorBuffer? = null): ColorBuffer {
val s = shape()
require(s.numDimensions() == 2 || s.numDimensions() == 3)