-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update model and optimize valid step.
- Loading branch information
1 parent
83f9352
commit 8aedde8
Showing
21 changed files
with
442 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
training: | ||
method: GCH | ||
method: GANCMH | ||
dataName: Mirflickr25K | ||
batchSize: 64 | ||
bit: 64 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2019/12/2 | ||
# @Author : Godder | ||
# @Github : https://github.com/WangGodder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2019/12/2 | ||
# @Author : Godder | ||
# @Github : https://github.com/WangGodder | ||
import torch | ||
__all__ = ['multi_label_acc'] | ||
|
||
|
||
def multi_label_acc(predict: torch.Tensor, label: torch.Tensor): | ||
""" | ||
Multi-label acc evaluate. | ||
acc = \frac{predict right label}{number of label} | ||
:param predict: the predict label | ||
:param label: the ground truth label with same shape as predict | ||
:return: the mean accuracy of all predict instances. | ||
""" | ||
assert predict.shape == label.shape | ||
label_num = torch.sum(label, dim=-1) | ||
acc = 0 | ||
for i in range(predict.size(0)): | ||
_, predict_ind = torch.topk(predict[i, :], int(label_num[i])) | ||
right_num = torch.sum(label[i][predict_ind]) | ||
acc += (right_num / label_num[i]).item() | ||
acc /= predict.size(0) | ||
return acc | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2019/12/2 | ||
# @Author : Godder | ||
# @Github : https://github.com/WangGodder | ||
import torch | ||
__all__ = ['calc_map_k', 'calc_precisions_topn'] | ||
|
||
|
||
def calc_hammingDist(B1, B2): | ||
q = B2.shape[1] | ||
if len(B1.shape) < 2: | ||
B1 = B1.unsqueeze(0) | ||
distH = 0.5 * (q - B1.mm(B2.transpose(0, 1))) | ||
return distH | ||
|
||
|
||
def calc_map_k(qB, rB, query_L, retrieval_L, k=None): | ||
# qB: {-1,+1}^{mxq} | ||
# rB: {-1,+1}^{nxq} | ||
# query_L: {0,1}^{mxl} | ||
# retrieval_L: {0,1}^{nxl} | ||
num_query = query_L.shape[0] | ||
qB = torch.sign(qB) | ||
rB = torch.sign(rB) | ||
map = 0 | ||
if k is None: | ||
k = retrieval_L.shape[0] | ||
for iter in range(num_query): | ||
q_L = query_L[iter] | ||
if len(q_L.shape) < 2: | ||
q_L = q_L.unsqueeze(0) # [1, hash length] | ||
gnd = (q_L.mm(retrieval_L.transpose(0, 1)) > 0).squeeze().type(torch.float32) | ||
tsum = torch.sum(gnd) | ||
if tsum == 0: | ||
continue | ||
hamm = calc_hammingDist(qB[iter, :], rB) | ||
_, ind = torch.sort(hamm) | ||
ind.squeeze_() | ||
gnd = gnd[ind] | ||
total = min(k, int(tsum)) | ||
count = torch.arange(1, total + 1).type(torch.float32) | ||
tindex = torch.nonzero(gnd)[:total].squeeze().type(torch.float32) + 1.0 | ||
if tindex.is_cuda: | ||
count = count.cuda() | ||
map = map + torch.mean(count / tindex) | ||
map = map / num_query | ||
return map | ||
|
||
|
||
def calc_precisions_topn(qB, rB, query_L, retrieval_L, recall_gas=0.02, num_retrieval=10000): | ||
qB = qB.float() | ||
rB = rB.float() | ||
qB = torch.sign(qB - 0.5) | ||
rB = torch.sign(rB - 0.5) | ||
num_query = query_L.shape[0] | ||
# num_retrieval = retrieval_L.shape[0] | ||
precisions = [0] * int(1 / recall_gas) | ||
for iter in range(num_query): | ||
q_L = query_L[iter] | ||
if len(q_L.shape) < 2: | ||
q_L = q_L.unsqueeze(0) # [1, hash length] | ||
gnd = (q_L.mm(retrieval_L.transpose(0, 1)) > 0).squeeze().type(torch.float32) | ||
hamm = calc_hammingDist(qB[iter, :], rB) | ||
_, ind = torch.sort(hamm) | ||
ind.squeeze_() | ||
gnd = gnd[ind] | ||
for i, recall in enumerate(np.arange(recall_gas, 1 + recall_gas, recall_gas)): | ||
total = int(num_retrieval * recall) | ||
right = torch.nonzero(gnd[: total]).squeeze().numpy() | ||
# right_num = torch.nonzero(gnd[: total]).squeeze().shape[0] | ||
right_num = right.size | ||
precisions[i] += (right_num/total) | ||
for i in range(len(precisions)): | ||
precisions[i] /= num_query | ||
return precisions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# -*- coding: utf-8 -*- | ||
# @Time : 2019/7/22 | ||
# @Author : Godder | ||
# @Github : https://github.com/WangGodder | ||
from torch import nn | ||
from torch.nn import functional as F | ||
from torchcmh.models import BasicModule | ||
import torch | ||
|
||
|
||
__all__ = ['MLP'] | ||
|
||
|
||
def weights_init(m): | ||
if type(m) == nn.Conv2d: | ||
nn.init.normal_(m.weight.data, 0.0, 0.01) | ||
nn.init.normal_(m.bias.data, 0.0, 0.01) | ||
elif type(m) == nn.Conv1d: | ||
nn.init.normal_(m.weight.data, 0.0, 0.01) | ||
nn.init.normal_(m.bias.data, 0.0, 0.01) | ||
|
||
|
||
class MLP(BasicModule): | ||
def __init__(self, input_dim, output_dim, hidden_nodes=[8192], dropout=None, leakRelu=True): | ||
""" | ||
:param input_dim: dimension of input | ||
:param output_dim: bit number of the final binary code | ||
""" | ||
super(MLP, self).__init__() | ||
self.module_name = "MLP" | ||
|
||
# full-conv layers | ||
full_conv_layers = [] | ||
in_channel = 1 | ||
for hidden_node in hidden_nodes: | ||
kernel_size = input_dim if in_channel == 1 else 1 | ||
full_conv_layers.append(nn.Conv1d(in_channel, hidden_node, kernel_size=kernel_size, stride=1)) | ||
in_channel = hidden_node | ||
if dropout: | ||
full_conv_layers.append(nn.Dropout(dropout)) | ||
if leakRelu: | ||
full_conv_layers.append(nn.LeakyReLU(inplace=True)) | ||
else: | ||
full_conv_layers.append(nn.ReLU(inplace=True)) | ||
full_conv_layers.append(nn.Conv1d(in_channel, output_dim, kernel_size=1, stride=1)) | ||
self.layers = nn.Sequential(*full_conv_layers) | ||
# self.conv1 = nn.Conv2d(1, hidden_node, kernel_size=(input_dim, 1), stride=(1, 1)) | ||
# self.dropout = nn.Dropout(dropout) if dropout else None | ||
# self.conv2 = nn.Conv2d(hidden_node, bit, kernel_size=1, stride=(1, 1)) | ||
self.apply(weights_init) | ||
|
||
def forward(self, x: torch.Tensor): | ||
if len(x.shape) == 2: | ||
x = x.unsqueeze(1) | ||
if len(x.shape) > 3: | ||
x = x.squeeze().unsqueeze(1) | ||
x = self.layers(x) | ||
x = x.squeeze() | ||
return x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.