-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add AvgPool3D layer #99
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a7e343d
Add AvgPool3D layer
mkaze 90753b7
Merge branch 'master' into fix-81
mkaze 12be2f6
Remove dataFormat; Update poolSize and strides types; Update with rec…
mkaze 195f320
Fix function names
mkaze ca84bde
Merge branch 'master' into fix-81
mkaze 3bd564a
Change strides property type and default value
mkaze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
api/src/main/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/pooling/AvgPool3D.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2020 JetBrains s.r.o. and Kotlin Deep Learning project contributors. All Rights Reserved. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlinx.dl.api.core.layer.pooling | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.KGraph | ||
import org.jetbrains.kotlinx.dl.api.core.layer.Layer | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.ConvPadding | ||
import org.jetbrains.kotlinx.dl.api.core.shape.convOutputLength | ||
import org.tensorflow.Operand | ||
import org.tensorflow.Shape | ||
import org.tensorflow.op.Ops | ||
|
||
/** | ||
* Average pooling operation for 3D data (e.g. video, spatio-temporal). | ||
* | ||
* Downsamples the input by taking the average over a window of size [poolSize]. | ||
* | ||
* @property [poolSize] Size of the pooling window for each dimension of input. | ||
* @property [strides] The amount of shift for pooling window per each input dimension in each pooling step. | ||
* @property [padding] Padding strategy; can be either of [ConvPadding.VALID] which means no | ||
* padding, or [ConvPadding.SAME] which means padding the input equally such that the output | ||
* has the same dimension as the input. | ||
*/ | ||
public class AvgPool3D( | ||
public val poolSize: LongArray = longArrayOf(1, 2, 2, 2, 1), | ||
public val strides: LongArray = longArrayOf(1, 2, 2, 2, 1), | ||
public val padding: ConvPadding = ConvPadding.VALID, | ||
name: String = "" | ||
) : Layer(name) { | ||
|
||
override val hasActivation: Boolean | ||
get() = false | ||
override val paramCount: Int | ||
get() = 0 | ||
override var weights: Map<String, Array<*>> | ||
get() = emptyMap() | ||
set(value) = assignWeights(value) | ||
|
||
init { | ||
require(poolSize.size == 5) { | ||
"The poolSize should be an array of size 5." | ||
} | ||
|
||
require(strides.size == 5) { | ||
"The strides should be an array of size 5." | ||
} | ||
|
||
require(padding == ConvPadding.VALID || padding == ConvPadding.SAME) { | ||
"The padding should be either ${ConvPadding.VALID} or ${ConvPadding.SAME}." | ||
} | ||
} | ||
|
||
override fun build(tf: Ops, kGraph: KGraph, inputShape: Shape) {} | ||
|
||
override fun computeOutputShape(inputShape: Shape): Shape { | ||
var dim1 = inputShape.size(1) | ||
var dim2 = inputShape.size(2) | ||
var dim3 = inputShape.size(3) | ||
dim1 = convOutputLength(dim1, poolSize[1].toInt(), padding, strides[1].toInt()) | ||
dim2 = convOutputLength(dim2, poolSize[2].toInt(), padding, strides[2].toInt()) | ||
dim3 = convOutputLength(dim3, poolSize[3].toInt(), padding, strides[3].toInt()) | ||
|
||
return Shape.make(inputShape.size(0), dim1, dim2, dim3, inputShape.size(4)) | ||
} | ||
|
||
override fun forward( | ||
tf: Ops, | ||
input: Operand<Float>, | ||
isTraining: Operand<Boolean>, | ||
numberOfLosses: Operand<Float>? | ||
): Operand<Float> { | ||
val tfPadding = padding.paddingName | ||
return tf.nn.avgPool3d( | ||
input, | ||
poolSize.toList(), | ||
strides.toList(), | ||
tfPadding | ||
) | ||
} | ||
|
||
override fun toString(): String = | ||
"AvgPool3D(poolSize=$poolSize, strides=$strides, padding=$padding)" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
244 changes: 244 additions & 0 deletions
244
api/src/test/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/AvgPool3DTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
/* | ||
* Copyright 2020 JetBrains s.r.o. and Kotlin Deep Learning project contributors. All Rights Reserved. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file. | ||
*/ | ||
|
||
package org.jetbrains.kotlinx.dl.api.core.layer | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.KGraph | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.ConvPadding | ||
import org.jetbrains.kotlinx.dl.api.core.layer.pooling.AvgPool3D | ||
import org.jetbrains.kotlinx.dl.api.core.shape.toIntArray | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
import org.tensorflow.EagerSession | ||
import org.tensorflow.Graph | ||
import org.tensorflow.Shape | ||
import org.tensorflow.op.Ops | ||
|
||
const val EPS: Float = 1e-6f | ||
|
||
internal class AvgPool3DTest { | ||
|
||
private val input = arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
floatArrayOf(1.0f, -2.0f, 3.0f), | ||
floatArrayOf(0.5f, 2.0f, 5.0f), | ||
floatArrayOf(-1.0f, 3.0f, 2.0f), | ||
floatArrayOf(1.5f, -1.0f, 0.5f) | ||
), | ||
arrayOf( | ||
floatArrayOf(-1.0f, 2.0f, -2.0f), | ||
floatArrayOf(2.5f, 3.0f, 1.0f), | ||
floatArrayOf(-2.0f, 3.0f, 2.5f), | ||
floatArrayOf(-3.0f, 1.0f, 1.5f) | ||
), | ||
), | ||
arrayOf( | ||
arrayOf( | ||
floatArrayOf(1.0f, 3.0f, 1.0f), | ||
floatArrayOf(6.0f, -2.5f, 4.0f), | ||
floatArrayOf(7.0f, 0.0f, 5.0f), | ||
floatArrayOf(1.0f, 2.0f, 4.0f) | ||
), | ||
arrayOf( | ||
floatArrayOf(7.0f, -3.0f, 2.0f), | ||
floatArrayOf(1.0f, 2.0f, 2.0f), | ||
floatArrayOf(3.0f, 5.0f, -2.0f), | ||
floatArrayOf(3.0f, -1.0f, 0.0f) | ||
), | ||
), | ||
), | ||
) | ||
|
||
private val inputShape: Shape = Shape.make( | ||
input.size.toLong(), | ||
input[0].size.toLong(), | ||
input[0][0].size.toLong(), | ||
input[0][0][0].size.toLong(), | ||
input[0][0][0][0].size.toLong(), | ||
) | ||
|
||
@Test | ||
fun default() { | ||
val layer = AvgPool3D() | ||
val expected = arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
floatArrayOf(18.0f/8, 4.5f/8, 16.0f/8), | ||
floatArrayOf(9.5f/8, 12.0f/8, 13.5f/8), | ||
), | ||
), | ||
), | ||
) | ||
|
||
EagerSession.create().use { | ||
val tf = Ops.create() | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
|
||
val inputOp = tf.constant(input) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput() | ||
|
||
// Check output shape is correct. | ||
val expectedShape = intArrayOf(input.size, 1, 1, 2, input[0][0][0][0].size) | ||
Assertions.assertArrayEquals( | ||
expectedShape, | ||
output.shape().toIntArray() | ||
) | ||
|
||
// Check output values are correct. | ||
val actual = Array(input.size) { | ||
Array(1) { Array(1) { Array(2) { FloatArray(input[0][0][0][0].size) } } } | ||
} | ||
output.tensor().copyTo(actual) | ||
for (i in expected.indices) { | ||
for (j in expected[i].indices) { | ||
for (k in expected[i][j].indices) { | ||
for (l in expected[i][j][k].indices) { | ||
Assertions.assertArrayEquals( | ||
expected[i][j][k][l], | ||
actual[i][j][k][l], | ||
EPS | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun withPaddingAndStride() { | ||
val layer = AvgPool3D(strides = longArrayOf(1, 1, 1, 1, 1), padding = ConvPadding.SAME) | ||
val expected = arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
floatArrayOf(18.0f/8, 4.5f/8, 16.0f/8), | ||
floatArrayOf(17.0f/8, 15.5f/8, 19.5f/8), | ||
floatArrayOf(9.5f/8, 12.0f/8, 13.5f/8), | ||
floatArrayOf(5.0f/8, 2.0f/8, 12.0f/8) | ||
), | ||
arrayOf( | ||
floatArrayOf(19.0f/8, 8.0f/8, 6.0f/8), | ||
floatArrayOf(9.0f/8, 26.0f/8, 7.0f/8), | ||
floatArrayOf(2.0f/8, 16.0f/8, 4.0f/8), | ||
floatArrayOf(0.0f/8, 0.0f/8, 6.0f/8) | ||
), | ||
), | ||
arrayOf( | ||
arrayOf( | ||
floatArrayOf(30.0f/8, -1.0f/8, 18.0f/8), | ||
floatArrayOf(34.0f/8, 9.0f/8, 18.0f/8), | ||
floatArrayOf(28.0f/8, 12.0f/8, 14.0f/8), | ||
floatArrayOf(16.0f/8, 4.0f/8, 16.0f/8) | ||
), | ||
arrayOf( | ||
floatArrayOf(32.0f/8, -4.0f/8, 16.0f/8), | ||
floatArrayOf(16.0f/8, 28.0f/8, 0.0f/8), | ||
floatArrayOf(24.0f/8, 16.0f/8, -8.0f/8), | ||
floatArrayOf(24.0f/8, -8.0f/8, 0.0f/8) | ||
), | ||
), | ||
), | ||
) | ||
|
||
EagerSession.create().use { | ||
val tf = Ops.create() | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
|
||
val inputOp = tf.constant(input) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput() | ||
|
||
// Check output shape is correct. | ||
val expectedShape = inputShape.toIntArray() | ||
Assertions.assertArrayEquals( | ||
expectedShape, | ||
output.shape().toIntArray() | ||
) | ||
|
||
// Check output values are correct. | ||
val actual = Array(input.size) { | ||
Array(input[0].size) { | ||
Array(input[0][0].size) { | ||
Array(input[0][0][0].size) { | ||
FloatArray(input[0][0][0][0].size) | ||
} | ||
} | ||
} | ||
} | ||
output.tensor().copyTo(actual) | ||
for (i in expected.indices) { | ||
for (j in expected[i].indices) { | ||
for (k in expected[i][j].indices) { | ||
for (l in expected[i][j][k].indices) { | ||
Assertions.assertArrayEquals( | ||
expected[i][j][k][l], | ||
actual[i][j][k][l], | ||
EPS | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun withPoolSizeAndStride() { | ||
val layer = AvgPool3D(poolSize = longArrayOf(1, 2, 2, 3, 1), strides = longArrayOf(1, 1, 1, 1, 1)) | ||
val expected = arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
arrayOf( | ||
floatArrayOf(25.0f/12, 15.5f/12, 23.5f/12), | ||
floatArrayOf(19.5f/12, 16.5f/12, 25.5f/12), | ||
), | ||
), | ||
), | ||
) | ||
|
||
EagerSession.create().use { | ||
val tf = Ops.create() | ||
layer.build(tf, KGraph(Graph().toGraphDef()), inputShape) | ||
|
||
val inputOp = tf.constant(input) | ||
val isTraining = tf.constant(true) | ||
val numberOfLosses = tf.constant(1.0f) | ||
val output = layer.forward(tf, inputOp, isTraining, numberOfLosses).asOutput() | ||
|
||
// Check output shape is correct. | ||
val expectedShape = intArrayOf(input.size, 1, 1, 2, input[0][0][0][0].size) | ||
Assertions.assertArrayEquals( | ||
expectedShape, | ||
output.shape().toIntArray() | ||
) | ||
|
||
// Check output values are correct. | ||
val actual = Array(input.size) { | ||
Array(1) { Array(1) { Array(2) { FloatArray(input[0][0][0][0].size) } } } | ||
} | ||
output.tensor().copyTo(actual) | ||
for (i in expected.indices) { | ||
for (j in expected[i].indices) { | ||
for (k in expected[i][j].indices) { | ||
for (l in expected[i][j][k].indices) { | ||
Assertions.assertArrayEquals( | ||
expected[i][j][k][l], | ||
actual[i][j][k][l], | ||
EPS | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you take these values from Keras tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I created the test data myself :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not bad:)
Sometimes I just copied test data from Keras; sometimes, if test data seems not appropriate, I use Keras like a test machine writing some simple test in Python to put some test data in inputs and get outputs. A few times, I ignored this method and just used tested primitive itself to get data, but it has a bug in implementation and, as a result, a wrong test:(( But I agree that test inputs/outputs for pooling could be calculated manually (just shared my experience)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, actually I have created test data for all test cases I have written so far; but certainly it's not a sustainable approach and using another tested tool, like Keras or maybe even numpy, for generating test data seems to be a better and more reliable approach.