-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
140 lines (89 loc) · 3.92 KB
/
run.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
from Utils.dict2Class import Dict2Class
import torch
torch.autograd.set_detect_anomaly(True)
from Dataset.dataset import getDataset
from Experiment.ModelTracker import ModelTracker
from Experiment.train import train
from Experiment.test import test
import os
import shutil
def prepareExperimentDir(options, cleanPreviousDir):
# create experiment directory
targetDir = options.workDir + options.logRelDir + "/" + options.expGroup + "/" + options.expName
print(targetDir)
if(cleanPreviousDir and os.path.exists(targetDir)):
shutil.rmtree(targetDir)
print("deleting {}".format(targetDir))
os.makedirs(targetDir, exist_ok=True)
# write options
optionLogFile = open(targetDir + "/options.txt", "w")
for item in list(vars(options).items()):
optionLogFile.write("{} : {}\n".format(item[0], item[1]))
optionLogFile.close()
# freeze existing code
if(os.path.exists(targetDir + "/frozenCode")):
shutil.rmtree(targetDir + "/frozenCode")
os.makedirs(targetDir + "/frozenCode", exist_ok=True)
shutil.copytree(options.workDir + "Dataset", targetDir+"/frozenCode/Dataset")
shutil.copytree(options.workDir + "Model", targetDir+"/frozenCode/Model")
shutil.copytree(options.workDir + "Experiment", targetDir+"/frozenCode/Experiment")
shutil.copytree(options.workDir + "Utils", targetDir+"/frozenCode/Utils")
shutil.copyfile(options.workDir + "/run.py", targetDir + "/frozenCode/run.py")
def experimentRunner(options, cleanPreviousDir=False):
prepareExperimentDir(options, cleanPreviousDir)
from Model.model import Model
# SET DATASET
dataset = getDataset(options)
# SET TRACKER
trackerOptions = Dict2Class({
"model" : Model,
"nOfTrains" : dataset.get_nOfTrains_perFold()
})
modelTracker = ModelTracker(options, trackerOptions)
# RUN TRAIN
targetFolds = range(options.kFold)
previousRuns = modelTracker.get_fullAndSemi_finisheds()
if(previousRuns != None):
(doneFolds, (semiFinishedFold, lastDoneEpoch)) = previousRuns
# first handle non finished fold, if there is any
if(semiFinishedFold != None):
trainDict = Dict2Class({
"dataset" : dataset,
"targetFolds" : [semiFinishedFold],
"startEpoch" : lastDoneEpoch+1,
"totalEpoch" : options.nOfEpochs,
"modelTracker" : modelTracker,
"device" : options.device,
})
testDict = Dict2Class({
"dataset" : dataset,
"targetFolds" : [semiFinishedFold],
"modelTracker" : modelTracker,
"device" : options.device,
})
train(trainDict)
test(testDict)
# now it is finished
doneFolds.append(semiFinishedFold)
else:
doneFolds = []
# handle rest of the folds
targetFolds = [k for k in targetFolds if k not in doneFolds]
for fold in targetFolds:
trainDict = Dict2Class({
"dataset" : dataset,
"targetFolds" : [fold],
"startEpoch" : 0,
"totalEpoch" : options.nOfEpochs,
"modelTracker" : modelTracker,
"device" : options.device,
})
testDict = Dict2Class({
"dataset" : dataset,
"targetFolds" : [fold],
"modelTracker" : modelTracker,
"device" : options.device,
})
train(trainDict)
test(testDict)
modelTracker.finalizeExperiment()