-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_mnist.py
227 lines (194 loc) · 6.77 KB
/
main_mnist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import argparse
import os
import torch
from torch_geometric.datasets import MNISTSuperpixels
from torch_geometric.loader import DataLoader
import pytorch_lightning as pl
from lightning_wrappers.callbacks import EMA, EpochTimer
from lightning_wrappers.mnist import PONITA_MNIST
from torch_geometric.transforms import BaseTransform
# TODO: do we need this?
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy("file_system")
class Sparsify(BaseTransform):
def __init__(self, threshold=0.5):
super().__init__()
self.threshold = threshold
def __call__(self, graph):
select = graph.x[:, 0] > self.threshold
graph.x = graph.x[select]
graph.pos = graph.pos[select]
if graph.batch is not None:
graph.batch = graph.batch[select]
graph.edge_index = None
return graph
class RemoveDuplicatePoints(BaseTransform):
def __init__(self):
super().__init__()
def __call__(self, graph):
dists = (graph.pos[:, None, :] - graph.pos[None, :, :]).norm(dim=-1)
dists = dists + 100.0 * torch.tril(torch.ones_like(dists), diagonal=0)
min_dists = dists.min(dim=1)[0]
select = min_dists > 0.0
graph.x = graph.x[select]
graph.pos = graph.pos[select]
graph.edge_index = None
return graph
# ------------------------ Start of the main experiment script
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# ------------------------ Input arguments
# Run parameters
parser.add_argument("--epochs", type=int, default=50, help="number of epochs")
parser.add_argument("--warmup", type=int, default=0, help="number of epochs")
parser.add_argument(
"--batch_size",
type=int,
default=96,
help="Batch size. Does not scale with number of gpus.",
)
parser.add_argument("--lr", type=float, default=5e-4, help="learning rate")
parser.add_argument(
"--weight_decay", type=float, default=1e-10, help="weight decay"
)
parser.add_argument("--log", type=eval, default=True, help="logging flag")
parser.add_argument(
"--enable_progress_bar", type=eval, default=True, help="enable progress bar"
)
parser.add_argument(
"--num_workers", type=int, default=0, help="Num workers in dataloader"
)
parser.add_argument("--seed", type=int, default=0, help="Random seed")
# Train settings
parser.add_argument(
"--train_augm",
type=eval,
default=True,
help="whether or not to use random rotations during training",
)
# QM9 Dataset
parser.add_argument(
"--root", type=str, default="datasets/mnist", help="Data set location"
)
# Graph connectivity settings
parser.add_argument(
"--radius",
type=eval,
default=None,
help="radius for the radius graph construction in front of the force loss",
)
parser.add_argument(
"--loop", type=eval, default=True, help="enable self interactions"
)
# PONTA model settings
parser.add_argument(
"--num_ori", type=int, default=10, help="num elements of spherical grid"
)
parser.add_argument(
"--hidden_dim", type=int, default=128, help="internal feature dimension"
)
parser.add_argument(
"--basis_dim", type=int, default=256, help="number of basis functions"
)
parser.add_argument(
"--degree", type=int, default=3, help="degree of the polynomial embedding"
)
parser.add_argument(
"--layers", type=int, default=5, help="Number of message passing layers"
)
parser.add_argument(
"--widening_factor",
type=int,
default=4,
help="Number of message passing layers",
)
parser.add_argument(
"--layer_scale",
type=float,
default=0,
help="Initial layer scale factor in ConvNextBlock, 0 means do not use layer scale",
)
parser.add_argument(
"--multiple_readouts",
type=eval,
default=False,
help="Whether or not to readout after every layer",
)
# Parallel computing stuff
parser.add_argument(
"-g",
"--gpus",
default=1,
type=int,
help="number of gpus to use (assumes all are on one node)",
)
# Arg parser
args = parser.parse_args()
# ------------------------ Device settings
if args.gpus > 0:
accelerator = "gpu"
devices = args.gpus
else:
accelerator = "cpu"
devices = "auto"
if args.num_workers == -1:
args.num_workers = os.cpu_count()
# ------------------------ Dataset
# Load the dataset and set the dataset specific settings
# transform = Compose([RemoveDuplicatePoints(), KNNGraph(k=4, loop=False)])
transform = None
dataset_train = MNISTSuperpixels(root=args.root, train=True, transform=transform)
dataset_test = MNISTSuperpixels(root=args.root, train=False, transform=transform)
# Create train, val, test splits
train_size = int(0.9 * len(dataset_train))
val_size = len(dataset_train) - train_size
dataset_train, dataset_val = torch.utils.data.random_split(
dataset_train, [train_size, val_size]
)
datasets = {"train": dataset_train, "val": dataset_val, "test": dataset_test}
# Select the right target
# Make the dataloaders
dataloaders = {
split: DataLoader(
dataset,
batch_size=args.batch_size,
shuffle=(split == "train"),
num_workers=args.num_workers,
)
for split, dataset in datasets.items()
}
# ------------------------ Load and initialize the model
model = PONITA_MNIST(args)
# ------------------------ Weights and Biases logger
if args.log:
logger = pl.loggers.WandbLogger(
project="PONITA-MNIST", name=None, config=args, save_dir="logs"
)
else:
logger = None
# ------------------------ Set up the trainer
# Seed
pl.seed_everything(args.seed, workers=True)
# Pytorch lightning call backs
callbacks = [
EMA(0.99),
pl.callbacks.ModelCheckpoint(monitor="valid ACC", mode="max"),
EpochTimer(),
]
if args.log:
callbacks.append(pl.callbacks.LearningRateMonitor(logging_interval="epoch"))
# Initialize the trainer
trainer = pl.Trainer(
logger=logger,
max_epochs=args.epochs,
callbacks=callbacks,
inference_mode=False, # Important for force computation via backprop
gradient_clip_val=0.5,
accelerator=accelerator,
devices=devices,
enable_progress_bar=args.enable_progress_bar,
)
# Do the training
trainer.fit(model, dataloaders["train"], dataloaders["val"])
# And test
trainer.test(model, dataloaders["test"], ckpt_path="best")