public void saveModel() { try { File tmpFile = File.createTempFile("model", null); ModelSerializer.writeModel(net.getNN(), tmpFile, true); } catch (IOException ioe) { } }
public void loadModel(File file) { try { ModelSerializer.restoreMultiLayerNetwork(file); } catch (IOException ioe) { } }
public static void main(String[] args) throws Exception { int height = 28; int width = 28; int channels = 1; // recordReader.getLabels() // In this version Labels are always in order // So this is no longer needed // List<Integer> labelList = Arrays.asList(2,3,7,1,6,4,0,5,8,9); List<Integer> labelList = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // pop up file chooser String filechose = fileChose().toString(); // LOAD NEURAL NETWORK // Where to save model File locationToSave = new File("trained_mnist_model.zip"); // Check for presence of saved model if (locationToSave.exists()) { System.out.println("\n######Saved Model Found######\n"); } else { System.out.println("\n\n#######File not found!#######"); System.out.println("This example depends on running "); System.out.println("MnistImagePipelineExampleSave"); System.out.println("Run that Example First"); System.out.println("#############################\n\n"); System.exit(0); } MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(locationToSave); log.info("*********TEST YOUR IMAGE AGAINST SAVED NETWORK********"); // FileChose is a string we will need a file File file = new File(filechose); // Use NativeImageLoader to convert to numerical matrix NativeImageLoader loader = new NativeImageLoader(height, width, channels); // Get the image into an INDarray INDArray image = loader.asMatrix(file); // 0-255 // 0-1 DataNormalization scaler = new ImagePreProcessingScaler(0, 1); scaler.transform(image); // Pass through to neural Net INDArray output = model.output(image); log.info("## The FILE CHOSEN WAS " + filechose); log.info("## The Neural Nets Pediction ##"); log.info("## list of probabilities per label ##"); // log.info("## List of Labels in Order## "); // In new versions labels are always in order log.info(output.toString()); log.info(labelList.toString()); }