-
Notifications
You must be signed in to change notification settings - Fork 3
/
resnet_experiment.py
64 lines (52 loc) · 2.03 KB
/
resnet_experiment.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
import torch
import pytorch_lightning as pl
from pytorch_lightning import Trainer
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.callbacks import ModelCheckpoint, LearningRateMonitor
import argparse
from src.data_stuff.NEW_patch_dataset import PatchDataModule
from src.model_stuff.MyResNet import MyResNet
from src.callback_stuff.PatientLevelValidation import PatientLevelValidation
# from src.data_stuff.pip_tools import install
# install(["pytorch-lightning", "albumentations", "seaborn", "timm", "wandb", "plotly", "lightly"], quietly=True)
if __name__ == "__main__":
print(f"🚙 Starting Resnet Experiment! 🚗")
pl.seed_everything(42)
# Data Dir and Model Checkpoint dir
data_dir = "/workspace/repos/data/tcga_data_formatted/"
model_save_path = "/workspace/repos/hrdl/saved_models/moco/{EXP_NAME}"
# parse args
parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=16)
parser.add_argument('--num_epochs', type=int, default=100)
args = parser.parse_args()
# make experiment name
EXP_NAME = f"Resnet_baseline_bs{bs}"
print(f"\tExperiment Name: {EXP_NAME}")
# logger
logger=WandbLogger(project="colorectal_cancer_ai", name=EXP_NAME)
# callbacks
patient_level_validation_callback = PatientLevelValidation(group_size=args.group_size, debug_mode=False)
checkpoint_callback = ModelCheckpoint(
dirpath=model_save_path,
filename='{epoch}-{val_majority_vote_acc:.2f}-{val_acc_epoch}',
save_top_k=3,
verbose=True,
monitor='val_majority_vote_acc',
mode='max'
)
# model
model = MyResNet()
# data
dm = PatchDataModule(
data_dir=args.data_dir,
batch_size=args.batch_size,
group_size=args.group_size,
num_workers=os.cpu_count(),
)
# trainer
trainer = Trainer(
logger=logger,
callbacks=[checkpoint_callback, patient_level_validation_callback]
)
trainer.fit(model, dm)