public string GenerateImage(float[] latent, string fname, string dir = SampleDirName, List<float[,,]> fmapMods = null, int outLayer = -1, bool outAllLayers = false, bool doRecordFmaps = false)
        {
            string path = Path.Combine(dir, fname);
            if (latent.Contains(float.NaN))
            {
                System.IO.File.WriteAllText(string.Format("{0}.txt", path), "Sorry, something went wrong generating this image");
                return path;
            }
            string fmapsName = "";

            List<Tuple<string, int[]>> layersIn = new List<Tuple<string, int[]>>()
            {
                new Tuple<string, int[]>("generator_1/Res4/FmapRes4", new int[] { 1, 8, 4, 512 }),
                new Tuple<string, int[]>("generator_1/Res8/FmapRes8", new int[] { 1, 16, 8, 512 }),
                new Tuple<string, int[]>("generator_1/Res16/FmapRes16", new int[] { 1, 32, 16, 512 }),
                new Tuple<string, int[]>("generator_1/Res32/FmapRes32", new int[] { 1, 64, 32, 512 }),
                new Tuple<string, int[]>("generator_1/Res64/FmapRes64", new int[] { 1, 128, 64, 256 }),
                new Tuple<string, int[]>("generator_1/Res128/FmapRes128", new int[] { 1, 256, 128, 128 }),
                new Tuple<string, int[]>("generator_1/Res256/FmapRes256", new int[] { 1, 512, 256, 64 })
                //new Tuple<string, int[]>("generator_1/Res64/FmapRes64", new int[] { 1, 128, 64, 512 }),
                //new Tuple<string, int[]>("generator_1/Res128/FmapRes128", new int[] { 1, 256, 128, 256 }),
                //new Tuple<string, int[]>("generator_1/Res256/FmapRes256", new int[] { 1, 512, 256, 128 })
            };
            List<Tuple<string, int[]>> layersOut = new List<Tuple<string, int[]>>()
            {
                new Tuple<string, int[]>("generator_1/Res4/add", new int[] { 1, 8, 4, 512 }),
                new Tuple<string, int[]>("generator_1/Res8/add", new int[] { 1, 16, 8, 512 }),
                new Tuple<string, int[]>("generator_1/Res16/add", new int[] { 1, 32, 16, 512 }),
                new Tuple<string, int[]>("generator_1/Res32/add", new int[] { 1, 64, 32, 512 }),
                new Tuple<string, int[]>("generator_1/Res64/add", new int[] { 1, 128, 64, 256 }),
                new Tuple<string, int[]>("generator_1/Res128/add", new int[] { 1, 256, 128, 128 }),
                new Tuple<string, int[]>("generator_1/Res256/add", new int[] { 1, 512, 256, 64 })
                //new Tuple<string, int[]>("generator_1/Res64/add", new int[] { 1, 128, 64, 512 }),
                //new Tuple<string, int[]>("generator_1/Res128/add", new int[] { 1, 256, 128, 256 }),
                //new Tuple<string, int[]>("generator_1/Res256/add", new int[] { 1, 512, 256, 128 })
            };
            
            List<Tuple<string, int[]>> noiseData = new List<Tuple<string, int[]>>
            {
                new Tuple<string, int[]>("generator_1/Res4/noise_add1/Noise4_1/Reshape_1", new int[] { 1, 8, 4, 512 }),
                new Tuple<string, int[]>("generator_1/Res4/noise_add2/Noise4_2/Reshape_1", new int[] { 1, 8, 4, 512 }),

                new Tuple<string, int[]>("generator_1/Res8/noise_add1/Noise8_1/Reshape_1", new int[] { 1, 16, 8, 512 }),
                new Tuple<string, int[]>("generator_1/Res8/noise_add2/Noise8_2/Reshape_1", new int[] { 1, 16, 8, 512 }),

                new Tuple<string, int[]>("generator_1/Res16/noise_add1/Noise16_1/Reshape_1", new int[] { 1, 32, 16, 512 }),
                new Tuple<string, int[]>("generator_1/Res16/noise_add2/Noise16_2/Reshape_1", new int[] { 1, 32, 16, 512 }),

                new Tuple<string, int[]>("generator_1/Res32/noise_add1/Noise32_1/Reshape_1", new int[] { 1, 64, 32, 512 }),
                new Tuple<string, int[]>("generator_1/Res32/noise_add2/Noise32_2/Reshape_1", new int[] { 1, 64, 32, 512 }),

                new Tuple<string, int[]>("generator_1/Res64/noise_add1/Noise64_1/Reshape_1", new int[] { 1, 128, 64, 256 }),
                new Tuple<string, int[]>("generator_1/Res64/noise_add2/Noise64_2/Reshape_1", new int[] { 1, 128, 64, 256 }),

                new Tuple<string, int[]>("generator_1/Res128/noise_add1/Noise128_1/Reshape_1", new int[] { 1, 256, 128, 128 }),
                new Tuple<string, int[]>("generator_1/Res128/noise_add2/Noise128_2/Reshape_1", new int[] { 1, 256, 128, 128 }),

                new Tuple<string, int[]>("generator_1/Res256/noise_add1/Noise256_1/Reshape_1", new int[] { 1, 512, 256, 64 }),
                new Tuple<string, int[]>("generator_1/Res256/noise_add2/Noise256_2/Reshape_1", new int[] { 1, 512, 256, 64 }),
            };

            // Build session inputs
            List<GanTools.TensorData> inputs = new List<GanTools.TensorData>();
            inputs.Add(new TensorData("intermediate_latent", latent, 2, new int[] { 1, 512 }));
            //fmapMod = VectorAdd3D(fmapMod, _currentFmap.Item2);
            if (fmapMods != null)
            {
                for (int mod = 0; mod < fmapMods.Count; mod++)
                {
                    int width = fmapMods[mod].GetLength(1);
                    int layerIndex = (int)Math.Log(width, 2) - 2;
                    //List<string> layers = new List<string>(new string[]{"generator_1/Res4/adaptive_instance_norm_1/add_1",
                    //"generator_1/Res8/adaptive_instance_norm_1/add_1",
                    //"generator_1/Res16/adaptive_instance_norm_1/add_1",
                    //"generator_1/Res32/adaptive_instance_norm_1/add_1",
                    //"generator_1/Res64/adaptive_instance_norm_1/add_1",
                    //"generator_1/Res128/adaptive_instance_norm_1/add_1",
                    //"generator_1/Res256/adaptive_instance_norm_1/add_1" });

                    fmapsName = layersIn[layerIndex].Item1;
                    inputs.Add(new TensorData(fmapsName, fmapMods[mod].Cast<float>().ToArray(),
                        4, new int[] { 1, fmapMods[mod].GetLength(0), fmapMods[mod].GetLength(1), fmapMods[mod].GetLength(2) }));
                }
            }
            //if (_currentNoise != null)
            //{
            //    foreach (var noise in _currentNoise)
            //    {
            //        inputs.Add(new TensorData(noise.Item1, noise.Item2.Cast<float>().ToArray(), 4,
            //            new int[] { 1, noise.Item2.GetLength(0), noise.Item2.GetLength(1), noise.Item2.GetLength(2)}));
            //    }
            //}

            List<GanTools.TensorData> outputs = new List<GanTools.TensorData>();
            outputs.Add(new TensorData("output", null, 0, null));

            if (outAllLayers)
            {
                for (int i = 0; i < layersOut.Count; i++)
                {
                    fmapsName = layersOut[i].Item1;
                    int[] dims = layersOut[i].Item2;
                    float[] newFmap = new float[dims[1] * dims[2] * dims[3]];
                    // ToArray() may copy, in which case this won't work
                    outputs.Add(new TensorData(fmapsName, newFmap, 4, new int[] { 1, dims[1], dims[2], dims[3] }));
                }
            }
            else if (outLayer != -1)
            {
                fmapsName = layersOut[outLayer].Item1;
                int[] dims = layersOut[outLayer].Item2;
                float[] newFmap = new float[dims[1] * dims[2] * dims[3]];
                // ToArray() may copy, in which case this won't work
                outputs.Add(new TensorData(fmapsName, newFmap, 4, new int[] { 1, dims[1], dims[2], dims[3] }));
            }
            //if (_currentNoise == null)
            //{
            //    //float[][] noiseOut;
            //    foreach (var noiseTuple in noiseData)
            //    {
            //        float[] noiseOut = new float[noiseTuple.Item2[0] * noiseTuple.Item2[1] * noiseTuple.Item2[2] * noiseTuple.Item2[3]];
            //        outputs.Add(new TensorData(noiseTuple.Item1, noiseOut, 4, noiseTuple.Item2));
            //    }
            //}
            //if (setCurrentNoise)
            //{
            //    outputs.Add(new TensorData(fmapsName, _currentFmap.Item2.Cast<float>().ToArray(), 4, new int[] { 1, fmapMod.GetLength(1), fmapMod.GetLength(2), fmapMod.GetLength(3) }));
            //}


            g.GenerateImageFromIntermediate(inputs, ref outputs, path);
            //if (!fmapsUsedAsInput)
            //{
            //    _currentFmap = outMaps;
            //}
            if (outAllLayers)
            {
                currentFmaps = new List<List<float[,]>>();
                for (int layerIndex = 0; layerIndex < layersOut.Count; layerIndex++)
                {
                    int[] dims = layersOut[layerIndex].Item2;
                    float[,,] fmapShaped = new float[dims[1], dims[2], dims[3]];
                    Buffer.BlockCopy(outputs[layerIndex+1].Data, 0, fmapShaped, 0, dims[1] * dims[2] * dims[3] * sizeof(float));
                    List<float[,]> currentResFmap = new List<float[,]>();
                    for (int i = 0; i < dims[3]; i++)
                    {
                        float[,] newFmap = new float[dims[1], dims[2]];
                        for (int j = 0; j < dims[1]; j++)
                        {
                            for (int k = 0; k < dims[2]; k++)
                            {
                                newFmap[j, k] = fmapShaped[j, k, i];
                            }
                        }
                        currentResFmap.Add(newFmap);
                    }
                    currentFmaps.Add(currentResFmap);
                }
            }
            else if (outLayer != -1)
            {
                int[] dims = layersOut[outLayer].Item2;
                float[,,] fmapShaped = new float[dims[1], dims[2], dims[3]];
                Buffer.BlockCopy(outputs[1].Data, 0, fmapShaped, 0, dims[1] * dims[2] * dims[3] * sizeof(float));
                List<float[,]> currentResFmap = new List<float[,]>();
                for (int i = 0; i < dims[3]; i++)
                {
                    float[,] newFmap = new float[dims[1], dims[2]];
                    for(int j = 0; j < dims[1]; j++)
                    {
                        for (int k = 0; k < dims[2]; k++)
                        {
                            newFmap[j, k] = fmapShaped[j, k, i];
                        }
                    }
                    currentResFmap.Add(newFmap);
                }
                int oldCount = currentFmaps.Count;
                for (int i = 0; i <= outLayer - oldCount; i++)
                {
                    currentFmaps.Add(null);
                }
                currentFmaps[outLayer] = currentResFmap;
                if (doRecordFmaps)
                {
                    imageAndFmapsRecord.Add(new Tuple<string, List<float[,]>>(path, currentResFmap));
                    if (fmapsSum == null || fmapsSum.Count == 0 || currentResFmap[0].GetLength(0) != fmapsSum[0].GetLength(0))
                    {
                        fmapsSum = currentResFmap;
                    }
                    else
                    {
                        for (int fmap = 0; fmap < currentFmaps.Count; fmap++)
                        {
                            for (int i = 0; i < currentResFmap[0].GetLength(0); i++)
                            {
                                for (int j = 0; j < currentResFmap[0].GetLength(1); j++)
                                {
                                    fmapsSum[fmap][i, j] += currentResFmap[fmap][i, j];
                                    
                                }
                            }
                        }
                    }
                    fmapsTotalSummed += 1;
                }
            }
            //if (_currentNoise == null)
            //{
            //    int i = 2;
            //    _currentNoise = new List<Tuple<string, float[,,]>>();
            //    foreach (var noiseTuple in noiseData)
            //    {
            //        float[,,] noiseShaped = new float[noiseTuple.Item2[1], noiseTuple.Item2[2], noiseTuple.Item2[3]];
            //        Buffer.BlockCopy(outputs[i].Data, 0, noiseShaped, 0, noiseTuple.Item2[1] * noiseTuple.Item2[2] * noiseTuple.Item2[3] * sizeof(float));
            //        _currentNoise.Add(new Tuple<string, float[,,]>(noiseTuple.Item1, noiseShaped));
            //        i++;
            //    }
            //}
            AppendLatentToImage(latent, path);
            return path;
        }
Esempio n. 2
0
        public string GenerateImage(float[] latent, string fname, string dir = SampleDirName, List <float[, , ]> fmapMods = null, List <int> outLayers = null, bool doRecordFmaps = false)
        {
            string path = Path.Combine(dir, fname);

            if (latent.Contains(float.NaN))
            {
                System.IO.File.WriteAllText(string.Format("{0}.txt", path), "Sorry, something went wrong generating this image");
                return(path);
            }
            string fmapsName = "";


            // Build session inputs
            List <GanTools.TensorData> inputs = new List <GanTools.TensorData>();

            inputs.Add(new TensorData(_latentTensorName, latent, 2, new int[] { 1, 512 }));
            //fmapMod = VectorAdd3D(fmapMod, _currentFmap.Item2);
            if (fmapMods != null)
            {
                for (int mod = 0; mod < fmapMods.Count; mod++)
                {
                    int width      = fmapMods[mod].GetLength(1);
                    int layerIndex = widthToIndex[width]; //(int)Math.Log(width, 2) - 2;

                    fmapsName = _layersIn[layerIndex].Item1;
                    inputs.Add(new TensorData(fmapsName, fmapMods[mod].Cast <float>().ToArray(),
                                              4, new int[] { 1, fmapMods[mod].GetLength(0), fmapMods[mod].GetLength(1), fmapMods[mod].GetLength(2) }));
                }
            }

            List <GanTools.TensorData> outputs = new List <GanTools.TensorData>();

            outputs.Add(new TensorData(_outputTensorName, null, 0, null));

            if (outLayers != null)
            {
                foreach (int i in outLayers)
                {
                    fmapsName = _layersOut[i].Item1;
                    int[]   dims    = _layersOut[i].Item2;
                    float[] newFmap = new float[dims[1] * dims[2] * dims[3]];
                    // ToArray() may copy, in which case this won't work
                    outputs.Add(new TensorData(fmapsName, newFmap, 4, new int[] { 1, dims[1], dims[2], dims[3] }));
                }
            }


            g.GenerateImageFromIntermediate(inputs, ref outputs, path);
            //if (!fmapsUsedAsInput)
            //{
            //    _currentFmap = outMaps;
            //}
            if (outLayers != null)
            {
                if (outLayers.Count == 1)
                {
                    int   outLayer = outLayers[0];
                    int[] dims     = _layersOut[outLayer].Item2;
                    float[,,] fmapShaped = new float[dims[1], dims[2], dims[3]];
                    Buffer.BlockCopy(outputs[1].Data, 0, fmapShaped, 0, dims[1] * dims[2] * dims[3] * sizeof(float));
                    List <float[, ]> currentResFmap = new List <float[, ]>();
                    for (int i = 0; i < dims[3]; i++)
                    {
                        float[,] newFmap = new float[dims[1], dims[2]];
                        for (int j = 0; j < dims[1]; j++)
                        {
                            for (int k = 0; k < dims[2]; k++)
                            {
                                newFmap[j, k] = fmapShaped[j, k, i];
                            }
                        }
                        currentResFmap.Add(newFmap);
                    }
                    int oldCount = currentFmaps.Count;
                    for (int i = 0; i <= outLayer - oldCount; i++)
                    {
                        currentFmaps.Add(null);
                    }
                    currentFmaps[outLayer] = currentResFmap;
                    if (doRecordFmaps)
                    {
                        imageAndFmapsRecord.Add(new Tuple <string, List <float[, ]> >(path, currentResFmap));
                        if (fmapsSum == null || fmapsSum.Count == 0 || currentResFmap[0].GetLength(0) != fmapsSum[0].GetLength(0))
                        {
                            fmapsSum = currentResFmap;
                        }
                        else
                        {
                            for (int fmap = 0; fmap < currentFmaps.Count; fmap++)
                            {
                                for (int i = 0; i < currentResFmap[0].GetLength(0); i++)
                                {
                                    for (int j = 0; j < currentResFmap[0].GetLength(1); j++)
                                    {
                                        fmapsSum[fmap][i, j] += currentResFmap[fmap][i, j];
                                    }
                                }
                            }
                        }
                        fmapsTotalSummed += 1;
                    }
                }
                else
                {
                    currentFmaps = new List <List <float[, ]> >();
                    int outputCounter = 0;
                    for (int layerIndex = 0; layerIndex < _layersOut.Count; layerIndex++)
                    {
                        currentFmaps.Add(null);
                    }
                    foreach (int layerIndex in outLayers)
                    {
                        int[] dims = _layersOut[layerIndex].Item2;
                        float[,,] fmapShaped = new float[dims[1], dims[2], dims[3]];
                        Buffer.BlockCopy(outputs[outputCounter + 1].Data, 0, fmapShaped, 0, dims[1] * dims[2] * dims[3] * sizeof(float));
                        outputCounter += 1;
                        List <float[, ]> currentResFmap = new List <float[, ]>();
                        for (int i = 0; i < dims[3]; i++)
                        {
                            float[,] newFmap = new float[dims[1], dims[2]];
                            for (int j = 0; j < dims[1]; j++)
                            {
                                for (int k = 0; k < dims[2]; k++)
                                {
                                    newFmap[j, k] = fmapShaped[j, k, i];
                                }
                            }
                            currentResFmap.Add(newFmap);
                        }
                        currentFmaps[layerIndex] = currentResFmap;
                    }
                }
            }
            //if (_currentNoise == null)
            //{
            //    int i = 2;
            //    _currentNoise = new List<Tuple<string, float[,,]>>();
            //    foreach (var noiseTuple in noiseData)
            //    {
            //        float[,,] noiseShaped = new float[noiseTuple.Item2[1], noiseTuple.Item2[2], noiseTuple.Item2[3]];
            //        Buffer.BlockCopy(outputs[i].Data, 0, noiseShaped, 0, noiseTuple.Item2[1] * noiseTuple.Item2[2] * noiseTuple.Item2[3] * sizeof(float));
            //        _currentNoise.Add(new Tuple<string, float[,,]>(noiseTuple.Item1, noiseShaped));
            //        i++;
            //    }
            //}
            AppendLatentToImage(latent, path);
            return(path);
        }