-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrun.py
More file actions
182 lines (154 loc) · 6.17 KB
/
run.py
File metadata and controls
182 lines (154 loc) · 6.17 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import cv2
import uuid
import pickle
from functools import reduce
from time import time, strftime
from datetime import datetime
import torch
import numpy as np
import torchvision.transforms as T
from torch.utils.data import DataLoader, Dataset, sampler
from matplotlib import pyplot as plt
from pdb import set_trace
from logger import *
from eval_perf import *
from predict import *
if torch.cuda.is_available():
device = torch.device("cuda")
torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
device = torch.device("cpu")
# ignore all of the "invalid value encountered in true_divide" errors
np.seterr(divide='ignore', invalid='ignore')
# ==================================
# PARAMETERS
# ==================================
WRITE_TO_FILE = True
# whether to load from an intermediate point.
LOAD_SAVED = False
# required
SAVED_MODEL_PATH = "./saved_models/12-15_16-57-52_FADE57.pt"
RUN_ID = None
# optional
SAVED_STATS_PATH = None
# from ddh import *
# from ddh2 import *
# from ddh3 import *
from ddh4 import *
if LOAD_SAVED and SAVED_MODEL_PATH:
print("Loading existing model...")
model.load_state_dict(torch.load(SAVED_MODEL_PATH))
if SAVED_STATS_PATH:
print("Loading existing stats...")
with open(SAVED_STATS_PATH, "rb") as file:
stats = pickle.load(file)
if not LOAD_SAVED or not SAVED_STATS_PATH:
print("Creating new stats...")
stats = {
"val_mean_aps": [],
"val_avg_pre": [],
"val_avg_rec": [],
"val_avg_hmean": [],
"highest_map": 0.0,
"test_avg_pre": 0.0,
"test_avg_rec": 0.0,
"test_avg_hmean": 0.0,
"test_mean_ap": 0.0,
"test_pre_curve": None,
"test_rec_curve": None,
}
if LOAD_SAVED and RUN_ID:
run_id = RUN_ID
else:
run_id = uuid.uuid4().hex.upper()[0:6]
now = datetime.now().strftime("%m-%d_%H-%M-%S")
file_name = now + "_" + run_id
# model checkpoint
saved_models_path = os.getcwd() + "/saved_models"
mkdir(saved_models_path)
checkpoint_path = saved_models_path + "/{}.pt" \
.format(file_name)
# stats collection
stats_path = os.getcwd() + "/stats"
mkdir(stats_path)
stats_file_path = stats_path + "/{}.pickle".format(file_name)
with Logger(write_to_file=WRITE_TO_FILE, file_name=file_name) as logger:
logger.write(
"Starting run {} for {} epochs with model {}, and following params"
.format(run_id, NUM_EPOCHS, type(model).__name__))
logger.write("hash_dim: " + str(HASH_DIM))
logger.write(OPTIM_PARAMS)
logger.write(CUSTOM_PARAMS)
logger.write(BATCH_SIZE)
logger.write(LOADER_PARAMS)
logger.write(DATASET_PARAMS)
logger.write("====== START ======")
logger.write("")
for epoch in range(NUM_EPOCHS):
# ======================================================================
# TRAINING
# ======================================================================
logger.write("Epoch {}/{}".format(epoch+1, NUM_EPOCHS))
logger.write("--------------")
start = time()
train(model, loader_train, optimizer, logger,
device=device,
**CUSTOM_PARAMS)
logger.write("Training completed in {:.0f} seconds."
.format(time() - start))
logger.write("")
# ======================================================================
# validation
# ======================================================================
start = time()
# get all of the codes for gallery and test images
gallery_codes, gallery_label, test_codes, test_label = \
predict(model, loader_gallery, loader_val, logger, device=device)
# evaluate the performance
avg_pre, avg_rec, avg_hmean, _, _, mean_ap = \
eval_perf(gallery_codes, gallery_label, test_codes, test_label,
top_k=TOP_K, hamm_radius=HAMM_RADIUS)
stats['val_mean_aps'].append(mean_ap)
stats['val_avg_pre'].append(avg_pre)
stats['val_avg_rec'].append(avg_rec)
stats['val_avg_hmean'].append(avg_hmean)
if mean_ap > stats["highest_map"]:
logger.write(
"Higher mean avg precision {:.8f}/{:.8f}, saving!"
.format(stats["highest_map"], mean_ap))
# saves the state of this model
torch.save(model.state_dict(), checkpoint_path)
stats["highest_map"] = mean_ap
logger.write("Validation completed in {:.0f} seconds."
.format(time() - start))
logger.write("val MAP: {:.8f}, ".format(mean_ap) +
"avg precision: {:.6f}, ".format(avg_pre) +
"avg recall: {:.6f}, ".format(avg_rec) +
"avg harmonic mean: {:0.6f}".format(avg_hmean))
logger.write("")
# ==========================================================================
# test
# ==========================================================================
best_model = model_class(hash_dim=HASH_DIM)
best_model.load_state_dict(torch.load(checkpoint_path))
start = time()
# get all of the codes for gallery and test images
gallery_codes, gallery_label, test_codes, test_label= \
predict(best_model, loader_gallery, loader_test, logger, device=device)
# evaluate the performance
stats['test_avg_pre'], stats['test_avg_rec'], stats['test_avg_hmean'], \
stats['test_pre_curve'], stats['test_rec_curve'], stats['test_mean_ap'] = \
eval_perf(gallery_codes, gallery_label, test_codes, test_label,
top_k=TOP_K, hamm_radius=HAMM_RADIUS)
logger.write("Test completed in {:0.0f} seconds"
.format(time() - start))
logger.write("test MAP: {:.8f}, ".format(stats['test_mean_ap']) +
"avg precision: {:.6f}, ".format(stats['test_avg_pre']) +
"avg recall: {:.6f}, ".format(stats['test_avg_rec']) +
"avg harmonic mean: {:0.6f}"
.format(stats['test_avg_hmean']))
logger.write("====== END ======")
logger.write("Completed run for {}".format(run_id))
with open(stats_file_path, 'wb') as file:
pickle.dump(stats, file)