-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (63 loc) · 2.51 KB
/
Copy pathmain.py
File metadata and controls
72 lines (63 loc) · 2.51 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
print("importing...")
import argparse
import torch
from os import mkdir
from src.preprocessing import Preprocessor
from src.model import SimpleCNN
from src.train import TrainingLoop
def parse_args():
parser = argparse.ArgumentParser(description="Train and evaluate MNIST CNN")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose training logs")
parser.add_argument("-b", "--batch-size", type=int, default=32, help="Batch size for train/test loaders")
parser.add_argument("-l", "--learning-rate", type=float, default=0.001, help="Learning rate")
parser.add_argument("-e", "--epochs", type=int, default=10, help="Number of training epochs")
parser.add_argument("-r", "--run-name", type=str, default="experiment", help="Run name prefix for TensorBoard logs")
parser.add_argument(
"-o",
"--optimizer",
type=str,
default="adam",
choices=["adam", "sgd"],
help="Optimizer to use",
)
return parser.parse_args()
def main():
args = parse_args()
print("Program start.")
mkdir("runs", exist_ok=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print("Using device:", device)
# Additional info if CUDA
if torch.cuda.is_available():
print(f"Number of GPUs available: {torch.cuda.device_count()}")
for i in range(torch.cuda.device_count()):
print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
print(f" Memory Allocated: {torch.cuda.memory_allocated(i)/1024**2:.2f} MB")
print(f" Memory Cached : {torch.cuda.memory_reserved(i)/1024**2:.2f} MB")
print(f"Peak: {torch.cuda.max_memory_allocated()/1024**2:.2f} MB")
else:
import platform
print(f"CPU Info: {platform.processor()}")
# Python and CUDA version
print(f"PyTorch version: {torch.__version__}")
print(f"CUDA version : {torch.version.cuda}")
print("Initializing preprocessor")
preprocessor = Preprocessor(batch_size=args.batch_size)
preprocessor.show_example()
model = SimpleCNN().to(device)
training_loop = TrainingLoop(
model=model,
preprocessor=preprocessor,
device=device,
learning_rate=args.learning_rate,
epochs=args.epochs,
optimizer_name=args.optimizer,
verbose=args.verbose,
run_name=args.run_name,
)
training_loop.train()
training_loop.validate()
training_loop.infer()
print("Program finished.")
if __name__ == "__main__":
main()