Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug: noise model not training on GPU #360

Merged
merged 2 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/careamics/models/lvae/noise_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class GaussianMixtureNoiseModel(nn.Module):
# TODO training a NM relies on getting a clean data(N2V e.g,)
def __init__(self, config: GaussianMixtureNMConfig) -> None:
super().__init__()
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.device = torch.device("cpu")

if config.path is not None:
params = np.load(config.path)
Expand Down Expand Up @@ -319,10 +319,8 @@ def _set_model_mode(self, mode: str) -> None:
"""Move parameters to the device and set weights' requires_grad depending on the mode"""
if mode == "train":
self.weight.requires_grad = True
self.to_device(self.device)
else:
self.weight.requires_grad = False
self.to_device(torch.device("cpu"))

def polynomial_regressor(
self, weight_params: torch.Tensor, signals: torch.Tensor
Expand Down Expand Up @@ -548,6 +546,8 @@ def fit(
Upper percentile for clipping. Default is 100.
"""
self._set_model_mode(mode="train")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.to_device(device)
optimizer = torch.optim.Adam([self.weight], lr=learning_rate)

sig_obs_pairs = self.get_signal_observation_pairs(
Expand Down Expand Up @@ -589,6 +589,7 @@ def fit(
counter += 1

self._set_model_mode(mode="prediction")
self.to_device(torch.device("cpu"))
print("===================\n")
return train_losses

Expand Down
Loading