-
Notifications
You must be signed in to change notification settings - Fork 28
Add support for training trackastra with SAM2 features #61
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
base: main
Are you sure you want to change the base?
Changes from all commits
e40fdfa
71e365d
f1e8fcd
6de6013
64003ba
d7b8448
118fdbc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -208,7 +208,22 @@ def _common_step(self, batch, eps=torch.finfo(torch.float32).eps): | |
| padding_mask = batch["padding_mask"] | ||
| padding_mask = padding_mask.bool() | ||
|
|
||
| A_pred = self.model(coords, feats, padding_mask=padding_mask) | ||
| pretrained_feats = batch.get("pretrained_feats", None) | ||
| if pretrained_feats is not None and pretrained_feats.numel() > 0: | ||
| pretrained_feats = pretrained_feats.to(coords.device) | ||
| else: | ||
| pretrained_feats = None | ||
|
|
||
| if pretrained_feats is not None: | ||
| A_pred = self.model( | ||
| coords, | ||
| feats, | ||
| pretrained_features=pretrained_feats, | ||
| padding_mask=padding_mask, | ||
| ) | ||
| else: | ||
| A_pred = self.model(coords, feats, padding_mask=padding_mask) | ||
|
|
||
| # remove inf values that might happen due to float16 numerics | ||
| A_pred.clamp_(torch.finfo(torch.float16).min, torch.finfo(torch.float16).max) | ||
|
|
||
|
|
@@ -632,7 +647,8 @@ def on_validation_end(self, trainer, pl_module): | |
|
|
||
| def create_run_name(args): | ||
| timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") | ||
| # name = f"{timestamp}_{args.name}_feats_{args.features}_pos_{args.attn_positional_bias}_causal_norm_{args.causal_norm}" | ||
| # name = f"{timestamp}_{args.name}_feats_{args.features}_pos_" + \ | ||
| # f"{args.attn_positional_bias}_causal_norm_{args.causal_norm}" | ||
| if args.timestamp: | ||
| name = f"{timestamp}_{args.name}" | ||
| else: | ||
|
|
@@ -817,6 +833,9 @@ def train(args): | |
| sanity_dist=args.sanity_dist, | ||
| crop_size=args.crop_size, | ||
| compress=args.compress, | ||
| pretrained_feats_model=args.pretrained_feats_model, | ||
| pretrained_feats_mode=args.pretrained_feats_mode, | ||
| pretrained_feats_additional_props=args.pretrained_feats_additional_props, | ||
| ) | ||
| sampler_kwargs = dict( | ||
| batch_size=args.batch_size, | ||
|
|
@@ -920,7 +939,8 @@ def train(args): | |
| # Compiling does not work! | ||
| # model_lightning = torch.compile(model_lightning) | ||
|
|
||
| # if logdir already exists and --resume option is set, load the last checkpoint (eg when continuing training after crash) | ||
| # if logdir already exists and --resume option is set, | ||
| # load the last checkpoint (eg when continuing training after crash) | ||
| if logdir is not None and logdir.exists() and args.resume: | ||
| logging.info("logdir exists, loading last state of model") | ||
| fpath = model_lightning.checkpoint_path(logdir) | ||
|
|
@@ -1065,9 +1085,69 @@ def parse_train_args(): | |
| "patch", | ||
| "patch_regionprops", | ||
| "wrfeat", | ||
| "pretrained_feats", | ||
| "pretrained_feats_aug", | ||
| ], | ||
| default="wrfeat", | ||
| ) | ||
| parser.add_argument( | ||
| "--pretrained_feats_model", | ||
| type=str, | ||
| default=None, | ||
| help="Model name for pretrained feature extraction (e.g. facebook/sam2.1-hiera-base-plus)", | ||
| ) | ||
| parser.add_argument( | ||
| "--pretrained_feats_mode", | ||
| type=str, | ||
| default="mean_patches_exact", | ||
| help="Pooling mode for pretrained features", | ||
| ) | ||
| parser.add_argument( | ||
| "--pretrained_feats_additional_props", | ||
| type=none_or_str, | ||
| default=None, | ||
| help="Additional region properties to concatenate with pretrained features (e.g. regionprops_small)", | ||
| ) | ||
| parser.add_argument( | ||
| "--pretrained_n_augs", | ||
| type=int, | ||
| default=15, | ||
| help="Number of augmented dataset copies to create for pretrained features extraction", | ||
| ) | ||
| parser.add_argument( | ||
| "--reduced_pretrained_feat_dim", | ||
| type=int, | ||
| default=None, | ||
| help="Reduce pretrained feature dimension via PCA to this size", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it does not look like you did explicitely re-implement the PCA dimred I used at some point (but that never made it into the final pipeline), I think this refers to the dim of pretrained features after a single FCL as in https://github.com/C-Achard/trackastra/blob/a238b2cadc8e3b954c4af4afeba6df8faf18be71/trackastra/model/model.py#L296. |
||
| ) | ||
| parser.add_argument( | ||
| "--rotate_features", | ||
| type=str2bool, | ||
| default=True, | ||
| help="Apply feature disambiguation to pretrained features based on coordinates to mitigate overfitting and avoid proximity-induced ambiguity in pretrained features", | ||
| ) | ||
|
anwai98 marked this conversation as resolved.
|
||
| parser.add_argument( | ||
| "--disable_all_coords", | ||
| type=str2bool, | ||
| default=False, | ||
| ) | ||
| parser.add_argument( | ||
| "--disable_xy_coords", | ||
| type=str2bool, | ||
| default=False, | ||
| ) | ||
| parser.add_argument( | ||
| "--pretrained_model_path", | ||
| type=none_or_str, | ||
| default=None, | ||
| help="Path to a local pretrained model folder (overrides --model for loading weights)", | ||
| ) | ||
| parser.add_argument( | ||
| "--weight_decay", | ||
| type=float, | ||
| default=0.0, | ||
| help="AdamW weight decay", | ||
| ) | ||
| parser.add_argument( | ||
| "--causal_norm", | ||
| type=str, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| # ruff: noqa: F401 | ||
|
|
||
| # Core data utilities (no training dependencies required) | ||
| from .data import ( | ||
| CTCData, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| # ruff: noqa: F401 | ||
|
|
||
| import os | ||
|
|
||
| from .model import TrackingTransformer | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| # ruff: noqa: F401 | ||
|
|
||
| from .track_graph import TrackGraph | ||
| from .tracking import ( | ||
| build_graph, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,3 @@ | ||
| # ruff: noqa: F401 | ||
|
|
||
| from .utils import ( | ||
| blockwise_causal_norm, | ||
| blockwise_sum, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note : this may be a bit high, depending on the dataset size. I'd recommend starting with much lower values and leaning on the feature disambiguation to avoid overfitting