From a43a95b7028488cd84ff141c1e793094c941e33b Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Tue, 13 Apr 2021 08:52:17 +0200 Subject: [PATCH] Upgrade to OPENRNDR 0.3.47, Javacpp 1.5.5, Tensorflow 0.3.1, Gradle 7.0 --- build.gradle | 12 ++--- gradle/wrapper/gradle-wrapper.properties | 2 +- orx-tensorflow/src/main/kotlin/Tensor.kt | 65 +++++++++++------------- 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/build.gradle b/build.gradle index 39245755..7543c0ee 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { } } plugins { - id 'org.jetbrains.kotlin.jvm' version '1.4.31' + id 'org.jetbrains.kotlin.jvm' version '1.4.32' } def openrndrUseSnapshot = false @@ -18,15 +18,15 @@ def openrndrUseSnapshot = false apply plugin: 'org.jetbrains.dokka' project.ext { - openrndrVersion = openrndrUseSnapshot? "0.4.0-SNAPSHOT" : "0.3.47-rc.2" + openrndrVersion = openrndrUseSnapshot? "0.4.0-SNAPSHOT" : "0.3.47" kotlinVersion = "1.4.31" spekVersion = "2.0.15" - libfreenectVersion = "0.5.7-1.5.4" - librealsense2Version = "2.29.0-1.5.4" + libfreenectVersion = "0.5.7-1.5.5" + librealsense2Version = "2.40.0-1.5.5" gsonVersion = "2.8.6" antlrVersion = "4.9.1" - tensorflowVersion = "0.2.0" - mklDnnVersion = "0.21.5-1.5.4" + tensorflowVersion = "0.3.1" + mklDnnVersion = "0.21.5-1.5.5" } switch (org.gradle.internal.os.OperatingSystem.current()) { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 442d9132..f371643e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/orx-tensorflow/src/main/kotlin/Tensor.kt b/orx-tensorflow/src/main/kotlin/Tensor.kt index 6cfa163a..4e27fa76 100644 --- a/orx-tensorflow/src/main/kotlin/Tensor.kt +++ b/orx-tensorflow/src/main/kotlin/Tensor.kt @@ -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) { +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) { +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.copyTo(colorBuffer: ColorBuffer) { + +fun TFloat32.copyTo(colorBuffer: ColorBuffer) { val s = shape() val components = when { @@ -54,92 +57,84 @@ fun Tensor.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 Tensor.summary() { - println("type: ${this.dataType().name()}") +fun TType.summary() { + println("type: ${this.dataType().name}") println("shape: [${this.shape().asArray().joinToString(", ")}]") } -fun Tensor.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.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.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.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.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.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.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.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.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.toColorBuffer(target: ColorBuffer? = null): ColorBuffer { @JvmName("toColorBufferTInt8") -fun Tensor.toColorBuffer(target: ColorBuffer? = null): ColorBuffer { +fun TUint8.toColorBuffer(target: ColorBuffer? = null): ColorBuffer { val s = shape() require(s.numDimensions() == 2 || s.numDimensions() == 3)