List of usage examples for weka.core SerializationHelper readAll
public static Object[] readAll(InputStream stream) throws Exception
From source file:meka.classifiers.multilabel.Evaluation.java
License:Open Source License
/** * RunExperiment - Build and evaluate a model with command-line options. * @param h multi-label classifier * @param options command line options *///from w ww. j a va2 s . c om public static void runExperiment(MultiLabelClassifier h, String options[]) throws Exception { // Help if (Utils.getOptionPos('h', options) >= 0) { System.out.println("\nHelp requested"); Evaluation.printOptions(h.listOptions()); return; } h.setOptions(options); if (h.getDebug()) System.out.println("Loading and preparing dataset ..."); // Load Instances from a file Instances D_train = loadDataset(options); Instances D_full = D_train; // Try extract and set a class index from the @relation name MLUtils.prepareData(D_train); // Override the number of classes with command-line option (optional) if (Utils.getOptionPos('C', options) >= 0) { int L = Integer.parseInt(Utils.getOption('C', options)); D_train.setClassIndex(L); } // We we still haven't found -C option, we can't continue (don't know how many labels) int L = D_train.classIndex(); if (L <= 0) { throw new Exception( "[Error] Number of labels not specified.\n\tYou must set the number of labels with the -C option, either inside the @relation tag of the Instances file, or on the command line."); // apparently the dataset didn't contain the '-C' flag, check in the command line options ... } // Randomize (Instances) int seed = (Utils.getOptionPos('s', options) >= 0) ? Integer.parseInt(Utils.getOption('s', options)) : 0; if (Utils.getFlag('R', options)) { D_train.randomize(new Random(seed)); } boolean Threaded = false; if (Utils.getOptionPos("Thr", options) >= 0) { Threaded = Utils.getFlag("Thr", options); } // Verbosity Option String voption = "1"; if (Utils.getOptionPos("verbosity", options) >= 0) { voption = Utils.getOption("verbosity", options); } // Save for later? //String fname = null; //if (Utils.getOptionPos('f',options) >= 0) { // fname = Utils.getOption('f',options); //} // Dump for later? String dname = null; if (Utils.getOptionPos('d', options) >= 0) { dname = Utils.getOption('d', options); } // Load from file? String lname = null; Instances dataHeader = null; if (Utils.getOptionPos('l', options) >= 0) { lname = Utils.getOption('l', options); Object[] data = SerializationHelper.readAll(lname); h = (MultiLabelClassifier) data[0]; if (data.length > 1) dataHeader = (Instances) data[1]; //Object o[] = SerializationHelper.readAll(lname); //h = (MultilabelClassifier)o[0]; } try { Result r = null; // Threshold OPtion String top = "PCut1"; // default if (Utils.getOptionPos("threshold", options) >= 0) top = Utils.getOption("threshold", options); if (Utils.getOptionPos('x', options) >= 0) { // CROSS-FOLD-VALIDATION int numFolds = MLUtils.getIntegerOption(Utils.getOption('x', options), 10); // default 10 // Check for remaining options Utils.checkForRemainingOptions(options); r = Evaluation.cvModel(h, D_train, numFolds, top, voption); System.out.println(r.toString()); } else { // TRAIN-TEST SPLIT Instances D_test = null; if (Utils.getOptionPos('T', options) >= 0) { // load separate test set try { D_test = loadDataset(options, 'T'); MLUtils.prepareData(D_test); } catch (Exception e) { throw new Exception("[Error] Failed to Load Test Instances from file.", e); } } else { // split training set into train and test sets // default split int N_T = (int) (D_train.numInstances() * 0.60); if (Utils.getOptionPos("split-percentage", options) >= 0) { // split by percentage double percentTrain = Double.parseDouble(Utils.getOption("split-percentage", options)); N_T = (int) Math.round((D_train.numInstances() * (percentTrain / 100.0))); } else if (Utils.getOptionPos("split-number", options) >= 0) { // split by number N_T = Integer.parseInt(Utils.getOption("split-number", options)); } int N_t = D_train.numInstances() - N_T; D_test = new Instances(D_train, N_T, N_t); D_train = new Instances(D_train, 0, N_T); } // Invert the split? if (Utils.getFlag('i', options)) { //boolean INVERT = Utils.getFlag('i',options); Instances temp = D_test; D_test = D_train; D_train = temp; } // Check for remaining options Utils.checkForRemainingOptions(options); if (h.getDebug()) System.out.println(":- Dataset -: " + MLUtils.getDatasetName(D_train) + "\tL=" + L + "\tD(t:T)=(" + D_train.numInstances() + ":" + D_test.numInstances() + ")\tLC(t:T)=" + Utils.roundDouble(MLUtils.labelCardinality(D_train, L), 2) + ":" + Utils.roundDouble(MLUtils.labelCardinality(D_test, L), 2) + ")"); if (lname != null) { // h is already built, and loaded from a file, test it! r = testClassifier(h, D_test); String t = top; if (top.startsWith("PCut")) { // if PCut is specified we need the training data, // so that we can calibrate the threshold! t = MLEvalUtils.getThreshold(r.predictions, D_train, top); } r = evaluateModel(h, D_test, t, voption); } else { //check if train and test set size are > 0 if (D_train.numInstances() > 0 && D_test.numInstances() > 0) { if (Threaded) { r = evaluateModelM(h, D_train, D_test, top, voption); } else { r = evaluateModel(h, D_train, D_test, top, voption); } } else { // otherwise just train on full set. Maybe better throw an exception. h.buildClassifier(D_full); } } // @todo, if D_train==null, assume h is already trained if (D_train.numInstances() > 0 && D_test.numInstances() > 0) { System.out.println(r.toString()); } } // Save model to file? if (dname != null) { dataHeader = new Instances(D_train, 0); SerializationHelper.writeAll(dname, new Object[] { h, dataHeader }); } } catch (Exception e) { e.printStackTrace(); Evaluation.printOptions(h.listOptions()); System.exit(1); } System.exit(0); }
From source file:meka.gui.explorer.classify.OpenModel.java
License:Open Source License
/** * Returns the action lister to use in the menu. * * @return the listener/*from ww w .ja v a 2 s . com*/ */ public ActionListener getActionListener(final ClassifyTab owner) { return new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MekaFileChooser filechooser; if (!hasSessionValue(owner, SESSION_KEY_MODELCHOOSER)) { filechooser = new MekaFileChooser(); ExtensionFileFilter filter = new ExtensionFileFilter(".model", "Model files (*.model)"); filechooser.addChoosableFileFilter(filter); filechooser.setFileFilter(filter); filechooser.setAcceptAllFileFilterUsed(true); setSessionValue(owner, SESSION_KEY_MODELCHOOSER, filechooser); } else { filechooser = (MekaFileChooser) getSessionValue(owner, SESSION_KEY_MODELCHOOSER); } int retVal = filechooser.showOpenDialog(owner.getOwner()); if (retVal != MekaFileChooser.APPROVE_OPTION) return; File model = filechooser.getSelectedFile(); try { Object[] objs = SerializationHelper.readAll(model.getAbsolutePath()); MultiLabelClassifier classifier = (MultiLabelClassifier) objs[0]; Instances data = null; if (objs.length > 1) data = (Instances) objs[1]; Result result = new Result(); owner.addResultToHistory(result, new Object[] { classifier, new Instances(data, 0) }, classifier.getClass().getName().replace("meka.classifiers.", "")); } catch (Exception ex) { owner.handleException("Loading of model '" + model + "' failed:", ex); JOptionPane.showMessageDialog(owner, "Loading of model '" + model + "' failed:\n" + ex, "Error", JOptionPane.ERROR_MESSAGE); } } }; }
From source file:meka.gui.modelviewer.ModelViewer.java
License:Open Source License
/** * Opens the specified model file.//from w ww .j a v a 2 s .com * * @param file the model file to load */ public void open(File file) { Object[] data; java.util.List<AbstractObjectRenderer> renderers; JPanel panel; m_TabbedPane.removeAll(); try { data = SerializationHelper.readAll(file.getAbsolutePath()); for (Object obj : data) { if (obj == null) continue; JTextArea text = new JTextArea(20, 40); text.setFont(GUIHelper.getMonospacedFont()); renderers = AbstractObjectRenderer.getRenderer(obj); if (renderers.size() == 0) continue; panel = new JPanel(new BorderLayout()); renderers.get(0).render(obj, panel); m_TabbedPane.addTab(obj.getClass().getName(), panel); } m_RecentFilesHandler.addRecentItem(file); m_LabelFile.setText(file.getAbsolutePath()); } catch (Exception e) { m_TabbedPane.removeAll(); m_LabelFile.setText(NO_FILE_LOADED); System.err.println("Failed to load data from '" + file + "':"); e.printStackTrace(); JOptionPane.showMessageDialog(this, "Failed to load dataset from '" + file + "':\n" + e, "Error loading", JOptionPane.ERROR_MESSAGE); } }