-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtrain_pets.py
More file actions
165 lines (140 loc) · 6.21 KB
/
train_pets.py
File metadata and controls
165 lines (140 loc) · 6.21 KB
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
## Author: Thomas Capelle, Soumik Rakshit
## Mail: tcapelle@wandb.com, soumik.rakshit@wandb.com
""""Benchmarking apple M1Pro with Tensorflow
@wandbcode{apple_m1_pro}"""
import re, math, argparse
from types import SimpleNamespace
from pathlib import Path
from time import perf_counter
import wandb
from PIL import Image
from tqdm import tqdm
import torch
import torch.nn as nn
import torchvision as tv
import torchvision.transforms as T
from torch.cuda.amp import autocast
from utils import get_gpu_name
from pets import get_pets_dataloader, get_fast_pets_dataloader
PROJECT = "pytorch-M1Pro"
ENTITY = "capecape"
GROUP = "pytorch"
config_defaults = SimpleNamespace(
batch_size=128,
device="cuda",
epochs=1,
num_experiments=1,
learning_rate=1e-3,
image_size=512,
model_name="resnet50",
dataset="PETS",
num_workers=0,
gpu_name=get_gpu_name(),
mixed_precision=False,
channels_last=False,
optimizer="Adam",
compile=False,
tags=None,
dl="full",
syncro=False,
)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--entity", type=str, default=ENTITY)
parser.add_argument('--batch_size', type=int, default=config_defaults.batch_size)
parser.add_argument('--epochs', type=int, default=config_defaults.epochs)
parser.add_argument('--num_experiments', type=int, default=config_defaults.num_experiments)
parser.add_argument('--learning_rate', type=float, default=config_defaults.learning_rate)
parser.add_argument('--image_size', type=int, default=config_defaults.image_size)
parser.add_argument('--model_name', type=str, default=config_defaults.model_name)
parser.add_argument('--dataset', type=str, default=config_defaults.dataset)
parser.add_argument('--device', type=str, default=config_defaults.device)
parser.add_argument('--gpu_name', type=str, default=config_defaults.gpu_name)
parser.add_argument('--num_workers', type=int, default=config_defaults.num_workers)
parser.add_argument('--mixed_precision', action="store_true")
parser.add_argument('--channels_last', action="store_true")
parser.add_argument('--optimizer', type=str, default=config_defaults.optimizer)
parser.add_argument('--compile', action="store_true")
parser.add_argument('--tags', type=str, default=None)
parser.add_argument('--dl', type=str, default=config_defaults.dl)
parser.add_argument('--syncro', action="store_true")
return parser.parse_args()
def get_model(n_out, arch="resnet50"):
model = getattr(tv.models, arch)(weights=tv.models.ResNet50_Weights.IMAGENET1K_V1)
model.fc = nn.Linear(model.fc.in_features, n_out)
return model
def check_cuda(config):
if torch.cuda.is_available():
config.device = "cuda"
config.mixed_precision = True
config.pt_version = torch.__version__
config.cuda_version = torch.version.cuda
return config
def train(config):
config = check_cuda(config)
tags = [f"pt{torch.__version__}", f"cuda{torch.version.cuda}"] + (config.tags.split(",") if config.tags is not None else [])
print(tags)
with wandb.init(project=PROJECT, entity=args.entity, group=GROUP, tags=tags, config=config):
# Copy your config
config = wandb.config
# Get the data
if not config.dl == "one_batch":
train_dl = get_pets_dataloader(batch_size=config.batch_size,
image_size=config.image_size,
num_workers=config.num_workers)
else:
train_dl = get_fast_pets_dataloader(batch_size=config.batch_size,
image_size=config.image_size,
num_workers=0)
n_steps_per_epoch = len(train_dl)
model = get_model(len(train_dl.dataset.vocab), config.model_name)
model.to(config.device)
if config.channels_last:
model.to(memory_format=torch.channels_last)
if torch.__version__ >= "2.0" and config.compile:
print("Compiling model...")
model = torch.compile(model)
# Make the loss and optimizer
loss_func = nn.CrossEntropyLoss()
optimizer = getattr(torch.optim, config.optimizer)
optimizer = optimizer(model.parameters(), lr=config.learning_rate)
# Training
example_ct = 0
for epoch in tqdm(range(config.epochs)):
tf = t0 = perf_counter()
model.train()
for step, (images, labels) in enumerate(tqdm(train_dl, leave=False)):
images, labels = images.to(config.device), labels.to(config.device)
if config.channels_last:
images = images.contiguous(memory_format=torch.channels_last)
# compute the model froward and backward pass time
ti = perf_counter()
if config.mixed_precision:
with autocast():
outputs = model(images)
train_loss = loss_func(outputs, labels)
else:
outputs = model(images)
train_loss = loss_func(outputs, labels)
train_loss.backward()
optimizer.step()
optimizer.zero_grad()
if config.syncro:
torch.cuda.synchronize(device="cuda")
tf_with_dataloader = perf_counter() - tf
tf = perf_counter()
# log the metrics
example_ct += len(images)
metrics = {"train/train_loss": train_loss,
"train/epoch": (step + 1 + (n_steps_per_epoch * epoch)) / n_steps_per_epoch,
"train/example_ct": example_ct,
"samples_per_sec":len(images)/(tf-ti),
"samples_per_sec_dl":len(images)/tf_with_dataloader,
"samples_per_sec_epoch":example_ct/(tf-t0)}
if step + 1 < n_steps_per_epoch:
# 🐝 Log train metrics to wandb
wandb.log(metrics)
if __name__ == "__main__":
args = parse_args()
for _ in range(args.num_experiments):
train(config=args)