Ejemplo n.º 1
0
        ///<summary>Реализует слой StyleSwap.</summary>
        ///<param name="Content">Карты признаков контентного изображения.</param>
        ///<param name="Style">Карты признаков стилевого изображения.</param>
        public static Tensor StyleSwap(Tensor Content, Tensor Style, int patch_size = 3, int stride = 2)
        {
            var style_kernels      = extract_image_patches(Style, patch_size, stride);
            var style_kernels_norm = l2_norm(style_kernels);
            var ss_enc             = Layers.Conv2D(Content, style_kernels_norm, stride);
            var ss_argmax          = argmax(ss_enc);
            var encC        = ss_enc.Depth;
            var ss_oh       = one_hot(ss_argmax, encC);
            var ss_dec      = Layers.ConvTranspose2D(ss_oh, style_kernels, stride);
            var ss_oh_sum   = reduce_sum(ss_oh);
            var filter_ones = new Tensor[1] {
                ones(patch_size, patch_size, stride)
            };
            var counting = Layers.ConvTranspose2D(ss_oh_sum, filter_ones, stride);

            counting = tile(counting, Content.Depth);
            return(divide(ss_dec, counting));
        }
        ///<summary>Выполняет прямой проход через сеть-трансформер.</summary>
        ///<param name="input">Входные данные.</param>
        public Tensor Stylize(Tensor input)
        {
            var Temp = Layers.Conv2D(input, this.Data.Conv1_Weights, 1);

            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.Conv1_Shift, this.Data.Conv1_Scale);
            Temp = Layers.ReLU(Temp);
            Temp = Layers.Conv2D(Temp, this.Data.Conv2_Weights, 2);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.Conv2_Shift, this.Data.Conv2_Scale);
            Temp = Layers.ReLU(Temp);
            Temp = Layers.Conv2D(Temp, this.Data.Conv3_Weights, 2);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.Conv3_Shift, this.Data.Conv3_Scale);
            Temp = Layers.ReLU(Temp);
            Temp = Layers.ResBlock(Temp, this.Data.ResBlock1);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.ResBlock(Temp, this.Data.ResBlock2);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.ResBlock(Temp, this.Data.ResBlock3);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.ResBlock(Temp, this.Data.ResBlock4);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.ResBlock(Temp, this.Data.ResBlock5);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv1_Weights, 2);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv1_Shift, this.Data.TConv1_Scale);
            Temp = Layers.ReLU(Temp);
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv2_Weights, 2);
            if (Step != null)
            {
                Step(10);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv2_Shift, this.Data.TConv2_Scale);
            Temp = Layers.ReLU(Temp);
            Temp = Layers.Conv2D(Temp, this.Data.TConv3_Weights, 1);
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv3_Shift, this.Data.TConv3_Scale);
            Temp = Layers.Tanh(Temp);
            return(Temp);
        }
Ejemplo n.º 3
0
        ///<summary>Выполняет прямой проход через декодирующую нейросеть.</summary>
        ///<param name="input">Входные данные.</param>
        public Tensor Decode(Tensor Content, Tensor Style)
        {
            var Temp = Layers.StyleSwap(Content, Style);

            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv3_1_Weights, this.Data.TConv3_1_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv3_1_Shift, this.Data.TConv3_1_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv2_4_Weights, this.Data.TConv2_4_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv2_4_Shift, this.Data.TConv2_4_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.Upsample2D(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv2_3_Weights, this.Data.TConv2_3_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv2_3_Shift, this.Data.TConv2_3_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv2_2_Weights, this.Data.TConv2_2_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv2_2_Shift, this.Data.TConv2_2_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv2_1_Weights, this.Data.TConv2_1_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv2_1_Shift, this.Data.TConv2_1_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv1_4_Weights, this.Data.TConv1_4_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv1_4_Shift, this.Data.TConv1_4_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.Upsample2D(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv1_3_Weights, this.Data.TConv1_3_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv1_3_Shift, this.Data.TConv1_3_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv1_2_Weights, this.Data.TConv1_2_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.InstanceNorm2D(Temp, this.Data.TConv1_2_Shift, this.Data.TConv1_2_Scale);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ReLU(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.ConvTranspose2D(Temp, this.Data.TConv1_1_Weights, this.Data.TConv1_1_Biases, 1);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            Temp = Layers.Sigmoid(Temp);
            if (this.Step != null)
            {
                this.Step(1f / 29f * 100f);
            }
            return(Temp);
        }