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 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); }