Ejemplo n.º 1
0
  public static void main(String[] args) throws IOException {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    System.out.println("Hello OpenCV " + Core.VERSION);

    getTrainData();

    List<Mat> traindataList = new ArrayList<Mat>();
    List<Integer> trainLabelList = new ArrayList<Integer>();
    readTrainData(outDataPath + "Arbys100Datas.csv", traindataList, trainLabelList);

    Classifier classifier = new Classifier();
    classifier.train(traindataList, trainLabelList);
    classifier.test(traindataList, trainLabelList);
    classifier.save(outDataPath + "svm.xml");
  }
  public static void trainAndValidate(
      Map<String, List<FeatureVector>> songs, Map<String, List<FeatureVector>> testSongs)
      throws Exception {
    List<String> genres =
        Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
            .stream()
            .map((n) -> Classify.intToGenre(n))
            .collect(Collectors.toList());
    System.out.println("Binary logistics classifier on every genre:");
    System.out.println("\t\tPositiv\tFalsePos\tFalseNeg\tAccuracy");
    for (String genre : genres) {
      Classifier c = new LogisticsRegressionClassifier(genre);
      c.train(songs);
      int positive = 0;
      int total = 0;
      int falsePositive = 0;
      int falseNegative = 0;

      for (String label : songs.keySet()) {
        for (FeatureVector features : songs.get(label)) {
          String result = c.classify(features);
          if (result != null) {
            if (genre.equals(label)) {
              positive++;
            } else {
              falsePositive++;
            }
          } else {
            if (label.equals(genre)) {
              falseNegative++;
            }
          }
          total++;
        }
      }
      System.out.println(
          Classify.shortenGenre(genre)
              + "\t"
              + positive
              + "\t\t"
              + falsePositive
              + "\t\t\t"
              + falseNegative
              + "\t\t\t"
              + (positive * 1.0) / (positive + falseNegative + falsePositive));
    }
  }