private static void SortMaster3000(string filename, string weightfile) { int i = 0; Network net = Parser.parse_network_cfg(filename); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); string[] paths = Data.Data.GetPaths("Data.Data/compare.sort.list"); int n = paths.Length; SortableBbox[] boxes = new SortableBbox[n]; Console.Write($"Sorting %d boxes...\n", n); for (i = 0; i < n; ++i) { boxes[i].Filename = paths[i]; boxes[i].Net = net; boxes[i].Sclass = 7; boxes[i].Elo = 1500; } var sw = new Stopwatch(); sw.Start(); Array.Sort(boxes, bbox_comparator); for (i = 0; i < n; ++i) { Console.Write($"%s\n", boxes[i].Filename); } sw.Stop(); Console.Write($"Sorted ini %d compares, %f secs\n", TotalCompares, sw.Elapsed.Seconds); }
private static void bbox_update(SortableBbox a, SortableBbox b, int sclass, bool result) { int k = 32; float ea = 1.0f / (1 + (float)Math.Pow(10, (b.Elos[sclass] - a.Elos[sclass]) / 400f)); float eb = 1.0f / (1 + (float)Math.Pow(10, (a.Elos[sclass] - b.Elos[sclass]) / 400f)); float sa = result ? 1 : 0; float sb = result ? 0 : 1; a.Elos[sclass] += k * (sa - ea); b.Elos[sclass] += k * (sb - eb); }
private static int elo_comparator(SortableBbox a, SortableBbox b) { if (a.Elos[CurrentClass] == b.Elos[CurrentClass]) { return(0); } if (a.Elos[CurrentClass] > b.Elos[CurrentClass]) { return(-1); } return(1); }
private static int bbox_comparator(SortableBbox a, SortableBbox b) { ++TotalCompares; Network net = a.Net; int sclass = a.Sclass; Image im1 = LoadArgs.load_image_color(a.Filename, net.W, net.H); Image im2 = LoadArgs.load_image_color(b.Filename, net.W, net.H); float[] x = new float[net.W * net.H * net.C]; Array.Copy(im1.Data, 0, x, 0, im1.Data.Length); Array.Copy(im2.Data, 0, x, im1.Data.Length, im2.Data.Length); float[] predictions = Network.network_predict(net, x); if (predictions[sclass * 2] > predictions[sclass * 2 + 1]) { return(1); } return(-1); }
private static void bbox_fight(Network net, SortableBbox a, SortableBbox b, int classes, int sclass) { Image im1 = LoadArgs.load_image_color(a.Filename, net.W, net.H); Image im2 = LoadArgs.load_image_color(b.Filename, net.W, net.H); float[] x = new float[net.W * net.H * net.C]; Array.Copy(im1.Data, 0, x, 0, im1.Data.Length); Array.Copy(im2.Data, 0, x, im1.Data.Length, im2.Data.Length); float[] predictions = Network.network_predict(net, x); ++TotalCompares; int i; for (i = 0; i < classes; ++i) { if (sclass < 0 || sclass == i) { bool result = predictions[i * 2] > predictions[i * 2 + 1]; bbox_update(a, b, i, result); } } }
private static void BattleRoyaleWithCheese(string filename, string weightfile) { int classes = 20; int i, j; Network net = Parser.parse_network_cfg(filename); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); string[] paths = Data.Data.GetPaths("Data.Data/compare.sort.list"); int n = paths.Length; int total = n; SortableBbox[] boxes = new SortableBbox[n]; Console.Write($"Battling %d boxes...\n", n); for (i = 0; i < n; ++i) { boxes[i].Filename = paths[i]; boxes[i].Net = net; boxes[i].Classes = classes; boxes[i].Elos = new float[classes]; for (j = 0; j < classes; ++j) { boxes[i].Elos[j] = 1500; } } int round; var swTotal = new Stopwatch(); swTotal.Start(); for (round = 1; round <= 4; ++round) { var sw = new Stopwatch(); sw.Start(); Console.Write($"Round: %d\n", round); Utils.Shuffle(boxes); for (i = 0; i < n / 2; ++i) { bbox_fight(net, boxes[i * 2], boxes[i * 2 + 1], classes, -1); } sw.Stop(); Console.Write($"Round: %f secs, %d remaining\n", sw.Elapsed.Seconds, n); } for (var iClass = 0; iClass < classes; ++iClass) { n = total; CurrentClass = iClass; Array.Sort(boxes, elo_comparator); n /= 2; for (round = 1; round <= 100; ++round) { var sw = new Stopwatch(); sw.Start(); Console.Write($"Round: %d\n", round); Utils.sorta_shuffle(boxes, 10); for (i = 0; i < n / 2; ++i) { bbox_fight(net, boxes[i * 2], boxes[i * 2 + 1], classes, iClass); } Array.Sort(boxes, elo_comparator); if (round <= 20) { n = (n * 9 / 10) / 2 * 2; } sw.Stop(); Console.Write($"Round: %f secs, %d remaining\n", sw.Elapsed.Seconds, n); } string buff = $"results/battle_{iClass}.(float)Math.Log"; var lines = new List <string>(); for (i = 0; i < n; ++i) { lines.Add($"{boxes[i].Filename} {boxes[i].Elos[iClass]}"); } File.WriteAllLines(buff, lines); } swTotal.Stop(); Console.Write($"Tournament ini %d compares, %f secs\n", TotalCompares, swTotal.Elapsed.Seconds); }