From f14df82dc5c01cf5e6d834ded378b1af9b484c7b Mon Sep 17 00:00:00 2001 From: Raphael Attias Date: Wed, 11 Jan 2023 14:00:33 -0500 Subject: [PATCH 1/5] Removed deprecated torch.lstsq, replaced by torch.linalg.lstsq --- .vscode/launch.json | 16 ++++++++++++++++ example.py | 3 --- torchstain/torch/normalizers/macenko.py | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..534cb81 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Utilisez IntelliSense pour en savoir plus sur les attributs possibles. + // Pointez pour afficher la description des attributs existants. + // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python : fichier actif", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "justMyCode": true + } + ] +} \ No newline at end of file diff --git a/example.py b/example.py index 6de0f36..7a733f2 100644 --- a/example.py +++ b/example.py @@ -21,8 +21,6 @@ torch_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='torch') torch_normalizer.fit(T(target)) -tf_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='tensorflow') -tf_normalizer.fit(T(target)) t_to_transform = T(to_transform) @@ -81,7 +79,6 @@ plt.show() t_ = time.time() -norm, H, E = tf_normalizer.normalize(I=t_to_transform, stains=True) print("tf runtime:", time.time() - t_) plt.figure() diff --git a/torchstain/torch/normalizers/macenko.py b/torchstain/torch/normalizers/macenko.py index 710b7e3..c3917a5 100644 --- a/torchstain/torch/normalizers/macenko.py +++ b/torchstain/torch/normalizers/macenko.py @@ -49,7 +49,7 @@ def __find_concentration(self, OD, HE): Y = OD.T # determine concentrations of the individual stains - return torch.lstsq(Y, HE)[0][:2] + return torch.linalg.lstsq(HE, Y)[0] def __compute_matrices(self, I, Io, alpha, beta): OD, ODhat = self.__convert_rgb2od(I, Io=Io, beta=beta) From 6883215ebc4d144bbe5788d4aa8405ad219001f1 Mon Sep 17 00:00:00 2001 From: Raphael Attias Date: Wed, 11 Jan 2023 15:19:41 -0500 Subject: [PATCH 2/5] removed .vscode, revert change to example.py and updated min version of Torch to 1.9 --- .github/workflows/tests_full.yml | 2 +- .vscode/launch.json | 16 ---------------- example.py | 4 +++- 3 files changed, 4 insertions(+), 18 deletions(-) delete mode 100644 .vscode/launch.json diff --git a/.github/workflows/tests_full.yml b/.github/workflows/tests_full.yml index e550c88..ee9cb06 100644 --- a/.github/workflows/tests_full.yml +++ b/.github/workflows/tests_full.yml @@ -73,7 +73,7 @@ jobs: matrix: os: [ windows-2019, ubuntu-18.04, macos-11 ] python-version: [ 3.6, 3.7, 3.8, 3.9 ] - pytorch-version: [1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.12.0] + pytorch-version: [1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.13.0] exclude: - python-version: 3.6 pytorch-version: 1.11.0 diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 534cb81..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - // Utilisez IntelliSense pour en savoir plus sur les attributs possibles. - // Pointez pour afficher la description des attributs existants. - // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Python : fichier actif", - "type": "python", - "request": "launch", - "program": "${file}", - "console": "integratedTerminal", - "justMyCode": true - } - ] -} \ No newline at end of file diff --git a/example.py b/example.py index 7a733f2..1208721 100644 --- a/example.py +++ b/example.py @@ -21,6 +21,8 @@ torch_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='torch') torch_normalizer.fit(T(target)) +tf_normalizer = torchstain.normalizers.MacenkoNormalizer(backend='tensorflow') +tf_normalizer.fit(T(target)) t_to_transform = T(to_transform) @@ -80,7 +82,7 @@ t_ = time.time() print("tf runtime:", time.time() - t_) - +norm, H, E = tf_normalizer.normalize(I=t_to_transform, stains=True) plt.figure() plt.suptitle('tensorflow normalizer') plt.subplot(2, 2, 1) From 26091e408f3f96c20baec78ef64d3cc8250083ba Mon Sep 17 00:00:00 2001 From: Raphael Attias Date: Wed, 11 Jan 2023 15:24:11 -0500 Subject: [PATCH 3/5] fix example --- example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.py b/example.py index 1208721..98ba5b9 100644 --- a/example.py +++ b/example.py @@ -81,8 +81,8 @@ plt.show() t_ = time.time() -print("tf runtime:", time.time() - t_) norm, H, E = tf_normalizer.normalize(I=t_to_transform, stains=True) +print("tf runtime:", time.time() - t_) plt.figure() plt.suptitle('tensorflow normalizer') plt.subplot(2, 2, 1) From c5b715ca6f5df605cf2381a4becaa40112a0b20e Mon Sep 17 00:00:00 2001 From: Raphael Attias Date: Wed, 11 Jan 2023 15:42:10 -0500 Subject: [PATCH 4/5] add torch version checking for backward compatibility --- .github/workflows/tests_full.yml | 2 +- torchstain/torch/normalizers/macenko.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests_full.yml b/.github/workflows/tests_full.yml index ee9cb06..0865876 100644 --- a/.github/workflows/tests_full.yml +++ b/.github/workflows/tests_full.yml @@ -73,7 +73,7 @@ jobs: matrix: os: [ windows-2019, ubuntu-18.04, macos-11 ] python-version: [ 3.6, 3.7, 3.8, 3.9 ] - pytorch-version: [1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.13.0] + pytorch-version: [1.8.0, 1.9.0, 1.10.0, 1.11.0, 1.12.0, 1.13.0] exclude: - python-version: 3.6 pytorch-version: 1.11.0 diff --git a/torchstain/torch/normalizers/macenko.py b/torchstain/torch/normalizers/macenko.py index c3917a5..43c0c5f 100644 --- a/torchstain/torch/normalizers/macenko.py +++ b/torchstain/torch/normalizers/macenko.py @@ -49,7 +49,11 @@ def __find_concentration(self, OD, HE): Y = OD.T # determine concentrations of the individual stains - return torch.linalg.lstsq(HE, Y)[0] + + if torch.__version__ >= (1,9,0): + return torch.linalg.lstsq(HE, Y)[0] + else: + return torch.lstsq(Y, HE)[0][:2] def __compute_matrices(self, I, Io, alpha, beta): OD, ODhat = self.__convert_rgb2od(I, Io=Io, beta=beta) From 2b4dd90057beef28a820cae963e22ddf96ec84e6 Mon Sep 17 00:00:00 2001 From: Raphael Attias Date: Tue, 17 Jan 2023 09:55:42 -0500 Subject: [PATCH 5/5] Added torch version check in the __init__ Updated eigen vectors solver --- torchstain/torch/normalizers/macenko.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/torchstain/torch/normalizers/macenko.py b/torchstain/torch/normalizers/macenko.py index 43c0c5f..b04cd28 100644 --- a/torchstain/torch/normalizers/macenko.py +++ b/torchstain/torch/normalizers/macenko.py @@ -14,7 +14,8 @@ def __init__(self): [0.7201, 0.8012], [0.4062, 0.5581]]) self.maxCRef = torch.tensor([1.9705, 1.0308]) - + self.deprecated_torch = torch.__version__ < (1,9,0) + def __convert_rgb2od(self, I, Io, beta): I = I.permute(1, 2, 0) @@ -49,17 +50,16 @@ def __find_concentration(self, OD, HE): Y = OD.T # determine concentrations of the individual stains - - if torch.__version__ >= (1,9,0): - return torch.linalg.lstsq(HE, Y)[0] - else: + if self.deprecated_torch: return torch.lstsq(Y, HE)[0][:2] + + return torch.linalg.lstsq(HE, Y)[0] def __compute_matrices(self, I, Io, alpha, beta): OD, ODhat = self.__convert_rgb2od(I, Io=Io, beta=beta) # compute eigenvectors - _, eigvecs = torch.symeig(cov(ODhat.T), eigenvectors=True) + _, eigvecs = torch.linalg.eigh(cov(ODhat.T)) eigvecs = eigvecs[:, [1, 2]] HE = self.__find_HE(ODhat, eigvecs, alpha)