/// <summary> /// Use the generator to create a list of fake images/ /// </summary> /// <param name="generator">The generator to use.</param> /// <param name="batchSize">The batch size.</param> /// <param name="latentDimensions">The number of dimensions in the latent input vector.</param> /// <returns>A list of images created by the generator.</returns> public static IList <IList <float> > GenerateImages( CNTK.Function generator, int batchSize, int latentDimensions) { // set up a Gaussian random number generator var random = new Random(); var gaussianRandom = new GaussianRandom(random); // set up randomized input for the generator var random_latent_vectors = gaussianRandom.getFloatSamples(batchSize * latentDimensions); var random_latent_vectors_nd = new CNTK.NDArrayView(new int[] { latentDimensions, 1, batchSize }, random_latent_vectors, NetUtil.CurrentDevice); var generator_inputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { generator.Arguments[0], new CNTK.Value(random_latent_vectors_nd) } }; var generator_outputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { generator.Output, null } }; // run the generator and collect the images generator.Evaluate(generator_inputs, generator_outputs, NetUtil.CurrentDevice); return(generator_outputs[generator.Output].GetDenseData <float>(generator.Output)); }
byte[] inference(CNTK.Function model, float[][] labels) { var batch = create_batch(model, labels); var outputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { model.Outputs[0], null } }; model.Evaluate(batch, outputs, computeDevice); var img = outputs[model.Outputs[0]].GetDenseData <float>(model.Outputs[0])[0].ToArray(); var img_data = Util.convert_from_channels_first(img, offsets); return(img_data); }
/// <summary> /// Infer the image from the trained model. /// </summary> /// <param name="model">The model to use.</param> /// <param name="batch">The evaluation batch to use.</param> /// <returns>The image inferred from the model.</returns> public static byte[] InferImage( this CNTK.Function model, Dictionary <CNTK.Variable, CNTK.Value> batch) { var outputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { model.Outputs[0], null } }; model.Evaluate(batch, outputs, NetUtil.CurrentDevice); var img = outputs[model.Outputs[0]].GetDenseData <float>(model.Outputs[0])[0].ToArray(); return(UnflattenByChannel(img, VGG19_Offsets)); }
float[][] compute_labels(CNTK.Function model, float[] target_image, float[] style_reference_image) { var input_shape = model.Arguments[0].Shape.Dimensions.ToArray(); System.Diagnostics.Debug.Assert(input_shape[0] * input_shape[1] * input_shape[2] == target_image.Length); System.Diagnostics.Debug.Assert(target_image.Length == style_reference_image.Length); #if false var cpuDevice = CNTK.DeviceDescriptor.CPUDevice; var target_image_nd = new CNTK.NDArrayView(input_shape, target_image, cpuDevice, readOnly: true); var style_reference_image_nd = new CNTK.NDArrayView(input_shape, style_reference_image, cpuDevice, readOnly: true); var batch_nd = new CNTK.NDArrayView[] { target_image_nd, style_reference_image_nd }; var batch = CNTK.Value.Create(input_shape, batch_nd, computeDevice, readOnly: true); #else var batch_buffer = new float[2 * target_image.Length]; Array.Copy(target_image, 0, batch_buffer, 0, target_image.Length); Array.Copy(style_reference_image, 0, batch_buffer, target_image.Length, target_image.Length); var batch_nd = new CNTK.NDArrayView(new int[] { model.Arguments[0].Shape[0], model.Arguments[0].Shape[1], model.Arguments[0].Shape[2], 1, 2 }, batch_buffer, computeDevice); var batch = new CNTK.Value(batch_nd); #endif var inputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { model.Arguments[0], batch } }; var outputs = new Dictionary <CNTK.Variable, CNTK.Value>(); foreach (var output in model.Outputs) { outputs.Add(output, null); } model.Evaluate(inputs, outputs, computeDevice); float[][] labels = new float[model.Outputs.Count][]; labels[0] = outputs[model.Outputs[0]].GetDenseData <float>(model.Outputs[0])[0].ToArray(); for (int i = 1; i < labels.Length; i++) { labels[i] = outputs[model.Outputs[i]].GetDenseData <float>(model.Outputs[i])[1].ToArray(); } return(labels); }
byte[] inference(CNTK.Function model, float[][] labels) { var batch = create_batch(model, labels); var outputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { model.Outputs[0], null } }; model.Evaluate(batch, outputs, computeDevice); var img = outputs[model.Outputs[0]].GetDenseData <float>(model.Outputs[0])[0].ToArray(); var img_data = new byte[img.Length]; var image_size = img_height * img_width; for (int i = 0; i < img_data.Length; i += 3) { img_data[i] = (byte)Math.Max(0, Math.Min(img[i / 3] + offsets[0], 255)); img_data[i + 1] = (byte)Math.Max(0, Math.Min(img[i / 3 + image_size] + offsets[1], 255)); img_data[i + 2] = (byte)Math.Max(0, Math.Min(img[i / 3 + 2 * image_size] + offsets[2], 255)); } return(img_data); }
/// <summary> /// Calculate the output labels for style transfer. /// </summary> /// <param name="model">The neural network to use.</param> /// <param name="contentImage">The content image to use.</param> /// <param name="styleImage">The style image to use.</param> /// <returns></returns> public static float[][] CalculateLabels(CNTK.Function model, float[] contentImage, float[] styleImage) { // make sure the content image dimensions match the neural network input size // make sure the content and style images are the same size var input_shape = model.Arguments[0].Shape.Dimensions.ToArray(); System.Diagnostics.Debug.Assert(input_shape[0] * input_shape[1] * input_shape[2] == contentImage.Length); System.Diagnostics.Debug.Assert(contentImage.Length == styleImage.Length); // set up a batch with the content and the style image var batch_buffer = new float[2 * contentImage.Length]; Array.Copy(contentImage, 0, batch_buffer, 0, contentImage.Length); Array.Copy(styleImage, 0, batch_buffer, contentImage.Length, contentImage.Length); var batch_nd = new CNTK.NDArrayView(new int[] { model.Arguments[0].Shape[0], model.Arguments[0].Shape[1], model.Arguments[0].Shape[2], 1, 2 }, batch_buffer, NetUtil.CurrentDevice); var batch = new CNTK.Value(batch_nd); // let the model evaluate the batch var inputs = new Dictionary <CNTK.Variable, CNTK.Value>() { { model.Arguments[0], batch } }; var outputs = new Dictionary <CNTK.Variable, CNTK.Value>(); foreach (var output in model.Outputs) { outputs.Add(output, null); } model.Evaluate(inputs, outputs, NetUtil.CurrentDevice); // collect and return the model outputs float[][] labels = new float[model.Outputs.Count][]; labels[0] = outputs[model.Outputs[0]].GetDenseData <float>(model.Outputs[0])[0].ToArray(); for (int i = 1; i < labels.Length; i++) { labels[i] = outputs[model.Outputs[i]].GetDenseData <float>(model.Outputs[i])[1].ToArray(); } return(labels); }