-
-
Save eraly/0824294d453261c58ef0585d70496464 to your computer and use it in GitHub Desktop.
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
from __future__ import print_function | |
import numpy as np | |
import keras | |
from keras.layers import Conv2DTranspose, Input | |
from keras.models import Model | |
input_shape=(3, 3, 2) | |
inputs = Input(shape=input_shape, name='encoder_input') | |
outputs = Conv2DTranspose(filters=2,kernel_size=(2,2),strides=2,padding='same')(inputs) | |
model = Model(inputs, outputs, name='decoder') | |
model.layers[1].set_weights([np.ones((2,2,2,2)),np.zeros((2,))+[0.1,0.2]]) | |
a = np.arange(18).reshape(1,3,3,2) | |
aa = model.predict(a) | |
print(aa) | |
model.save("de_conv_bias.h5") |
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
package org.deeplearning4j.examples.modelimport.keras.basic; | |
import org.deeplearning4j.nn.graph.ComputationGraph; | |
import org.deeplearning4j.nn.modelimport.keras.KerasModelImport; | |
import org.deeplearning4j.nn.modelimport.keras.exceptions.InvalidKerasConfigurationException; | |
import org.deeplearning4j.nn.modelimport.keras.exceptions.UnsupportedKerasConfigurationException; | |
import org.nd4j.linalg.api.ndarray.INDArray; | |
import org.nd4j.linalg.api.ops.CustomOp; | |
import org.nd4j.linalg.api.ops.DynamicCustomOp; | |
import org.nd4j.linalg.factory.Nd4j; | |
import java.io.IOException; | |
public class TestImport { | |
public static void main(String[] args) throws UnsupportedKerasConfigurationException, IOException, InvalidKerasConfigurationException { | |
final String MODEL_PATH = "/Users/susaneraly/SKYMIND/de_conv_bias.h5"; | |
INDArray in = Nd4j.linspace(0, 17, 18).reshape(1, 3, 3, 2).permute(0, 3, 1, 2); | |
//Load the keras model | |
ComputationGraph model = KerasModelImport.importKerasModelAndWeights(MODEL_PATH); | |
INDArray modelOutput = model.output(in)[0]; | |
System.out.println(modelOutput.permute(0, 2, 3, 1)); | |
System.out.println("======================="); | |
//This is the call to libnd4j that happens when model.output is called - I am just replicating it here manually | |
INDArray weights = Nd4j.ones(2, 2, 2, 2); | |
INDArray bias = Nd4j.zeros(1, 2); | |
bias.put(0,0,0.1); | |
bias.put(0,1,0.2); | |
INDArray[] opInputs = new INDArray[]{in, weights, bias}; | |
INDArray[] opOutputs = new INDArray[]{Nd4j.ones(1, 2, 6, 6)}; | |
int[] argsA = new int[]{2, 2, 2, 2, 4, 4, 1, 1, 1, 0}; | |
CustomOp op = DynamicCustomOp.builder("deconv2d") | |
.addInputs(opInputs) | |
.addIntegerArguments(argsA) | |
.addOutputs(opOutputs) | |
.callInplace(false) | |
.build(); | |
Nd4j.getExecutioner().exec(op); | |
System.out.println(opOutputs[0].permute(0, 2, 3, 1)); | |
/* | |
array([[[[ 1.1, 1.2], | |
[ 1.1, 1.2], | |
[ 5.1, 5.2], | |
[ 5.1, 5.2], | |
[ 9.1, 9.2], | |
[ 9.1, 9.2]], | |
[[ 1.1, 1.2], | |
[ 1.1, 1.2], | |
[ 5.1, 5.2], | |
[ 5.1, 5.2], | |
[ 9.1, 9.2], | |
[ 9.1, 9.2]], | |
[[13.1, 13.2], | |
[13.1, 13.2], | |
[17.1, 17.2], | |
[17.1, 17.2], | |
[21.1, 21.2], | |
[21.1, 21.2]], | |
[[13.1, 13.2], | |
[13.1, 13.2], | |
[17.1, 17.2], | |
[17.1, 17.2], | |
[21.1, 21.2], | |
[21.1, 21.2]], | |
[[25.1, 25.2], | |
[25.1, 25.2], | |
[29.1, 29.2], | |
[29.1, 29.2], | |
[33.1, 33.2], | |
[33.1, 33.2]], | |
[[25.1, 25.2], | |
[25.1, 25.2], | |
[29.1, 29.2], | |
[29.1, 29.2], | |
[33.1, 33.2], | |
[33.1, 33.2]]]], dtype=float32) | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment