-
Notifications
You must be signed in to change notification settings - Fork 151
/
bnn_pynq_train.py
147 lines (122 loc) · 5.46 KB
/
bnn_pynq_train.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
# MIT License
#
# Copyright (c) 2019 Xilinx
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Original file can be found at https://github.com/Xilinx/brevitas/blob/8c3d9de0113528cf6693c6474a13d802a66682c6/src/brevitas_examples/bnn_pynq/bnn_pynq_train.py
import argparse
import os
import sys
import torch
from trainer import Trainer
# PyTorch precision
torch.set_printoptions(precision=10)
# Util method to add mutually exclusive boolean
def add_bool_arg(parser, name, default):
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument("--" + name, dest=name, action="store_true")
group.add_argument("--no_" + name, dest=name, action="store_false")
parser.set_defaults(**{name: default})
# Util method to pass None as a string and be recognized as None value
def none_or_str(value):
if value == "None":
return None
return value
def none_or_int(value):
if value == "None":
return None
return int(value)
def parse_args(args):
parser = argparse.ArgumentParser(description="PyTorch CIFAR10 Training/Evaluation")
# I/O
parser.add_argument("--datadir", default="./data/", help="Dataset location")
parser.add_argument("--experiments", default="./experiments", help="Path to experiments folder")
parser.add_argument("--dry_run", action="store_true", help="Disable output files generation")
parser.add_argument("--log_freq", type=int, default=10)
# Execution modes
parser.add_argument(
"--evaluate", dest="evaluate", action="store_true", help="evaluate model on validation set"
)
parser.add_argument(
"--resume",
dest="resume",
type=none_or_str,
help="Resume from checkpoint. Overrides --pre-trained flag.",
)
add_bool_arg(parser, "detect_nan", default=False)
# Compute resources
parser.add_argument("--num_workers", default=0, type=int, help="Number of workers")
parser.add_argument("--gpus", type=none_or_str, default=None, help="Comma separated GPUs")
# Optimizer hyper-parameters
parser.add_argument("--batch_size", default=100, type=int, help="batch size")
parser.add_argument("--lr", default=0.02, type=float, help="Learning rate")
parser.add_argument("--optim", type=none_or_str, default="ADAM", help="Optimizer to use")
parser.add_argument("--loss", type=none_or_str, default="SqrHinge", help="Loss function to use")
parser.add_argument("--scheduler", default="FIXED", type=none_or_str, help="LR Scheduler")
parser.add_argument(
"--milestones", type=none_or_str, default="100,150,200,250", help="Scheduler milestones"
)
parser.add_argument("--momentum", default=0.9, type=float, help="Momentum")
parser.add_argument("--weight_decay", default=0, type=float, help="Weight decay")
parser.add_argument("--epochs", default=1000, type=int, help="Number of epochs")
parser.add_argument("--random_seed", default=1, type=int, help="Random seed")
# Neural network Architecture
parser.add_argument("--network", default="CNV_2W2A", type=str, help="neural network")
parser.add_argument("--pre-trained", action="store_true", help="Load pre-trained model")
parser.add_argument("--strict", action="store_true", help="Strict state dictionary loading")
return parser.parse_args(args)
class objdict(dict):
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError("No such attribute: " + name)
def __setattr__(self, name, value):
self[name] = value
def __delattr__(self, name):
if name in self:
del self[name]
else:
raise AttributeError("No such attribute: " + name)
def launch(cmd_args):
args = parse_args(cmd_args)
# Set relative paths relative to current working directory
path_args = ["datadir", "experiments", "resume"]
for path_arg in path_args:
path = getattr(args, path_arg)
if path is not None and not os.path.isabs(path):
abs_path = os.path.abspath(os.path.join(os.getcwd(), path))
setattr(args, path_arg, abs_path)
# Access config as an object
args = objdict(args.__dict__)
# Avoid creating new folders etc.
if args.evaluate:
args.dry_run = True
# Init trainer
trainer = Trainer(args)
# Execute
if args.evaluate:
with torch.no_grad():
trainer.eval_model()
else:
trainer.train_model()
def main():
launch(sys.argv[1:])
if __name__ == "__main__":
main()