|
| 1 | +# Deeplearning4j - 使用nd4j导入tensorflow模型 |
| 2 | + |
| 3 | +在dl4j-example里面新增了模型导入的例子,这里简单的说一下。 |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +在dl4j新版本的特性介绍:https://github.com/deeplearning4j/deeplearning4j-docs/blob/releasenotes_100a/releasenotes.md 中,对于nd4j的模型导入进行了特别强调。 |
| 8 | +``` |
| 9 | +ND4J: New Features |
| 10 | +
|
| 11 | +Technology preview of tensorflow import added (supports 1.4.0 and up) |
| 12 | +``` |
| 13 | +其中一项就是对于tf模型的导入提供了功能预览版本,所支持的tf版本为1.4版本及其以上。 |
| 14 | + |
| 15 | +并于最近增加了导入tensorflow模型的示例代码,导入模型为 MINST 手写数字分类模型。从代码注释上面来看,因为是预览版本,目前只支持cpu运行,还不支持gpu的加速。 |
| 16 | + |
| 17 | +<center>![资源文件列表.png-23.3kB][1]</center> |
| 18 | + |
| 19 | +并且提供了如上的文件,用于本次示例的测试。 |
| 20 | + |
| 21 | + 1. freeze_model_after.py 和 generate_model.py 是生成模型的 python 文件。 |
| 22 | + 2. frozen_model.pb 为tensorflow的模型文件 |
| 23 | + 3. input_*.csv 和 input_*.shape为配套的特征数据文件。csv文件存放的是特征数据,一个特征一行;shape文件保存的是输入模型时的形状。 |
| 24 | + 4. prediction 文件同理,为预测的标签数据文件。 |
| 25 | + |
| 26 | +**注:** 使用该示例的时候,最好 IDE 已经安装了的相对的 lombok 插件。 |
| 27 | + |
| 28 | +# 资源文件夹 |
| 29 | +``` |
| 30 | + //Python code for this can be found in resources/import/tensorflow under generate_model.py and freeze_model_after.py |
| 31 | +//Input node/Placeholder in this graph is names "input" |
| 32 | +//Output node/op in this graph is names "output" |
| 33 | +public final static String BASE_DIR = "import/tensorflow"; |
| 34 | +``` |
| 35 | +首先定义了一个根目录用于寻找对应的文件。 |
| 36 | + |
| 37 | +接下来在主函数中获取模型文件的绝对路径 |
| 38 | +``` |
| 39 | +final String FROZEN_MLP = new ClassPathResource(BASE_DIR + "/frozen_model.pb").getFile().getPath(); |
| 40 | +
|
| 41 | +``` |
| 42 | + |
| 43 | +# 读取tf中的占位符输入 |
| 44 | +``` |
| 45 | +//Load placeholder inputs and corresponding predictions generated from tensorflow |
| 46 | +Map<String, INDArray> inputsPredictions = readPlaceholdersAndPredictions(); |
| 47 | +``` |
| 48 | +这里面所用`readPlaceholdersAndPredictions`方法的全部代码如下: |
| 49 | +``` |
| 50 | +//A simple helper function to load the inputs and corresponding outputs generated from tensorflow |
| 51 | +//Two cases: {input_a,prediction_a} and {input_b,prediction_b} |
| 52 | +protected static Map<String, INDArray> readPlaceholdersAndPredictions() throws IOException { |
| 53 | + String[] toReadList = {"input_a", "input_b", "prediction_a", "prediction_b"}; |
| 54 | + Map<String, INDArray> arraysFromPython = new HashMap<>(); |
| 55 | + for (int i = 0; i < toReadList.length; i++) { |
| 56 | + String varShapePath = new ClassPathResource(BASE_DIR + "/" + toReadList[i] + ".shape").getFile().getPath(); |
| 57 | + String varValuePath = new ClassPathResource(BASE_DIR + "/" + toReadList[i] + ".csv").getFile().getPath(); |
| 58 | + int[] varShape = Nd4j.readNumpy(varShapePath, ",").data().asInt(); |
| 59 | + float[] varContents = Nd4j.readNumpy(varValuePath).data().asFloat(); |
| 60 | + arraysFromPython.put(toReadList[i], Nd4j.create(varContents).reshape(varShape)); |
| 61 | + } |
| 62 | + return arraysFromPython; |
| 63 | +} |
| 64 | +``` |
| 65 | +这段代码不难理解,就是把前缀为`toReadList`数组内容中的数据成对读取出来,并且转换成为INDArray对象,并且返回回去。 |
| 66 | + |
| 67 | +# 模型读取 |
| 68 | +``` |
| 69 | +//Load the graph into samediff |
| 70 | +val graph = TFGraphMapper.getInstance().importGraph(new File(FROZEN_MLP)); |
| 71 | +``` |
| 72 | +这里面的 val 并非java 10提供的变量自动推断,而是 lombok 所提供的功能。 |
| 73 | + |
| 74 | +# 数据关联 |
| 75 | +``` |
| 76 | +//libnd4j executor |
| 77 | +//running with input_a array expecting to get prediction_a |
| 78 | +graph.associateArrayWithVariable(inputsPredictions.get("input_a"), graph.variableMap().get("input")); |
| 79 | +``` |
| 80 | +这段代码是将从文件中读取出来的 `input_a` INDArray关联模型的数据。 |
| 81 | + |
| 82 | +# 模型预测 |
| 83 | +``` |
| 84 | +val executioner = new NativeGraphExecutioner(); |
| 85 | +val results = executioner.executeGraph(graph); //returns an array of the outputs |
| 86 | +INDArray libnd4jPred = ((INDArray[]) results)[0]; |
| 87 | +System.out.println("LIBND4J exec prediction for input_a:\n" + libnd4jPred); |
| 88 | +``` |
| 89 | +模型预测,并且获取模型的结果输出。并且将其打印到控制台上。 |
| 90 | + |
| 91 | +# 结果判断 |
| 92 | +``` |
| 93 | +if (libnd4jPred.equals(inputsPredictions.get("prediction_a"))) { |
| 94 | + //this is true and therefore predictions are equal |
| 95 | + System.out.println("Predictions are equal to tensorflow"); |
| 96 | +} else { |
| 97 | + throw new RuntimeException("Predictions don't match!"); |
| 98 | +} |
| 99 | +``` |
| 100 | +用于判断结果预测,和所给的标签是否相同 |
| 101 | + |
| 102 | +# 使用不同的API用于预测 input_b 的值 |
| 103 | +``` |
| 104 | +//Now to run with the samediff executor, with input_b array expecting to get prediction_b |
| 105 | +val graphSD = TFGraphMapper.getInstance().importGraph(new File(FROZEN_MLP)); //Reimport graph here, necessary for the 1.0 alpha release |
| 106 | +graphSD.associateArrayWithVariable(inputsPredictions.get("input_b"), graph.variableMap().get("input")); |
| 107 | +INDArray samediffPred = graphSD.execAndEndResult(); |
| 108 | +System.out.println("SameDiff exec prediction for input_b:\n" + samediffPred); |
| 109 | +if (samediffPred.equals(inputsPredictions.get("prediction_b"))) { |
| 110 | + //this is true and therefore predictions are equal |
| 111 | + System.out.println("Predictions are equal to tensorflow"); |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +# 对模型进行新增op |
| 116 | +``` |
| 117 | +//add to graph to demonstrate pytorch like capability |
| 118 | +System.out.println("Adding new op to graph.."); |
| 119 | +SDVariable linspaceConstant = graphSD.var("linspace", Nd4j.linspace(1, 10, 10)); |
| 120 | +SDVariable totalOutput = graphSD.getVariable("output").add(linspaceConstant); |
| 121 | +INDArray totalOutputArr = totalOutput.eval(); |
| 122 | +System.out.println(totalOutputArr); |
| 123 | +``` |
| 124 | +这个代码的意思就是对原有模型添加新的操作。 |
| 125 | + |
| 126 | + 1. 首先使用`graphSD.var("linspace", Nd4j.linspace(1, 10, 10))`获取[1,2,3 ... 10],10个整数的向量 |
| 127 | + 2. `graphSD.getVariable("output").add(linspaceConstant);`将这个向量加入到模型的输出中。 |
| 128 | + |
| 129 | +# 整体输出 |
| 130 | +``` |
| 131 | +LIBND4J exec prediction for input_a: |
| 132 | +[[ 0, 0, 0, 0, 0, 0, 0, 1.0000, 0, 0]] |
| 133 | +Predictions are equal to tensorflow |
| 134 | +22:39:16,498 WARN ~ No input found for Add and op name mmul |
| 135 | +22:39:16,498 WARN ~ No input found for Add_1 and op name mmul |
| 136 | +SameDiff exec prediction for input_b: |
| 137 | +[[ 0, 0, 1.0000, 0, 0, 0, 0, 0, 0, 0]] |
| 138 | +Predictions are equal to tensorflow |
| 139 | +Adding new op to graph.. |
| 140 | +[[ 1.0000, 2.0000, 4.0000, 4.0000, 5.0000, 6.0000, 7.0000, 8.0000, 9.0000, 10.0000]] |
| 141 | +``` |
| 142 | +在最后我们可以看到,因为增加了新的op操作, 模型的原本输出`[[ 0, 0, 1.0000, 0, 0, 0, 0, 0, 0, 0]]` 加上了`[1,2,3 ... 10]`就会变成对应的`[[ 1.0000, 2.0000, 4.0000, 4.0000, 5.0000, 6.0000, 7.0000, 8.0000, 9.0000, 10.0000]]`。 |
| 143 | + |
| 144 | + |
| 145 | +--- |
| 146 | +更多文档可以查看 https://github.com/sjsdfg/deeplearning4j-issues。 |
| 147 | +你的star是我持续分享的动力 |
| 148 | + |
| 149 | +代码地址已经放在github上面,自行下载即可: https://github.com/sjsdfg/dl4j-tutorials |
| 150 | + |
| 151 | + |
| 152 | + [1]: http://static.zybuluo.com/ZzzJoe/zrkt70us7dx0dmnpsqjud2w0/%E8%B5%84%E6%BA%90%E6%96%87%E4%BB%B6%E5%88%97%E8%A1%A8.png |
0 commit comments