List of usage examples for org.deeplearning4j.util ModelSerializer writeModel
public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater) throws IOException
From source file:com.circle_technologies.cnn4j.predictive.network.Network.java
License:Apache License
/** * Saves the network model to a specified file. * * @param file The file to save the model to. * @return <code>true - </code> if the model saved correctly to the file. <br> * <code>false - </code> on failure (IOExeption) *//*from ww w .j a va 2 s. c o m*/ public boolean save(File file) { try { ModelSerializer.writeModel(mMultiLayerNetwork, file, true); return true; } catch (IOException e) { Log.debug("ERROR", "Failed storing model"); return false; } }
From source file:com.sliit.neuralnetwork.RecurrentNN.java
private void loadSaveNN(String name, boolean save) { System.out.println("recNN loadSaveNN"); File directory = new File(uploadDirectory); File[] allNN = directory.listFiles(); boolean status = false; try {// w w w.jav a 2 s. c o m if (model == null && save) { buildModel(); } if (allNN != null && allNN.length > 0) { for (File NN : allNN) { String fnme = FilenameUtils.removeExtension(NN.getName()); if (name.equals(fnme)) { status = true; if (save) { ModelSerializer.writeModel(model, NN, true); System.out.println("Model Saved With Weights Successfully"); } else { model = ModelSerializer.restoreMultiLayerNetwork(NN); } break; } } } if (!status && save) { //File tempFIle = File.createTempFile(name,".zip",directory); File tempFile = new File(directory.getAbsolutePath() + "/" + name + ".zip"); if (!tempFile.exists()) { tempFile.createNewFile(); } ModelSerializer.writeModel(model, tempFile, true); } } catch (IOException e) { System.out.println("Error occurred:" + e.getMessage()); } }
From source file:examples.cnn.NetworkTrainer.java
License:Apache License
public void train(JavaRDD<DataSet> train, JavaRDD<DataSet> test) { int batchSize = 12 * cores; int lrCount = 0; double bestAccuracy = Double.MIN_VALUE; double learningRate = initialLearningRate; int trainCount = Long.valueOf(train.count()).intValue(); log.info("Number of training images {}", trainCount); log.info("Number of test images {}", test.count()); MultiLayerNetwork net = new MultiLayerNetwork( model.apply(learningRate, width, height, channels, numLabels)); net.init();//from w w w .j a va2 s. c om Map<Integer, Double> acc = new HashMap<>(); for (int i = 0; i < epochs; i++) { SparkDl4jMultiLayer sparkNetwork = networkToSparkNetwork.apply(net); final MultiLayerNetwork nn = sparkNetwork.fitDataSet(train, batchSize, trainCount, cores); log.info("Epoch {} completed", i); JavaPairRDD<Object, Object> predictionsAndLabels = test.mapToPair( ds -> new Tuple2<>(label(nn.output(ds.getFeatureMatrix(), false)), label(ds.getLabels()))); MulticlassMetrics metrics = new MulticlassMetrics(predictionsAndLabels.rdd()); double accuracy = 1.0 * predictionsAndLabels.filter(x -> x._1.equals(x._2)).count() / test.count(); log.info("Epoch {} accuracy {} ", i, accuracy); acc.put(i, accuracy); predictionsAndLabels.take(10).forEach(t -> log.info("predicted {}, label {}", t._1, t._2)); log.info("confusionMatrix {}", metrics.confusionMatrix()); INDArray params = nn.params(); if (accuracy > bestAccuracy) { bestAccuracy = accuracy; try { ModelSerializer.writeModel(nn, new File(workingDir, Double.toString(accuracy)), false); } catch (IOException e) { log.error("Error writing trained model", e); } lrCount = 0; } else { if (++lrCount % stepDecayTreshold == 0) { learningRate *= learningRateDecayFactor; } if (lrCount >= resetLearningRateThreshold) { lrCount = 0; learningRate = initialLearningRate; } if (learningRate < minimumLearningRate) { lrCount = 0; learningRate = initialLearningRate; } if (bestAccuracy - accuracy > downgradeAccuracyThreshold) { params = ModelLoader.load(workingDir, bestAccuracy); } } net = new MultiLayerNetwork(model.apply(learningRate, width, height, channels, numLabels)); net.init(); net.setParameters(params); log.info("Learning rate {} for epoch {}", learningRate, i + 1); } log.info("Training completed"); }
From source file:org.apache.tika.dl.imagerec.DL4JVGG16Net.java
License:Apache License
@Override public void initialize(Map<String, Param> params) throws TikaConfigException { try {/*from ww w .j a v a 2s . co m*/ if (serialize) { if (cacheDir.exists()) { model = ModelSerializer.restoreComputationGraph(cacheDir); LOG.info("Preprocessed Model Loaded from {}", cacheDir); } else { LOG.warn("Preprocessed Model doesn't exist at {}", cacheDir); cacheDir.getParentFile().mkdirs(); ZooModel zooModel = VGG16.builder().build(); model = (ComputationGraph) zooModel.initPretrained(PretrainedType.IMAGENET); LOG.info( "Saving the Loaded model for future use. Saved models are more optimised to consume less resources."); ModelSerializer.writeModel(model, cacheDir, true); } } else { LOG.info("Weight graph model loaded via dl4j Helper functions"); ZooModel zooModel = VGG16.builder().build(); model = (ComputationGraph) zooModel.initPretrained(PretrainedType.IMAGENET); } imageNetLabels = new ImageNetLabels(); available = true; } catch (Exception e) { available = false; LOG.warn(e.getMessage(), e); throw new TikaConfigException(e.getMessage(), e); } }
From source file:org.apache.tika.parser.recognition.dl4j.DL4JImageRecogniser.java
License:Apache License
@Override public void initialize(Map<String, Param> params) throws TikaConfigException { try {/*from ww w . j a v a 2s . co m*/ TrainedModelHelper helper; switch (modelType) { case "VGG16NOTOP": throw new TikaConfigException("VGG16NOTOP is not supported right now"); /*# TODO hookup VGGNOTOP by uncommenting following code once the issue is resolved by dl4j team modelFile = new File(MODEL_DIR_PREPROCESSED+File.separator+"vgg16_notop.zip"); locationToSave= new File(MODEL_DIR+File.separator+"tikaPreprocessed"+File.separator+"vgg16.zip"); helper = new TrainedModelHelper(TrainedModels.VGG16NOTOP); break;*/ case "VGG16": helper = new TrainedModelHelper(TrainedModels.VGG16); modelFile = new File(MODEL_DIR_PREPROCESSED + File.separator + "vgg16.zip"); locationToSave = new File( MODEL_DIR + File.separator + "tikaPreprocessed" + File.separator + "vgg16.zip"); break; default: throw new TikaConfigException("Unknown or unsupported model"); } if (serialize.trim().toLowerCase(Locale.ROOT).equals("yes")) { if (!modelFile.exists()) { LOG.warn("Preprocessed Model doesn't exist at {}", modelFile); modelFile.getParentFile().mkdirs(); model = helper.loadModel(); LOG.info( "Saving the Loaded model for future use. Saved models are more optimised to consume less resources."); ModelSerializer.writeModel(model, locationToSave, true); available = true; } else { model = ModelSerializer.restoreComputationGraph(locationToSave); LOG.info("Preprocessed Model Loaded from {}", locationToSave); available = true; } } else if (serialize.trim().toLowerCase(Locale.ROOT).equals("no")) { LOG.info("Weight graph model loaded via dl4j Helper functions"); model = helper.loadModel(); available = true; } else { throw new TikaConfigException("Configuration Error. serialization can be either yes or no."); } if (!available) { return; } HashMap<Pattern, String> patterns = new HashMap<>(); patterns.put(Pattern.compile(outPattern), null); setMetadataExtractionPatterns(patterns); setIgnoredLineConsumer(IGNORED_LINE_LOGGER); } catch (Exception e) { LOG.warn("exception occured"); throw new TikaConfigException(e.getMessage(), e); } }
From source file:org.audiveris.omrdataset.train.Training.java
License:Open Source License
/** * Perform the training of the neural network. * <p>//from ww w . j a v a 2 s . c o m * Before training is launched, if the network model exists on disk it is reloaded, otherwise a * brand new one is created. * * @throws Exception in case of IO problem or interruption */ public void process() throws Exception { Files.createDirectories(MISTAKES_PATH); int nChannels = 1; // Number of input channels int batchSize = 64; // Batch size int nEpochs = 1; //3; //10; //2; // Number of training epochs int iterations = 1; // 2; //10; // Number of training iterations int seed = 123; // // Pixel norms NormalizerStandardize normalizer = NormalizerSerializer.getDefault().restore(PIXELS_PATH.toFile()); // Get the dataset using the record reader. CSVRecordReader handles loading/parsing int labelIndex = CONTEXT_WIDTH * CONTEXT_HEIGHT; // format: all cells then label int numLinesToSkip = 1; // Because of header comment line String delimiter = ","; RecordReader trainRecordReader = new CSVRecordReader(numLinesToSkip, delimiter); trainRecordReader.initialize(new FileSplit(FEATURES_PATH.toFile())); logger.info("Getting dataset from {} ...", FEATURES_PATH); RecordReaderDataSetIterator trainIter = new RecordReaderDataSetIterator(trainRecordReader, batchSize, labelIndex, numClasses, -1); trainIter.setCollectMetaData(true); //Instruct the iterator to collect metadata, and store it in the DataSet objects RecordReader testRecordReader = new CSVRecordReader(numLinesToSkip, delimiter); testRecordReader.initialize(new FileSplit(FEATURES_PATH.toFile())); RecordReaderDataSetIterator testIter = new RecordReaderDataSetIterator(testRecordReader, batchSize, labelIndex, numClasses, -1); testIter.setCollectMetaData(true); //Instruct the iterator to collect metadata, and store it in the DataSet objects // Normalization DataSetPreProcessor preProcessor = new MyPreProcessor(normalizer); trainIter.setPreProcessor(preProcessor); testIter.setPreProcessor(preProcessor); if (false) { System.out.println("\n +++++ Test Set Examples MetaData +++++"); while (testIter.hasNext()) { DataSet ds = testIter.next(); List<RecordMetaData> testMetaData = ds.getExampleMetaData(RecordMetaData.class); for (RecordMetaData recordMetaData : testMetaData) { System.out.println(recordMetaData.getLocation()); } } testIter.reset(); } final MultiLayerNetwork model; if (Files.exists(MODEL_PATH)) { model = ModelSerializer.restoreMultiLayerNetwork(MODEL_PATH.toFile(), false); logger.info("Model restored from {}", MODEL_PATH.toAbsolutePath()); } else { logger.info("Building model from scratch"); MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder() // .seed(seed) // .iterations(iterations) // .regularization(true) // .l2(0.0005) // .learningRate(.002) // HB: was .01 initially //.biasLearningRate(0.02) //.learningRateDecayPolicy(LearningRatePolicy.Inverse).lrPolicyDecayRate(0.001).lrPolicyPower(0.75) .weightInit(WeightInit.XAVIER) // .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT) // .updater(Updater.NESTEROVS).momentum(0.9) // .list() // .layer(0, new ConvolutionLayer.Builder(5, 5) // .name("C0") // .nIn(nChannels) // .stride(1, 1) // .nOut(20) // .activation(Activation.IDENTITY) // .build()) // .layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) // .name("S1") // .kernelSize(2, 2) // .stride(2, 2) // .build()) // .layer(2, new ConvolutionLayer.Builder(5, 5) // .name("C2") // .stride(1, 1) // .nOut(50) // .activation(Activation.IDENTITY) // .build()) // .layer(3, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX) // .name("S3") // .kernelSize(2, 2) // .stride(2, 2) // .build()) // .layer(4, new DenseLayer.Builder() // .name("D4") // .nOut(500) // .activation(Activation.RELU) // .build()) // .layer(5, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD) // .name("O5") // .nOut(numClasses) // .activation(Activation.SOFTMAX) // .build()) // .setInputType(InputType.convolutionalFlat(CONTEXT_HEIGHT, CONTEXT_WIDTH, 1)); MultiLayerConfiguration conf = builder.build(); model = new MultiLayerNetwork(conf); model.init(); } // Prepare monitoring UIServer uiServer = null; try { if (true) { //Initialize the user interface backend uiServer = UIServer.getInstance(); //Configure where the network information (gradients, score vs. time etc) is to be stored. Here: store in memory. StatsStorage statsStorage = new InMemoryStatsStorage(); //Alternative: new FileStatsStorage(File), for saving and loading later //Attach the StatsStorage instance to the UI: this allows the contents of the StatsStorage to be visualized uiServer.attach(statsStorage); //Then add the StatsListener to collect this information from the network, as it trains model.setListeners(new StatsListener(statsStorage), new ScoreIterationListener(10)); } else { model.setListeners(new ScoreIterationListener(10)); } logger.info("Training model..."); for (int epoch = 1; epoch <= nEpochs; epoch++) { Path epochFolder = Main.cli.mistakes ? MISTAKES_PATH.resolve("epoch#" + epoch) : null; long start = System.currentTimeMillis(); model.fit(trainIter); long stop = System.currentTimeMillis(); double dur = stop - start; logger.info(String.format("*** End epoch#%d, time: %.0f sec", epoch, dur / 1000)); // Save model ModelSerializer.writeModel(model, MODEL_PATH.toFile(), false); ModelSerializer.addNormalizerToModel(MODEL_PATH.toFile(), normalizer); logger.info("Model+normalizer stored as {}", MODEL_PATH.toAbsolutePath()); // // logger.info("Evaluating model..."); // // Evaluation eval = new Evaluation(OmrShapes.NAMES); // // while (testIter.hasNext()) { // DataSet ds = testIter.next(); // List<RecordMetaData> testMetaData = ds.getExampleMetaData(RecordMetaData.class); // INDArray output = model.output(ds.getFeatureMatrix(), false); // eval.eval(ds.getLabels(), output, testMetaData); // } // // System.out.println(eval.stats()); // testIter.reset(); // // //Get a list of prediction errors, from the Evaluation object // //Prediction errors like this are only available after calling iterator.setCollectMetaData(true) // List<Prediction> mistakes = eval.getPredictionErrors(); // logger.info("Epoch#{} Prediction Errors: {}", epoch, mistakes.size()); // // //We can also load a subset of the data, to a DataSet object: // //Here we load the raw data: // List<RecordMetaData> predictionErrorMetaData = new ArrayList<RecordMetaData>(); // // for (Prediction p : mistakes) { // predictionErrorMetaData.add(p.getRecordMetaData(RecordMetaData.class)); // } // // List<Record> predictionErrorRawData = testRecordReader.loadFromMetaData( // predictionErrorMetaData); // // for (int ie = 0; ie < mistakes.size(); ie++) { // Prediction p = mistakes.get(ie); // List<Writable> rawData = predictionErrorRawData.get(ie).getRecord(); // saveMistake(p, rawData, epochFolder); // } // // // // To avoid long useless sessions... // if (mistakes.isEmpty()) { // logger.info("No mistakes left, training stopped."); // // break; // } } } finally { // Stop monitoring if (uiServer != null) { uiServer.stop(); } } logger.info("****************Example finished********************"); }
From source file:org.ensor.fftmusings.autoencoder.DeepAutoencoder.java
public static void main(String[] args) throws Exception { double learningRate = 0.0001; if (args.length > 0) { learningRate = Double.parseDouble(args[0]); }//from w w w . j a v a 2s . co m MultiLayerNetwork model = readAutoencoder("data/daa/deepmodel.daa", learningRate); PrintStream log = System.out; DataSetIterator iter = createIterator(); log.println("Train model...."); int epoch = 0; for (int i = 0; i < 100; i++) { model.fit(iter); iter.reset(); evaluateModel(model, epoch); ModelSerializer.writeModel(model, "data/daa/deepmodel.daa", true); epoch++; } }
From source file:org.ensor.fftmusings.autoencoder.GenericAutoencoder.java
public static void main(String[] args) throws Exception { String encoderFile = args[0]; //encoderFile = "data/daa/autoencoder.json"; Autoencoder ae = readAutoencoder(encoderFile); PrintStream log = System.out; DataSetIterator iter = createIterator(ae); log.println("Train model...."); int epoch = 0; for (int i = 0; i < ae.getMetadata().getEpochs(); i++) { ae.getModel().fit(iter);// w w w . java 2s . c o m iter.reset(); evaluateModel(ae, epoch); ModelSerializer.writeModel(ae.getModel(), ae.getMetadata().getFilename(), true); epoch++; } }
From source file:org.ensor.fftmusings.autoencoder.RNNTrainer.java
public static void main(String[] args) throws Exception { MultiLayerNetwork stackedAutoencoder = ModelSerializer.restoreMultiLayerNetwork("stack.rnn"); Random rng = new Random(); RNNIterator iter = new RNNIterator(stackedAutoencoder, rng, 100, 100, System.out); double learningRate = 0.0001; if (args.length != 0) { learningRate = Double.parseDouble(args[0]); }/*from w w w. ja va 2s . c o m*/ int nGaussians = 8; int labelWidth = iter.totalOutcomes(); int inputWidth = iter.inputColumns(); int lstmLayerSize = 400; int bttLength = 50; LossMixtureDensity costFunction = LossMixtureDensity.builder().gaussians(nGaussians).labelWidth(inputWidth) .build(); //Set up network configuration: MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1) .learningRate(learningRate).rmsDecay(0.95).seed(12345).iterations(1).regularization(true).l2(0.001) .weightInit(WeightInit.XAVIER).list() .layer(0, new GravesLSTM.Builder().nIn(inputWidth).nOut(lstmLayerSize).updater(Updater.RMSPROP) .activation(Activation.TANH).build()) .layer(1, new GravesLSTM.Builder().nIn(lstmLayerSize).nOut(lstmLayerSize).updater(Updater.RMSPROP) .activation(Activation.TANH).build()) // .layer(2, new RnnOutputLayer.Builder() // .nIn(lstmLayerSize) // .nOut((labelWidth + 2) * nGaussians) // .activation(Activation.IDENTITY) // //.lossFunction(LossFunctions.LossFunction.MSE) // .lossFunction(LossMixtureDensity.builder() // .gaussians(nGaussians) // .labelWidth(inputWidth) // .build()) // .updater(Updater.RMSPROP) // .weightInit(WeightInit.DISTRIBUTION) // .dist(new UniformDistribution(-0.08, 0.08)).build()) .layer(2, new MixtureDensityRNNOutputLayer.Builder().gaussians(nGaussians).nIn(lstmLayerSize) .nOut(labelWidth).updater(Updater.RMSPROP).build()) .pretrain(false).backprop(true).backpropType(BackpropType.TruncatedBPTT) .tBPTTForwardLength(bttLength).tBPTTBackwardLength(bttLength).build(); MultiLayerNetwork model = new MultiLayerNetwork(conf); model.init(); model.setListeners(new ScoreIterationListener(System.out)); for (int epoch = 0; epoch < 300; epoch++) { model.fit(iter); iter.reset(); evaluateModel(model, costFunction, stackedAutoencoder, rng, epoch); ModelSerializer.writeModel(model, "stack-timeseries.rnn", true); } }
From source file:org.ensor.fftmusings.autoencoder.RNNTrainer2.java
public static void main(String[] args) throws Exception { MultiLayerNetwork stackedAutoencoder = ModelSerializer.restoreMultiLayerNetwork("stack.rnn"); Random rng = new Random(); RNNIterator iter = new RNNIterator(stackedAutoencoder, rng, 100, 100, System.out); int labels = iter.inputColumns(); int lstmLayerSize = 200; int bttLength = 50; //Set up network configuration: MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1).learningRate(0.1) .rmsDecay(0.95).seed(12345).iterations(1).regularization(true).l2(0.001).list() .layer(0,//from ww w. ja v a2s. c o m new GravesLSTM.Builder().nIn(labels).nOut(lstmLayerSize).updater(Updater.RMSPROP) .activation(Activation.TANH).weightInit(WeightInit.DISTRIBUTION) .dist(new UniformDistribution(-0.08, 0.08)).build()) .layer(1, new GravesLSTM.Builder().nIn(lstmLayerSize).nOut(lstmLayerSize).updater(Updater.RMSPROP) .activation(Activation.TANH).weightInit(WeightInit.DISTRIBUTION) .dist(new UniformDistribution(-0.08, 0.08)).build()) .layer(2, new RnnOutputLayer.Builder().nIn(lstmLayerSize).nOut(labels).lossFunction(LossFunction.MSE) .updater(Updater.RMSPROP).weightInit(WeightInit.DISTRIBUTION) .dist(new UniformDistribution(-0.08, 0.08)).build()) .pretrain(false).backprop(true).backpropType(BackpropType.TruncatedBPTT) .tBPTTForwardLength(bttLength).tBPTTBackwardLength(bttLength).build(); MultiLayerNetwork model = new MultiLayerNetwork(conf); model.init(); model.setListeners(new ScoreIterationListener(System.out)); for (int epoch = 0; epoch < 300; epoch++) { model.fit(iter); iter.reset(); evaluateModel(model, stackedAutoencoder, rng, epoch); ModelSerializer.writeModel(model, "stack-timeseries.rnn", true); } }