Skip to content

Commit

Permalink
Upgrade to OPENRNDR 0.3.47, Javacpp 1.5.5, Tensorflow 0.3.1, Gradle 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinRNDR committed Apr 13, 2021
1 parent d2c77a0 commit a43a95b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ buildscript {
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.31'
id 'org.jetbrains.kotlin.jvm' version '1.4.32'
}

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()) {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
65 changes: 30 additions & 35 deletions orx-tensorflow/src/main/kotlin/Tensor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit a43a95b

Please sign in to comment.