private static void test_cifar_csvtrain(string filename, string weightfile) { Network net = Parser.parse_network_cfg(filename); if (string.IsNullOrEmpty(weightfile)) { Parser.load_weights(net, weightfile); } Data.Data test = Data.Data.load_all_cifar10(); Matrix pred = Network.network_predict_data(net, test); int i; for (i = 0; i < test.X.Rows; ++i) { Image im = new Image(32, 32, 3, test.X.Vals[i]); LoadArgs.flip_image(im); } Matrix pred2 = Network.network_predict_data(net, test); pred.scale_matrix(.5f); pred2.scale_matrix(.5f); Matrix.matrix_add_matrix(pred2, pred); pred.to_csv(); Console.Error.Write($"Accuracy: %f\n", Matrix.matrix_topk_accuracy(test.Y, pred, 1)); }
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 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)); } }