void generateImages()
        {
            if (model == null)
            {
                System.Diagnostics.Debug.Assert(System.IO.File.Exists(model_filename));
                model = CNTK.Function.Load(model_filename, computeDevice);
            }

            var node_z        = model.FindByName("input_z");
            var decoder_start = Util.find_function_with_input(model, node_z);
            var z_sample_var  = CNTK.Variable.InputVariable(node_z.Output.Shape, CNTK.DataType.Float, "z_sample");
            var replacements  = new Dictionary <CNTK.Variable, CNTK.Variable>()
            {
                { node_z, z_sample_var }
            };
            var decoder = model.Clone(CNTK.ParameterCloningMethod.Freeze, replacements);

            var n                     = 15; // figure with 15x15 digits
            var xy_buffer             = new float[n * n * 2];
            var sample_start          = -2f;
            var sample_interval_width = 4f;

            for (int i = 0, pos = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    xy_buffer[pos++] = sample_start + (sample_interval_width / (n - 1)) * i;
                    xy_buffer[pos++] = sample_start + (sample_interval_width / (n - 1)) * j;
                }
            }

            var ndArrayView = new CNTK.NDArrayView(new int[] { 2, 1, xy_buffer.Length / 2 }, xy_buffer, computeDevice, true);
            var value       = new CNTK.Value(ndArrayView);
            var inputs_dir  = new Dictionary <CNTK.Variable, CNTK.Value>()
            {
                { z_sample_var, value }
            };
            var outputs_dir = new Dictionary <CNTK.Variable, CNTK.Value>()
            {
                { decoder.Output, null }
            };

            decoder.Evaluate(inputs_dir, outputs_dir, computeDevice);
            var values = outputs_dir[decoder.Output].GetDenseData <float>(decoder.Output);

            plotImages(values);
        }
示例#2
0
        public static float[] ValueToArray(CNTK.Value value)
        {
            if (value.IsSparse)
            {
                throw new NotImplementedException("Sparse value is not supported yet");
            }

            if (value.DataType != CNTK.DataType.Float)
            {
                throw new NotImplementedException("Only float value is supported");
            }

            var variable = CNTK.Variable.InputVariable(value.Shape, CNTK.DataType.Float);

            var result = value.GetDenseData <float>(variable);

            return(result[0].ToArray());
        }
示例#3
0
        public static DataSourceBase <float, IList <float> > FromVariable(CNTK.Variable variable)
        {
            var array = variable.GetValue();

            if (array.IsSparse)
            {
                throw new NotImplementedException("Sparse value is not supported yet");
            }

            if (array.DataType != CNTK.DataType.Float)
            {
                throw new NotImplementedException("Only float value is supported");
            }

            var value = new CNTK.Value(array);

            var result = value.GetDenseData <float>(variable);

            return(new DataSourceBase <float, IList <float> >(result[0], value.Shape.Dimensions.ToArray()));
        }
示例#4
0
        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);
        }
示例#5
0
        Dictionary <CNTK.Variable, CNTK.Value> create_batch(CNTK.Function model, float[][] labels)
        {
            var dict_inputs = new Dictionary <CNTK.Variable, CNTK.Value>();

            for (int i = 0; i < model.Arguments.Count; i++)
            {
                var loss_input_variable = model.Arguments[i];
                if (loss_input_variable.Name == "dummy_features")
                {
                    var dummy_scalar_buffer = new float[] { 1 };
                    var dummy_scalar_nd     = new CNTK.NDArrayView(new int[] { 1 }, dummy_scalar_buffer, computeDevice, readOnly: true);
                    dict_inputs[loss_input_variable] = new CNTK.Value(dummy_scalar_nd);
                }
                else
                {
                    var cs_index = Int32.Parse(loss_input_variable.Name.Substring("content_and_style_".Length));
                    var nd       = new CNTK.NDArrayView(loss_input_variable.Shape, labels[cs_index], computeDevice, readOnly: true);
                    dict_inputs[loss_input_variable] = new CNTK.Value(nd);
                }
            }
            return(dict_inputs);
        }
示例#6
0
        /// <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);
        }
示例#7
0
 public static IDataSource <float> ValueToDataSource(CNTK.Value value)
 {
     return(DataSourceFactory.FromValue(value));
 }
示例#8
0
 public static DataSourceBase <float, float[]> FromValue(CNTK.Value value)
 {
     return(new DataSourceBase <float, float[]>(Converter.ValueToArray(value), value.Shape.Dimensions.ToArray()));
 }