///<summary>Выполняет стилизацию. Метод выполняется в потоке Stylizer.</summary> private void Stylize() { var content = IOConverters.ImageToTensor(this.ContentImage.Image as Bitmap); var style = IOConverters.ImageToTensor(this.StyleImage.Image as Bitmap); var content_features = encoder.Encode(content); var style_features = encoder.Encode(style); var Total = Layers.AdaIN(content_features, style_features); var Decoded = decoder.Decode(Total); this.ResultImage.Image = IOConverters.TensorToImage(Decoded); this.Stylized = true; this.Progress.Value = 0; this.TotalProgress = 0f; this.GenerateOrSaveResult.Text = "Сохранить"; this.GenerateOrSaveResult.Click -= this.StopProcess; this.GenerateOrSaveResult.Click += this.SaveResult; this.OpenContent.Enabled = true; this.OpenStyle.Enabled = true; }
public static Tensor Stylize(Encoder Encoder1, Decoder Decoder1, Encoder Encoder2, Decoder Decoder2, Encoder Encoder3, Decoder Decoder3, Encoder Encoder4, Decoder Decoder4, Encoder Encoder5, Decoder Decoder5, Tensor Content, Tensor Style, TransferMethod Method = TransferMethod.WCT, float alpha = 0.6f) { Encoder1.Step += FixStep; Decoder1.Step += FixStep; Encoder2.Step += FixStep; Decoder2.Step += FixStep; Encoder3.Step += FixStep; Decoder3.Step += FixStep; Encoder4.Step += FixStep; Decoder4.Step += FixStep; Encoder5.Step += FixStep; Decoder5.Step += FixStep; TotalSteps = 0f; LastPercent = 0; if (Method == TransferMethod.WCT) { // WCT 5 var Conv5 = Decoder5.Decode(Layers.WCT(Encoder5.Encode(Content), Encoder5.Encode(Style), alpha)); // WCT 4 var Conv4 = Decoder4.Decode(Layers.WCT(Encoder4.Encode(Conv5), Encoder4.Encode(Style), alpha)); // WCT 3 var Conv3 = Decoder3.Decode(Layers.WCT(Encoder3.Encode(Conv4), Encoder3.Encode(Style), alpha)); // WCT 2 var Conv2 = Decoder2.Decode(Layers.WCT(Encoder2.Encode(Conv3), Encoder2.Encode(Style), alpha)); // WCT 1 var Conv1 = Decoder1.Decode(Layers.WCT(Encoder1.Encode(Conv2), Encoder1.Encode(Style), alpha)); // Uninstall Encoder1.Step -= FixStep; Decoder1.Step -= FixStep; Encoder2.Step -= FixStep; Decoder2.Step -= FixStep; Encoder3.Step -= FixStep; Decoder3.Step -= FixStep; Encoder4.Step -= FixStep; Decoder4.Step -= FixStep; Encoder5.Step -= FixStep; Decoder5.Step -= FixStep; return(Conv1); } else { // AdaIN 5 var Conv5 = Decoder5.Decode(Layers.AdaIN(Encoder5.Encode(Content), Encoder5.Encode(Style), alpha)); // AdaIN 4 var Conv4 = Decoder4.Decode(Layers.AdaIN(Encoder4.Encode(Conv5), Encoder4.Encode(Style), alpha)); // AdaIN 3 var Conv3 = Decoder3.Decode(Layers.AdaIN(Encoder3.Encode(Conv4), Encoder3.Encode(Style), alpha)); // AdaIN 2 var Conv2 = Decoder2.Decode(Layers.AdaIN(Encoder2.Encode(Conv3), Encoder2.Encode(Style), alpha)); // AdaIN 1 var Conv1 = Decoder1.Decode(Layers.AdaIN(Encoder1.Encode(Conv2), Encoder1.Encode(Style), alpha)); // Uninstall Encoder1.Step -= FixStep; Decoder1.Step -= FixStep; Encoder2.Step -= FixStep; Decoder2.Step -= FixStep; Encoder3.Step -= FixStep; Decoder3.Step -= FixStep; Encoder4.Step -= FixStep; Decoder4.Step -= FixStep; Encoder5.Step -= FixStep; Decoder5.Step -= FixStep; return(Conv1); } }
///<summary>Выполняет прямой проход через декодирующую нейросеть.</summary> ///<param name="input">Входные данные.</param> public Tensor Decode(Tensor Content, Tensor[] Styles) { var Temp = Layers.StyleDecorator(Content, Styles[3]); Temp = Layers.Conv2D3x3(Temp, this.Data.Conv4_1_Weights, this.Data.Conv4_1_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Upsample2D(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Conv2D3x3(Temp, this.Data.Conv3_4_Weights, this.Data.Conv3_4_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Conv2D3x3(Temp, this.Data.Conv3_3_Weights, this.Data.Conv3_3_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Conv2D3x3(Temp, this.Data.Conv3_2_Weights, this.Data.Conv3_2_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.AdaIN(Temp, Styles[2]); Temp = Layers.Conv2D3x3(Temp, this.Data.Conv3_1_Weights, this.Data.Conv3_1_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Upsample2D(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Conv2D3x3(Temp, this.Data.Conv2_2_Weights, this.Data.Conv2_2_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.AdaIN(Temp, Styles[1]); Temp = Layers.Conv2D3x3(Temp, this.Data.Conv2_1_Weights, this.Data.Conv2_1_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Upsample2D(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Conv2D3x3(Temp, this.Data.Conv1_2_Weights, this.Data.Conv1_2_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.AdaIN(Temp, Styles[0]); Temp = Layers.Conv2D3x3(Temp, this.Data.Conv1_1_Weights, this.Data.Conv1_1_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.ReLU(Temp); if (this.Step != null) { this.Step(1f / 22f * 100f); } Temp = Layers.Conv2D7x7(Temp, this.Data.Conv_Out_Weights, this.Data.Conv_Out_Biases); if (this.Step != null) { this.Step(1f / 22f * 100f); } return(Temp); }