File size: 10,900 Bytes
e857f97 | 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | import math
import pickle
import numpy as np
import torch
import torch.nn as nn
import random
from collections import namedtuple
def calc_recalls(S):
"""
Computes recall at 1, 5, and 10 given a similarity matrix S.
By convention, rows of S are assumed to correspond to images and columns are captions.
"""
assert(S.dim() == 2)
assert(S.size(0) == S.size(1))
if isinstance(S, torch.autograd.Variable):
S = S.data
n = S.size(0)
A2I_scores, A2I_ind = S.topk(10, 0)
I2A_scores, I2A_ind = S.topk(10, 1)
A_r1 = AverageMeter()
A_r5 = AverageMeter()
A_r10 = AverageMeter()
I_r1 = AverageMeter()
I_r5 = AverageMeter()
I_r10 = AverageMeter()
for i in range(n):
A_foundind = -1
I_foundind = -1
for ind in range(10):
if A2I_ind[ind, i] == i:
I_foundind = ind
if I2A_ind[i, ind] == i:
A_foundind = ind
# do r1s
if A_foundind == 0:
A_r1.update(1)
else:
A_r1.update(0)
if I_foundind == 0:
I_r1.update(1)
else:
I_r1.update(0)
# do r5s
if A_foundind >= 0 and A_foundind < 5:
A_r5.update(1)
else:
A_r5.update(0)
if I_foundind >= 0 and I_foundind < 5:
I_r5.update(1)
else:
I_r5.update(0)
# do r10s
if A_foundind >= 0 and A_foundind < 10:
A_r10.update(1)
else:
A_r10.update(0)
if I_foundind >= 0 and I_foundind < 10:
I_r10.update(1)
else:
I_r10.update(0)
recalls = {'A_r1':A_r1.avg, 'A_r5':A_r5.avg, 'A_r10':A_r10.avg,
'I_r1':I_r1.avg, 'I_r5':I_r5.avg, 'I_r10':I_r10.avg}
#'A_meanR':A_meanR.avg, 'I_meanR':I_meanR.avg}
return recalls
def computeMatchmap(I, A):
assert(I.dim() == 3)
assert(A.dim() == 2)
D = I.size(0)
H = I.size(1)
W = I.size(2)
T = A.size(1)
Ir = I.view(D, -1).t()
matchmap = torch.mm(Ir, A)
matchmap = matchmap.view(H, W, T)
return matchmap
def matchmapSim(M, simtype):
assert(M.dim() == 3)
if simtype == 'SISA':
return M.mean()
elif simtype == 'MISA':
M_maxH, _ = M.max(0)
M_maxHW, _ = M_maxH.max(0)
return M_maxHW.mean()
elif simtype == 'SIMA':
M_maxT, _ = M.max(2)
return M_maxT.mean()
else:
raise ValueError
def sampled_margin_rank_loss(image_outputs, audio_outputs, nframes, margin=1., simtype='MISA'):
"""
Computes the triplet margin ranking loss for each anchor image/caption pair
The impostor image/caption is randomly sampled from the minibatch
"""
assert(image_outputs.dim() == 4)
assert(audio_outputs.dim() == 3)
n = image_outputs.size(0)
loss = torch.zeros(1, device=image_outputs.device, requires_grad=True)
for i in range(n):
I_imp_ind = i
A_imp_ind = i
while I_imp_ind == i:
I_imp_ind = np.random.randint(0, n)
while A_imp_ind == i:
A_imp_ind = np.random.randint(0, n)
nF = nframes[i]
nFimp = nframes[A_imp_ind]
anchorsim = matchmapSim(computeMatchmap(image_outputs[i], audio_outputs[i][:, 0:nF]), simtype)
Iimpsim = matchmapSim(computeMatchmap(image_outputs[I_imp_ind], audio_outputs[i][:, 0:nF]), simtype)
Aimpsim = matchmapSim(computeMatchmap(image_outputs[i], audio_outputs[A_imp_ind][:, 0:nFimp]), simtype)
A2I_simdif = margin + Iimpsim - anchorsim
if (A2I_simdif.data > 0).all():
loss = loss + A2I_simdif
I2A_simdif = margin + Aimpsim - anchorsim
if (I2A_simdif.data > 0).all():
loss = loss + I2A_simdif
loss = loss / n
return loss
def compute_matchmap_similarity_matrix(image_outputs, audio_outputs, nframes, simtype='MISA'):
"""
Assumes image_outputs is a (batchsize, embedding_dim, rows, height) tensor
Assumes audio_outputs is a (batchsize, embedding_dim, 1, time) tensor
Returns similarity matrix S where images are rows and audios are along the columns
"""
assert(image_outputs.dim() == 4)
assert(audio_outputs.dim() == 3)
n = image_outputs.size(0)
S = torch.zeros(n, n, device=image_outputs.device)
for image_idx in range(n):
for audio_idx in range(n):
nF = max(1, nframes[audio_idx])
S[image_idx, audio_idx] = matchmapSim(computeMatchmap(image_outputs[image_idx], audio_outputs[audio_idx][:, 0:nF]), simtype)
return S
def compute_pooldot_similarity_matrix(image_outputs, audio_outputs, nframes):
"""
Assumes image_outputs is a (batchsize, embedding_dim, rows, height) tensor
Assumes audio_outputs is a (batchsize, embedding_dim, 1, time) tensor
Returns similarity matrix S where images are rows and audios are along the columns
S[i][j] is computed as the dot product between the meanpooled embeddings of
the ith image output and jth audio output
"""
assert(image_outputs.dim() == 4)
assert(audio_outputs.dim() == 4)
n = image_outputs.size(0)
imagePoolfunc = nn.AdaptiveAvgPool2d((1, 1))
pooled_image_outputs = imagePoolfunc(image_outputs).squeeze(3).squeeze(2)
audioPoolfunc = nn.AdaptiveAvgPool2d((1, 1))
pooled_audio_outputs_list = []
for idx in range(n):
nF = max(1, nframes[idx])
pooled_audio_outputs_list.append(audioPoolfunc(audio_outputs[idx][:, :, 0:nF]).unsqueeze(0))
pooled_audio_outputs = torch.cat(pooled_audio_outputs_list).squeeze(3).squeeze(2)
S = torch.mm(pooled_image_outputs, pooled_audio_outputs.t())
return S
def one_imposter_index(i, N):
imp_ind = random.randint(0, N - 2)
if imp_ind == i:
imp_ind = N - 1
return imp_ind
def basic_get_imposter_indices(N):
imposter_idc = []
for i in range(N):
# Select an imposter index for example i:
imp_ind = one_imposter_index(i, N)
imposter_idc.append(imp_ind)
return imposter_idc
def semihardneg_triplet_loss_from_S(S, margin):
"""
Input: Similarity matrix S as an autograd.Variable
Output: The one-way triplet loss from rows of S to columns of S. Impostors are taken
to be the most similar point to the anchor that is still less similar to the anchor
than the positive example.
You would need to run this function twice, once with S and once with S.t(),
in order to compute the triplet loss in both directions.
"""
assert(S.dim() == 2)
assert(S.size(0) == S.size(1))
N = S.size(0)
loss = torch.autograd.Variable(torch.zeros(1).type(S.data.type()), requires_grad=True)
# Imposter - ground truth
Sdiff = S - torch.diag(S).view(-1, 1)
eps = 1e-12
# All examples less similar than ground truth
mask = (Sdiff < -eps).type(torch.LongTensor)
maskf = mask.type_as(S)
# Mask out all examples >= gt with minimum similarity
Sp = maskf * Sdiff + (1 - maskf) * torch.min(Sdiff).detach()
# Find the index maximum similar of the remaining
_, idc = Sp.max(dim=1)
idc = idc.data.cpu()
# Vector mask: 1 iff there exists an example < gt
has_neg = (mask.sum(dim=1) > 0).data.type(torch.LongTensor)
# Random imposter indices
random_imp_ind = torch.LongTensor(basic_get_imposter_indices(N))
# Use hardneg if there exists an example < gt, otherwise use random imposter
imp_idc = has_neg * idc + (1 - has_neg) * random_imp_ind
# This could probably be vectorized too, but I haven't.
for i, imp in enumerate(imp_idc):
local_loss = Sdiff[i, imp] + margin
if (local_loss.data > 0).all():
loss = loss + local_loss
loss = loss / N
return loss
def sampled_triplet_loss_from_S(S, margin):
"""
Input: Similarity matrix S as an autograd.Variable
Output: The one-way triplet loss from rows of S to columns of S. Imposters are
randomly sampled from the columns of S.
You would need to run this function twice, once with S and once with S.t(),
in order to compute the triplet loss in both directions.
"""
assert(S.dim() == 2)
assert(S.size(0) == S.size(1))
N = S.size(0)
loss = torch.autograd.Variable(torch.zeros(1).type(S.data.type()), requires_grad=True)
# Imposter - ground truth
Sdiff = S - torch.diag(S).view(-1, 1)
imp_ind = torch.LongTensor(basic_get_imposter_indices(N))
# This could probably be vectorized too, but I haven't.
for i, imp in enumerate(imp_ind):
local_loss = Sdiff[i, imp] + margin
if (local_loss.data > 0).all():
loss = loss + local_loss
loss = loss / N
return loss
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def adjust_learning_rate(base_lr, lr_decay, optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every lr_decay epochs"""
lr = base_lr * (0.1 ** (epoch // lr_decay))
print('now learning rate changed to {:f}'.format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def adjust_learning_rate2(base_lr, lr_decay, optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every lr_decay epochs"""
for param_group in optimizer.param_groups:
cur_lr = param_group['lr']
print('current learing rate is {:f}'.format(lr))
lr = cur_lr * 0.1
print('now learning rate changed to {:f}'.format(lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def load_progress(prog_pkl, quiet=False):
"""
load progress pkl file
Args:
prog_pkl(str): path to progress pkl file
Return:
progress(list):
epoch(int):
global_step(int):
best_epoch(int):
best_avg_r10(float):
"""
def _print(msg):
if not quiet:
print(msg)
with open(prog_pkl, "rb") as f:
prog = pickle.load(f)
epoch, global_step, best_epoch, best_avg_r10, _ = prog[-1]
_print("\nPrevious Progress:")
msg = "[%5s %7s %5s %7s %6s]" % ("epoch", "step", "best_epoch", "best_avg_r10", "time")
_print(msg)
return prog, epoch, global_step, best_epoch, best_avg_r10
def count_parameters(model):
return sum([p.numel() for p in model.parameters() if p.requires_grad])
PrenetConfig = namedtuple(
'PrenetConfig', ['input_size', 'hidden_size', 'num_layers', 'dropout'])
RNNConfig = namedtuple(
'RNNConfig',
['input_size', 'hidden_size', 'num_layers', 'dropout', 'residual']) |