-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
131 lines (99 loc) · 7.61 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
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
from utils.utilities import initialize_model, load_dataset
import torch, argparse, os
from models.train import train
from models.test import test
from config.config import cfg
from torch.utils.data import TensorDataset, DataLoader
from torch.autograd import Variable
import numpy as np
from datetime import datetime
########################################################################################################################
# STARTING THE RAINING OF THE NET
########################################################################################################################
def main():
parser = argparse.ArgumentParser(description="Train the CNN and LSTM model")
parser.add_argument("--train_path", dest="train", default=None, help="path of the train csv file")
parser.add_argument("--valid_path", dest="valid", default=None, help="path of the validation csv file")
parser.add_argument("--test_path", dest="test", default=None, help="path of the test csv file")
parser.add_argument("--model_path", dest="model", default=None, help="path of the model weight")
parser.add_argument("--epochs", dest="epochs", default=200, help="number of epochs")
parser.add_argument("--val_period", dest="period", default=1, help="choose when use the validation")
parser.add_argument("--device", dest="device", default='0', help="choose GPU")
parser.add_argument("--model_type", dest="model_type", default='single', help="define the model to use: sigle-frame, multi-frame or depth")
args = parser.parse_args()
if (args.train == None and args.test == None):
print("you have to decide : do train or test")
exit()
####################################################################################################################
# TRAIN PHASE
####################################################################################################################
if args.test == None:
if (args.valid == None):
print("please insert valid")
exit()
else:
# current date and time
now = datetime.now()
timestamp = datetime.timestamp(now)
dir_name = os.path.dirname(os.path.abspath(__file__))
save_weight_path = dir_name + cfg.SAVE_WEIGHT_PATH[args.model_type] + 'weight_' + args.epochs + '_lenseq_' + str(cfg.TRAIN.LEN_SEQUENCES) + '_' + str(timestamp)
tensor_board_path = dir_name + cfg.TENSORBOARD_PATH[args.model_type] + "weight_" + args.epochs + '_lenseq_' + str(cfg.TRAIN.LEN_SEQUENCES) + '_' + str(timestamp)
print()
print("SUMMARIZE : ")
print()
print("train data path: {}" .format(args.train))
print("valid data path: {}" .format(args.valid))
print("weight save path: {}" .format(save_weight_path))
print("epoch: {}" .format(args.epochs))
print("validation period: {}" .format(args.period))
print("batch size: {}" .format(cfg.TRAIN.BATCH_SIZE))
print("learning rate: {}" .format(cfg.TRAIN.LEARNING_RATE))
print("GPU device: {}" .format(args.device))
print("len_seq: {}" .format(cfg.TRAIN.LEN_SEQUENCES))
print("hidden_dimension: {}" .format(cfg.DIMENSION[args.model_type]))
print("Loss Function: {}" .format(cfg.TRAIN.LOSS))
print("Optimizer: {}" .format(cfg.TRAIN.OPTIMIZER))
print("Decrement period: {}" .format(cfg.TRAIN.DEC_PERIOD))
print("num_layers: {}" .format(cfg.LAYERS))
print("you are working with {} model" .format(args.model_type))
print("To use tensorboardX log this --logdir : " + tensor_board_path)
print()
if not os.path.exists(save_weight_path):
os.mkdir(save_weight_path)
if not os.path.exists(tensor_board_path):
os.mkdir(tensor_board_path)
train_images, valid_images, train_coordinates, valid_coordinates = load_dataset(len_sequence=cfg.TRAIN.LEN_SEQUENCES, model_type=args.model_type, train_path=args.train, valid_path=args.valid)
model, criterion, optimizer = initialize_model(model_type=args.model_type, cfg=cfg, mode='train')
train_data = TensorDataset(torch.from_numpy(train_images), torch.from_numpy(train_coordinates))
val_data = TensorDataset(torch.from_numpy(valid_images), torch.from_numpy(valid_coordinates))
train_loader = DataLoader(train_data, shuffle=cfg.TRAIN.SHUFFLE_T, batch_size=cfg.TRAIN.BATCH_SIZE, drop_last=True)
val_loader = DataLoader(val_data, shuffle=cfg.TRAIN.SHUFFLE_V, batch_size=cfg.TRAIN.BATCH_SIZE, drop_last=True)
train(model=model, criterion=criterion, optimizer=optimizer, train_loader=train_loader, val_loader=val_loader, epochs=int(args.epochs), val_period=int(args.period), save_weights=save_weight_path, event_log_path=tensor_board_path, dev=args.device, cfg=cfg)
####################################################################################################################
# TEST PHASE
####################################################################################################################
if args.train == None:
if (args.model == None):
print("please insert the path to load the model for the test")
exit()
else:
print()
print("SUMMARIZE : ")
print()
print("test data path: {}" .format(args.test))
print("model path: {}" .format(args.model))
print("batch size: {}" .format(cfg.TEST.BATCH_SIZE))
print("hidden dimension: {}" .format(cfg.DIMENSION[args.model_type]))
print("GPU device: {}" .format(args.device))
print("len_seq: {}" .format(cfg.TEST.LEN_SEQUENCES))
print("Loss Function: {}" .format(cfg.TRAIN.LOSS))
print("you are working with {} model" .format(args.model_type))
print()
test_images, test_coordinates, image_path = load_dataset(test_path=args.test, len_sequence=cfg.TEST.LEN_SEQUENCES, model_type=args.model_type)
model, criterion = initialize_model(model_type=args.model_type, cfg=cfg, mode='test')
test_data = TensorDataset(torch.from_numpy(test_images), torch.from_numpy(test_coordinates))
test_loader = DataLoader(test_data, shuffle=cfg.TEST.SHUFFLE, batch_size=cfg.TEST.BATCH_SIZE, drop_last=True)
dir_name = os.path.dirname(os.path.abspath(__file__))
test(model=model, criterion=criterion, model_path=args.model, dir_name=dir_name, test_loader=test_loader, paths=image_path, dev=args.device, model_type=args.model_type)
if __name__ == '__main__':
main()