-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Morris
committed
Aug 2, 2024
1 parent
602a3e0
commit 402bfe8
Showing
7 changed files
with
194 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .jepa_base import JEPABase | ||
from .jepa_base import JEPABase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,57 @@ | ||
import torch.nn as nn | ||
|
||
from cyto_dl.models.jepa import JEPABase | ||
|
||
|
||
class IJEPA(JEPABase): | ||
def __init__( | ||
self, | ||
*, | ||
encoder: nn.Module, | ||
predictor: nn.Module, | ||
x_key: str, | ||
save_dir: str= './', | ||
momentum: float=0.998, | ||
max_epochs: int=100, | ||
self, | ||
*, | ||
encoder: nn.Module, | ||
predictor: nn.Module, | ||
x_key: str, | ||
save_dir: str = "./", | ||
momentum: float = 0.998, | ||
max_epochs: int = 100, | ||
**base_kwargs, | ||
): | ||
"""JEPA for self-supervised learning on 2D and 3D images. | ||
Parameters | ||
---------- | ||
encoder : nn.Module | ||
The encoder module used for feature extraction. | ||
predictor : nn.Module | ||
The predictor module used for generating predictions. | ||
x_key : str | ||
The key used to access the input data. | ||
momentum : float, optional | ||
The momentum value for the exponential moving average of the model weights (default is 0.998). | ||
max_epochs : int, optional | ||
The maximum number of training epochs (default is 100). | ||
**base_kwargs : dict | ||
Additional arguments passed to the BaseModel. | ||
""" | ||
super().__init__( | ||
encoder=encoder, | ||
predictor=predictor, | ||
x_key=x_key, | ||
save_dir=save_dir, | ||
momentum=momentum, | ||
max_epochs=max_epochs, | ||
**base_kwargs, | ||
): | ||
""" | ||
Initialize the IJEPA model. | ||
Parameters | ||
---------- | ||
encoder : nn.Module | ||
The encoder module used for feature extraction. | ||
predictor : nn.Module | ||
The predictor module used for generating predictions. | ||
x_key : str | ||
The key used to access the input data. | ||
momentum : float, optional | ||
The momentum value for the exponential moving average of the model weights (default is 0.998). | ||
max_epochs : int, optional | ||
The maximum number of training epochs (default is 100). | ||
**base_kwargs : dict | ||
Additional arguments passed to the BaseModel. | ||
""" | ||
super().__init__(encoder=encoder, predictor=predictor, x_key=x_key, save_dir=save_dir, momentum=momentum, max_epochs=max_epochs, **base_kwargs) | ||
) | ||
|
||
def model_step(self, stage, batch, batch_idx): | ||
self.update_teacher() | ||
input=batch[self.hparams.x_key] | ||
input = batch[self.hparams.x_key] | ||
|
||
target_masks = self.get_mask(batch, 'target_mask') | ||
context_masks = self.get_mask(batch, 'context_mask') | ||
target_masks = self.get_mask(batch, "target_mask") | ||
context_masks = self.get_mask(batch, "context_mask") | ||
|
||
target_embeddings = self.get_target_embeddings(input, target_masks) | ||
context_embeddings = self.get_context_embeddings(input, context_masks) | ||
predictions= self.predictor(context_embeddings, target_masks) | ||
predictions = self.predictor(context_embeddings, target_masks) | ||
|
||
loss = self.loss(predictions, target_embeddings) | ||
return loss, None, None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.