Пример #1
0
        /**
         * Find the theme closest on average to a given set of themes (compare[key])
         */
        private PaletteData GetOracleTheme(String key, Dictionary<String, List<PaletteData>> compare)
        {
            //find the palette that has the lowest distance to all other palettes
            int trials = 20;

            PaletteExtractor extractor = new PaletteExtractor(evalDir, weightsDir, json);
            PaletteData swatches = extractor.GetPaletteSwatches(key);

            List<CIELAB> shuffled = new List<CIELAB>();
            foreach (CIELAB c in swatches.lab)
                shuffled.Add(c);

            Random random = new Random();

            PaletteData best = new PaletteData();
            double bestScore = Double.NegativeInfinity;

            Stopwatch watch = new Stopwatch();

            //Generate all the random starts first
            List<PaletteData> starts = new List<PaletteData>();
            PaletteData[] allOptions = new PaletteData[trials];
            double[] allScores = new double[trials];

            for (int t = 0; t < trials; t++)
            {
                //setup
                PaletteData option = new PaletteData();

                //pick k random colors. First shuffle the colors and pick the top k
                for (int j = shuffled.Count() - 1; j >= 0; j--)
                {
                    int idx = random.Next(j + 1);
                    CIELAB temp = shuffled[j];
                    shuffled[j] = shuffled[idx];
                    shuffled[idx] = temp;
                }

                for (int i = 0; i < 5; i++)
                {
                    option.lab.Add(shuffled[i]);
                    option.colors.Add(Util.LABtoRGB(shuffled[i]));
                }
                starts.Add(option);
                allOptions[t] = new PaletteData(option);
                double optionScore = 1 - GetAvgDist(new List<PaletteData> { option }, compare[key]);
                allScores[t] = optionScore;
            }

            watch.Restart();

            Parallel.For(0, trials, t =>
            {
                //setup
                PaletteData option = new PaletteData(starts[t]);

                double optionScore = allScores[t];

                //Now hill climb, for each swatch, consider replacing it with a better swatch
                //Pick the best replacement, and continue until we reach the top of a hill
                int changes = 1;
                int iters = 0;

                watch.Restart();
                while (changes > 0)
                {
                    changes = 0;

                    for (int i = 0; i < option.lab.Count(); i++)
                    {
                        //find the best swatch replacement for this color
                        double bestTempScore = optionScore;
                        CIELAB bestRep = option.lab[i];

                        double[] scores = new double[swatches.lab.Count()];

                        for (int s = 0; s < swatches.lab.Count(); s++)
                        {
                            CIELAB r = swatches.lab[s];

                            PaletteData temp = new PaletteData(option);
                            if (!temp.lab.Contains(r))
                            {

                                temp.lab[i] = r;
                                temp.colors[i] = swatches.colors[s];

                                double tempScore = 1 - GetAvgDist(new List<PaletteData> { temp }, compare[key]);
                                scores[s] = tempScore;
                            }
                            else
                            {
                                scores[s] = Double.NegativeInfinity;
                            }
                        }

                        //aggregate results
                        for (int s = 0; s < scores.Count(); s++)
                        {
                            if (scores[s] > bestTempScore)
                            {
                                bestTempScore = scores[s];
                                bestRep = swatches.lab[s];
                            }
                        }

                        if (!option.lab[i].Equals(bestRep))
                        {
                            option.lab[i] = bestRep;
                            optionScore = bestTempScore;
                            changes++;
                        }
                    }

                    iters++;
                }

                if (optionScore > allScores[t])
                {
                    allOptions[t] = option;
                    allScores[t] = optionScore;
                }
            });

            //aggregate scores
            for (int i = 0; i < allScores.Count(); i++)
            {
                if (allScores[i] > bestScore)
                {
                    bestScore = allScores[i];
                    best = allOptions[i];
                }
            }

            //convert best lab to rgb
            best.colors = new List<Color>();
            foreach (CIELAB l in best.lab)
                best.colors.Add(Util.LABtoRGB(l));

            return best;
        }
Пример #2
0
        /**
         * Extract k-means, c-means, model, and oracle themes for comparison
         */
        private void extractThemesToCompare_Click(object sender, EventArgs e)
        {
            if (backgroundWorker.IsBusy)
            {
                statusBox.Text = "Please wait...";
                return;
            }
            backgroundWorker.WorkerReportsProgress = true;

            backgroundWorker = MakeBackgroundWorker(delegate
            {
                backgroundWorker.ReportProgress(0, "Extracting themes to compare...");

                PaletteExtractor extractor = new PaletteExtractor(evalDir, weightsDir, json);
                Dictionary<String, Dictionary<String, List<PaletteData>>> referenceP = ItemsToDictionary(reference);

                String[] files = GetImageFiles(evalDir);
                String headers = "pid\tid\timage\tcolors\tnumColors\tlog\n";

                backgroundWorker.ReportProgress(0, "Extracting random themes...");
                //extract random
                String randomFile = Path.Combine(evalDir, "random.tsv");
                if (!File.Exists(randomFile) || File.ReadLines(randomFile).Count() != (40 * files.Count()) + 1)
                {
                    int count = 0;
                    File.WriteAllText(randomFile, "");
                    File.AppendAllText(randomFile, headers);

                    foreach (String f in files)
                    {
                        String basename = new FileInfo(f).Name;
                        PaletteData swatches = extractor.GetPaletteSwatches(basename);

                        List<PaletteData> random = GenerateRandomPalettesNonBinned(40, swatches);

                        foreach (PaletteData data in random)
                        {
                            count++;
                            String colorString = data.ToString();
                            File.AppendAllText(randomFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n");
                        }

                    }
                }
                else
                {
                    Console.WriteLine("Random palettes file already exists");
                    backgroundWorker.ReportProgress(100, "Random themes file already exist...skipping");
                    Thread.Sleep(1000);
                }

                backgroundWorker.ReportProgress(0, "Extracting K-means themes...");

                //extract kmeans
                String kmeansFile = Path.Combine(evalDir, "kmeans.tsv");
                if (!File.Exists(kmeansFile) || File.ReadLines(kmeansFile).Count() != files.Count() + 1)
                {
                    File.WriteAllText(kmeansFile, "");
                    File.AppendAllText(kmeansFile, headers);
                    int count = 0;
                    foreach (String f in files)
                    {
                        count++;
                        String basename = new FileInfo(f).Name;

                        PaletteData data = KMeansPalette(f);

                        //save to file
                        String colorString = data.ToString();
                        File.AppendAllText(kmeansFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n");
                        backgroundWorker.ReportProgress(100 * count / files.Count(), "");
                    }
                }
                else
                {
                    Console.WriteLine("Kmeans palettes file already exists");
                    backgroundWorker.ReportProgress(100, "K-means themes already exist...skipping");
                    Thread.Sleep(1000);
                }

                //extract cmeans
                backgroundWorker.ReportProgress(0, "Extracting C-means themes...");
                String cmeansFile = Path.Combine(evalDir, "cmeans.tsv");
                if (!File.Exists(cmeansFile) || File.ReadLines(cmeansFile).Count() != files.Count() + 1)
                {
                    File.WriteAllText(cmeansFile, "");
                    File.AppendAllText(cmeansFile, headers);
                    int count = 0;
                    foreach (String f in files)
                    {
                        count++;
                        String basename = new FileInfo(f).Name;

                        PaletteData data = CMeansPalette(f);

                        //save to file
                        String colorString = data.ToString();
                        File.AppendAllText(cmeansFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n");
                        backgroundWorker.ReportProgress(100 * count / files.Count(), "");
                    }
                }
                else
                {
                    Console.WriteLine("Cmeans palettes file already exists");
                    backgroundWorker.ReportProgress(100, "C-means themes already exist...skipping");
                    Thread.Sleep(1000);
                }

                //extract model
                backgroundWorker.ReportProgress(0, "Extracting Optimized themes...");
                String modelFile = Path.Combine(evalDir, "optimized.tsv");
                if (!File.Exists(modelFile) || File.ReadLines(modelFile).Count() != files.Count() + 1)
                {
                    File.WriteAllText(modelFile, "");
                    File.AppendAllText(modelFile, headers);
                    int count = 0;
                    foreach (String f in files)
                    {
                        count++;
                        String basename = new FileInfo(f).Name;

                        //The saliency pattern "_Judd" is just an additional annotation after the image filename if it exists
                        //i.e. if the image filename is A.png, the saliency map filename is A_Judd.png
                        PaletteData data = extractor.HillClimbPalette(basename, "_Judd", debug);

                        //save to file
                        String colorString = data.ToString();
                        File.AppendAllText(modelFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n");
                        backgroundWorker.ReportProgress(100 * count / files.Count(), "");
                    }
                }
                else
                {
                    Console.WriteLine("Optimized palettes file already exists");
                    backgroundWorker.ReportProgress(100, "Optimized themes already exist...skipping");
                    Thread.Sleep(1000);
                }

                //extract oracle
                foreach (String name in referenceP.Keys)
                {
                    backgroundWorker.ReportProgress(0, "Extracting Oracle themes for..." + name);
                    String oracleFile = Path.Combine(evalDir, "oracle-" + name + ".tsv");
                    if (!File.Exists(oracleFile) || File.ReadLines(oracleFile).Count() != referenceP[name].Keys.Count() + 1)
                    {
                        File.WriteAllText(oracleFile, "");
                        File.AppendAllText(oracleFile, headers);
                        int count = 0;
                        foreach (String f in files)
                        {

                            String basename = new FileInfo(f).Name;
                            if (!referenceP[name].ContainsKey(basename))
                                continue;

                            count++;
                            PaletteData data = GetOracleTheme(basename, referenceP[name]);

                            //save to file
                            String colorString = data.ToString();
                            File.AppendAllText(oracleFile, count + "\t-1\t" + basename + "\t" + colorString + "\t5\t\n");
                            backgroundWorker.ReportProgress(100 * count / files.Count(), "");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Oracle palettes file already exists");
                        backgroundWorker.ReportProgress(100, "Oracle themes already exist...skipping");
                        Thread.Sleep(1000);
                    }
                }

            });

            RunWorker();
        }
Пример #3
0
        /**
         * Calculate average distance between themes from different sources, and average overlap over different thresholds
         */
        private void compareThemes_Click(object sender, EventArgs e)
        {
            if (backgroundWorker.IsBusy)
            {
                statusBox.Text = "Please wait...";
                return;
            }
            backgroundWorker.WorkerReportsProgress = true;

            backgroundWorker =  MakeBackgroundWorker(delegate
            {
                    backgroundWorker.ReportProgress(0, "Comparing themes...");
                    //load the reference palettes
                    //load the palettes to compare against
                    Dictionary<String, Dictionary<String, List<PaletteData>>> referenceP = ItemsToDictionary(reference);
                    Dictionary<String, Dictionary<String, List<PaletteData>>> compareP = ItemsToDictionary(compare);
                    Dictionary<String, List<PaletteData>> testKeys = LoadFilePalettes(refkeys);

                    //compare each source in compare to each reference
                    List<String> desc = new List<String>();
                    List<double> dists = new List<double>();

                    foreach (var r in reference)
                    {
                        foreach (var c in compare)
                        {
                            desc.Add(c.Name + "-" + r.Name);
                            dists.Add(0);
                        }
                    }

                    int progress = 0;
                    foreach (String key in testKeys.Keys)
                    {
                        int idx = 0;
                        foreach (var r in reference)
                        {
                            foreach (var c in compare)
                            {
                                //count number of keys in common between reference, compare, and test set
                                if (referenceP[r.Name].ContainsKey(key) && compareP[c.Name].ContainsKey(key))
                                {
                                    double factor = referenceP[r.Name].Keys.Intersect(compareP[c.Name].Keys).Intersect(testKeys.Keys).Count();
                                    dists[idx] += GetAvgDist(referenceP[r.Name][key], compareP[c.Name][key], c.Path == r.Path) / factor;
                                }
                                idx++;
                            }
                        }

                        progress++;
                        backgroundWorker.ReportProgress(50 * progress/testKeys.Keys.Count(), "Comparing themes...Calculating distance...");
                    }

                    //Write out the average distances
                    List<String> lines = new List<String>();
                    lines.Add(String.Join(",", desc.ToArray<String>()));
                    lines.Add(String.Join(",", dists.Select(i => i.ToString())));
                    File.WriteAllLines(Path.Combine(evalDir, "avgDists.csv"), lines.ToArray<String>());

                    backgroundWorker.ReportProgress(50, "Comparing themes...Calculating overlap...");
                    //Output csv file for the overlap graph
                    //clamp each palette to the closest color swatch
                    //when computing overlap for the graph
                    PaletteExtractor extractor = new PaletteExtractor(evalDir, weightsDir, json);
                    foreach (String key in testKeys.Keys)
                    {
                        PaletteData swatches = extractor.GetPaletteSwatches(key);
                        foreach (var r in reference)
                        {
                            if (!referenceP[r.Name].ContainsKey(key))
                                continue;
                            var palettes = referenceP[r.Name][key];
                            for (int i = 0; i < palettes.Count(); i++)
                                palettes[i] = SnapColors(palettes[i], swatches);
                        }
                        foreach (var c in compare)
                        {
                            if (!compareP[c.Name].ContainsKey(key))
                                continue;
                            var palettes = compareP[c.Name][key];
                            for (int i = 0; i < palettes.Count(); i++)
                                palettes[i] = SnapColors(palettes[i], swatches);

                        }
                    }

                    lines.Clear();

                    //compute the overlaps for different thresholds
                    lines.Add("thresh,type,overlap");

                    for (int t = 1; t < 100; t += 2)
                    {
                        double[] overlaps = new double[reference.Count() * compare.Count()];
                        int idx = 0;
                        foreach (var r in reference)
                        {
                            foreach (var c in compare)
                            {
                                foreach (String key in testKeys.Keys)
                                    if (compareP[c.Name].ContainsKey(key) && referenceP[r.Name].ContainsKey(key))
                                        overlaps[idx] += GetAvgOverlap(compareP[c.Name][key], referenceP[r.Name][key], t, c.Path == r.Path);

                                double factor = referenceP[r.Name].Keys.Intersect(compareP[c.Name].Keys).Intersect(testKeys.Keys).Count();
                                lines.Add(t + "," + desc[idx] + "," + overlaps[idx] / factor);
                                idx++;
                            }
                        }
                        backgroundWorker.ReportProgress(t/2+50, String.Format("Comparing themes...Calculating overlap for thresh {0}/{1}...",t,100));

                    }
                    File.WriteAllLines(Path.Combine(evalDir, "overlaps.csv"), lines.ToArray<String>());
            });

            RunWorker();
        }
Пример #4
0
        /**
         * Create diagrams of the themes showing source image, swatches, and aligned themes
         */
        private void diagramThemes_Click(object sender, EventArgs e)
        {
            if (backgroundWorker.IsBusy)
            {
                statusBox.Text = "Please wait...";
                return;
            }

            backgroundWorker = MakeBackgroundWorker(delegate
            {
                backgroundWorker.ReportProgress(0, "Diagramming themes...");

                //create directory
                String diagramDir = Path.Combine(evalDir, "diagrams");
                Directory.CreateDirectory(diagramDir);

                Dictionary<String, Dictionary<String, List<PaletteData>>> leftP = ItemsToDictionary(diagramLeft);
                Dictionary<String, Dictionary<String, List<PaletteData>>> rightP = ItemsToDictionary(diagramRight);

                int imageWidth = 200;
                int colorSize = 20;
                int padding = 5;
                int gutter = 50;
                int labelTop = 100;

                PaletteExtractor extractor = new PaletteExtractor(evalDir, weightsDir, json);

                SortedSet<String> keys = new SortedSet<String>();
                foreach (String key in leftP.Keys)
                    keys.UnionWith(leftP[key].Keys);
                foreach (String key in rightP.Keys)
                    keys.UnionWith(rightP[key].Keys);

                int progress = 0;
                foreach (String key in keys)
                {

                    //check total number of themes to draw
                    int totalThemes = 0;
                    PaletteData swatches = extractor.GetPaletteSwatches(key);

                    //Count total number of themes, and sort the themes by average distance
                    foreach (String n in leftP.Keys)
                    {
                        if (!leftP[n].ContainsKey(key))
                            continue;
                        totalThemes += leftP[n][key].Count();
                        leftP[n][key] = SortByDist(leftP[n][key]);
                    }

                    foreach (String n in rightP.Keys)
                    {
                        if (!rightP[n].ContainsKey(key))
                            continue;
                        totalThemes += rightP[n][key].Count();
                        rightP[n][key] = SortByDist(rightP[n][key]);
                    }

                    int width = imageWidth + (leftP.Keys.Count() + rightP.Keys.Count() + 2) * gutter + colorSize * totalThemes;
                    int height = labelTop + swatches.colors.Count()*colorSize;

                    Bitmap result = new Bitmap(width, height);
                    Graphics g = Graphics.FromImage(result);
                    Bitmap image = new Bitmap(Path.Combine(evalDir, key));

                    //draw the source image
                    g.DrawImage(image, 0, labelTop, imageWidth, imageWidth*image.Height / image.Width);

                    int startX = imageWidth+gutter;

                    //draw the themes to the left of the swatches
                    foreach (EvalItem item in diagramLeft)
                    {
                        String name = item.Name;

                        if (!leftP[name].ContainsKey(key))
                            continue;

                        DrawThemes(g, name, leftP[name][key], swatches, startX, labelTop, colorSize, padding);
                        startX += gutter + leftP[name][key].Count()*colorSize;
                    }

                    //draw the swatches
                    for (int c = 0; c < swatches.colors.Count; c++)
                    {
                        int y = c * colorSize+labelTop;
                        g.FillRectangle(new SolidBrush(swatches.colors[c]), startX, y, colorSize - padding, colorSize - padding);
                        g.DrawRectangle(new Pen(Color.Black), startX, y, colorSize - padding, colorSize - padding);
                    }

                    startX += colorSize+gutter;
                    //draw the themes to the right of the swatches
                    foreach (EvalItem item in diagramRight)
                    {
                        String name = item.Name;
                        if (!rightP[name].ContainsKey(key))
                            continue;
                        DrawThemes(g, name, rightP[name][key], swatches, startX, labelTop, colorSize, padding);
                        startX += gutter+ rightP[name][key].Count()*colorSize;
                    }

                    result.Save(Path.Combine(diagramDir, key));
                    progress++;

                    backgroundWorker.ReportProgress(100*progress / keys.Count, "Diagrammed " + key);

                }

            });

            RunWorker();
        }
Пример #5
0
        /**
         * We're doing rejection sampling here, so this is pretty slow for getting palettes that are scored very highly or very lowly.
         * */
        private void SaveRandomPalettes(String filename, Dictionary<String, List<PaletteData>> palettes, PaletteExtractor extractor)
        {
            int numPalettes = 1000;
            if (File.Exists(filename) && File.ReadLines(filename).Count()==(palettes.Keys.Count()*numPalettes)+1)
            {
                Console.WriteLine("Binned random palettes already generated...skipping");
                backgroundWorker.ReportProgress(100, "Random palettes already generated from before...skipping");
                Thread.Sleep(1000);
                return;
            }

            int counter = -1;
            //for each key, generate random palettes and save them
            //pid, id, image, colors
            List<String> outLines = new List<String>();
            outLines.Add("pid\tid\timage\tcolors\tnumColors\tlog");
            int progress = 0;
            foreach (String key in palettes.Keys)
            {
                backgroundWorker.ReportProgress(-1, "Generating random palettes for " + key);
                List<PaletteData> rand = GenerateRandomPalettes(numPalettes, palettes[key], extractor.GetPaletteSwatches(key));
                for (int i = 0; i < rand.Count(); i++)
                {
                    List<String> colors = new List<String>();
                    foreach (Color c in rand[i].colors)
                    {
                        colors.Add(c.R + "," + c.G + "," + c.B);
                    }
                    outLines.Add(counter + "\t-1\t" + key + "\t" + String.Join(" ", colors.ToArray<String>()) + "\t5\t");
                    counter--;
                }
                progress++;
                backgroundWorker.ReportProgress(progress * 100 / palettes.Keys.Count(), "Generated Random Palettes for Image..."+progress+" of "+palettes.Keys.Count());

                Log(Path.Combine(trainOutDir, "randlog.txt"), key + "-" + DateTime.Now.ToString());
            }

            File.WriteAllLines(filename, outLines.ToArray<String>());
        }