Пример #1
0
        /// <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));
        }
Пример #2
0
        /// <summary>
        /// Get a new batch of images to train the discriminator.
        /// The batch will contain generated images with label 1 and actual images with label 0.
        /// </summary>
        /// <param name="discriminatorVar">The input variable for the discriminator.</param>
        /// <param name="generatedImages">The list of generated images.</param>
        /// <param name="actualImages">The list of actual images.</param>
        /// <param name="batchSize">The batch size.</param>
        /// <param name="start">The start position in the training partition.</param>
        /// <returns>A tuple with the feature batch and label batch for training.</returns>
        public static (CNTK.Value featureBatch, CNTK.Value labelBatch) GetTrainingBatch(
            CNTK.Variable discriminatorVar,
            IList <IList <float> > generatedImages,
            float[][] actualImages,
            int batchSize,
            int start)
        {
            // set up a Gaussian random number generator
            var random         = new Random();
            var gaussianRandom = new GaussianRandom(random);

            // create a training batch for the discriminator
            // the first half of the mini-batch are the fake images (marked with label='1')
            // the second half are real images (marked with label='0')
            var combined_images = new float[2 * batchSize][];
            var labels          = new float[2 * batchSize];

            for (int i = 0; i < batchSize; i++)
            {
                combined_images[i] = generatedImages[i].ToArray();
                labels[i]          = (float)(1 + 0.05 * gaussianRandom.NextGaussian());

                combined_images[i + batchSize] = actualImages[start + i];
                labels[i + batchSize]          = (float)(0.05 * gaussianRandom.NextGaussian());
            }

            // create batches
            var combined_images_minibatch = discriminatorVar.GetBatch(combined_images, 0, combined_images.Length);
            var labels_minibatch          = CNTK.Value.CreateBatch(new CNTK.NDShape(0), labels, NetUtil.CurrentDevice, true);

            // return results
            return(combined_images_minibatch, labels_minibatch);
        }
Пример #3
0
        public static (CNTK.Value featureBatch, CNTK.Value labelBatch) GetMisleadingBatch(
            CNTK.Function gan,
            int batchSize,
            int latentDimensions)
        {
            // set up a Gaussian random number generator
            var random         = new Random();
            var gaussianRandom = new GaussianRandom(random);

            // prepare a batch to fool the discriminator: we generate fake images
            // but we label them as real with label=0
            var random_latent_vectors    = gaussianRandom.getFloatSamples(batchSize * latentDimensions);
            var misleading_targets       = new float[batchSize];
            var random_latent_vectors_nd = new CNTK.NDArrayView(new int[] { latentDimensions, 1, batchSize }, random_latent_vectors, NetUtil.CurrentDevice);

            // return results
            return(
                new CNTK.Value(random_latent_vectors_nd),
                CNTK.Value.CreateBatch(new CNTK.NDShape(0), misleading_targets, NetUtil.CurrentDevice, true)
                );
        }