private static void test_writing(string cfgfile, string weightfile, string filename) { Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); Utils.Rand = new Random(2222222); var sw = new Stopwatch(); string input = ""; while (true) { if (!string.IsNullOrEmpty(filename)) { input = filename; } else { Console.Write($"Enter Image Path: "); input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { return; } input = input.TrimEnd(); } Image im = LoadArgs.load_image_color(input, 0, 0); Network.resize_network(net, im.W, im.H); Console.Write($"%d %d %d\n", im.H, im.W, im.C); float[] x = im.Data; sw.Reset(); sw.Start(); Network.network_predict(net, x); sw.Stop(); Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds); Image pred = Network.get_network_image(net); Image upsampled = LoadArgs.resize_image(pred, im.W, im.H); Image thresh = LoadArgs.threshold_image(upsampled, .5f); pred = thresh; LoadArgs.show_image(pred, "prediction"); LoadArgs.show_image(im, "orig"); CvInvoke.WaitKey(); CvInvoke.DestroyAllWindows(); if (!string.IsNullOrEmpty(filename)) { break; } } }
private static void demo_art(string cfgfile, string weightfile, int camIndex) { Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); Utils.Rand = new Random(2222222); using (VideoCapture cap = new VideoCapture(camIndex)) { string window = "ArtJudgementBot9000!!!"; if (cap != null) { Utils.Error("Couldn't connect to webcam.\n"); } int i; int[] idx = { 37, 401, 434 }; int n = idx.Length; while (true) { Image ini = LoadArgs.get_image_from_stream(cap); Image inS = LoadArgs.resize_image(ini, net.W, net.H); LoadArgs.show_image(ini, window); float[] p = Network.network_predict(net, inS.Data); Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); float score = 0; for (i = 0; i < n; ++i) { float s = p[idx[i]]; if (s > score) { score = s; } } Console.Write($"I APPRECIATE THIS ARTWORK: %10.7f%%\n", score * 100); Console.Write($"["); int upper = 30; for (i = 0; i < upper; ++i) { Console.Write($"%c", ((i + .5) < score * upper) ? 219 : ' '); } Console.Write($"]\n"); CvInvoke.WaitKey(1); } } }
private static void test_coco(string cfgfile, string weightfile, string filename, float thresh) { Image[][] alphabet = LoadArgs.load_alphabet(); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Layer l = net.Layers[net.N - 1]; Network.set_batch_network(net, 1); Utils.Rand = new Random(2222222); float nms = .4f; var sw = new Stopwatch(); int j; Box[] boxes = new Box[l.Side * l.Side * l.N]; float[][] probs = new float[l.Side * l.Side * l.N][]; for (j = 0; j < l.Side * l.Side * l.N; ++j) probs[j] = new float[l.Classes]; while (true) { string input; if (!string.IsNullOrEmpty(filename)) { input = filename; } else { Console.Write($"Enter Image Path: "); input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) return; input = input.TrimEnd(); } Image im = LoadArgs.load_image_color(input, 0, 0); Image sized = LoadArgs.resize_image(im, net.W, net.H); float[] x = sized.Data; sw.Reset(); sw.Start(); Network.network_predict(net, x); sw.Stop(); Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds); l.get_detection_boxes( 1, 1, thresh, probs, boxes, false); if (nms != 0) Box.do_nms_sort(boxes, probs, l.Side * l.Side * l.N, l.Classes, nms); LoadArgs.draw_detections(im, l.Side * l.Side * l.N, thresh, boxes, probs, CocoClasses, alphabet, 80); LoadArgs.save_image(im, "prediction"); LoadArgs.show_image(im, "predictions"); CvInvoke.WaitKey(); CvInvoke.DestroyAllWindows(); if (!string.IsNullOrEmpty(filename)) break; } }
private static void gun_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename) { int[] badCats = { 218, 539, 540, 1213, 1501, 1742, 1911, 2415, 4348, 19223, 368, 369, 370, 1133, 1200, 1306, 2122, 2301, 2537, 2823, 3179, 3596, 3639, 4489, 5107, 5140, 5289, 6240, 6631, 6762, 7048, 7171, 7969, 7984, 7989, 8824, 8927, 9915, 10270, 10448, 13401, 15205, 18358, 18894, 18895, 19249, 19697 }; Console.Write($"Classifier Demo\n"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); var options = OptionList.read_data_cfg(datacfg); Utils.Rand = new Random(2222222); using (VideoCapture cap = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { int top = OptionList.option_find_int(options, "top", 1); string nameList = OptionList.option_find_str(options, "names", ""); string[] names = Data.Data.get_labels(nameList); int[] indexes = new int[top]; if (cap.IsOpened) { Utils.Error("Couldn't connect to webcam.\n"); } float fps = 0; int i; while (true) { var sw = new Stopwatch(); sw.Start(); Image ini = LoadArgs.get_image_from_stream(cap); Image inS = LoadArgs.resize_image(ini, net.W, net.H); LoadArgs.show_image(ini, "Threat Detection"); float[] predictions = Network.network_predict(net, inS.Data); Network.top_predictions(net, top, indexes); Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); bool threat = false; for (i = 0; i < badCats.Length; ++i) { int index = badCats[i]; if (predictions[index] > .01) { Console.Write($"Threat Detected!\n"); threat = true; break; } } if (threat) { Console.Write($"Scanning...\n"); } for (i = 0; i < badCats.Length; ++i) { int index = badCats[i]; if (predictions[index] > .01) { Console.Write($"%s\n", names[index]); } } CvInvoke.WaitKey(10); sw.Stop(); float curr = 1000.0f / sw.ElapsedMilliseconds; fps = .9f * fps + .1f * curr; } } }
private static void threat_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename) { float threat = 0; float roll = .2f; Console.Write($"Classifier Demo\n"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); var options = OptionList.read_data_cfg(datacfg); Utils.Rand = new Random(2222222); using (VideoCapture cap = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { int top = OptionList.option_find_int(options, "top", 1); string nameList = OptionList.option_find_str(options, "names", ""); string[] names = Data.Data.get_labels(nameList); int[] indexes = new int[top]; if (!cap.IsOpened) { Utils.Error("Couldn't connect to webcam.\n"); } float fps = 0; int i; int count = 0; while (true) { ++count; var sw = new Stopwatch(); sw.Start(); Image ini = LoadArgs.get_image_from_stream(cap); if (ini.Data.Length == 0) { break; } Image inS = LoadArgs.resize_image(ini, net.W, net.H); Image outo = ini; int x1 = outo.W / 20; int y1 = outo.H / 20; int x2 = 2 * x1; int y2 = outo.H - outo.H / 20; int border = (int)(.01f * outo.H); int h = y2 - y1 - 2 * border; int w = x2 - x1 - 2 * border; float[] predictions = Network.network_predict(net, inS.Data); float currThreat = 0; currThreat = predictions[0] * 0f + predictions[1] * .6f + predictions[2]; threat = roll * currThreat + (1 - roll) * threat; LoadArgs.draw_box_width(outo, x2 + border, (int)(y1 + .02 * h), (int)(x2 + .5 * w), (int)(y1 + .02 * h + border), border, 0, 0, 0); if (threat > .97) { LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .02 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .02 * h + 3 * border), 3 * border, 1, 0, 0); } LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .02 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .02 * h + 3 * border), (int)(.5 * border), 0, 0, 0); LoadArgs.draw_box_width(outo, x2 + border, (int)(y1 + .42 * h), (int)(x2 + .5 * w), (int)(y1 + .42 * h + border), border, 0, 0, 0); if (threat > .57) { LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .42 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .42 * h + 3 * border), 3 * border, 1, 1, 0); } LoadArgs.draw_box_width(outo, (int)(x2 + .5 * w + border), (int)(y1 + .42 * h - 2 * border), (int)(x2 + .5 * w + 6 * border), (int)(y1 + .42 * h + 3 * border), (int)(.5 * border), 0, 0, 0); LoadArgs.draw_box_width(outo, x1, y1, x2, y2, border, 0, 0, 0); for (i = 0; i < threat * h; ++i) { float ratio = (float)i / h; float r = (ratio < .5f) ? (2 * (ratio)) : 1; float g = (ratio < .5f) ? 1 : 1 - 2 * (ratio - .5f); LoadArgs.draw_box_width(outo, x1 + border, y2 - border - i, x2 - border, y2 - border - i, 1, r, g, 0); } Network.top_predictions(net, top, indexes); string buff = $"/home/pjreddie/tmp/threat_{count:06}"; Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); Console.Write($"\nFPS:%.0f\n", fps); for (i = 0; i < top; ++i) { int index = indexes[i]; Console.Write($"%.1f%%: %s\n", predictions[index] * 100, names[index]); } LoadArgs.show_image(outo, "Threat"); CvInvoke.WaitKey(10); sw.Stop(); float curr = 1000.0f / sw.ElapsedMilliseconds; fps = .9f * fps + .1f * curr; } } }
private static void demo_classifier(string datacfg, string cfgfile, string weightfile, int camIndex, string filename) { Console.Write($"Classifier Demo\n"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); var options = OptionList.read_data_cfg(datacfg); Utils.Rand = new Random(2222222); using (VideoCapture cap = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { int top = OptionList.option_find_int(options, "top", 1); string nameList = OptionList.option_find_str(options, "names", ""); string[] names = Data.Data.get_labels(nameList); int[] indexes = new int[top]; if (cap != null) { Utils.Error("Couldn't connect to webcam.\n"); } float fps = 0; int i; while (true) { var sw = new Stopwatch(); sw.Start(); Image ini = LoadArgs.get_image_from_stream(cap); Image inS = LoadArgs.resize_image(ini, net.W, net.H); LoadArgs.show_image(ini, "Classifier"); float[] predictions = Network.network_predict(net, inS.Data); if (net.Hierarchy != null) { net.Hierarchy.Hierarchy_predictions(predictions, 0, net.Outputs, true); } Network.top_predictions(net, top, indexes); Console.Write($"\033[2J"); Console.Write($"\033[1;1H"); Console.Write($"\nFPS:%.0f\n", fps); for (i = 0; i < top; ++i) { int index = indexes[i]; Console.Write($"%.1f%%: %s\n", predictions[index] * 100, names[index]); } CvInvoke.WaitKey(10); sw.Stop(); float curr = 1000.0f / sw.ElapsedMilliseconds; fps = .9f * fps + .1f * curr; } } }
public static void run_nightmare(List <string> args) { if (args.Count < 4) { Console.Error.Write($"usage: %s %s [cfg] [weights] [Image] [Layer] [options! (optional)]\n", args[0], args[1]); return; } string cfg = args[2]; string weights = args[3]; string input = args[4]; int maxLayer = int.Parse(args[5]); int range = Utils.find_int_arg(args, "-range", 1); bool norm = Utils.find_int_arg(args, "-norm", 1) != 0; int rounds = Utils.find_int_arg(args, "-rounds", 1); int iters = Utils.find_int_arg(args, "-iters", 10); int octaves = Utils.find_int_arg(args, "-octaves", 4); float zoom = Utils.find_int_arg(args, "-zoom", 1); float rate = Utils.find_int_arg(args, "-rate", .04f); float thresh = Utils.find_int_arg(args, "-thresh", 1); float rotate = Utils.find_int_arg(args, "-rotate", 0); float momentum = Utils.find_int_arg(args, "-momentum", .9f); float lambda = Utils.find_int_arg(args, "-lambda", .01f); string prefix = Utils.find_int_arg(args, "-prefix", ""); bool reconstruct = Utils.find_arg(args, "-reconstruct"); int smoothSize = Utils.find_int_arg(args, "-smooth", 1); Network net = Parser.parse_network_cfg(cfg); Parser.load_weights(net, weights); string cfgbase = Utils.Basecfg(cfg); string imbase = Utils.Basecfg(input); Network.set_batch_network(net, 1); Image im = LoadArgs.load_image_color(input, 0, 0); float[] features = new float[0]; Image update = null; if (reconstruct) { Network.resize_network(net, im.W, im.H); int zz = 0; Network.network_predict(net, im.Data); Image outIm = Network.get_network_image(net); Image crop = LoadArgs.crop_image(outIm, zz, zz, outIm.W - 2 * zz, outIm.H - 2 * zz); Image fIm = LoadArgs.resize_image(crop, outIm.W, outIm.H); Console.Write($"%d features\n", outIm.W * outIm.H * outIm.C); im = LoadArgs.resize_image(im, im.W, im.H); fIm = LoadArgs.resize_image(fIm, fIm.W, fIm.H); features = fIm.Data; int i; for (i = 0; i < 14 * 14 * 512; ++i) { features[i] += Utils.rand_uniform(-.19f, .19f); } im = LoadArgs.make_random_image(im.W, im.H, im.C); update = new Image(im.W, im.H, im.C); } int e; int n; for (e = 0; e < rounds; ++e) { Console.Error.Write($"Iteration: "); for (n = 0; n < iters; ++n) { Console.Error.Write($"%d, ", n); if (reconstruct) { reconstruct_picture(net, features, im, update, rate, momentum, lambda, smoothSize, 1); //if ((n+1)%30 == 0) rate *= .5; LoadArgs.show_image(im, "reconstruction"); CvInvoke.WaitKey(10); } else { int layer = maxLayer + Utils.Rand.Next() % range - range / 2; int octave = Utils.Rand.Next() % octaves; optimize_picture(net, im, layer, 1 / (float)Math.Pow(1.33333333, octave), rate, thresh, norm); } } Console.Error.Write($"done\n"); string buff; if (!string.IsNullOrEmpty(prefix)) { buff = $"{prefix}_{imbase}_{cfgbase}_{maxLayer}_{e:06}%s/%s_%s_%d_%06d"; } else { buff = $"{imbase}_{cfgbase}_{maxLayer}_{e:06}"; } Console.Write($"%d %s\n", e, buff); LoadArgs.save_image(im, buff); //LoadArgs.show_image(im, buff); //CvInvoke.WaitKey(); if (rotate != 0) { Image rot = LoadArgs.rotate_image(im, rotate); im = rot; } Image crop = LoadArgs.crop_image(im, (int)(im.W * (1f - zoom) / 2f), (int)(im.H * (1f - zoom) / 2f), (int)(im.W * zoom), (int)(im.H * zoom)); Image resized = LoadArgs.resize_image(crop, im.W, im.H); im = resized; } }
public static void DemoRun(string cfgfile, string weightfile, float thresh, int camIndex, string filename, string[] names, int classes, int frameSkip, string prefix) { //skip = frame_skip; Image[][] alphabet = LoadArgs.load_alphabet(); int delay = frameSkip; demoNames = names; demoAlphabet = alphabet; demoClasses = classes; demoThresh = thresh; Console.Write($"Demo\n"); net = Parser.parse_network_cfg(cfgfile); if (!string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); if (!string.IsNullOrEmpty(filename)) { Console.Write($"video file: %s\n", filename); } using (var capture = !string.IsNullOrEmpty(filename) ? new VideoCapture(filename) : new VideoCapture(camIndex)) { vidCap = capture; if (!vidCap.IsOpened) { Utils.Error("Couldn't connect to webcam.\n"); } Layer l = net.Layers[net.N - 1]; int j; avg = new float[l.Outputs]; for (j = 0; j < frames; ++j) { predictions[j] = new float[l.Outputs]; } for (j = 0; j < frames; ++j) { images[j] = new Image(1, 1, 3); } boxes = new Box[l.W * l.H * l.N]; probs = new float[l.W * l.H * l.N][]; for (j = 0; j < l.W * l.H * l.N; ++j) { probs[j] = new float[l.Classes]; } Thread fetchThread; Thread detectThread; fetch_in_thread(); det = inputImage; detS = inS; fetch_in_thread(); detect_in_thread(); disp = det; det = inputImage; detS = inS; for (j = 0; j < frames / 2; ++j) { fetch_in_thread(); detect_in_thread(); disp = det; det = inputImage; detS = inS; } int count = 0; var sw = new Stopwatch(); sw.Stop(); while (true) { ++count; fetchThread = new Thread(fetch_in_thread); detectThread = new Thread(detect_in_thread); fetchThread.Start(); detectThread.Start(); if (string.IsNullOrEmpty(prefix)) { LoadArgs.show_image(disp, "Demo"); int c = CvInvoke.WaitKey(1); if (c == 10) { if (frameSkip == 0) { frameSkip = 60; } else if (frameSkip == 4) { frameSkip = 0; } else if (frameSkip == 60) { frameSkip = 4; } else { frameSkip = 0; } } } else { var buff = $"{prefix}_{count:08}"; LoadArgs.save_image(disp, buff); } fetchThread.Join(); detectThread.Join(); if (delay == 0) { disp = det; } det = inputImage; detS = inS; --delay; if (delay < 0) { delay = frameSkip; sw.Stop(); float curr = 1f / sw.Elapsed.Seconds; fps = curr; sw.Reset(); sw.Start(); } } } vidCap = null; }
public static void test_detector(string datacfg, string cfgfile, string weightfile, string filename, float thresh) { var options = OptionList.read_data_cfg(datacfg); string nameList = OptionList.option_find_str(options, "names", "Data.Data/names.list"); string[] names = Data.Data.get_labels(nameList); Image[][] alphabet = LoadArgs.load_alphabet(); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); Utils.Rand = new Random(2222222); var sw = new Stopwatch(); string input = ""; int j; float nms = .4f; while (true) { if (!string.IsNullOrEmpty(filename)) { input = filename; } else { Console.Write($"Enter Image Path: "); input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) { return; } input = input.TrimEnd(); } Image im = LoadArgs.load_image_color(input, 0, 0); Image sized = LoadArgs.resize_image(im, net.W, net.H); Layer l = net.Layers[net.N - 1]; Box[] boxes = new Box[l.W * l.H * l.N]; float[][] probs = new float[l.W * l.H * l.N][]; for (j = 0; j < l.W * l.H * l.N; ++j) { probs[j] = new float[l.Classes]; } float[] x = sized.Data; sw.Start(); Network.network_predict(net, x); sw.Stop(); Console.Write($"%s: Predicted ini %f seconds.\n", input, sw.Elapsed.Seconds); Layer.get_region_boxes(l, 1, 1, thresh, probs, boxes, false, new int[0]); if (nms != 0) { Box.do_nms_sort(boxes, probs, l.W * l.H * l.N, l.Classes, nms); } LoadArgs.draw_detections(im, l.W * l.H * l.N, thresh, boxes, probs, names, alphabet, l.Classes); LoadArgs.save_image(im, "predictions"); LoadArgs.show_image(im, "predictions"); CvInvoke.WaitKey(); CvInvoke.DestroyAllWindows(); if (!string.IsNullOrEmpty(filename)) { break; } } }