package anotacja; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.StringTokenizer; import anotacja.WebCBIR.Ordered; public class GradientEntropyTest { private static List GetAnnotationsFromFile(File file) throws Exception { List list = new LinkedList(); BufferedReader readFile = new BufferedReader(new FileReader(file)); StringTokenizer tokens = new StringTokenizer(readFile.readLine()); while (tokens.hasMoreTokens()) list.add(Integer.parseInt(tokens.nextToken())); readFile.close(); return list; } private static List GetAnnotationsForFile(File file) throws Exception { return GetAnnotationsFromFile(GetAnnotationFile(file)); } private static File GetAnnotationFile(File imageFile) { String temp = imageFile.getName(); int resIndex = temp.lastIndexOf(".png"); temp = temp.substring(0, resIndex + 4); return new File(AnnotationPath, temp + ".annot"); } private static String AnnotationPath; public static void main(String[] args) { try { String CBIRPath = args[0]; AnnotationPath = args[1]; File Queries = new File(args[2]); int Annotations = Integer.parseInt(args[3]); int SimLimit = Integer.parseInt(args[4]); int Clusters = Integer.parseInt(args[5]); String Extension = args[6]; double wT = Double.parseDouble(args[7]); double wF = Double.parseDouble(args[8]); String FullExtension = Extension + ".cluster_" + Clusters; WebCBIR CBIR = new WebCBIR(args[0], "", 500); System.out.print("[?] Building data structures... "); List TrainingFiles = new ArrayList(); for (File f : new File(CBIRPath, "files-out").listFiles()) if (f.getName().endsWith(FullExtension)) TrainingFiles.add(f); List TestFiles = new ArrayList(); for (File f : Queries.listFiles()) if (f.getName().endsWith(FullExtension)) TestFiles.add(f); double[][][] TrainingData = new double[Annotations][TrainingFiles.size()][SimLimit]; boolean[][] TrainingClass = new boolean[Annotations][TrainingFiles.size()]; for (int a = 0; a < Annotations; a++) for (int b = 0; b < TrainingFiles.size(); b++) { TrainingClass[a][b] = false; for (int c = 0; c < SimLimit; c++) TrainingData[a][b][c] = wF; } double[][][] TestData = new double[Annotations][TestFiles.size()][SimLimit]; boolean[][] TestClass = new boolean[Annotations][TestFiles.size()]; for (int a = 0; a < Annotations; a++) for (int b = 0; b < TestFiles.size(); b++) { TestClass[a][b] = false; for (int c = 0; c < SimLimit; c++) TestData[a][b][c] = wF; } System.out.println("[DONE]"); System.out.println("[?] Filling data structures..."); System.out.println("\t[-] Filling training set..."); int N = 0; for (File f : TrainingFiles) { System.out.println("\t[-] Now reading: " + f.getName()); List AnnotationList; List> Similarity = CBIR.compareAllToQuery(f.getAbsolutePath(), Clusters); AnnotationList = GetAnnotationsForFile(f); for (Integer i : AnnotationList) TrainingClass[i][N] = true; int M = 0; for (Ordered o : Similarity) { if (M >= SimLimit) break; if (f.getName().equalsIgnoreCase(o.get())) continue; AnnotationList = GetAnnotationsForFile(new File(o.get())); for (Integer i : AnnotationList) TrainingData[i][N][M] = wT; M++; } N++; } N = 0; System.out.println("\t[-] Filling test set..."); for (File f : TestFiles) { System.out.println("\t[-] Now reading: " + f.getName()); List AnnotationList; List> Similarity = CBIR.compareAllToQuery(f.getAbsolutePath(), Clusters); AnnotationList = GetAnnotationsForFile(f); for (Integer i : AnnotationList) TestClass[i][N] = true; int M = 0; for (Ordered o : Similarity) { if (M >= SimLimit) break; if (f.getName().equalsIgnoreCase(o.get())) continue; AnnotationList = GetAnnotationsForFile(new File(o.get())); for (Integer i : AnnotationList) TestData[i][N][M] = wT; M++; } N++; } double Precision = 0.0; int PrecisionN = 0; double Recall = 0.0; int RecallN = 0; System.out.println("[?] Collecting results..."); for (int i = 0; i < Annotations; i++) { System.out.println("\t[?] Annotation index: " + i); int TP = 0; int FN = 0; int FP = 0; EntropiaGrad Test = new EntropiaGrad(0); //Test.losoweCykle = 0; Test.ucz(TrainingData[i], TrainingClass[i]); for (int t = 0; t < TestFiles.size(); t++) { if (Test.testuj(TestData[i][t])) { if (TestClass[i][t]) TP++; else FP++; } else if (TestClass[i][t]) FN++; } if (TP + FP > 0) { double PartialPrecision = (double)TP / (double)(TP + FP); System.out.println("\t\t[?] Precision: " + PartialPrecision); Precision += PartialPrecision; PrecisionN++; } else System.out.println("\t\t[!] Could not calculate Precision!"); if (TP + FN > 0) { double PartialRecall = (double)TP / (double)(TP + FN); System.out.println("\t\t[?] Recall: " + PartialRecall); Recall += PartialRecall; RecallN++; } else System.out.println("\t\t[!] Could not calculate Recall!"); } Precision /= PrecisionN; Recall /= RecallN; double F1 = (2 * Precision * Recall) / (Precision + Recall); System.out.println("[!] Results:"); System.out.println("\t[-] Precision: " + Precision); System.out.println("\t[-] Recall: " + Recall); System.out.println("\t[-] F-score: " + F1); } catch (Exception Ex) { System.out.println(Ex.getMessage()); Ex.printStackTrace(); } } }