Skip to content

Commit 6fe706c

Browse files
committed
Demo and PR_Curve
1 parent 84670de commit 6fe706c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

148 files changed

+1751
-617
lines changed

ADSH.py

+6-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from utils.tools import *
22
from network import *
3-
43
import os
54
import torch
65
import torch.optim as optim
7-
import torch.nn as nn
86
from tqdm import tqdm
97
import time
108
import numpy as np
@@ -128,34 +126,13 @@ def train_val(config, bit):
128126
V[:, k] = -(2 * V_ @ (U_.t() @ Uk) + Qk).sign()
129127

130128
if (iter + 1) % config["test_map"] == 0:
131-
# print("calculating test binary code......")
132-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
133-
134-
# print("calculating dataset binary code.......")
135-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
136-
137-
# print("calculating map.......")
138-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
139-
config["topK"])
140-
141-
if mAP > Best_mAP:
142-
Best_mAP = mAP
143-
144-
if "save_path" in config:
145-
if not os.path.exists(config["save_path"]):
146-
os.makedirs(config["save_path"])
147-
print("save in ", config["save_path"])
148-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
149-
trn_binary.numpy())
150-
torch.save(net.state_dict(),
151-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
152-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
153-
config["info"], iter + 1, bit, config["dataset"], mAP, Best_mAP))
154-
print(config)
129+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
155130

156-
157-
if __name__ == "__main__":
131+
def main():
158132
config = get_config()
159-
print(config)
160133
for bit in config["bit_list"]:
134+
config["pr_curve_path"] = f"log/alexnet/ADSH_{config['dataset']}_{bit}.json"
161135
train_val(config, bit)
136+
137+
if __name__ == "__main__":
138+
main()

CNNH.py

+2-24
Original file line numberDiff line numberDiff line change
@@ -145,30 +145,8 @@ def train_val(config, bit):
145145
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
146146

147147
if (epoch + 1) % config["test_map"] == 0:
148-
# print("calculating test binary code......")
149-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
150-
151-
# print("calculating dataset binary code.......")\
152-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
153-
154-
# print("calculating map.......")
155-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
156-
config["topK"])
157-
158-
if mAP > Best_mAP:
159-
Best_mAP = mAP
160-
161-
if "save_path" in config:
162-
if not os.path.exists(config["save_path"]):
163-
os.makedirs(config["save_path"])
164-
print("save in ", config["save_path"])
165-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
166-
trn_binary.numpy())
167-
torch.save(net.state_dict(),
168-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
169-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
170-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
171-
print(config)
148+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
149+
172150

173151

174152
if __name__ == "__main__":

CSQ.py

+3-24
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_config():
4646
"epoch": 150,
4747
"test_map": 10,
4848
# "device":torch.device("cpu"),
49-
"device": torch.device("cuda:1"),
49+
"device": torch.device("cuda:0"),
5050
"bit_list": [64],
5151
}
5252
config = config_dataset(config)
@@ -153,34 +153,13 @@ def train_val(config, bit):
153153
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
154154

155155
if (epoch + 1) % config["test_map"] == 0:
156-
# print("calculating test binary code......")
157-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
156+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
158157

159-
# print("calculating dataset binary code.......")\
160-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
161-
162-
# print("calculating map.......")
163-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
164-
config["topK"])
165-
166-
if mAP > Best_mAP:
167-
Best_mAP = mAP
168-
169-
if "save_path" in config:
170-
if not os.path.exists(config["save_path"]):
171-
os.makedirs(config["save_path"])
172-
print("save in ", config["save_path"])
173-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
174-
trn_binary.numpy())
175-
torch.save(net.state_dict(),
176-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
177-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
178-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
179-
print(config)
180158

181159

182160
if __name__ == "__main__":
183161
config = get_config()
184162
print(config)
185163
for bit in config["bit_list"]:
164+
config["pr_curve_path"] = f"log/alexnet/CSQ_{config['dataset']}_{bit}.json"
186165
train_val(config, bit)

DAPH.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -160,30 +160,7 @@ def train_val(config, bit):
160160
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
161161

162162
if (epoch + 1) % config["test_map"] == 0:
163-
# print("calculating test binary code......")
164-
tst_binary, tst_label = compute_result(test_loader, net_bottom, device=device)
165-
166-
# print("calculating dataset binary code.......")\
167-
trn_binary, trn_label = compute_result(dataset_loader, net_bottom, device=device)
168-
169-
# print("calculating map.......")
170-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
171-
config["topK"])
172-
173-
if mAP > Best_mAP:
174-
Best_mAP = mAP
175-
176-
if "save_path" in config:
177-
if not os.path.exists(config["save_path"]):
178-
os.makedirs(config["save_path"])
179-
print("save in ", config["save_path"])
180-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
181-
trn_binary.numpy())
182-
torch.save(net.state_dict(),
183-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
184-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
185-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
186-
print(config)
163+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net_bottom, bit, epoch, num_dataset)
187164

188165

189166
if __name__ == "__main__":

DBDH.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -120,30 +120,7 @@ def train_val(config, bit):
120120
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
121121

122122
if (epoch + 1) % config["test_map"] == 0:
123-
# print("calculating test binary code......")
124-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
125-
126-
# print("calculating dataset binary code.......")\
127-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
128-
129-
# print("calculating map.......")
130-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
131-
config["topK"])
132-
133-
if mAP > Best_mAP:
134-
Best_mAP = mAP
135-
136-
if "save_path" in config:
137-
if not os.path.exists(config["save_path"]):
138-
os.makedirs(config["save_path"])
139-
print("save in ", config["save_path"])
140-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
141-
trn_binary.numpy())
142-
torch.save(net.state_dict(),
143-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
144-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
145-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
146-
print(config)
123+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
147124

148125

149126
if __name__ == "__main__":

DCH.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,7 @@ def train_val(config, bit):
124124
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
125125

126126
if (epoch + 1) % config["test_map"] == 0:
127-
# print("calculating test binary code......")
128-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
129-
130-
# print("calculating dataset binary code.......")\
131-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
132-
133-
# print("calculating map.......")
134-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
135-
config["topK"])
136-
137-
if mAP > Best_mAP:
138-
Best_mAP = mAP
139-
140-
if "save_path" in config:
141-
if not os.path.exists(config["save_path"]):
142-
os.makedirs(config["save_path"])
143-
print("save in ", config["save_path"])
144-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
145-
trn_binary.numpy())
146-
torch.save(net.state_dict(),
147-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
148-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
149-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
150-
print(config)
127+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
151128

152129

153130
if __name__ == "__main__":

DFH.py

+1-24
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,7 @@ def train_val(config, bit):
149149
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
150150

151151
if (epoch + 1) % config["test_map"] == 0:
152-
# print("calculating test binary code......")
153-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
154-
155-
# print("calculating dataset binary code.......")\
156-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
157-
158-
# print("calculating map.......")
159-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
160-
config["topK"])
161-
162-
if mAP > Best_mAP:
163-
Best_mAP = mAP
164-
165-
if "save_path" in config:
166-
if not os.path.exists(config["save_path"]):
167-
os.makedirs(config["save_path"])
168-
print("save in ", config["save_path"])
169-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
170-
trn_binary.numpy())
171-
torch.save(net.state_dict(),
172-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
173-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
174-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
175-
print(config)
152+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
176153

177154

178155
if __name__ == "__main__":

DHN.py

+2-26
Original file line numberDiff line numberDiff line change
@@ -109,36 +109,12 @@ def train_val(config, bit):
109109
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
110110

111111
if (epoch + 1) % config["test_map"] == 0:
112-
# print("calculating test binary code......")
113-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
114-
115-
# print("calculating dataset binary code.......")\
116-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
117-
118-
# print("calculating map.......")
119-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
120-
config["topK"])
121-
122-
if mAP > Best_mAP:
123-
Best_mAP = mAP
124-
if "cifar10-1" == config["dataset"] and epoch > 29:
125-
P, R = pr_curve(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy())
126-
print(f'Precision Recall Curve data:\n"DHN":[{P},{R}],')
127-
if "save_path" in config:
128-
if not os.path.exists(config["save_path"]):
129-
os.makedirs(config["save_path"])
130-
print("save in ", config["save_path"])
131-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
132-
trn_binary.numpy())
133-
torch.save(net.state_dict(),
134-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
135-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
136-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
137-
print(config)
112+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
138113

139114

140115
if __name__ == "__main__":
141116
config = get_config()
142117
print(config)
143118
for bit in config["bit_list"]:
119+
config["pr_curve_path"] = f"log/alexnet/DHN_{config['dataset']}_{bit}.json"
144120
train_val(config, bit)

DPN.py

+4-25
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ def get_config():
3232
"batch_size": 32,
3333
"net": AlexNet,
3434
# "net": ResNet,
35-
# "dataset": "cifar10-1",
35+
"dataset": "cifar10-1",
3636
# "dataset": "imagenet",
3737
# "dataset": "coco",
38-
"dataset": "nuswide_21",
38+
# "dataset": "nuswide_21",
3939
"epoch": 150,
4040
"test_map": 10,
4141
# "device":torch.device("cpu"),
@@ -139,34 +139,13 @@ def train_val(config, bit):
139139
print("\b\b\b\b\b\b\b loss:%.3f" % (train_loss))
140140

141141
if (epoch + 1) % config["test_map"] == 0:
142-
# print("calculating test binary code......")
143-
tst_binary, tst_label = compute_result(test_loader, net, device=device)
142+
Best_mAP = validate(config, Best_mAP, test_loader, dataset_loader, net, bit, epoch, num_dataset)
144143

145-
# print("calculating dataset binary code.......")\
146-
trn_binary, trn_label = compute_result(dataset_loader, net, device=device)
147-
148-
# print("calculating map.......")
149-
mAP = CalcTopMap(trn_binary.numpy(), tst_binary.numpy(), trn_label.numpy(), tst_label.numpy(),
150-
config["topK"])
151-
152-
if mAP > Best_mAP:
153-
Best_mAP = mAP
154-
155-
if "save_path" in config:
156-
if not os.path.exists(config["save_path"]):
157-
os.makedirs(config["save_path"])
158-
print("save in ", config["save_path"])
159-
np.save(os.path.join(config["save_path"], config["dataset"] + str(mAP) + "-" + "trn_binary.npy"),
160-
trn_binary.numpy())
161-
torch.save(net.state_dict(),
162-
os.path.join(config["save_path"], config["dataset"] + "-" + str(mAP) + "-model.pt"))
163-
print("%s epoch:%d, bit:%d, dataset:%s, MAP:%.3f, Best MAP: %.3f" % (
164-
config["info"], epoch + 1, bit, config["dataset"], mAP, Best_mAP))
165-
print(config)
166144

167145

168146
if __name__ == "__main__":
169147
config = get_config()
170148
print(config)
171149
for bit in config["bit_list"]:
150+
config["pr_curve_path"] = f"log/alexnet/DPN_{config['dataset']}_{bit}.json"
172151
train_val(config, bit)

0 commit comments

Comments
 (0)