forked from deeplearning4j/deeplearning4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request deeplearning4j#4041 from deeplearning4j/mp_upsampl…
…ing2d_import Upsampling2d model import + DCGAN import example
- Loading branch information
Showing
14 changed files
with
283 additions
and
9 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
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
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
98 changes: 98 additions & 0 deletions
98
.../java/org/deeplearning4j/nn/modelimport/keras/layers/convolutional/KerasUpsampling2D.java
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,98 @@ | ||
/*- | ||
* | ||
* * 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.modelimport.keras.KerasLayer; | ||
import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException; | ||
import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException; | ||
|
||
import java.util.Map; | ||
|
||
|
||
/** | ||
* Keras Upsampling2D layer support | ||
* | ||
* @author Max Pumperla | ||
*/ | ||
public class KerasUpsampling2D 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 KerasUpsampling2D(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 KerasUpsampling2D(Map<String, Object> layerConfig, boolean enforceTrainingConfig) | ||
throws InvalidKerasConfigurationException, UnsupportedKerasConfigurationException { | ||
super(layerConfig, enforceTrainingConfig); | ||
|
||
int[] size = KerasConvolutionUtils.getUpsamplingSizeFromConfig(layerConfig, 2, conf); | ||
if (size[0] != size[1]) | ||
throw new UnsupportedKerasConfigurationException("First and second size arguments have to be the same" + | ||
"got: " + size[0] + " and " + size[1]); | ||
|
||
Upsampling2D.Builder builder = new Upsampling2D.Builder() | ||
.name(this.layerName) | ||
.dropOut(this.dropout) | ||
.size(size[0]); | ||
|
||
this.layer = builder.build(); | ||
this.vertex = null; | ||
} | ||
|
||
/** | ||
* Get DL4J Upsampling2D layer. | ||
* | ||
* @return Upsampling2D layer | ||
*/ | ||
public Upsampling2D getUpsampling2DLayer() { | ||
return (Upsampling2D) this.layer; | ||
} | ||
|
||
/** | ||
* Get layer output type. | ||
* | ||
* @param inputType Array of InputTypes | ||
* @return output type as InputType | ||
* @throws InvalidKerasConfigurationException | ||
*/ | ||
@Override | ||
public InputType getOutputType(InputType... inputType) throws InvalidKerasConfigurationException { | ||
if (inputType.length > 1) | ||
throw new InvalidKerasConfigurationException( | ||
"Keras Subsampling layer accepts only one input (received " + inputType.length + ")"); | ||
return this.getUpsampling2DLayer().getOutputType(-1, inputType[0]); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
.../main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasGlobalPooling.java
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
17 changes: 17 additions & 0 deletions
17
.../src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling1D.java
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
17 changes: 17 additions & 0 deletions
17
.../src/main/java/org/deeplearning4j/nn/modelimport/keras/layers/pooling/KerasPooling2D.java
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
73 changes: 73 additions & 0 deletions
73
...ava/org/deeplearning4j/nn/modelimport/keras/layers/convolution/KerasUpsampling2DTest.java
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,73 @@ | ||
/*- | ||
* | ||
* * 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.convolution; | ||
|
||
import org.deeplearning4j.nn.conf.layers.Upsampling2D; | ||
import org.deeplearning4j.nn.conf.layers.ZeroPadding1DLayer; | ||
import org.deeplearning4j.nn.modelimport.keras.config.Keras1LayerConfiguration; | ||
import org.deeplearning4j.nn.modelimport.keras.config.Keras2LayerConfiguration; | ||
import org.deeplearning4j.nn.modelimport.keras.config.KerasLayerConfiguration; | ||
import org.deeplearning4j.nn.modelimport.keras.layers.convolutional.KerasUpsampling2D; | ||
import org.deeplearning4j.nn.modelimport.keras.layers.convolutional.KerasZeroPadding1D; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* @author Max Pumperla | ||
*/ | ||
public class KerasUpsampling2DTest { | ||
|
||
private final String LAYER_NAME = "upsampling_2D_layer"; | ||
private int[] size = new int[] {2, 2}; | ||
|
||
private Integer keras1 = 1; | ||
private Integer keras2 = 2; | ||
private Keras1LayerConfiguration conf1 = new Keras1LayerConfiguration(); | ||
private Keras2LayerConfiguration conf2 = new Keras2LayerConfiguration(); | ||
|
||
@Test | ||
public void testUpsampling2DLayer() throws Exception { | ||
buildUpsampling2DLayer(conf1, keras1); | ||
buildUpsampling2DLayer(conf2, keras2); | ||
} | ||
|
||
|
||
public void buildUpsampling2DLayer(KerasLayerConfiguration conf, Integer kerasVersion) throws Exception { | ||
Map<String, Object> layerConfig = new HashMap<>(); | ||
layerConfig.put(conf.getLAYER_FIELD_CLASS_NAME(), conf.getLAYER_CLASS_NAME_MAX_POOLING_1D()); | ||
Map<String, Object> config = new HashMap<>(); | ||
List<Integer> sizeList = new ArrayList<>(); | ||
sizeList.add(size[0]); | ||
sizeList.add(size[1]); | ||
config.put(conf.getLAYER_FIELD_UPSAMPLING_2D_SIZE(), sizeList); | ||
config.put(conf.getLAYER_FIELD_NAME(), LAYER_NAME); | ||
layerConfig.put(conf.getLAYER_FIELD_CONFIG(), config); | ||
layerConfig.put(conf.getLAYER_FIELD_KERAS_VERSION(), kerasVersion); | ||
|
||
Upsampling2D layer = new KerasUpsampling2D(layerConfig).getUpsampling2DLayer(); | ||
assertEquals(LAYER_NAME, layer.getLayerName()); | ||
assertEquals(size[0], layer.getSize()); | ||
} | ||
|
||
} |