org.tensorflow.framework.NodeDef Java Examples

The following examples show how to use org.tensorflow.framework.NodeDef. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TensorArrayV3.java    From nd4j with Apache License 2.0 6 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {

    val idd = nodeDef.getInput(nodeDef.getInputCount() - 1);
    NodeDef iddNode = null;
    for(int i = 0; i < graph.getNodeCount(); i++) {
        if(graph.getNode(i).getName().equals(idd)) {
            iddNode = graph.getNode(i);
        }
    }


    val arr = TFGraphMapper.getInstance().getNDArrayFromTensor("value",iddNode,graph);

    if (arr != null) {
        int idx = arr.getInt(0);
        addIArgument(idx);
    }

}
 
Example #2
Source File: StridedSlice.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val inputBegin = nodeDef.getInput(1);
    val inputEnd = nodeDef.getInput(2);
    val inputStrides = nodeDef.getInput(3);

    // bit masks for this slice
    val bm = nodeDef.getAttrOrThrow("begin_mask");
    val xm = nodeDef.getAttrOrThrow("ellipsis_mask");
    val em = nodeDef.getAttrOrThrow("end_mask");
    val nm = nodeDef.getAttrOrThrow("new_axis_mask");
    val sm = nodeDef.getAttrOrThrow("shrink_axis_mask");

    beginMask = (int)bm.getI();
    ellipsisMask = (int) xm.getI();
    endMask = (int) em.getI();
    newAxisMask = (int) nm.getI();
    shrinkAxisMask = (int) sm.getI();

    addIArgument(beginMask);
    addIArgument(ellipsisMask);
    addIArgument(endMask);
    addIArgument(newAxisMask);
    addIArgument(shrinkAxisMask);
}
 
Example #3
Source File: TypeConverter.java    From vespa with Apache License 2.0 6 votes vote down vote up
private static TensorShapeProto tensorFlowShape(NodeDef node) {
    // Use specific shape if available...
    AttrValue attrShape = node.getAttrMap().get("shape");
    if (attrShape != null && attrShape.getValueCase() == AttrValue.ValueCase.SHAPE) {
        return attrShape.getShape();
    }

    // ... else use inferred shape
    AttrValue attrOutputShapes = node.getAttrMap().get("_output_shapes");
    if (attrOutputShapes == null)
        throw new IllegalArgumentException("_output_shapes attribute of '" + node.getName() + "' " +
                                           "does not exist");
    if (attrOutputShapes.getValueCase() != AttrValue.ValueCase.LIST)
        throw new IllegalArgumentException("_output_shapes attribute of '" + node.getName() + "' " +
                                           "is not of expected type");

    return attrOutputShapes.getList().getShape(0); // support multiple outputs?
}
 
Example #4
Source File: TypeConverter.java    From vespa with Apache License 2.0 6 votes vote down vote up
static void verifyType(NodeDef node, OrderedTensorType type) {
    TensorShapeProto shape = tensorFlowShape(node);
    if (shape != null) {
        if (shape.getDimCount() != type.rank()) {
            throw new IllegalArgumentException("TensorFlow shape of '" + node.getName() + "' " +
                                               "does not match Vespa shape");
        }
        for (int tensorFlowIndex = 0; tensorFlowIndex < type.dimensions().size(); ++tensorFlowIndex) {
            int vespaIndex = type.dimensionMap(tensorFlowIndex);
            TensorShapeProto.Dim tensorFlowDimension = shape.getDim(tensorFlowIndex);
            TensorType.Dimension vespaDimension = type.type().dimensions().get(vespaIndex);
            if (tensorFlowDimension.getSize() != vespaDimension.size().orElse(-1L)) {
                throw new IllegalArgumentException("TensorFlow dimensions of '" + node.getName() + "' " +
                                                   "does not match Vespa dimensions");
            }
        }
    }
}
 
Example #5
Source File: ExtractImagePatches.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    //TF includes redundant leading and training 1s for kSizes, strides, rates (positions 0/3)
    kSizes = parseIntList(attributesForNode.get("ksizes").getList());
    strides = parseIntList(attributesForNode.get("strides").getList());
    rates = parseIntList(attributesForNode.get("rates").getList());
    String s = attributesForNode.get("padding").getS().toStringUtf8();
    isSameMode = s.equalsIgnoreCase("SAME");
    addArgs();
}
 
Example #6
Source File: Split.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val numSplits = (int) attributesForNode.get("num_split").getI();
    this.numSplit = numSplits;
    addIArgument(numSplits);

    val splitDim = TFGraphMapper.getInstance().getArrayFrom(TFGraphMapper.getInstance().getNodeWithNameFromGraph(graph,nodeDef.getInput(0)),graph);
    if(splitDim != null) {
        this.splitDim = splitDim.getInt(0);
        addIArgument(splitDim.getInt(0));
    }



}
 
Example #7
Source File: Pad.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    //Based on TF codebase: gen_array_ops.mirror_pad is osed for BOTH REFLECT and SYMMETRIC mode. Hence only constant being imported here
    this.mode = Mode.CONSTANT;
    addIArgument(mode.ordinal());
    //Constant value is resolved just before execution
}
 
Example #8
Source File: SplitV.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val numSplits = (int) attributesForNode.get("num_split").getI();
    this.numSplit = numSplits;
    val splitDim = TFGraphMapper.getInstance().getArrayFrom(TFGraphMapper.getInstance().getNodeWithNameFromGraph(graph,nodeDef.getInput(0)),graph);
    if(splitDim != null) {
        this.splitDim = splitDim.getInt(0);
        addIArgument(splitDim.getInt(0));
    }

    addIArgument(numSplits);

}
 
Example #9
Source File: CheckNumerics.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    String str = attributesForNode.get("message").getS().toStringUtf8();
    //No "string args" support in libnd4j custom ops -> make it a constant instead
    String name = nodeDef.getName();
    SDVariable msg = initWith.constant(name + "/message", Nd4j.scalar(str));
    List<String> newInputs = new ArrayList<>(2);
    newInputs.addAll(initWith.getOps().get(name).getInputsToOp());
    newInputs.add(msg.name());
    initWith.getOps().get(name).setInputsToOp(newInputs);
    initWith.getVariables().get(msg.name()).setInputsForOp(Collections.singletonList(getOwnName()));    }
 
Example #10
Source File: LeakyReLU.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode,
        GraphDef graph) {
    alpha = attributesForNode.get("alpha").getF();
    extraArgs = new Object[]{alpha};
    this.setScalar(Nd4j.scalar(org.nd4j.linalg.api.buffer.DataType.FLOAT, alpha));
}
 
Example #11
Source File: ResizeArea.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);

    val attrC = attributesForNode.get("align_corners");
    this.alignCorners = attrC != null ? attrC.getB() : false;

    addArgs();
}
 
Example #12
Source File: SavedModel.java    From jpmml-tensorflow with GNU Affero General Public License v3.0 5 votes vote down vote up
public NodeDef getNodeDef(String name){
	Map<String, NodeDef> nodeMap = getNodeMap();

	int colon = name.indexOf(':');

	NodeDef nodeDef = nodeMap.get(colon > -1 ? name.substring(0, colon) : name);
	if(nodeDef == null){
		throw new IllegalArgumentException(name);
	}

	return nodeDef;
}
 
Example #13
Source File: SparseSoftmaxCrossEntropyLossWithLogits.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);

    //Switch order: TF uses [logits, labels]; libnd4j expects [labels, logits]
    SameDiffOp op = initWith.getOps().get(this.getOwnName());
    List<String> list = op.getInputsToOp();
    List<String> newList = Arrays.asList(list.get(1), list.get(0));
    op.setInputsToOp(newList);
}
 
Example #14
Source File: ScatterMin.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);

    if (nodeDef.containsAttr("use_locking")) {
        if (nodeDef.getAttrOrThrow("use_locking").getB() == true) {
            bArguments.add(true);
        } else {
            bArguments.add(false);
        }
    } else
        bArguments.add(false);
}
 
Example #15
Source File: Transpose.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    super.initFromTensorFlow(nodeDef, initWith, attributesForNode, graph);
    //permute dimensions are not specified as second input
    if (nodeDef.getInputCount() < 2)
        return;
    NodeDef permuteDimsNode = null;
    for (int i = 0; i < graph.getNodeCount(); i++) {
        if (graph.getNode(i).getName().equals(nodeDef.getInput(1))) {
            permuteDimsNode = graph.getNode(i);
        }

    }

    INDArray permuteArrayOp = TFGraphMapper.getNDArrayFromTensor(permuteDimsNode);
    if (permuteArrayOp != null) {
        this.permuteDims = permuteArrayOp.data().asInt();
    }

    //handle once properly mapped
    if (arg().getShape() == null || arg().getVariableType() == VariableType.PLACEHOLDER || arg().getArr() == null) {
        return;
    }

    INDArray arr = sameDiff.getArrForVarName(arg().name());

    if(permuteArrayOp != null){
        addInputArgument(arr, permuteArrayOp);
    } else {
        addInputArgument(arr);
    }

    if (arr != null && permuteDims == null) {
        this.permuteDims = ArrayUtil.reverseCopy(ArrayUtil.range(0, arr.rank()));
    }

    if (permuteDims != null && permuteDims.length < arg().getShape().length)
        throw new ND4JIllegalStateException("Illegal permute found. Not all dimensions specified");
}
 
Example #16
Source File: ScatterSub.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);

    if (nodeDef.containsAttr("use_locking")) {
        if (nodeDef.getAttrOrThrow("use_locking").getB() == true) {
            bArguments.add(true);
        } else {
            bArguments.add(false);
        }
    } else
        bArguments.add(false);
}
 
Example #17
Source File: GraphImporter.java    From vespa with Apache License 2.0 5 votes vote down vote up
private static List<IntermediateOperation> importControlInputs(NodeDef node,
                                                               GraphDef tfGraph,
                                                               IntermediateGraph intermediateGraph,
                                                               SavedModelBundle bundle) {
    return node.getInputList().stream()
            .filter(nodeName -> isControlDependency(nodeName))
            .map(nodeName -> importOperation(nodeName, tfGraph, intermediateGraph, bundle))
            .collect(Collectors.toList());
}
 
Example #18
Source File: OneHot.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);
    addArgs();
    if(attributesForNode.containsKey("T")) {
        outputType = TFGraphMapper.convertType(attributesForNode.get("T").getType());
    }
}
 
Example #19
Source File: BiasAdd.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    super.initFromTensorFlow(nodeDef, initWith, attributesForNode, graph);
    if(attributesForNode.containsKey("data_format")){
        nchw = "NCHW".equalsIgnoreCase(attributesForNode.get("data_format").getS().toStringUtf8());
    } else {
        nchw = false;   //TF default is NHWC
    }
    bArguments.clear();
    bArguments.add(nchw);
}
 
Example #20
Source File: FakeQuantWithMinMaxVarsPerChannel.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    if(attributesForNode.containsKey("narrow_range")){
        this.narrowRange = attributesForNode.get("narrow_range").getB();
    }
    if(attributesForNode.containsKey("num_bits")) {
        this.numBits = (int) attributesForNode.get("num_bits").getI();
    }
    addIArgument(numBits);
    addBArgument(narrowRange);
}
 
Example #21
Source File: FakeQuantWithMinMaxVars.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    if(attributesForNode.containsKey("narrow_range")){
        this.narrowRange = attributesForNode.get("narrow_range").getB();
    }
    this.numBits = (int)attributesForNode.get("num_bits").getI();
    addArgs();
}
 
Example #22
Source File: ScatterUpdate.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    if (nodeDef.containsAttr("use_locking")) {
        if (nodeDef.getAttrOrThrow("use_locking").getB() == true) {
            bArguments.add(true);
        } else {
            bArguments.add(false);
        }
    } else
        bArguments.add(false);
}
 
Example #23
Source File: Rank.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val name = TFGraphMapper.getInstance().getNodeName(nodeDef.getName());
    val input = initWith.getVariable(name);
    val outputVertex = input.getVarName();
    if (!initWith.isPlaceHolder(input.getVarName()) && initWith.shapeAlreadyExistsForVarName(outputVertex)) {
        val inputShape = initWith.getShapeForVarName(input.getVarName());
        val resultLength = Nd4j.scalar(inputShape.length);
        val thisResultId = outputVertex;
        initWith.putArrayForVarName(thisResultId, resultLength);
        initWith.putShapeForVarName(thisResultId, new long[]{1, 1});
    }
}
 
Example #24
Source File: BaseTensorOp.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val inputOne = nodeDef.getInput(1);
    val varFor = initWith.getVariable(inputOne);
    val nodeWithIndex = TFGraphMapper.getInstance().getNodeWithNameFromGraph(graph,inputOne);
    val var = TFGraphMapper.getInstance().getArrayFrom(nodeWithIndex,graph);
    if(var != null) {
        val idx = var.getInt(0);
        addIArgument(idx);
    }
}
 
Example #25
Source File: ScatterAdd.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);

    if (nodeDef.containsAttr("use_locking")) {
        if (nodeDef.getAttrOrThrow("use_locking").getB() == true) {
            bArguments.add(true);
        } else {
            bArguments.add(false);
        }
    } else
        bArguments.add(false);
}
 
Example #26
Source File: BaseTensorOp.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val inputOne = nodeDef.getInput(1);
    val varFor = initWith.getVariable(inputOne);
    val nodeWithIndex = TFGraphMapper.getNodeWithNameFromGraph(graph,inputOne);
    val var = TFGraphMapper.getArrayFrom(nodeWithIndex,graph);
    if(var != null) {
        val idx = var.getInt(0);
        addIArgument(idx);
    }
}
 
Example #27
Source File: Pow.java    From nd4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    val weightsName = nodeDef.getInput(1);
    val variable = initWith.getVariable(weightsName);
    val tmp = initWith.getArrForVarName(weightsName);

    // if second argument is scalar - we should provide array of same shape
    if (tmp != null) {
        if (tmp.isScalar()) {
            this.pow = tmp.getDouble(0);
        }
    }
}
 
Example #28
Source File: CropAndResize.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    String method = attributesForNode.get("method").getS().toStringUtf8();
    if(method.equalsIgnoreCase("nearest")){
        this.method = Method.NEAREST;
    } else {
        this.method = Method.BILINEAR;
    }

    if(attributesForNode.containsKey("extrapolation_value")){
        extrapolationValue = attributesForNode.get("extrapolation_value").getF();
    }

    addArgs();
}
 
Example #29
Source File: Squeeze.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    nodeDef.getAttrMap().get("squeeze_dims");
    List<Long> dimList = attributesForNode.get("squeeze_dims").getList().getIList();
    squeezeDims = new int[dimList.size()];
    for( int i=0; i<dimList.size(); i++ )
        squeezeDims[i] = dimList.get(i).intValue();
    addIArgument(squeezeDims);
}
 
Example #30
Source File: SigmoidCrossEntropyLoss.java    From nd4j with Apache License 2.0 4 votes vote down vote up
@Override
public void initFromTensorFlow(NodeDef nodeDef, SameDiff initWith, Map<String, AttrValue> attributesForNode, GraphDef graph) {
    TFGraphMapper.getInstance().initFunctionFromProperties(nodeDef.getOp(), this, attributesForNode, nodeDef, graph);
    addArgs();
}