@Override public byte[] getSerializedData() { File file = null; try { // libsvm doesn't have a method to serialize the model into a buffer, so write it out to a file and then read it back in file = File.createTempFile("svm", null); svm.svm_save_model(file.getAbsolutePath(), model); return Files.readAllBytes(file.toPath()); } catch (IOException e) { throw new UncheckedIOException(e); } finally { if (file != null) { //noinspection ResultOfMethodCallIgnored file.delete(); } } }
public void save(String filename) throws IOException{ svm.svm_save_model(filename, model); }
public void save(String directory, String filename) throws IOException{ File dir = new File( directory ); if( ! dir.exists() ) dir.mkdirs(); svm.svm_save_model(directory + filename, model); }
@Override public byte[] getSerializedData() { File file = null; try { // libsvm doesn't have a method to serialize the model into a buffer, so write it out to a file and then read it back in file = File.createTempFile("svm", null); svm.svm_save_model(file.getAbsolutePath(), model); return Files.readAllBytes(file.toPath()); } catch (IOException e) { throw new UncheckedIOException(e); } finally { if (file != null) { //noinspection ResultOfMethodCallIgnored file.delete(); } } }
@Override void doWrite(final Path path) throws IOException { // Write the dictionary this.dictionary.writeTo(path.resolve("dictionary")); // Write the model final File tmpFile = File.createTempFile("svm", ".bin"); tmpFile.deleteOnExit(); svm.svm_save_model(tmpFile.getAbsolutePath(), this.model); final String modelString = com.google.common.io.Files.toString(tmpFile, Charset.defaultCharset()); tmpFile.delete(); Files.write(path.resolve("model"), modelString.getBytes(Charsets.UTF_8)); }
@Override public void persistModel(String modelFile) throws IOException { if (this.labelIndexer == null) { throw new RuntimeException("LabelIndexer is null!"); } ZipOutputStream zipos = new ZipOutputStream(new FileOutputStream(modelFile)); String svmModelAbsolutePath = modelFile + ".model"; String svmModelFilename = new File(svmModelAbsolutePath).getName(); svm.svm_save_model(svmModelAbsolutePath, model); ZipEntry libSVMModelZipEntry = new ZipEntry(svmModelFilename); zipos.putNextEntry(libSVMModelZipEntry); IOUtils.copy(new FileInputStream(svmModelAbsolutePath), zipos); zipos.closeEntry(); String labelIndexerAbsolutePath = modelFile + ".lbindexer"; String labelIndexerFileName = new File(labelIndexerAbsolutePath).getName(); String labelIndexerString = this.labelIndexer.serializeToString(); ZipEntry labelIndexerZipEntry = new ZipEntry(labelIndexerFileName); zipos.putNextEntry(labelIndexerZipEntry); IOUtils.write(labelIndexerString, zipos, Charset.defaultCharset()); zipos.closeEntry(); IOUtils.closeQuietly(zipos); FileUtils.forceDelete(new File(svmModelAbsolutePath)); }
public void write(String modelDir, IClassifier learningData) throws IOException { SvmClassifier cl = (SvmClassifier) learningData; File f = new File(modelDir); f.mkdirs(); String vc = modelDir + Os.pathSeparator() + "validCategories.db"; DataOutputStream valid_os = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(vc), 4096)); valid_os.writeInt(cl.getCategoryCount()); // Write categories vectors and ranges. for (short catID = 0; catID < cl.getCategoryCount(); catID++) { valid_os.writeShort(catID); String filename = modelDir + Os.pathSeparator() + catID + ".db"; svm.svm_save_model(filename, cl._models[catID]); } valid_os.close(); }
public void write(String modelDir, IClassifier learningData) throws IOException { SvmRegressionClassifier cl = (SvmRegressionClassifier) learningData; File f = new File(modelDir); f.mkdirs(); String vc = modelDir + Os.pathSeparator() + "validCategories.db"; DataOutputStream valid_os = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(vc), 4096)); valid_os.writeInt(cl.getCategoryCount()); // Write categories vectors and ranges. for (short catID = 0; catID < cl.getCategoryCount(); catID++) { valid_os.writeShort(catID); String filename = modelDir + Os.pathSeparator() + catID + ".db"; svm.svm_save_model(filename, cl.model()); } valid_os.close(); }
@Override public void saveModel(String modelPath) throws IOException { Formatter formatter = new Formatter(new StringBuilder()); BufferedWriter fp_save; fp_save = new BufferedWriter(new FileWriter(modelPath + ".range")); Double lower = 0.0; Double upper = 1.0; formatter.format("x\n"); formatter.format("%.16g %.16g\n", lower, upper); for(Integer i=0 ; i<featureVectorBuilder.size() ; ++i) { formatter.format("%d %.16g %.16g\n", i, scaler.getLimits()[i].getMin(), scaler.getLimits()[i].getMax()); } fp_save.write(formatter.toString()); fp_save.close(); svm.svm_save_model(modelPath, model); }
private static Classifier trainJava(final Parameters parameters, final Iterable<LabelledVector> trainingSet) throws IOException { // Prepare the svm_parameter object based on supplied parameters final svm_parameter parameter = encodeParameters(parameters); // Encode the training set as an svm_problem object, filling a dictionary meanwhile final Dictionary<String> dictionary = Dictionary.create(); final svm_problem problem = encodeProblem(dictionary, trainingSet); // Perform training final svm_model model = svm.svm_train(problem, parameter); // Compute model hash, by saving and reloading SVM model final File tmpFile = File.createTempFile("svm", ".bin"); tmpFile.deleteOnExit(); svm.svm_save_model(tmpFile.getAbsolutePath(), model); final String modelString = com.google.common.io.Files.toString(tmpFile, Charset.defaultCharset()); final String modelHash = computeHash(dictionary, modelString); final svm_model reloadedModel = svm .svm_load_model(new BufferedReader(new StringReader(modelString))); tmpFile.delete(); // Build and return the SVM object return new LibSvmClassifier(parameters, modelHash, dictionary, reloadedModel); }
File f = new File(filename); f.getParentFile().mkdirs(); svm.svm_save_model(filename, cl.model());
public void run(String argv[]) throws Exception { parse_command_line(argv); read_problem(); error_msg = svm.svm_check_parameter(prob, param); if (error_msg != null) { throw new Exception(error_msg); } model = svm.svm_train(prob, param); svm.svm_save_model(model_file_name, model); }
private void run(String argv[]) throws IOException { parse_command_line(argv); read_problem(); error_msg = svm.svm_check_parameter(prob, param); if (error_msg != null) { System.err.print("ERROR: " + error_msg + "\n"); System.exit(1); } if (cross_validation != 0) { do_cross_validation(); } else { model = svm.svm_train(prob, param); svm.svm_save_model(model_file_name, model); } }
@Override public void write(SvmModel model,String fileName) throws IOException{ saveFeatureList(model.getFeatures(),fileName); svm.svm_save_model(fileName,model.getModel()); } @Override
public void run(String argv[]) throws Exception { parse_command_line(argv); read_problem(); error_msg = svm.svm_check_parameter(prob, param); if (error_msg != null) { throw new Exception(error_msg); } model = svm.svm_train(prob, param); svm.svm_save_model(model_file_name, model); }
public void run(File input_file, File model_file, double c, int mem, double[] weight) throws IOException { input_file_name = input_file.getAbsolutePath(); model_file_name = model_file.getAbsolutePath(); //System.out.println("input_file_name: " + input_file_name); //System.out.println("model_file_name: " + model_file_name); //System.out.println("mem: " + mem); set_param(c, mem, weight); read_problem(); error_msg = svm.svm_check_parameter(prob,param); if(error_msg != null) { System.err.print("Error: "+error_msg+"\n"); System.exit(1); } if(cross_validation != 0) { //do_cross_validation(); } else { model = svm.svm_train(prob,param); svm.svm_save_model(model_file_name, model); } }
public void internal_learn() throws Exception { // dumps a file with the vectors for the documents File learningFile = new File(this.vector_location); // make space parse_command_line(); if (cross_validation && nfold < 2) throw new Exception("n-fold cross validation: n must >= 2\n"); read_problem(learningFile); error_msg = svm.svm_check_parameter(prob, param); if (error_msg != null) { System.err.print("Error: " + error_msg + "\n"); throw new Exception(error_msg); } if (cross_validation) { do_cross_validation(); } else { model = svm.svm_train(prob, param); svm.svm_save_model(model_file_name, model); } }
private void run(String argv[]) throws IOException { parse_command_line(argv); read_problem(); error_msg = svm.svm_check_parameter(prob, param); if (error_msg != null) { System.err.print("ERROR: " + error_msg + "\n"); System.exit(1); } if (cross_validation != 0) { do_cross_validation(); } else { model = svm.svm_train(prob, param); svm.svm_save_model(model_file_name, model); } }
/** * {@inheritDoc} * @see org.openimaj.ml.training.BatchTrainer#train(java.util.List) */ @Override public void train( final List<? extends Annotated<OBJECT, ANNOTATION>> data ) { // Check the data has 2 classes and update the class map. if( this.checkInputDataOK( data ) ) { // Setup the SVM problem final svm_parameter param = SVMAnnotator.getDefaultSVMParameters(); final svm_problem prob = this.getSVMProblem( data, param, this.extractor ); // Train the SVM this.model = libsvm.svm.svm_train( prob, param ); // Save the model if we're going to do that. if( this.saveModel != null ) try { svm.svm_save_model( this.saveModel.getAbsolutePath(), this.model ); } catch( final IOException e ) { e.printStackTrace(); } } }
svm.svm_save_model(filename, cl._models[catID]);