public static void reconstruct_picture(Network net, float[] features, Image recon, Image update, float rate, float momentum, float lambda, int smoothSize, int iters) { int iter = 0; for (iter = 0; iter < iters; ++iter) { Image delta = new Image(recon.W, recon.H, recon.C); NetworkState state = new NetworkState(); state.Input = (float[])recon.Data.Clone(); state.Delta = (float[])delta.Data.Clone(); state.Truth = new float[Network.get_network_output_size(net)]; Array.Copy(features, 0, state.Truth, 0, state.Truth.Length); Network.forward_network_gpu(net, state); Network.backward_network_gpu(net, state); Array.Copy(state.Delta, delta.Data, delta.W * delta.H * delta.C); Blas.Axpy_cpu(recon.W * recon.H * recon.C, 1, delta.Data, update.Data); Smooth(recon, update, lambda, smoothSize); Blas.Axpy_cpu(recon.W * recon.H * recon.C, rate, update.Data, recon.Data); Blas.Scal_cpu(recon.W * recon.H * recon.C, momentum, update.Data, 1); LoadArgs.constrain_image(recon); } }
private static void optimize_picture(Network net, Image orig, int maxLayer, float scale, float rate, float thresh, bool norm) { net.N = maxLayer + 1; int dx = Utils.Rand.Next() % 16 - 8; int dy = Utils.Rand.Next() % 16 - 8; bool flip = Utils.Rand.Next() % 2 != 0; Image crop = LoadArgs.crop_image(orig, dx, dy, orig.W, orig.H); Image im = LoadArgs.resize_image(crop, (int)(orig.W * scale), (int)(orig.H * scale)); if (flip) { LoadArgs.flip_image(im); } Network.resize_network(net, im.W, im.H); Layer last = net.Layers[net.N - 1]; Image delta = new Image(im.W, im.H, im.C); NetworkState state = new NetworkState(); state.Input = (float[])im.Data.Clone(); state.Delta = (float[])im.Data.Clone(); Network.forward_network_gpu(net, state); Blas.copy_ongpu(last.Outputs, last.OutputGpu, last.DeltaGpu); Array.Copy(last.DeltaGpu, last.Delta, last.Outputs); calculate_loss(last.Delta, last.Delta, last.Outputs, thresh); Array.Copy(last.Delta, last.DeltaGpu, last.Outputs); Network.backward_network_gpu(net, state); Array.Copy(state.Delta, delta.Data, im.W * im.H * im.C); if (flip) { LoadArgs.flip_image(delta); } Image resized = LoadArgs.resize_image(delta, orig.W, orig.H); Image outi = LoadArgs.crop_image(resized, -dx, -dy, orig.W, orig.H); if (norm) { Utils.normalize_array(outi.Data, outi.W * outi.H * outi.C); } Blas.Axpy_cpu(orig.W * orig.H * orig.C, rate, outi.Data, orig.Data); LoadArgs.constrain_image(orig); }
private static void generate_vid_rnn(string cfgfile, string weightfile) { Network extractor = Parser.parse_network_cfg("cfg/extractor.recon.cfg"); Parser.load_weights(extractor, "/home/pjreddie/trained/yolo-coco.conv"); Network net = Parser.parse_network_cfg(cfgfile); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(extractor, 1); Network.set_batch_network(net, 1); int i; VideoCapture cap = new VideoCapture("/extra/vid/ILSVRC2015/Data.Data/VID/snippets/val/ILSVRC2015_val_00007030.mp4"); float[] feat; float[] next; next = null; Image last = null; for (i = 0; i < 25; ++i) { Image im = LoadArgs.get_image_from_stream(cap); Image re = LoadArgs.resize_image(im, extractor.W, extractor.H); feat = Network.network_predict(extractor, re.Data); if (i > 0) { Console.Write($"%f %f\n", Utils.mean_array(feat, 14 * 14 * 512), Utils.variance_array(feat, 14 * 14 * 512)); Console.Write($"%f %f\n", Utils.mean_array(next, 14 * 14 * 512), Utils.variance_array(next, 14 * 14 * 512)); Console.Write($"%f\n", Utils.mse_array(feat, 14 * 14 * 512)); Blas.Axpy_cpu(14 * 14 * 512, -1, feat, next); Console.Write($"%f\n", Utils.mse_array(next, 14 * 14 * 512)); } next = Network.network_predict(net, feat); if (i == 24) { last = new Image(re); } } for (i = 0; i < 30; ++i) { next = Network.network_predict(net, next); Image newi = save_reconstruction(extractor, last, next, "new", i); last = newi; } }
private static void test_cifar_multi(string filename, string weightfile) { Network net = Parser.parse_network_cfg(filename); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Network.set_batch_network(net, 1); float avgAcc = 0; Data.Data test = Data.Data.load_cifar10_data("Data.Data/cifar/cifar-10-batches-bin/test_batch.bin"); int i; for (i = 0; i < test.X.Rows; ++i) { Image im = new Image(32, 32, 3, test.X.Vals[i]); float[] pred = new float[10]; float[] p = Network.network_predict(net, im.Data); Blas.Axpy_cpu(10, 1, p, pred); LoadArgs.flip_image(im); p = Network.network_predict(net, im.Data); Blas.Axpy_cpu(10, 1, p, pred); int index = Utils.max_index(pred, 10); int sclass = Utils.max_index(test.Y.Vals[i], 10); if (index == sclass) { avgAcc += 1; } Console.Write($"%4d: %.2f%%\n", i, 100f * avgAcc / (i + 1)); } }
private static void validate_classifier_multi(string datacfg, string filename, string weightfile) { int i, j; Network net = Parser.parse_network_cfg(filename); Network.set_batch_network(net, 1); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } var options = OptionList.read_data_cfg(datacfg); string labelList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list"); string validList = OptionList.option_find_str(options, "valid", "Data.Data/train.list"); int classes = OptionList.option_find_int(options, "classes", 2); int topk = OptionList.option_find_int(options, "top", 1); string[] labels = Data.Data.get_labels(labelList); int[] scales = { 224, 288, 320, 352, 384 }; int nscales = scales.Length; string[] paths = Data.Data.GetPaths(validList); int m = paths.Length; float avgAcc = 0; float avgTopk = 0; int[] indexes = new int[topk]; for (i = 0; i < m; ++i) { int class2 = -1; string path = paths[i]; for (j = 0; j < classes; ++j) { if (path.Contains(labels[j])) { class2 = j; break; } } float[] pred = new float[classes]; Image im = LoadArgs.load_image_color(paths[i], 0, 0); for (j = 0; j < nscales; ++j) { Image r = LoadArgs.resize_min(im, scales[j]); Network.resize_network(net, r.W, r.H); float[] p = Network.network_predict(net, r.Data); if (net.Hierarchy != null) { net.Hierarchy.Hierarchy_predictions(p, 0, net.Outputs, true); } Blas.Axpy_cpu(classes, 1, p, pred); LoadArgs.flip_image(r); p = Network.network_predict(net, r.Data); Blas.Axpy_cpu(classes, 1, p, pred); } Utils.top_k(pred, classes, topk, indexes); if (indexes[0] == class2) { avgAcc += 1; } for (j = 0; j < topk; ++j) { if (indexes[j] == class2) { avgTopk += 1; } } Console.Write($"%d: top 1: %f, top %d: %f\n", i, avgAcc / (i + 1), topk, avgTopk / (i + 1)); } }
private static void validate_classifier_10(string datacfg, string filename, string weightfile) { int i, j; Network net = Parser.parse_network_cfg(filename); Network.set_batch_network(net, 1); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } var options = OptionList.read_data_cfg(datacfg); string labelList = OptionList.option_find_str(options, "labels", "Data.Data/labels.list"); string validList = OptionList.option_find_str(options, "valid", "Data.Data/train.list"); int classes = OptionList.option_find_int(options, "classes", 2); int topk = OptionList.option_find_int(options, "top", 1); string[] labels = Data.Data.get_labels(labelList); string[] paths = Data.Data.GetPaths(validList); int m = paths.Length; float avgAcc = 0; float avgTopk = 0; int[] indexes = new int[topk]; for (i = 0; i < m; ++i) { int class2 = -1; string path = paths[i]; for (j = 0; j < classes; ++j) { if (path.Contains(labels[j])) { class2 = j; break; } } int w = net.W; int h = net.H; int shift = 32; Image im = LoadArgs.load_image_color(paths[i], w + shift, h + shift); Image[] images = new Image[10]; images[0] = LoadArgs.crop_image(im, -shift, -shift, w, h); images[1] = LoadArgs.crop_image(im, shift, -shift, w, h); images[2] = LoadArgs.crop_image(im, 0, 0, w, h); images[3] = LoadArgs.crop_image(im, -shift, shift, w, h); images[4] = LoadArgs.crop_image(im, shift, shift, w, h); LoadArgs.flip_image(im); images[5] = LoadArgs.crop_image(im, -shift, -shift, w, h); images[6] = LoadArgs.crop_image(im, shift, -shift, w, h); images[7] = LoadArgs.crop_image(im, 0, 0, w, h); images[8] = LoadArgs.crop_image(im, -shift, shift, w, h); images[9] = LoadArgs.crop_image(im, shift, shift, w, h); float[] pred = new float[classes]; for (j = 0; j < 10; ++j) { float[] p = Network.network_predict(net, images[j].Data); if (net.Hierarchy != null) { net.Hierarchy.Hierarchy_predictions(p, 0, net.Outputs, true); } Blas.Axpy_cpu(classes, 1, p, pred); } Utils.top_k(pred, classes, topk, indexes); if (indexes[0] == class2) { avgAcc += 1; } for (j = 0; j < topk; ++j) { if (indexes[j] == class2) { avgTopk += 1; } } Console.Write($"%d: top 1: %f, top %d: %f\n", i, avgAcc / (i + 1), topk, avgTopk / (i + 1)); } }
private static void Average(List <string> args) { string cfgfile = args[2]; string outfile = args[3]; Network net = Parser.parse_network_cfg(cfgfile); Network sum = Parser.parse_network_cfg(cfgfile); string weightfile = args[4]; Parser.load_weights(sum, weightfile); int i, j; int n = args.Count - 5; for (i = 0; i < n; ++i) { weightfile = args[i + 5]; Parser.load_weights(net, weightfile); for (j = 0; j < net.N; ++j) { Layer l = net.Layers[j]; Layer outl = sum.Layers[j]; if (l.LayerType == LayerType.Convolutional) { int num = l.N * l.C * l.Size * l.Size; Blas.Axpy_cpu(l.N, 1, l.BiasesComplete, outl.BiasesComplete, l.BiasesIndex, outl.BiasesIndex); Blas.Axpy_cpu(num, 1, l.WeightsComplete, outl.WeightsComplete, l.WeightsIndex, outl.WeightsIndex); if (l.BatchNormalize) { Blas.Axpy_cpu(l.N, 1, l.Scales, outl.Scales); Blas.Axpy_cpu(l.N, 1, l.RollingMean, outl.RollingMean); Blas.Axpy_cpu(l.N, 1, l.RollingVariance, outl.RollingVariance); } } if (l.LayerType == LayerType.Connected) { Blas.Axpy_cpu(l.Outputs, 1, l.BiasesComplete, outl.BiasesComplete, l.BiasesIndex, outl.BiasesIndex); Blas.Axpy_cpu(l.Outputs * l.Inputs, 1, l.WeightsComplete, outl.WeightsComplete, l.WeightsIndex, outl.WeightsIndex); } } } n = n + 1; for (j = 0; j < net.N; ++j) { Layer l = sum.Layers[j]; if (l.LayerType == LayerType.Convolutional) { int num = l.N * l.C * l.Size * l.Size; Blas.Scal_cpu(l.N, 1.0f / n, l.BiasesComplete, 1, l.BiasesIndex); Blas.Scal_cpu(num, 1.0f / n, l.WeightsComplete, 1, l.WeightsIndex); if (l.BatchNormalize) { Blas.Scal_cpu(l.N, 1.0f / n, l.Scales, 1); Blas.Scal_cpu(l.N, 1.0f / n, l.RollingMean, 1); Blas.Scal_cpu(l.N, 1.0f / n, l.RollingVariance, 1); } } if (l.LayerType == LayerType.Connected) { Blas.Scal_cpu(l.Outputs, 1.0f / n, l.BiasesComplete, 1, l.BiasesIndex); Blas.Scal_cpu(l.Outputs * l.Inputs, 1.0f / n, l.WeightsComplete, 1, l.WeightsIndex); } } Parser.save_weights(sum, outfile); }