-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b1c592
commit 1d5492e
Showing
22 changed files
with
1,386 additions
and
74 deletions.
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
api/src/main/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/convolutional/Conv1DTranspose.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,103 @@ | ||
/* | ||
* Copyright 2021-2022 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.convolutional | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.activation.Activations | ||
import org.jetbrains.kotlinx.dl.api.core.initializer.HeNormal | ||
import org.jetbrains.kotlinx.dl.api.core.initializer.HeUniform | ||
import org.jetbrains.kotlinx.dl.api.core.initializer.Initializer | ||
import org.jetbrains.kotlinx.dl.api.core.layer.NoGradients | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.Conv1D.Companion.EXTRA_DIM | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.Conv1D.Companion.expand | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.Conv1D.Companion.expandKernel | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.Conv1D.Companion.withAdded | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.Conv1D.Companion.withExpandedDimensions | ||
import org.jetbrains.kotlinx.dl.api.core.layer.convolutional.Conv2DTranspose.Companion.withStandardPadding | ||
import org.jetbrains.kotlinx.dl.api.core.layer.requireArraySize | ||
import org.jetbrains.kotlinx.dl.api.core.layer.toLongList | ||
import org.jetbrains.kotlinx.dl.api.core.regularizer.Regularizer | ||
import org.tensorflow.Operand | ||
import org.tensorflow.op.Ops | ||
|
||
/** | ||
* 1D convolution transpose layer. | ||
* | ||
* This is an operation going in the opposite direction of a normal convolution: | ||
* it transforms a tensor shaped like an output of some convolution into tensor that has the shape of the input. | ||
* | ||
* This layer expects input data of size `(N, L, C)` where | ||
* ``` | ||
* N - batch size | ||
* L - length of signal sequence | ||
* C - number of channels | ||
* ``` | ||
* | ||
* Note: dilation values greater than 1 are not supported on cpu | ||
* (see https://github.com/tensorflow/tensorflow/issues/28264). | ||
* | ||
* @property [filters] dimensionality of the output space (i.e. the number of filters in the convolution) | ||
* @property [kernelLength] size of the convolutional kernel (one number) | ||
* @property [strides] strides of the convolution for each dimension of the input tensor (three numbers) | ||
* @property [dilations] dilations of the convolution for each dimension of the input tensor (three numbers). | ||
* Currently, dilation values greater than 1 are not supported on cpu. | ||
* @property [activation] activation function | ||
* @property [kernelInitializer] initializer for the kernel | ||
* @property [biasInitializer] initializer for the bias | ||
* @property [kernelRegularizer] regularizer for the kernel | ||
* @property [biasRegularizer] regularizer for the bias | ||
* @property [activityRegularizer] regularizer function applied to the output of the layer | ||
* @property [padding] type of padding to use | ||
* @property [outputPadding] the amount of explicit padding to use (six numbers: two for each dimension). | ||
* @property [useBias] a flag that specifies if the bias should be used | ||
* @param [name] custom layer name | ||
*/ | ||
public class Conv1DTranspose( | ||
public override val filters: Int = 3, | ||
public val kernelLength: Int = 3, | ||
public override val strides: IntArray = intArrayOf(1, 1, 1), | ||
public override val dilations: IntArray = intArrayOf(1, 1, 1), | ||
public override val activation: Activations = Activations.Relu, | ||
public override val kernelInitializer: Initializer = HeNormal(), | ||
public override val biasInitializer: Initializer = HeUniform(), | ||
public override val kernelRegularizer: Regularizer? = null, | ||
public override val biasRegularizer: Regularizer? = null, | ||
public override val activityRegularizer: Regularizer? = null, | ||
public override val padding: ConvPadding = ConvPadding.SAME, | ||
public override val outputPadding: IntArray? = null, | ||
public override val useBias: Boolean = true, | ||
name: String = "" | ||
) : ConvTranspose(dimensions = 1, name), NoGradients { | ||
|
||
init { | ||
requireArraySize(strides, dimensions + 2, "strides") | ||
requireArraySize(dilations, dimensions + 2, "dilations") | ||
if (outputPadding != null) requireArraySize(outputPadding, 2 * (dimensions + 2), "outputPadding") | ||
isTrainable = false | ||
} | ||
|
||
override val kernelSize: IntArray = intArrayOf(kernelLength) | ||
|
||
override fun convImplementation(tf: Ops, input: Operand<Float>): Operand<Float> { | ||
return tf.withExpandedDimensions(input) { expandedInput -> | ||
val expandedOutputPadding = outputPadding?.withAdded(EXTRA_DIM * 2, listOf(0, 0)) | ||
return@withExpandedDimensions tf.nn.conv2dBackpropInput( | ||
tf.shapeWithDynamicBatchSize(outputShape.expand(), input), | ||
tf.expandKernel(kernel.variable), | ||
expandedInput, | ||
expand(strides).toLongList(), | ||
if (outputPadding != null) Conv2DTranspose.EXPLICIT else padding.paddingName, | ||
*Conv2DTranspose.buildOptions( | ||
expand(dilations), | ||
expandedOutputPadding?.withStandardPadding( | ||
padding, | ||
expandKernel(kernelSize), | ||
expand(dilations) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
api/src/main/kotlin/org/jetbrains/kotlinx/dl/api/core/layer/convolutional/Conv2DTranspose.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,126 @@ | ||
/* | ||
* Copyright 2021-2022 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.convolutional | ||
|
||
import org.jetbrains.kotlinx.dl.api.core.activation.Activations | ||
import org.jetbrains.kotlinx.dl.api.core.initializer.HeNormal | ||
import org.jetbrains.kotlinx.dl.api.core.initializer.HeUniform | ||
import org.jetbrains.kotlinx.dl.api.core.initializer.Initializer | ||
import org.jetbrains.kotlinx.dl.api.core.layer.NoGradients | ||
import org.jetbrains.kotlinx.dl.api.core.layer.requireArraySize | ||
import org.jetbrains.kotlinx.dl.api.core.layer.toLongList | ||
import org.jetbrains.kotlinx.dl.api.core.regularizer.Regularizer | ||
import org.jetbrains.kotlinx.dl.api.core.shape.convTransposeSingleSidePadding | ||
import org.tensorflow.Operand | ||
import org.tensorflow.op.Ops | ||
import org.tensorflow.op.nn.Conv2dBackpropInput | ||
|
||
/** | ||
* 2D convolution transpose layer. | ||
* | ||
* This is an operation going in the opposite direction of a normal convolution: | ||
* it transforms a tensor shaped like an output of some convolution into tensor that has the shape of the input. | ||
* | ||
* This layer expects input data of size `(N, H, W, C)` where | ||
* ``` | ||
* N - batch size | ||
* H - height | ||
* W - width | ||
* C - number of channels | ||
* ``` | ||
* | ||
* Note: dilation values greater than 1 are not supported on cpu | ||
* (see https://github.com/tensorflow/tensorflow/issues/28264). | ||
* | ||
* @property [filters] dimensionality of the output space (i.e. the number of filters in the convolution) | ||
* @property [kernelSize] size of the convolutional kernel (two numbers) | ||
* @property [strides] strides of the convolution for each dimension of the input tensor (four numbers) | ||
* @property [dilations] dilations of the convolution for each dimension of the input tensor (four numbers). | ||
* Currently, dilation values greater than 1 are not supported on cpu. | ||
* @property [activation] activation function | ||
* @property [kernelInitializer] initializer for the kernel | ||
* @property [biasInitializer] initializer for the bias | ||
* @property [kernelRegularizer] regularizer for the kernel | ||
* @property [biasRegularizer] regularizer for the bias | ||
* @property [activityRegularizer] regularizer function applied to the output of the layer | ||
* @property [padding] type of padding to use | ||
* @property [outputPadding] the amount of explicit padding to use (eight numbers: two for each dimension). | ||
* @property [useBias] a flag that specifies if the bias should be used | ||
* @param [name] custom layer name | ||
*/ | ||
public class Conv2DTranspose( | ||
public override val filters: Int = 3, | ||
public override val kernelSize: IntArray = intArrayOf(3, 3), | ||
public override val strides: IntArray = intArrayOf(1, 1, 1, 1), | ||
public override val dilations: IntArray = intArrayOf(1, 1, 1, 1), | ||
public override val activation: Activations = Activations.Relu, | ||
public override val kernelInitializer: Initializer = HeNormal(), | ||
public override val biasInitializer: Initializer = HeUniform(), | ||
public override val kernelRegularizer: Regularizer? = null, | ||
public override val biasRegularizer: Regularizer? = null, | ||
public override val activityRegularizer: Regularizer? = null, | ||
public override val padding: ConvPadding = ConvPadding.SAME, | ||
public override val outputPadding: IntArray? = null, | ||
public override val useBias: Boolean = true, | ||
name: String = "" | ||
) : ConvTranspose(dimensions = 2, name), NoGradients { | ||
|
||
init { | ||
requireArraySize(kernelSize, dimensions, "kernelSize") | ||
requireArraySize(strides, dimensions + 2, "strides") | ||
requireArraySize(dilations, dimensions + 2, "dilations") | ||
if (outputPadding != null) requireArraySize(outputPadding, 2 * (dimensions + 2), "outputPadding") | ||
isTrainable = false | ||
} | ||
|
||
override fun convImplementation(tf: Ops, input: Operand<Float>): Operand<Float> { | ||
return tf.nn.conv2dBackpropInput( | ||
tf.shapeWithDynamicBatchSize(outputShape, input), | ||
kernel.variable, | ||
input, | ||
strides.toLongList(), | ||
if (outputPadding != null) EXPLICIT else padding.paddingName, | ||
*buildOptions( | ||
dilations, | ||
outputPadding?.withStandardPadding( | ||
padding, | ||
kernelSize, | ||
dilations | ||
) | ||
) | ||
) | ||
} | ||
|
||
internal companion object { | ||
internal const val EXPLICIT = "EXPLICIT" | ||
|
||
/** | ||
* Combines explicitly provided padding value with the standard padding from the provided padding method. | ||
* This is needed since [org.tensorflow.op.NnOps.conv2dBackpropInput] function does not support specifying | ||
* both padding method and explicit output padding at the same time. | ||
*/ | ||
internal fun IntArray.withStandardPadding(padding: ConvPadding, | ||
kernelSize: IntArray, | ||
dilations: IntArray | ||
): IntArray { | ||
val withStandardPadding = kernelSize.indices.flatMap { dim -> | ||
listOf( | ||
convTransposeSingleSidePadding(padding, this[2 * dim], kernelSize[dim], dilations[dim + 1]), | ||
convTransposeSingleSidePadding(padding, this[2 * dim + 1], kernelSize[dim], dilations[dim + 1]) | ||
) | ||
} | ||
return intArrayOf(0, 0, *(withStandardPadding.toIntArray()), 0, 0) | ||
} | ||
|
||
internal fun buildOptions(dilations: IntArray, outputPadding: IntArray?): Array<Conv2dBackpropInput.Options> { | ||
val options = mutableListOf(Conv2dBackpropInput.dilations(dilations.toLongList())) | ||
if (outputPadding != null) { | ||
options.add(Conv2dBackpropInput.explicitPaddings(outputPadding.toLongList())) | ||
} | ||
return options.map { it.dataFormat("NHWC") }.toTypedArray() | ||
} | ||
} | ||
} |
Oops, something went wrong.