-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
93 lines (73 loc) · 3.37 KB
/
main.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
"""
Main function used to train the Cassava Leaf Disease Detection model.
Utilizes file constants.py as well as the following hyperparameters:
- Epochs - how many times should the model pass through the dataset?
- Batch size - how many images to use in one training iteration?
- n_eval - how often should we evaluate the model (in iterations)?
- logdir - where should we output our tensorboard files to?
- datadir - where is the cassava leaf disease dataset (6 GB)?
- summaries - where should we output our training summaries to?
"""
import argparse
import os, torch
import constants
from data.StartingDataset import StartingDataset
from networks.StartingNetwork import StartingNetwork
from train_functions.starting_train import starting_train
def main():
# Get command line arguments
args = parse_arguments()
hyperparameters = {"epochs": args.epochs, "batch_size": args.batch_size, "data_dir": args.datadir}
# Create path for training summaries
summary_path = None
if args.logdir is not None:
summary_path = f"{args.summaries}/{args.logdir}"
os.makedirs(summary_path, exist_ok=True)
# Use GPU, if available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Print out training information
print("\n----------------- Summary Information --------------------")
print(f"\nSummary path: {summary_path}")
print(f" Data path: {constants.DATA_DIR}\n")
print(f"Epochs: {args.epochs}")
print(f" Evaluate: {constants.N_EVAL}")
print(f" Save Model: {constants.SAVE_INTERVAL} \n")
print(f"Training imgs: {constants.TRAIN_NUM * constants.IMG_TYPES}")
print(f" Testing imgs: {constants.TEST_NUM * constants.IMG_TYPES}")
print(f" Batch size: {args.batch_size}\n")
print("----------------------------------------------------------")
# Initalize dataset and model. Then train the model!
csv_path = args.datadir +'/train.csv'
train_dataset = StartingDataset(csv_path)
val_dataset = StartingDataset(csv_path, training_set = False)
# Create our model, and begin starting_train()
model = StartingNetwork()
starting_train(
train_dataset=train_dataset,
val_dataset=val_dataset,
model=model,
hyperparameters=hyperparameters,
n_eval=args.n_eval,
summary_path=summary_path,
)
"""
Add parser arguments using argparse
Default values taken from constants.py
- Epochs - how many times should the model pass through the dataset?
- Batch size - how many images to use in one training iteration?
- n_eval - how often should we evaluate the model (in iterations)?
- logdir - where should we output our tensorboard files to?
- datadir - where is the cassava leaf disease dataset (6 GB)?
- summaries - where should we output our training summaries to?
"""
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--epochs", type=int, default=constants.EPOCHS)
parser.add_argument("--batch_size", type=int, default=constants.BATCH_SIZE)
parser.add_argument("--n_eval", type=int, default=constants.N_EVAL)
parser.add_argument("--logdir", type=str, default=constants.LOG_DIR)
parser.add_argument("--datadir", type=str, default=constants.DATA_DIR)
parser.add_argument("--summaries", type=str, default=constants.SUMMARIES_PATH)
return parser.parse_args()
if __name__ == "__main__":
main()