Skip to content

Commit

Permalink
Merge pull request #5026 from deeplearning4j/mp_up_sub_sampling_3d
Browse files Browse the repository at this point in the history
[WIP] 3D Utility layer suite
  • Loading branch information
maxpumperla committed May 9, 2018
2 parents 9e86a0d + e9d5bf1 commit 5200062
Show file tree
Hide file tree
Showing 38 changed files with 2,901 additions and 145 deletions.

Large diffs are not rendered by default.

Expand Up @@ -103,6 +103,7 @@ public class KerasLayerConfiguration {
private final String LAYER_CLASS_NAME_LEAKY_RELU = "LeakyReLU";
private final String LAYER_CLASS_NAME_UPSAMPLING_1D = "UpSampling1D";
private final String LAYER_CLASS_NAME_UPSAMPLING_2D = "UpSampling2D";
private final String LAYER_CLASS_NAME_UPSAMPLING_3D = "UpSampling3D";
private final String LAYER_CLASS_NAME_SEPARABLE_CONVOLUTION_2D = ""; // 1: SeparableConvolution2D, 2: SeparableConv2D
private final String LAYER_CLASS_NAME_DECONVOLUTION_2D = ""; // 1: Deconvolution2D, 2: Conv2DTranspose

Expand Down Expand Up @@ -201,6 +202,7 @@ public class KerasLayerConfiguration {
private final String LAYER_FIELD_POOL_1D_STRIDES = ""; // 1: stride, 2: strides
private final String LAYER_FIELD_UPSAMPLING_1D_SIZE = ""; // 1: length, 2: size
private final String LAYER_FIELD_UPSAMPLING_2D_SIZE = "size";
private final String LAYER_FIELD_UPSAMPLING_3D_SIZE = "size";


/* Keras convolution border modes. */
Expand Down
Expand Up @@ -310,45 +310,47 @@ static int[] getPaddingFromConfig(Map<String, Object> layerConfig,
throw new InvalidKerasConfigurationException(
"Field " + layerField + " not found in Keras cropping or padding layer");
int[] padding;
if (dimension == 2) {
if (dimension >= 2) {
List<Integer> paddingList;
// For 2D layers, padding/cropping can either be a pair [[x_0, x_1].[y_0, y_1]] or a pair [x, y]
// or a single integer x. yeah, really.
// or a single integer x. Likewise for the 3D case.
try {
List paddingNoCast = (List) innerConfig.get(layerField);
boolean isNested;
try {
@SuppressWarnings("unchecked")
List<Integer> firstItem = (List<Integer>) paddingNoCast.get(0);
isNested = true;
paddingList = new ArrayList<>(4);
paddingList = new ArrayList<>(2 * dimension);
} catch (Exception e) {
int firstItem = (int) paddingNoCast.get(0);
isNested = false;
paddingList = new ArrayList<>(2);
paddingList = new ArrayList<>(dimension);
}

if ((paddingNoCast.size() == 2) && !isNested) {
paddingList.add((int) paddingNoCast.get(0));
paddingList.add((int) paddingNoCast.get(1));
if ((paddingNoCast.size() == dimension) && !isNested) {
for (int i=0; i < dimension; i++)
paddingList.add((int) paddingNoCast.get(i));
padding = ArrayUtil.toArray(paddingList);
} else if ((paddingNoCast.size() == 2) && isNested) {
@SuppressWarnings("unchecked")
List<Integer> first = (List<Integer>) paddingNoCast.get(0);
paddingList.add((first.get(0)));
paddingList.add((first.get(1)));
@SuppressWarnings("unchecked")
List<Integer> second = (List<Integer>) paddingNoCast.get(1);
paddingList.add((second.get(0)));
paddingList.add((second.get(1)));
} else if ((paddingNoCast.size() == dimension) && isNested) {
for (int j=0; j < dimension; j++) {
@SuppressWarnings("unchecked")
List<Integer> item = (List<Integer>) paddingNoCast.get(0);
paddingList.add((item.get(0)));
paddingList.add((item.get(1)));
}
padding = ArrayUtil.toArray(paddingList);
} else {
throw new InvalidKerasConfigurationException("Found Keras ZeroPadding2D layer with invalid "
+ paddingList.size() + "D padding.");
throw new InvalidKerasConfigurationException("Found Keras ZeroPadding" + dimension
+ "D layer with invalid " + paddingList.size() + "D padding.");
}
} catch (Exception e) {
int paddingInt = (int) innerConfig.get(layerField);
padding = new int[]{paddingInt, paddingInt};
if (dimension == 2) {
padding = new int[]{paddingInt, paddingInt, paddingInt, paddingInt};
} else {
padding = new int[]{paddingInt, paddingInt, paddingInt, paddingInt, paddingInt, paddingInt};
}
}

} else if (dimension == 1) {
Expand Down
@@ -0,0 +1,80 @@
package org.deeplearning4j.nn.modelimport.keras.layers.convolutional;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.convolutional.Cropping3D;
import org.deeplearning4j.nn.modelimport.keras.KerasLayer;
import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException;
import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException;

import java.util.Map;

import static org.deeplearning4j.nn.modelimport.keras.layers.convolutional.KerasConvolutionUtils.getPaddingFromConfig;

/**
* Imports a Keras Cropping 3D layer.
*
* @author Max Pumperla
*/
@Slf4j
@Data
@EqualsAndHashCode(callSuper = false)
public class KerasCropping3D extends KerasLayer {

/**
* Constructor from parsed Keras layer configuration dictionary.
*
* @param layerConfig dictionary containing Keras layer configuration.
* @throws InvalidKerasConfigurationException Invalid Keras config
* @throws UnsupportedKerasConfigurationException Unsupported Keras config
*/
public KerasCropping3D(Map<String, Object> layerConfig)
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
this(layerConfig, true);
}

/**
* Constructor from parsed Keras layer configuration dictionary.
*
* @param layerConfig dictionary containing Keras layer configuration
* @param enforceTrainingConfig whether to enforce training-related configuration options
* @throws InvalidKerasConfigurationException Invalid Keras config
* @throws UnsupportedKerasConfigurationException Unsupported Keras config
*/
public KerasCropping3D(Map<String, Object> layerConfig, boolean enforceTrainingConfig)
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
super(layerConfig, enforceTrainingConfig);
String croppingField = conf.getLAYER_FIELD_CROPPING();
int[] cropping = getPaddingFromConfig(layerConfig, conf, croppingField, 3);
Cropping3D.Builder builder = new Cropping3D.Builder(cropping)
.name(this.layerName).dropOut(this.dropout);
this.layer = builder.build();
this.vertex = null;
}

/**
* Get DL4J Cropping3D layer.
*
* @return Cropping3D layer
*/
public Cropping3D getCropping3DLayer() {
return (Cropping3D) this.layer;
}

/**
* Get layer output type.
*
* @param inputType Array of InputTypes
* @return output type as InputType
* @throws InvalidKerasConfigurationException Invalid Keras config
*/
@Override
public InputType getOutputType(InputType... inputType) throws InvalidKerasConfigurationException {
if (inputType.length > 1)
throw new InvalidKerasConfigurationException(
"Keras Cropping 3D layer accepts only one input (received " + inputType.length + ")");
return this.getCropping3DLayer().getOutputType(-1, inputType[0]);
}
}
@@ -0,0 +1,97 @@
/*-
*
* * Copyright 2017 Skymind,Inc.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/
package org.deeplearning4j.nn.modelimport.keras.layers.convolutional;

import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.Upsampling2D;
import org.deeplearning4j.nn.conf.layers.Upsampling3D;
import org.deeplearning4j.nn.modelimport.keras.KerasLayer;
import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException;
import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException;

import java.util.Map;


/**
* Keras Upsampling3D layer support
*
* @author Max Pumperla
*/
public class KerasUpsampling3D extends KerasLayer {

/**
* Constructor from parsed Keras layer configuration dictionary.
*
* @param layerConfig dictionary containing Keras layer configuration.
* @throws InvalidKerasConfigurationException Invalid Keras configuration exception
* @throws UnsupportedKerasConfigurationException Unsupported Keras configuration exception
*/
public KerasUpsampling3D(Map<String, Object> layerConfig)
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
this(layerConfig, true);
}

/**
* Constructor from parsed Keras layer configuration dictionary.
*
* @param layerConfig dictionary containing Keras layer configuration
* @param enforceTrainingConfig whether to enforce training-related configuration options
* @throws InvalidKerasConfigurationException Invalid Keras configuration exception
* @throws UnsupportedKerasConfigurationException Invalid Keras configuration exception
*/
public KerasUpsampling3D(Map<String, Object> layerConfig, boolean enforceTrainingConfig)
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
super(layerConfig, enforceTrainingConfig);

int[] size = KerasConvolutionUtils.getUpsamplingSizeFromConfig(layerConfig, 3, conf);
// TODO: make sure to allow different sizes.

Upsampling3D.Builder builder = new Upsampling3D.Builder()
.name(this.layerName)
.dropOut(this.dropout)
.size(size[0]);

this.layer = builder.build();
this.vertex = null;
}

/**
* Get DL4J Upsampling3D layer.
*
* @return Upsampling3D layer
*/
public Upsampling3D getUpsampling3DLayer() {
return (Upsampling3D) this.layer;
}

/**
* Get layer output type.
*
* @param inputType Array of InputTypes
* @return output type as InputType
* @throws InvalidKerasConfigurationException Invalid Keras config
*/
@Override
public InputType getOutputType(InputType... inputType) throws InvalidKerasConfigurationException {
if (inputType.length > 1)
throw new InvalidKerasConfigurationException(
"Keras Upsampling 3D layer accepts only one input (received " + inputType.length + ")");
return this.getUpsampling3DLayer().getOutputType(-1, inputType[0]);
}

}
Expand Up @@ -56,9 +56,9 @@ public KerasZeroPadding1D(Map<String, Object> layerConfig, boolean enforceTraini
}

/**
* Get DL4J SubsamplingLayer.
* Get DL4J ZeroPadding1DLayer.
*
* @return SubsamplingLayer
* @return ZeroPadding1DLayer
*/
public ZeroPadding1DLayer getZeroPadding1DLayer() {
return (ZeroPadding1DLayer) this.layer;
Expand Down
Expand Up @@ -56,9 +56,9 @@ public KerasZeroPadding2D(Map<String, Object> layerConfig, boolean enforceTraini
}

/**
* Get DL4J SubsamplingLayer.
* Get DL4J ZeroPadding2DLayer.
*
* @return SubsamplingLayer
* @return ZeroPadding2DLayer
*/
public ZeroPaddingLayer getZeroPadding2DLayer() {
return (ZeroPaddingLayer) this.layer;
Expand Down
@@ -0,0 +1,82 @@
package org.deeplearning4j.nn.modelimport.keras.layers.convolutional;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.deeplearning4j.nn.conf.inputs.InputType;
import org.deeplearning4j.nn.conf.layers.ZeroPadding3DLayer;
import org.deeplearning4j.nn.conf.layers.ZeroPaddingLayer;
import org.deeplearning4j.nn.modelimport.keras.KerasLayer;
import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException;
import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException;

import java.util.Map;

import static org.deeplearning4j.nn.modelimport.keras.layers.convolutional.KerasConvolutionUtils.getPaddingFromConfig;

/**
* Imports a Keras ZeroPadding 3D layer.
*
* @author Max Pumperla
*/
@Slf4j
@Data
@EqualsAndHashCode(callSuper = false)
public class KerasZeroPadding3D extends KerasLayer {

/**
* Constructor from parsed Keras layer configuration dictionary.
*
* @param layerConfig dictionary containing Keras layer configuration.
*
* @throws InvalidKerasConfigurationException Invalid Keras config
* @throws UnsupportedKerasConfigurationException Unsupported Keras config
*/
public KerasZeroPadding3D(Map<String, Object> layerConfig)
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
this(layerConfig, true);
}

/**
* Constructor from parsed Keras layer configuration dictionary.
*
* @param layerConfig dictionary containing Keras layer configuration
* @param enforceTrainingConfig whether to enforce training-related configuration options
* @throws InvalidKerasConfigurationException Invalid Keras config
* @throws UnsupportedKerasConfigurationException Unsupported Keras config
*/
public KerasZeroPadding3D(Map<String, Object> layerConfig, boolean enforceTrainingConfig)
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException {
super(layerConfig, enforceTrainingConfig);
String paddingField = conf.getLAYER_FIELD_ZERO_PADDING();
int[] padding = getPaddingFromConfig(layerConfig, conf, paddingField,3);
ZeroPadding3DLayer.Builder builder = new ZeroPadding3DLayer.Builder(padding)
.name(this.layerName).dropOut(this.dropout);
this.layer = builder.build();
this.vertex = null;
}

/**
* Get DL4J ZeroPadding3DLayer.
*
* @return ZeroPadding3DLayer
*/
public ZeroPadding3DLayer getZeroPadding3DLayer() {
return (ZeroPadding3DLayer) this.layer;
}

/**
* Get layer output type.
*
* @param inputType Array of InputTypes
* @return output type as InputType
* @throws InvalidKerasConfigurationException Invalid Keras config
*/
@Override
public InputType getOutputType(InputType... inputType) throws InvalidKerasConfigurationException {
if (inputType.length > 1)
throw new InvalidKerasConfigurationException(
"Keras ZeroPadding3D layer accepts only one input (received " + inputType.length + ")");
return this.getZeroPadding3DLayer().getOutputType(-1, inputType[0]);
}
}

0 comments on commit 5200062

Please sign in to comment.