use of org.nd4j.linalg.io.ClassPathResource in project nd4j by deeplearning4j.
the class OnnxGraphMapperTests method test1dCnn.
@Test
public void test1dCnn() throws Exception {
val loadedFile = new ClassPathResource("onnx_graphs/sm_cnn.onnx").getInputStream();
val mapped = OnnxGraphMapper.getInstance().importGraph(loadedFile);
System.out.println(mapped.variables());
}
use of org.nd4j.linalg.io.ClassPathResource in project nd4j by deeplearning4j.
the class OnnxGraphMapperTests method testLoadResnet.
@Test
public void testLoadResnet() throws Exception {
val loadedFile = new ClassPathResource("onnx_graphs/resnet50/model.pb").getInputStream();
val mapped = OnnxGraphMapper.getInstance().importGraph(loadedFile);
}
use of org.nd4j.linalg.io.ClassPathResource in project nd4j by deeplearning4j.
the class OnnxGraphMapperTests method testMapper.
@Test
public void testMapper() throws Exception {
try (val inputs = new ClassPathResource("onnx_graphs/embedding_only.onnx").getInputStream()) {
OnnxProto3.GraphProto graphProto = OnnxProto3.ModelProto.parseFrom(inputs).getGraph();
OnnxGraphMapper onnxGraphMapper = new OnnxGraphMapper();
assertEquals(graphProto.getNodeList().size(), onnxGraphMapper.getNodeList(graphProto).size());
assertEquals(4, onnxGraphMapper.variablesForGraph(graphProto).size());
val initializer = graphProto.getInput(0).getType().getTensorType();
INDArray arr = onnxGraphMapper.getNDArrayFromTensor(graphProto.getInitializer(0).getName(), initializer, graphProto);
assumeNotNull(arr);
for (val node : graphProto.getNodeList()) {
assertEquals(node.getAttributeList().size(), onnxGraphMapper.getAttrMap(node).size());
}
val sameDiff = onnxGraphMapper.importGraph(graphProto);
assertEquals(1, sameDiff.functions().length);
System.out.println(sameDiff);
}
}
use of org.nd4j.linalg.io.ClassPathResource in project nd4j by deeplearning4j.
the class TFGraphTestAllHelper method readVars.
protected static Map<String, INDArray> readVars(String modelName, String base_dir, String pattern) throws IOException {
Map<String, INDArray> varMap = new HashMap<>();
String modelDir = base_dir + "/" + modelName;
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new ClassPathResource(modelDir).getClassLoader());
Resource[] resources = resolver.getResources("classpath*:" + modelDir + "/" + pattern + ".shape");
val dtype = Nd4j.dataType();
for (int i = 0; i < resources.length; i++) {
String fileName = resources[i].getFilename();
String varPath = modelDir + "/" + fileName;
String[] varNameArr = fileName.split("\\.");
String varName = String.join(".", Arrays.copyOfRange(varNameArr, 0, varNameArr.length - 2));
int[] varShape = Nd4j.readNumpy(new ClassPathResource(varPath).getInputStream(), ",").data().asInt();
try {
float[] varContents = Nd4j.readNumpy(new ClassPathResource(varPath.replace(".shape", ".csv")).getInputStream(), ",").data().asFloat();
INDArray varValue;
if (varShape.length == 1) {
if (varShape[0] == 0) {
varValue = Nd4j.trueScalar(varContents[0]);
} else {
varValue = Nd4j.trueVector(varContents);
}
} else {
varValue = Nd4j.create(varContents, varShape);
}
// varValue = Nd4j.readNumpy(new ClassPathResource(varPath.replace(".shape", ".csv")).getInputStream(), ",").reshape(varShape);
if (varName.contains("____")) {
// these are intermediate node outputs
varMap.put(varName.replaceAll("____", "/"), varValue);
} else {
varMap.put(varName, varValue);
}
} catch (NumberFormatException e) {
// FIXME: we can't parse boolean arrays right now :(
continue;
}
}
return varMap;
}
use of org.nd4j.linalg.io.ClassPathResource in project nd4j by deeplearning4j.
the class TFGraphTestAllHelper method modelDirNames.
private static String[] modelDirNames(String base_dir, ExecuteWith executeWith) throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new ClassPathResource(base_dir).getClassLoader());
Resource[] resources = resolver.getResources("classpath*:" + base_dir + "/**/frozen_model.pb");
String[] exampleNames = new String[resources.length];
for (int i = 0; i < resources.length; i++) {
String nestedName = resources[i].getURL().toString().split(base_dir + "/")[1];
exampleNames[i] = nestedName.replaceAll(Pattern.quote(executeWith.getDefaultBaseDir()), "").replaceAll("/frozen_model.pb", "");
}
return exampleNames;
}
Aggregations