From 43ef464c097d7a64f9125745897a3e924753bcf5 Mon Sep 17 00:00:00 2001 From: Steffenhir <99601527+Steffenhir@users.noreply.github.com> Date: Sun, 20 Feb 2022 12:46:44 +0100 Subject: [PATCH 1/4] Revert "Feature/gpytorch gpr" --- .gitignore | 3 +- requirements.txt | 6 -- src/background_extraction.py | 20 +---- src/gpr_cuda.py | 151 ----------------------------------- src/gui.py | 9 --- 5 files changed, 2 insertions(+), 187 deletions(-) delete mode 100644 requirements.txt delete mode 100644 src/gpr_cuda.py diff --git a/.gitignore b/.gitignore index 38f5869..b57af1b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ *.tif *.png *.fits -__pycache__ -python-venv \ No newline at end of file +__pycache__ \ No newline at end of file diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 45c00d3..0000000 --- a/requirements.txt +++ /dev/null @@ -1,6 +0,0 @@ -astropy -gpytorch -Pillow -pykrige -scikit-image -scipy diff --git a/src/background_extraction.py b/src/background_extraction.py index 2f0084f..ba0c704 100644 --- a/src/background_extraction.py +++ b/src/background_extraction.py @@ -15,8 +15,6 @@ from pykrige.ok import OrdinaryKriging from skimage.transform import resize from skimage import img_as_float32 -from gpr_cuda import GPRegression - @@ -95,23 +93,10 @@ def interpol(x_sub,y_sub,subsample,shape,kind,smoothing): x_new = np.arange(0,shape[1],1).astype("float64") y_new = np.arange(0,shape[0],1).astype("float64") - result, var = OK.execute("grid", xpoints=x_new, ypoints=y_new, backend="C") return result - if(kind=='GPR_CUDA'): - # A likelihood in GPyTorch specifies the mapping from latent function values f(X) to observed labels y. - gpr = GPRegression( - x_sub=x_sub, - y_sub=y_sub, - subsample=subsample, - shape=shape - ) - result = gpr.run() - del gpr - return result - - + def downscale(imarray, background_points, downscale_factor): if downscale_factor == 1: @@ -123,6 +108,3 @@ def downscale(imarray, background_points, downscale_factor): - - - diff --git a/src/gpr_cuda.py b/src/gpr_cuda.py deleted file mode 100644 index fa432d6..0000000 --- a/src/gpr_cuda.py +++ /dev/null @@ -1,151 +0,0 @@ -import gpytorch -import numpy as np -import torch -from gpytorch.likelihoods import GaussianLikelihood -from skimage import img_as_uint -from torch.utils.data import DataLoader, TensorDataset - -gpytorch_available = True -cuda_available = torch.cuda.is_available() - -# cf. -# https://github.com/cornellius-gp/gpytorch/blob/master/examples/02_Scalable_Exact_GPs/Simple_GP_Regression_With_LOVE_Fast_Variances_and_Sampling.ipynb - -# The Deep RBF kernel (DKL) uses a neural network as an initial feature extractor. -class LargeFeatureExtractor(torch.nn.Sequential): - def __init__(self, input_dim): - super(LargeFeatureExtractor, self).__init__() - self.add_module("linear1", torch.nn.Linear(input_dim, 1000)) - self.add_module("relu1", torch.nn.ReLU()) - self.add_module("linear2", torch.nn.Linear(1000, 500)) - self.add_module("relu2", torch.nn.ReLU()) - self.add_module("linear3", torch.nn.Linear(500, 50)) - self.add_module("relu3", torch.nn.ReLU()) - self.add_module("linear4", torch.nn.Linear(50, 2)) - - -# Our GRP model for training and predictions -class GPRegressionModel(gpytorch.models.ExactGP): - def __init__(self, train_x, train_y, likelihood): - super(GPRegressionModel, self).__init__(train_x, train_y, likelihood) - - self.mean_module = gpytorch.means.ConstantMean() - self.covar_module = gpytorch.kernels.ScaleKernel( - gpytorch.kernels.RBFKernel(ard_num_dims=2) - + gpytorch.kernels.LinearKernel() - ) - self.feature_extractor = LargeFeatureExtractor(input_dim=train_x.size(-1)) - self.scale_to_bounds = gpytorch.utils.grid.ScaleToBounds(-1.0, 1.0) - - def forward(self, x): - # We're first putting our data through a deep net (feature extractor) - # We're also scaling the features so that they're nice values - projected_x = self.feature_extractor(x) - projected_x = self.scale_to_bounds(projected_x) - mean_x = self.mean_module(x) - covar_x = self.covar_module(x) - return gpytorch.distributions.MultivariateNormal(mean_x, covar_x) - - -class GPRegression: - def __init__(self, x_sub, y_sub, subsample, shape): - self.x_size = shape[1] - self.y_size = shape[0] - # map numpy arrays to normalized tensors - y_sub = torch.tensor(y_sub / self.y_size).float() - x_sub = torch.tensor(x_sub / self.x_size).float() - self.train_x = torch.cat( - ( - y_sub.contiguous().view(y_sub.numel(), 1), - x_sub.contiguous().view(x_sub.numel(), 1), - ), - dim=1, - ) - self.train_y = torch.tensor(subsample).float() - - self.likelihood = GaussianLikelihood() - self.model = GPRegressionModel( - self.train_x, self.train_y, likelihood=self.likelihood - ) - - def train(self, training_iterations=range(1000)): - # Find optimal model hyperparameters - self.model.train() - self.likelihood.train() - - # Use the SGD optimizer - optimizer = torch.optim.SGD( - self.model.parameters(), - lr=0.01, - momentum=0.9 - ) # Includes GaussianLikelihood parameters - - if torch.cuda.is_available: - self.train_x, self.train_y, self.likelihood, self.model = ( - self.train_x.cuda(), - self.train_y.cuda(), - self.likelihood.cuda(), - self.model.cuda(), - ) - else: - print( - "WARNING: pytorch has no CUDA support, cf. https://pytorch.org/get-started/locally/" - ) - - # "Loss" for GPs - the marginal log likelihood - mll = gpytorch.mlls.ExactMarginalLogLikelihood(self.likelihood, self.model) - - for i in training_iterations: - print("training iteration", i) - optimizer.zero_grad() - output = self.model(self.train_x) - loss = -mll(output, self.train_y) - loss.backward() - optimizer.step() - - def predict(self): - yv, xv = torch.meshgrid( - [ - torch.linspace(0, 1, self.y_size), - torch.linspace(0, 1, self.x_size), - ] - ) - test_x = torch.stack([yv, xv], -1).squeeze(1).contiguous() - test_y = torch.zeros(self.y_size, 1).contiguous() - - if torch.cuda.is_available: - test_x, test_y = (test_x.cuda(), test_y.cuda()) - else: - print( - "WARNING: pytorch has no CUDA support, cf. https://pytorch.org/get-started/locally/" - ) - - test_dataset = TensorDataset(test_x, test_y) - test_loader = DataLoader(test_dataset, batch_size=8, shuffle=False) - - print("calculating predictions") - - # Set into eval mode - self.model.eval() - self.likelihood.eval() - - means = torch.zeros(self.x_size, 1).reshape(1, -1) - with torch.no_grad(), gpytorch.settings.fast_pred_var(), gpytorch.settings.max_preconditioner_size( - 100 - ): - for x_batch, y_batch in test_loader: - print("processing batch", means.shape) - test_loader.batch_size - preds = self.likelihood(self.model(x_batch)) - means = torch.cat([means, preds.mean.cpu()]) - means = means[1:] - result = means.numpy() - - if torch.cuda.is_available(): - torch.cuda.empty_cache() - - return result - - def run(self): - self.train() - return self.predict() diff --git a/src/gui.py b/src/gui.py index e305085..9f5b1b7 100644 --- a/src/gui.py +++ b/src/gui.py @@ -72,20 +72,12 @@ def menu_intp_Splines_clicked(self): self.interpol_type = 'Splines' self.intp_type_text.configure(text="Method: " + self.interpol_type) - self.downscale_factor = 1 def menu_intp_Kriging_clicked(self): self.interpol_type = 'Kriging' self.intp_type_text.configure(text="Method: " + self.interpol_type) self.downscale_factor = 4 - - - def menu_intp_GPR_CUDA_clicked(self): - - self.interpol_type = 'GPR_CUDA' - self.intp_type_text.configure(text="Method: " + self.interpol_type) - self.downscale_factor = 1 def create_menu(self): @@ -109,7 +101,6 @@ def create_menu(self): self.interpolation_menu.add_command(label="RBF", command = self.menu_intp_RBF_clicked) self.interpolation_menu.add_command(label="Splines", command = self.menu_intp_Splines_clicked) self.interpolation_menu.add_command(label="Kriging", command = self.menu_intp_Kriging_clicked) - self.interpolation_menu.add_command(label="GPR-CUDA", command = self.menu_intp_GPR_CUDA_clicked) self.master.config(menu=self.menu_bar) From aa8e31a9d731fd1405c971cfe3647573d2c9928b Mon Sep 17 00:00:00 2001 From: David Schmelter Date: Thu, 24 Feb 2022 21:56:00 +0100 Subject: [PATCH 2/4] fix release tag in build-release.yml --- .github/workflows/build-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 5683a64..58a7249 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -110,7 +110,7 @@ jobs: - name: create release uses: softprops/action-gh-release@v1 with: - tag_name: v0.0.1 + tag_name: v0.0.2 files: | background-extraction-linux background-extraction-win64.exe From b05ee594c516a6d824710a65f9d121d9bf775c98 Mon Sep 17 00:00:00 2001 From: Steffen Hirtle Date: Mon, 7 Mar 2022 16:57:59 +0100 Subject: [PATCH 3/4] Prepare third snapshot release --- .github/workflows/build-release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index 58a7249..c268ffc 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -110,7 +110,7 @@ jobs: - name: create release uses: softprops/action-gh-release@v1 with: - tag_name: v0.0.2 + tag_name: v0.0.3 files: | background-extraction-linux background-extraction-win64.exe From ce7bb40ab01b79c67d3acd9e2697dc90e3483d8e Mon Sep 17 00:00:00 2001 From: Steffen Hirtle Date: Mon, 7 Mar 2022 17:06:22 +0100 Subject: [PATCH 4/4] Prepare third snapshot release --- src/skyall.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/skyall.py b/src/skyall.py index c320bc2..dc780a0 100644 --- a/src/skyall.py +++ b/src/skyall.py @@ -1,5 +1,4 @@ import numpy as np -import matplotlib.pyplot as plt from skimage import io, img_as_float32 """