Skip to content

Commit db3bcbe

Browse files
committed
create script for dry-running all example tasks
1 parent 454773a commit db3bcbe

File tree

12 files changed

+125
-49
lines changed

12 files changed

+125
-49
lines changed

ncalab/models/applications/classification/classificationNCAhead.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def forward(self, x):
3434
return self.classifier(x)
3535

3636
def freeze(self, freeze_last: bool = False):
37-
layers = self.classifier
37+
layers = [L for L in self.classifier.modules()]
3838
if not freeze_last:
39-
layers = self.classifier[:-1]
39+
layers = layers[:-1]
4040
for layer in layers:
4141
layer.requires_grad_(False)

ncalab/models/wrappers/cascadeNCA.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Dict, Optional, Tuple, List
1+
import copy
2+
from typing import Dict, List, Optional, Tuple
23

34
import numpy as np
45
import torch # type: ignore[import-untyped]
@@ -115,11 +116,11 @@ def __init__(
115116
self.steps = steps
116117

117118
self.single_model = single_model
118-
self.models: nn.ModuleList | List[BasicNCAModel]
119+
self.models: List[BasicNCAModel]
119120
if single_model:
120121
self.models = [wrapped for _ in scales]
121122
else:
122-
self.models = nn.ModuleList([wrapped for _ in scales])
123+
self.models = [copy.deepcopy(wrapped) for _ in scales]
123124

124125
def forward(self, x: torch.Tensor, *args, **kwargs) -> Prediction:
125126
"""

ncalab/training/trainer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class BasicNCATrainer:
2828
def __init__(
2929
self,
3030
nca: BasicNCAModel,
31-
model_path: Path | PosixPath,
31+
model_path: Optional[Path | PosixPath],
3232
gradient_clipping: bool = False,
3333
lr: Optional[float] = None,
3434
lr_gamma: float = 0.99,

scripts/dry_train_examples.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
set -e
3+
4+
uv run tasks/class_cifar10/train_class_cifar10.py --dry --max-epochs 2
5+
uv run tasks/growing_emoji/train_growing_emoji.py --dry --epochs 2
6+
uv run tasks/class_medmnist/train_class_dermamnist.py --dry --max-epochs 2
7+
uv run tasks/class_medmnist/train_class_bloodmnist.py --dry --max-epochs 2
8+
uv run tasks/class_medmnist/train_class_pathmnist.py --dry --max-epochs 2
9+
uv run tasks/segmentation_kvasir_seg/train_segmentation_kvasir_seg.py --dry --max-epochs 2
10+
uv run tasks/selfclass_mnist/train_selfclass_mnist.py --dry --max-epochs 2

scripts/train_examples.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
set -e
23

34
uv run tasks/class_cifar10/train_class_cifar10.py
45
uv run tasks/growing_emoji/train_growing_emoji.py

tasks/class_cifar10/train_class_cifar10.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def train_class_cifar10(
3737
hidden_channels: int,
3838
gpu: bool,
3939
gpu_index: int,
40+
max_epochs: int,
41+
dry: bool,
4042
):
4143
comment = "CIFAR10"
4244
comment += f"_hidden_{hidden_channels}"
@@ -45,7 +47,7 @@ def train_class_cifar10(
4547
comment += f"_AM_{alive_mask}"
4648
comment += f"_TE_{use_temporal_encoding}"
4749

48-
writer = SummaryWriter(comment=comment)
50+
writer = SummaryWriter(comment=comment) if not dry else None
4951

5052
device = get_compute_device(f"cuda:{gpu_index}" if gpu else "cpu")
5153

@@ -141,17 +143,18 @@ def train_class_cifar10(
141143
# Train the NCA model
142144
trainer = BasicNCATrainer(
143145
nca,
144-
WEIGHTS_PATH / "classification_cifar10",
146+
WEIGHTS_PATH / "classification_cifar10" if not dry else None,
145147
batch_repeat=2,
146-
max_epochs=120,
148+
max_epochs=max_epochs,
147149
gradient_clipping=gradient_clipping,
148150
)
149151
trainer.train(
150152
loader_train,
151153
loader_val,
152154
summary_writer=writer,
153155
)
154-
writer.close()
156+
if writer is not None:
157+
writer.close()
155158

156159

157160
@click.command()
@@ -163,12 +166,18 @@ def train_class_cifar10(
163166
@click.option(
164167
"--gpu-index", type=int, default=0, help="Index of GPU to use, if --gpu in use."
165168
)
166-
def main(batch_size, hidden_channels, gpu: bool, gpu_index: int):
169+
@click.option("--max-epochs", "-E", type=int, default=120)
170+
@click.option("--dry", "-D", is_flag=True)
171+
def main(
172+
batch_size, hidden_channels, gpu: bool, gpu_index: int, max_epochs: int, dry: bool
173+
):
167174
train_class_cifar10(
168175
batch_size=batch_size,
169176
hidden_channels=hidden_channels,
170177
gpu=gpu,
171178
gpu_index=gpu_index,
179+
max_epochs=max_epochs,
180+
dry=dry,
172181
)
173182

174183

tasks/class_medmnist/train_class_bloodmnist.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def train_class_bloodmnist(
3838
hidden_channels: int,
3939
gpu: bool,
4040
gpu_index: int,
41+
max_epochs: int,
42+
dry: bool,
4143
):
4244
print_NCALab_banner()
4345

@@ -48,7 +50,7 @@ def train_class_bloodmnist(
4850
comment += f"_AM_{alive_mask}"
4951
comment += f"_TE_{use_temporal_encoding}"
5052

51-
writer = SummaryWriter(comment=comment)
53+
writer = SummaryWriter(comment=comment) if not dry else None
5254

5355
device = get_compute_device(f"cuda:{gpu_index}" if gpu else "cpu")
5456

@@ -84,22 +86,23 @@ def train_class_bloodmnist(
8486
pad_noise=pad_noise,
8587
use_temporal_encoding=use_temporal_encoding,
8688
class_names=list(INFO["bloodmnist"]["label"].values()),
89+
training_timesteps=32,
90+
inference_timesteps=32,
8791
)
8892
trainer = BasicNCATrainer(
8993
nca,
90-
WEIGHTS_PATH / "classification_bloodmnist",
94+
WEIGHTS_PATH / "classification_bloodmnist" if not dry else None,
9195
batch_repeat=2,
92-
max_epochs=40,
96+
max_epochs=max_epochs,
9397
gradient_clipping=gradient_clipping,
94-
steps_range=(32, 33),
95-
steps_validation=32,
9698
)
9799
trainer.train(
98100
loader_train,
99101
loader_val,
100102
summary_writer=writer,
101103
)
102-
writer.close()
104+
if writer is not None:
105+
writer.close()
103106

104107

105108
@click.command()
@@ -111,12 +114,18 @@ def train_class_bloodmnist(
111114
@click.option(
112115
"--gpu-index", type=int, default=0, help="Index of GPU to use, if --gpu in use."
113116
)
114-
def main(batch_size, hidden_channels, gpu: bool, gpu_index: int):
117+
@click.option("--max-epochs", "-E", type=int, default=40)
118+
@click.option("--dry", "-D", is_flag=True)
119+
def main(
120+
batch_size, hidden_channels, gpu: bool, gpu_index: int, max_epochs: int, dry: bool
121+
):
115122
train_class_bloodmnist(
116123
batch_size=batch_size,
117124
hidden_channels=hidden_channels,
118125
gpu=gpu,
119126
gpu_index=gpu_index,
127+
max_epochs=max_epochs,
128+
dry=dry,
120129
)
121130

122131

tasks/class_medmnist/train_class_dermamnist.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ def train_class_dermamnist(
3838
hidden_channels: int,
3939
gpu: bool,
4040
gpu_index: int,
41+
max_epochs: int,
42+
dry: bool,
4143
):
4244
print_NCALab_banner()
4345

@@ -48,7 +50,7 @@ def train_class_dermamnist(
4850
comment += f"_AM_{alive_mask}"
4951
comment += f"_TE_{use_temporal_encoding}"
5052

51-
writer = SummaryWriter(comment=comment)
53+
writer = SummaryWriter(comment=comment) if not dry else None
5254

5355
device = get_compute_device(f"cuda:{gpu_index}" if gpu else "cpu")
5456

@@ -112,23 +114,24 @@ def train_class_dermamnist(
112114
pad_noise=pad_noise,
113115
use_temporal_encoding=use_temporal_encoding,
114116
class_names=list(INFO["dermamnist"]["label"].values()),
117+
training_timesteps=32,
118+
inference_timesteps=32,
115119
)
116120

117121
trainer = BasicNCATrainer(
118122
nca,
119-
WEIGHTS_PATH / "classification_dermamnist",
123+
WEIGHTS_PATH / "classification_dermamnist" if not dry else None,
120124
batch_repeat=2,
121-
max_epochs=40,
125+
max_epochs=max_epochs,
122126
gradient_clipping=gradient_clipping,
123-
steps_range=(32, 33),
124-
steps_validation=32,
125127
)
126128
trainer.train(
127129
loader_train,
128130
loader_val,
129131
summary_writer=writer,
130132
)
131-
writer.close()
133+
if writer is not None:
134+
writer.close()
132135

133136

134137
@click.command()
@@ -140,12 +143,18 @@ def train_class_dermamnist(
140143
@click.option(
141144
"--gpu-index", type=int, default=0, help="Index of GPU to use, if --gpu in use."
142145
)
143-
def main(batch_size, hidden_channels, gpu: bool, gpu_index: int):
146+
@click.option("--max-epochs", "-E", type=int, default=40)
147+
@click.option("--dry", "-D", is_flag=True)
148+
def main(
149+
batch_size, hidden_channels, gpu: bool, gpu_index: int, max_epochs: int, dry: bool
150+
):
144151
train_class_dermamnist(
145152
batch_size=batch_size,
146153
hidden_channels=hidden_channels,
147154
gpu=gpu,
148155
gpu_index=gpu_index,
156+
max_epochs=max_epochs,
157+
dry=dry,
149158
)
150159

151160

tasks/class_medmnist/train_class_pathmnist.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ def train_class_pathmnist(
3636
hidden_channels: int,
3737
gpu: bool,
3838
gpu_index: int,
39+
max_epochs: int,
40+
dry: bool,
3941
):
4042
comment = "PathMNIST"
4143
comment += f"_hidden_{hidden_channels}"
@@ -44,7 +46,7 @@ def train_class_pathmnist(
4446
comment += f"_AM_{alive_mask}"
4547
comment += f"_TE_{use_temporal_encoding}"
4648

47-
writer = SummaryWriter(comment=comment)
49+
writer = SummaryWriter(comment=comment) if not dry else None
4850

4951
device = get_compute_device(f"cuda:{gpu_index}" if gpu else "cpu")
5052

@@ -80,22 +82,23 @@ def train_class_pathmnist(
8082
pad_noise=pad_noise,
8183
use_temporal_encoding=use_temporal_encoding,
8284
class_names=list(INFO["pathmnist"]["label"].values()),
85+
training_timesteps=32,
86+
inference_timesteps=32,
8387
)
8488
trainer = BasicNCATrainer(
8589
nca,
86-
WEIGHTS_PATH / "classification_pathmnist",
90+
WEIGHTS_PATH / "classification_pathmnist" if not dry else None,
8791
batch_repeat=2,
88-
max_epochs=10,
92+
max_epochs=max_epochs,
8993
gradient_clipping=gradient_clipping,
90-
steps_range=(32, 33),
91-
steps_validation=32,
9294
)
9395
trainer.train(
9496
loader_train,
9597
loader_val,
9698
summary_writer=writer,
9799
)
98-
writer.close()
100+
if writer is not None:
101+
writer.close()
99102

100103

101104
@click.command()
@@ -107,12 +110,18 @@ def train_class_pathmnist(
107110
@click.option(
108111
"--gpu-index", type=int, default=0, help="Index of GPU to use, if --gpu in use."
109112
)
110-
def main(batch_size, hidden_channels, gpu: bool, gpu_index: int):
113+
@click.option("--max-epochs", "-E", type=int, default=10)
114+
@click.option("--dry", "-D", is_flag=True)
115+
def main(
116+
batch_size, hidden_channels, gpu: bool, gpu_index: int, max_epochs: int, dry: bool
117+
):
111118
train_class_pathmnist(
112119
batch_size=batch_size,
113120
hidden_channels=hidden_channels,
114121
gpu=gpu,
115122
gpu_index=gpu_index,
123+
max_epochs=max_epochs,
124+
dry=dry,
116125
)
117126

118127

tasks/growing_emoji/train_growing_emoji.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828

2929

3030
def train_growing_emoji(
31-
batch_size: int, hidden_channels: int, gpu: bool, gpu_index: int, max_epochs: int
31+
batch_size: int,
32+
hidden_channels: int,
33+
gpu: bool,
34+
gpu_index: int,
35+
max_epochs: int,
36+
dry: bool,
3237
):
3338
"""
3439
Main function to run the "growing emoji" example task.
@@ -40,7 +45,7 @@ def train_growing_emoji(
4045
fix_random_seed()
4146

4247
# Create tensorboard summary writer
43-
writer = SummaryWriter(comment=" Growing NCA (Lizard)")
48+
writer = SummaryWriter(comment=" Growing NCA (Lizard)") if not dry else None
4449

4550
# Select device, try to use GPU or fall back to CPU
4651
device = get_compute_device(f"cuda:{gpu_index}" if gpu else "cpu")
@@ -63,12 +68,13 @@ def train_growing_emoji(
6368
# Create Trainer and run training
6469
trainer = BasicNCATrainer(
6570
nca,
66-
WEIGHTS_PATH / "ncalab_growing_emoji",
71+
WEIGHTS_PATH / "ncalab_growing_emoji" if not dry else None,
6772
max_epochs=max_epochs,
6873
pool=pool,
6974
)
7075
trainer.train(dataloader_train, summary_writer=writer, save_every=500)
71-
writer.close()
76+
if writer is not None:
77+
writer.close()
7278

7379

7480
@click.command()
@@ -81,13 +87,15 @@ def train_growing_emoji(
8187
"--gpu-index", type=int, default=0, help="Index of GPU to use, if --gpu in use."
8288
)
8389
@click.option("--epochs", "-e", type=int, default=5000)
84-
def main(batch_size, hidden_channels, gpu, gpu_index, epochs):
90+
@click.option("--dry", "-D", is_flag=True)
91+
def main(batch_size, hidden_channels, gpu, gpu_index: int, epochs: int, dry: bool):
8592
train_growing_emoji(
8693
batch_size=batch_size,
8794
hidden_channels=hidden_channels,
8895
gpu=gpu,
8996
gpu_index=gpu_index,
9097
max_epochs=epochs,
98+
dry=dry,
9199
)
92100

93101

0 commit comments

Comments
 (0)