Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.nd4j.linalg.api.ops.impl.layers.convolution.DeConv2DTF Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2015-2018 Skymind, Inc.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://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.
*
* SPDX-License-Identifier: Apache-2.0
******************************************************************************/
package org.nd4j.linalg.api.ops.impl.layers.convolution;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import onnx.OnnxProto3;
import org.nd4j.autodiff.samediff.SDVariable;
import org.nd4j.autodiff.samediff.SameDiff;
import org.nd4j.imports.converters.DifferentialFunctionClassHolder;
import org.nd4j.imports.descriptors.properties.AttributeAdapter;
import org.nd4j.imports.descriptors.properties.PropertyMapping;
import org.nd4j.imports.descriptors.properties.adapters.ConditionalFieldValueIntIndexArrayAdapter;
import org.nd4j.imports.descriptors.properties.adapters.ConditionalFieldValueNDArrayShapeAdapter;
import org.nd4j.imports.descriptors.properties.adapters.SizeThresholdIntArrayIntIndexAdpater;
import org.nd4j.imports.descriptors.properties.adapters.StringEqualsAdapter;
import org.nd4j.imports.graphmapper.tf.TFGraphMapper;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.DynamicCustomOp;
import org.nd4j.linalg.api.ops.impl.layers.convolution.config.DeConv2DConfig;
import org.nd4j.linalg.util.ArrayUtil;
import org.tensorflow.framework.AttrValue;
import org.tensorflow.framework.GraphDef;
import org.tensorflow.framework.NodeDef;
import java.lang.reflect.Field;
import java.util.*;
/**
* DeConv2D operation, TF-wrapper
*/
@Slf4j
@Getter
@NoArgsConstructor
public class DeConv2DTF extends DynamicCustomOp {
protected DeConv2DConfig config;
@Builder(builderMethodName = "builder")
public DeConv2DTF(SameDiff sameDiff,
SDVariable[] inputs,
INDArray[] inputArrays, INDArray[] outputs,
DeConv2DConfig config) {
super(null, inputArrays, outputs);
this.sameDiff = sameDiff;
this.config = config;
if (inputArrays != null) {
addInputArgument(inputArrays);
}
if (outputs != null) {
addOutputArgument(outputs);
}
addArgs();
sameDiff.putFunctionForId(this.getOwnName(), this);
sameDiff.addArgsFor(inputs, this);
}
@Override
public long[] iArgs() {
if (iArguments.size() == 0)
addArgs();
return super.iArgs();
}
@Override
public Map propertiesForFunction() {
return config.toProperties();
}
private void addArgs() {
addIArgument(config.getKH());
addIArgument(config.getKW());
addIArgument(config.getSH());
addIArgument(config.getSW());
addIArgument(config.getPH());
addIArgument(config.getPW());
addIArgument(config.getDH());
addIArgument(config.getDW());
addIArgument(ArrayUtil.fromBoolean(config.isSameMode()));
addIArgument(ArrayUtil.fromBoolean(config.isNHWC()));
}
@Override
public boolean isConfigProperties() {
return true;
}
@Override
public String configFieldName() {
return "config";
}
@Override
public Object getValue(Field property) {
if (config == null) {
config = DeConv2DConfig.builder().build();
}
return config.getValue(property);
}
@Override
public void setValueFor(Field target, Object value) {
config.setValueFor(target, value);
}
@Override
public Map> mappingsForFunction() {
Map> ret = new HashMap<>();
Map map = new HashMap<>();
val strideMapping = PropertyMapping.builder()
.tfAttrName("strides")
.onnxAttrName("strides")
.propertyNames(new String[]{"sH", "sW"})
.build();
val kernelMapping = PropertyMapping.builder()
.propertyNames(new String[]{"kH", "kW"})
.tfInputPosition(1)
.onnxAttrName("kernel_shape")
.build();
val dilationMapping = PropertyMapping.builder()
.onnxAttrName("dilations")
.propertyNames(new String[]{"dW", "dH"})
.tfAttrName("rates")
.build();
val sameMode = PropertyMapping.builder()
.onnxAttrName("auto_pad")
.propertyNames(new String[]{"isSameMode"})
.tfAttrName("padding")
.build();
val dataFormat = PropertyMapping.builder()
.onnxAttrName("data_format")
.tfAttrName("data_format")
.propertyNames(new String[]{"isNHWC"})
.build();
map.put("sW", strideMapping);
map.put("sH", strideMapping);
map.put("kH", kernelMapping);
map.put("kW", kernelMapping);
map.put("dW", dilationMapping);
map.put("dH", dilationMapping);
map.put("isSameMode", sameMode);
map.put("isNHWC", dataFormat);
ret.put(tensorflowName(), map);
return ret;
}
@Override
public Map> attributeAdaptersForFunction() {
Map> ret = new HashMap<>();
Map tfMappings = new LinkedHashMap<>();
val fields = DifferentialFunctionClassHolder.getInstance().getFieldsForFunction(this);
tfMappings.put("kH", new ConditionalFieldValueNDArrayShapeAdapter("NCHW", 2, 0, fields.get("dataFormat")));
tfMappings.put("kW", new ConditionalFieldValueNDArrayShapeAdapter("NCHW", 3, 1, fields.get("dataFormat")));
tfMappings.put("sH", new ConditionalFieldValueIntIndexArrayAdapter("NCHW", 2, 1, fields.get("dataFormat")));
tfMappings.put("sW", new ConditionalFieldValueIntIndexArrayAdapter("NCHW", 3, 2, fields.get("dataFormat")));
tfMappings.put("isSameMode", new StringEqualsAdapter("SAME"));
tfMappings.put("isNHWC", new StringEqualsAdapter("NHWC"));
Map onnxMappings = new HashMap<>();
onnxMappings.put("kH", new SizeThresholdIntArrayIntIndexAdpater(0, 2, 0));
onnxMappings.put("kW", new SizeThresholdIntArrayIntIndexAdpater(1, 2, 0));
onnxMappings.put("dH", new SizeThresholdIntArrayIntIndexAdpater(0, 2, 0));
onnxMappings.put("dW", new SizeThresholdIntArrayIntIndexAdpater(1, 2, 0));
onnxMappings.put("sH", new SizeThresholdIntArrayIntIndexAdpater(0, 2, 0));
onnxMappings.put("sW", new SizeThresholdIntArrayIntIndexAdpater(1, 2, 0));
onnxMappings.put("isSameMode", new StringEqualsAdapter("SAME"));
onnxMappings.put("isNHWC", new StringEqualsAdapter("NHWC"));
ret.put(tensorflowName(), tfMappings);
return ret;
}
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map attributesForNode, GraphDef graph) {
TFGraphMapper.getInstance().initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);
addArgs();
}
@Override
public String opName() {
return "deconv2d_tf";
}
@Override
public String onnxName() {
return "ConvTranspose-Absent";
}
@Override
public String tensorflowName() {
return "Conv2DBackpropInput";
}
@Override
public List doDiff(List f1) {
throw new UnsupportedOperationException("To be implemented yet");
}
}