/* * Scans and reads the chain file and loads model properties from file content */ public void read(File file) throws FileNotFoundException, Exception { Scanner scanner = null; try { scanner = new Scanner(file); } catch (FileNotFoundException e) { System.out.println("Target file not found."); e.printStackTrace(); throw e; } while (scanner.hasNextLine()) { String line = scanner.nextLine(); if (line.startsWith("#")) { // Read channel information if (line.startsWith("# Channel")) { channel = line.replaceFirst("# Channel:", "").trim(); } else if (line.startsWith("# Image Size:")) { imageSize = line.replaceFirst("# Image Size:", "").trim(); } else if (line.startsWith("# Label:")) { label = line.replaceFirst("# Label:", "").trim(); } else if (line.startsWith("# Classes:")) { line = line.replaceFirst("# Classes:", ""); Scanner lineScanner = new Scanner(line); classNames = new HashMap<String, String>(); while (lineScanner.hasNext()) { String pair[] = lineScanner.next().split(":"); classNames.put(pair[0], pair[1]); } lineScanner.close(); } // Ignore other comments continue; } if (line.equals("[FEATURE_EXTRACTOR]")) { // First line after this tag should be name of the extractor line = scanner.nextLine(); Extractor ex = null; // Read extractor name if (line.startsWith("Name=")) { ex = new Extractor(line.replaceFirst("Name=", "")); } else { scanner.close(); throw new Exception("Invalid model file."); } // End extractor name // Read class name line = scanner.nextLine(); if (line.startsWith("ClassName=")) { ex.setClassName(line.replaceFirst("ClassName=", "")); } else { scanner.close(); throw new Exception("Invalid model file."); } // Read path line = scanner.nextLine(); if (line.startsWith("Path=")) { String path = line.replaceFirst("Path=", ""); if (!path.equals("null")) ex.setExternalPath(path); } else { scanner.close(); throw new Exception("Invalid model file."); } // Read extractor parameters line = scanner.nextLine(); if (line.equals("[PARAMETER_START]")) { while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.equals("[PARAMETER_END]")) break; String params[] = line.split("="); if (params.length == 2) ex.addParams(params[0], params[1]); else { scanner.close(); throw new Exception("Invalid extractor parameter."); } } } // End extractor parameters extractors.add(ex); } if (line.equals("[FEATURE_SELECTOR]")) { // First line after this tag should be name of the selector line = scanner.nextLine(); Selector sel = null; // Read selector name if (line.startsWith("Name=")) { sel = new Selector(line.replaceFirst("Name=", "")); } else { scanner.close(); throw new Exception("Invalid model file."); } // Read selected indices line = scanner.nextLine(); if (line.equals("[SELECTED_INDICES_START]")) { ArrayList<Integer> indices = new ArrayList<Integer>(); while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.equals("[SELECTED_INDICES_END]")) break; indices.add(Integer.valueOf(line)); } // Convert to int array int[] selectedIndices = new int[indices.size()]; for (int i = 0; i < indices.size(); i++) selectedIndices[i] = indices.get(i); sel.setSelectedIndices(selectedIndices); } selectors.add(sel); } // End Feature Selector while (line.equals("[CLASSIFIER]")) { // First line after this tag should be name of the Classifier line = scanner.nextLine(); System.out.println(line); ClassifierInfo cal = null; // Read Classfier name if (line.startsWith("Name=")) { cal = new ClassifierInfo(line.replaceFirst("Name=", "")); line = scanner.nextLine(); cal.setClassName(line.replaceFirst("ClassName=", "")); line = scanner.nextLine(); cal.setExternalPath(line.replaceFirst("Path=", "")); line = scanner.nextLine(); if (line.equals("[PARAMETER_START]")) { while (scanner.hasNextLine()) { line = scanner.nextLine(); if (line.equals("[PARAMETER_END]")) break; String params[] = line.split("="); if (params.length == 2) cal.addParams(params[0], params[1]); else { scanner.close(); throw new Exception("Invalid classifier parameter."); } } // line = scanner.nextLine(); // System.out.println(line); classifiersInfo.add(cal); // Classifier classifier = null; // String path = line.replaceFirst("Path=", "");; // path = file.getParent() + File.separatorChar + path; // classifier = (new Annotator()).getClassifierGivenName(cal.getClassName(), // cal.getExternalPath(), cal.getParams()); // if(classifier instanceof SavableClassifier) // ((SavableClassifier)classifier).setModel(((SavableClassifier)classifier).loadModel(path)); if (scanner.hasNext() && !line.equals("[CLASSIFIER]")) line = scanner.nextLine(); System.out.println("Chain Model Classifier: " + line); } else { scanner.close(); throw new Exception("Invalid model file."); } } } // End Classifier if (classifiersInfo.size() == 1) { String path = line.replaceFirst("Path=", ""); path = file.getParent() + File.separatorChar + path; classifier = (SavableClassifier) (new Annotator()) .getClassifierGivenName( classifiersInfo.get(0).getClassName(), classifiersInfo.get(0).getExternalPath(), classifiersInfo.get(0).getParams()); if (classifier instanceof SavableClassifier) ((SavableClassifier) classifier) .setModel(((SavableClassifier) classifier).loadModel(path)); } else if (classifiersInfo.size() > 1) { String path = line.replaceFirst("Path=", ""); ; path = file.getParent() + File.separatorChar + path; classifier = new CommitteeEnsemble(classifiersInfo); ((SavableClassifier) classifier).setModel(((SavableClassifier) classifier).loadModel(path)); } /* if(line.equals("[Ensemble]")) { line = scanner.nextLine(); //Read classifier name if(line.startsWith("Name=")) { ensembleName = line.replaceFirst("Name=", ""); } else throw new Exception("Invalid model file."); //Read class name line = scanner.nextLine(); if(line.startsWith("EnsName=")) { ensembleClass = line.replaceFirst("ClassName=", ""); } else throw new Exception("Invalid model file."); //Read path line = scanner.nextLine(); if(line.startsWith("Path=")) { String path = line.replaceFirst("Path=", ""); if(!path.equals("null")) ensemblePath = path; } else throw new Exception("Invalid model file."); //Read classifier parameters line = scanner.nextLine(); if(line.equals("[PARAMETER_START]")) { ensParams = new HashMap<String, String>(); while(scanner.hasNextLine()) { line = scanner.nextLine(); if(line.equals("[PARAMETER_END]")) break; String params[] = line.split("="); if(params.length == 2) ensParams.put(params[0], params[1]); else throw new Exception("Invalid Ensemble parameter."); } } }//End Ensemble parameters */ /* Removed 1/16/2014 if(line.equals("[CLASSIFIER]")) { line = scanner.nextLine(); //Read classifier name if(line.startsWith("Name=")) { classifierName = line.replaceFirst("Name=", ""); } else throw new Exception("Invalid model file."); //Read class name line = scanner.nextLine(); if(line.startsWith("ClassName=")) { classifierClass = line.replaceFirst("ClassName=", ""); } else throw new Exception("Invalid model file."); //Read path line = scanner.nextLine(); if(line.startsWith("Path=")) { String path = line.replaceFirst("Path=", ""); if(!path.equals("null")) classifierPath = path; } else throw new Exception("Invalid model file."); //Read classifier parameters line = scanner.nextLine(); if(line.equals("[PARAMETER_START]")) { classParams = new HashMap<String, String>(); while(scanner.hasNextLine()) { line = scanner.nextLine(); if(line.equals("[PARAMETER_END]")) break; String params[] = line.split("="); if(params.length == 2) classParams.put(params[0], params[1]); else throw new Exception("Invalid classifier parameter."); } }//End classifier parameters line = scanner.nextLine(); //Read classifier model path if(line.startsWith("Path=")) { String path = line.replaceFirst("Path=", ""); path = file.getParent() + File.separatorChar + path; classifier = (new Annotator()).getClassifierGivenName(classifierClass, classifierPath, classParams); if(classifier instanceof SavableClassifier) ((SavableClassifier)classifier).setModel(((SavableClassifier)classifier).loadModel(path)); } } */ } scanner.close(); }