unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: Liliana Marie Prikler <liliana.prikler@gmail.com>
To: 59607@debbugs.gnu.org
Subject: [bug#59607] [PATCH 7/8] gnu: Add python-gfpgan.
Date: Sat, 26 Nov 2022 11:46:23 +0100	[thread overview]
Message-ID: <9f038ba9a340cb10fea00673080c5d67d0a71cb9.camel@gmail.com> (raw)
In-Reply-To: <a9bdd1f5c70f0879615809184653358d7f07def3.camel@gmail.com>

* gnu/packages/patches/python-gfpgan-unfuse-leaky-relu.patch: New file.
* gnu/local.mk (dist_patch_DATA): Register it here.
* gnu/packages/machine-learning.scm (python-gfpgan): New variable.
---
 gnu/local.mk                                  |  1 +
 gnu/packages/machine-learning.scm             | 71 +++++++++++++++++++
 .../python-gfpgan-unfuse-leaky-relu.patch     | 57 +++++++++++++++
 3 files changed, 129 insertions(+)
 create mode 100644 gnu/packages/patches/python-gfpgan-unfuse-leaky-relu.patch

diff --git a/gnu/local.mk b/gnu/local.mk
index 8dd1abe07a..9312bec6d4 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1729,6 +1729,7 @@ dist_patch_DATA =						\
   %D%/packages/patches/python-execnet-read-only-fix.patch	\
   %D%/packages/patches/python-fixtures-remove-monkeypatch-test.patch	\
   %D%/packages/patches/python-flask-restful-werkzeug-compat.patch	\
+  %D%/packages/patches/python-gfpgan-unfuse-leaky-relu.patch	\
   %D%/packages/patches/python-ipython-documentation-chars.patch	\
   %D%/packages/patches/python-ipython-documentation-repro.patch	\
   %D%/packages/patches/python-keras-integration-test.patch	\
diff --git a/gnu/packages/machine-learning.scm b/gnu/packages/machine-learning.scm
index 6116834578..73263962f5 100644
--- a/gnu/packages/machine-learning.scm
+++ b/gnu/packages/machine-learning.scm
@@ -859,6 +859,77 @@ (define-public python-facexlib
                    license:asl2.0
                    license:expat))))
 
+(define-public python-gfpgan
+  (package
+    (name "python-gfpgan")
+    (version "1.3.8")
+    (source (origin
+              (method git-fetch)
+              (uri (git-reference
+                    (url "https://github.com/TencentARC/GFPGAN")
+                    (commit (string-append "v" version))))
+              (file-name (git-file-name name version))
+              (patches (search-patches
+                        "python-gfpgan-unfuse-leaky-relu.patch"))
+              (modules '((guix build utils)))
+              (snippet
+               #~(begin (substitute* (find-files "." "\\.py$")
+                          (("\\.cuda\\(\\)") "")
+                          (("torch\\.cuda\\.is_available\\(\\)") "False")
+                          (("'cuda' if False else ") "")
+                          (("'cuda'") "'cpu'"))))
+              (sha256
+               (base32
+                "1s2wn3r4h1if35yvacaihk2r9fswdh16nxgh229g07p2562pgcky"))))
+    (build-system python-build-system)
+    (arguments
+     (list
+      #:phases
+      #~(modify-phases %standard-phases
+          (add-after 'unpack 'fix-api
+            (lambda _
+              (substitute* (find-files "." "\\.py$")
+                (("from basicsr\\.losses\\.losses import .*")
+                 "import basicsr.losses\n")
+                (("GANLoss")
+                 "basicsr.losses.gan_loss.GANLoss")
+                (("(L1|Perceptual)Loss" loss)
+                 (string-append "basicsr.losses.basic_loss." loss)))))
+          (add-after 'unpack 'fix-requirements
+            (lambda _
+              (substitute* "requirements.txt"
+                (("opencv-python") "")  ; installed without egg-info
+                (("tb-nightly") ""))))
+          (add-before 'check 'delete-failing-tests
+            (lambda _
+              ;; XXX: Possibly genuine errors
+              (delete-file "tests/test_ffhq_degradation_dataset.py")
+              (delete-file "tests/test_gfpgan_model.py")
+              ;; Test requires pretrained model.
+              (delete-file "tests/test_utils.py")))
+          (replace 'check
+            (lambda* (#:key tests? #:allow-other-keys)
+              (when tests?
+                (invoke "pytest" "-vv")))))))
+    (propagated-inputs (list opencv
+                             python-basicsr
+                             python-facexlib
+                             python-lmdb
+                             python-numpy
+                             python-pyyaml
+                             python-scipy
+                             python-pytorch
+                             python-torchvision
+                             python-tqdm
+                             python-yapf))
+    (native-inputs (list python-cython python-pytest))
+    (home-page "https://github.com/TencentARC/GFPGAN")
+    (synopsis "Restore images of faces")
+    (description "GFPGAN uses generative adversarial networks to remove
+degradations, such as blur, noise, or compression artifacts, from facial
+images.")
+    (license license:asl2.0)))
+
 (define-public ncnn
   (package
     (name "ncnn")
diff --git a/gnu/packages/patches/python-gfpgan-unfuse-leaky-relu.patch b/gnu/packages/patches/python-gfpgan-unfuse-leaky-relu.patch
new file mode 100644
index 0000000000..cd09312676
--- /dev/null
+++ b/gnu/packages/patches/python-gfpgan-unfuse-leaky-relu.patch
@@ -0,0 +1,57 @@
+diff --git a/gfpgan/archs/gfpganv1_arch.py b/gfpgan/archs/gfpganv1_arch.py
+index eaf3162..34ae5a2 100644
+--- a/gfpgan/archs/gfpganv1_arch.py
++++ b/gfpgan/archs/gfpganv1_arch.py
+@@ -3,7 +3,6 @@ import random
+ import torch
+ from basicsr.archs.stylegan2_arch import (ConvLayer, EqualConv2d, EqualLinear, ResBlock, ScaledLeakyReLU,
+                                           StyleGAN2Generator)
+-from basicsr.ops.fused_act import FusedLeakyReLU
+ from basicsr.utils.registry import ARCH_REGISTRY
+ from torch import nn
+ from torch.nn import functional as F
+@@ -170,10 +169,7 @@ class ConvUpLayer(nn.Module):
+ 
+         # activation
+         if activate:
+-            if bias:
+-                self.activation = FusedLeakyReLU(out_channels)
+-            else:
+-                self.activation = ScaledLeakyReLU(0.2)
++            self.activation = ScaledLeakyReLU(0.2)
+         else:
+             self.activation = None
+ 
+diff --git a/gfpgan/archs/stylegan2_bilinear_arch.py b/gfpgan/archs/stylegan2_bilinear_arch.py
+index 1342ee3..5cffb44 100644
+--- a/gfpgan/archs/stylegan2_bilinear_arch.py
++++ b/gfpgan/archs/stylegan2_bilinear_arch.py
+@@ -1,7 +1,6 @@
+ import math
+ import random
+ import torch
+-from basicsr.ops.fused_act import FusedLeakyReLU, fused_leaky_relu
+ from basicsr.utils.registry import ARCH_REGISTRY
+ from torch import nn
+ from torch.nn import functional as F
+@@ -190,7 +189,7 @@ class StyleConv(nn.Module):
+             sample_mode=sample_mode,
+             interpolation_mode=interpolation_mode)
+         self.weight = nn.Parameter(torch.zeros(1))  # for noise injection
+-        self.activate = FusedLeakyReLU(out_channels)
++        self.activate = ScaledLeakyReLU()
+ 
+     def forward(self, x, style, noise=None):
+         # modulate
+@@ -568,10 +567,7 @@ class ConvLayer(nn.Sequential):
+                 and not activate))
+         # activation
+         if activate:
+-            if bias:
+-                layers.append(FusedLeakyReLU(out_channels))
+-            else:
+-                layers.append(ScaledLeakyReLU(0.2))
++            layers.append(ScaledLeakyReLU(0.2))
+ 
+         super(ConvLayer, self).__init__(*layers)
+ 
-- 
2.38.1





      parent reply	other threads:[~2022-11-26 12:13 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-26 11:47 [bug#59607] [PATCH 0/8] Upscale your anime pictures, now with 99% less malware Liliana Marie Prikler
2022-11-19 23:14 ` [bug#59607] [PATCH 1/8] gnu: Add ncnn Liliana Marie Prikler
2022-11-20  1:16 ` [bug#59607] [PATCH 2/8] gnu: Add real-esrgan-ncnn Liliana Marie Prikler
2022-11-20  9:10 ` [bug#59607] [PATCH 3/8] gnu: Add python-addict Liliana Marie Prikler
2022-11-20 16:25 ` [bug#59607] [PATCH 4/8] gnu: Add python-basicsr Liliana Marie Prikler
2022-11-22 20:55 ` [bug#59607] [PATCH 8/8] gnu: Add python-real-esrgan Liliana Marie Prikler
2022-11-25 20:37 ` [bug#59607] [PATCH 5/8] gnu: Add python-filterpy Liliana Marie Prikler
2022-11-25 22:31 ` [bug#59607] [PATCH 6/8] gnu: Add python-facexlib Liliana Marie Prikler
2022-11-26 10:46 ` Liliana Marie Prikler [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9f038ba9a340cb10fea00673080c5d67d0a71cb9.camel@gmail.com \
    --to=liliana.prikler@gmail.com \
    --cc=59607@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).