def forward(self, x):
x_shape = x.size() # (b, c, h, w)
offset = self.offset_filter(x) # (b, 2*c, h, w)
offset_w, offset_h = torch.split(offset, self.regular_filter.in_channels, 1) # (b, c, h, w)
offset_w = offset_w.contiguous().view(-1, int(x_shape[2]), int(x_shape[3])) # (b*c, h, w)
offset_h = offset_h.contiguous().view(-1, int(x_shape[2]), int(x_shape[3])) # (b*c, h, w)
if not self.input_shape or self.input_shape != x_shape:
self.input_shape = x_shape
grid_w, grid_h = np.meshgrid(np.linspace(-1, 1, x_shape[3]), np.linspace(-1, 1, x_shape[2])) # (h, w)
grid_w = torch.Tensor(grid_w)
grid_h = torch.Tensor(grid_h)
if self.cuda:
grid_w = grid_w.cuda()
grid_h = grid_h.cuda()
self.grid_w = nn.Parameter(grid_w)
self.grid_h = nn.Parameter(grid_h)
offset_w = offset_w + self.grid_w # (b*c, h, w)
offset_h = offset_h + self.grid_h # (b*c, h, w)
x = x.contiguous().view(-1, int(x_shape[2]), int(x_shape[3])).unsqueeze(1) # (b*c, 1, h, w)
x = F.grid_sample(x, torch.stack((offset_h, offset_w), 3)) # (b*c, h, w)
x = x.contiguous().view(-1, int(x_shape[1]), int(x_shape[2]), int(x_shape[3])) # (b, c, h, w)
x = self.regular_filter(x)
return x
python类stack()的实例源码
def forward(self, x):
outputs = []
h_t = Variable(torch.zeros(x.size(0), self.hidden_size).cuda())
c_t = Variable(torch.zeros(x.size(0), self.hidden_size).cuda())
for i, input_t in enumerate(x.chunk(x.size(1), dim=1)):
input_t = input_t.contiguous().view(input_t.size()[0], 1)
h_t, c_t = self.lstm1(input_t, (h_t, c_t))
outputs += [c_t]
outputs = torch.stack(outputs, 1).squeeze(2)
shp=(outputs.size()[0], outputs.size()[1])
out = outputs.contiguous().view(shp[0] *shp[1] , self.hidden_size)
out = self.fc(out)
out = out.view(shp[0], shp[1], self.num_classes)
return out
def Occlusion_exp(image,occluding_size,occluding_stride,model,preprocess,classes,groundTruth):
img = np.copy(image)
height, width,_= img.shape
output_height = int(math.ceil((height-occluding_size)/occluding_stride+1))
output_width = int(math.ceil((width-occluding_size)/occluding_stride+1))
ocludedImages=[]
for h in range(output_height):
for w in range(output_width):
#occluder region
h_start = h*occluding_stride
w_start = w*occluding_stride
h_end = min(height, h_start + occluding_size)
w_end = min(width, w_start + occluding_size)
input_image = copy.copy(img)
input_image[h_start:h_end,w_start:w_end,:] = 0
ocludedImages.append(preprocess(Image.fromarray(input_image)))
L = np.empty(output_height*output_width)
L.fill(groundTruth)
L = torch.from_numpy(L)
tensor_images = torch.stack([img for img in ocludedImages])
dataset = torch.utils.data.TensorDataset(tensor_images,L)
dataloader = torch.utils.data.DataLoader(dataset,batch_size=5,shuffle=False, num_workers=8)
heatmap=np.empty(0)
model.eval()
for data in dataloader:
images, labels = data
if use_gpu:
images, labels = (images.cuda()), (labels.cuda(async=True))
outputs = model(Variable(images))
m = nn.Softmax()
outputs=m(outputs)
if use_gpu:
outs=outputs.cpu()
heatmap = np.concatenate((heatmap,outs[0:outs.size()[0],groundTruth].data.numpy()))
return heatmap.reshape((output_height, output_width))
def detection_collate(batch):
"""Custom collate fn for dealing with batches of images that have a different
number of associated object annotations (bounding boxes).
Arguments:
batch: (tuple) A tuple of tensor images and lists of annotations
Return:
A tuple containing:
1) (tensor) batch of images stacked on their 0 dim
2) (list of tensors) annotations for a given image are stacked on 0 dim
"""
targets = []
imgs = []
for sample in batch:
imgs.append(sample[0])
targets.append(torch.FloatTensor(sample[1]))
return torch.stack(imgs, 0), targets
def batch_tensors(cls, tensor_list: List[DataArray]) -> DataArray: # type: ignore
"""
Takes the output of ``Field.as_tensor()`` from a list of ``Instances`` and merges it into
one batched tensor for this ``Field``. The default implementation here in the base class
handles cases where ``as_tensor`` returns a single torch tensor per instance, or a
dictionary of single tensors. If your subclass returns something other than this, you need
to override this method.
"""
if isinstance(tensor_list[0], dict):
# This is creating a dict of {token_indexer_key: batch_tensor} for each
# token indexer used to index this field. This is mostly utilised by TextFields.
token_indexer_key_to_batch_dict: Dict[str, List[torch.Tensor]] = defaultdict(list)
for encoding_name_dict in tensor_list:
for indexer_name, tensor in encoding_name_dict.items():
token_indexer_key_to_batch_dict[indexer_name].append(tensor)
return {indexer_name: torch.stack(tensor_list)
for indexer_name, tensor_list in token_indexer_key_to_batch_dict.items()}
else:
return torch.stack(tensor_list)
def make_batch(batch_size):
batch_idx = np.random.choice(len(data),batch_size)
batch_sequences = [data[idx] for idx in batch_idx]
strokes = []
lengths = []
indice = 0
for seq in batch_sequences:
len_seq = len(seq[:,0])
new_seq = np.zeros((Nmax,5))
new_seq[:len_seq,:2] = seq[:,:2]
new_seq[:len_seq-1,2] = 1-seq[:-1,2]
new_seq[:len_seq,3] = seq[:,2]
new_seq[(len_seq-1):,4] = 1
new_seq[len_seq-1,2:4] = 0
lengths.append(len(seq[:,0]))
strokes.append(new_seq)
indice += 1
if use_cuda:
batch = Variable(torch.from_numpy(np.stack(strokes,1)).cuda().float())
else:
batch = Variable(torch.from_numpy(np.stack(strokes,1)).float())
return batch, lengths
################################ adaptive lr
def make_target(self, batch, lengths):
if use_cuda:
eos = Variable(torch.stack([torch.Tensor([0,0,0,0,1])]\
*batch.size()[1]).cuda()).unsqueeze(0)
else:
eos = Variable(torch.stack([torch.Tensor([0,0,0,0,1])]\
*batch.size()[1])).unsqueeze(0)
batch = torch.cat([batch, eos], 0)
mask = torch.zeros(Nmax+1, batch.size()[1])
for indice,length in enumerate(lengths):
mask[:length,indice] = 1
if use_cuda:
mask = Variable(mask.cuda()).detach()
else:
mask = Variable(mask).detach()
dx = torch.stack([Variable(batch.data[:,:,0])]*hp.M,2).detach()
dy = torch.stack([Variable(batch.data[:,:,1])]*hp.M,2).detach()
p1 = Variable(batch.data[:,:,2]).detach()
p2 = Variable(batch.data[:,:,3]).detach()
p3 = Variable(batch.data[:,:,4]).detach()
p = torch.stack([p1,p2,p3],2)
return mask,dx,dy,p
def select_last(inputs, lengths, hidden_size):
"""
:param inputs: [T * B * D] D = 2 * hidden_size
:param lengths: [B]
:param hidden_size: dimension
:return: [B * D]
"""
batch_size = inputs.size(1)
batch_out_list = []
for b in range(batch_size):
batch_out_list.append(torch.cat((inputs[lengths[b] - 1, b, :hidden_size],
inputs[0, b, hidden_size:])
)
)
out = torch.stack(batch_out_list)
return out
def max_along_time(inputs, lengths):
"""
:param inputs: [T * B * D]
:param lengths: [B]
:return: [B * D] max_along_time
"""
ls = list(lengths)
b_seq_max_list = []
for i, l in enumerate(ls):
seq_i = inputs[:l, i, :]
seq_i_max, _ = seq_i.max(dim=0)
seq_i_max = seq_i_max.squeeze()
b_seq_max_list.append(seq_i_max)
return torch.stack(b_seq_max_list)
def _dist_and_values(self, *args, **kwargs):
# XXX currently this whole object is very inefficient
values, logits = [], []
for value, logit in self._gen_weighted_samples(*args, **kwargs):
ix = _index(values, value)
if ix == -1:
# Value is new.
values.append(value)
logits.append(logit)
else:
# Value has already been seen.
logits[ix] = util.log_sum_exp(torch.stack([logits[ix], logit]).squeeze())
logits = torch.stack(logits).squeeze()
logits -= util.log_sum_exp(logits)
if not isinstance(logits, torch.autograd.Variable):
logits = Variable(logits)
logits = logits - util.log_sum_exp(logits)
d = dist.Categorical(logits=logits, one_hot=False)
return d, values
def enumerate_support(self):
"""
Returns the Bernoulli distribution's support, as a tensor along the first dimension.
Note that this returns support values of all the batched RVs in lock-step, rather
than the full cartesian product. To iterate over the cartesian product, you must
construct univariate Bernoullis and use itertools.product() over all univariate
variables (may be expensive).
:return: torch variable enumerating the support of the Bernoulli distribution.
Each item in the return value, when enumerated along the first dimensions, yields a
value from the distribution's support which has the same dimension as would be returned by
sample.
:rtype: torch.autograd.Variable.
"""
return Variable(torch.stack([torch.Tensor([t]).expand_as(self.ps) for t in [0, 1]]))
def forward(self, input, c0=None, return_hidden=True):
assert input.dim() == 3 # (len, batch, n_in)
dir_ = 2 if self.bidirectional else 1
if c0 is None:
zeros = Variable(input.data.new(
input.size(1), self.n_out*dir_
).zero_())
c0 = [ zeros for i in range(self.depth) ]
else:
assert c0.dim() == 3 # (depth, batch, n_out*dir_)
c0 = [ x.squeeze(0) for x in c0.chunk(self.depth, 0) ]
prevx = input
lstc = []
for i, rnn in enumerate(self.rnn_lst):
h, c = rnn(prevx, c0[i])
prevx = h
lstc.append(c)
if return_hidden:
return prevx, torch.stack(lstc)
else:
return prevx
def translate(self, inputs, max_length):
targets, init_states = self.initialize(inputs, eval=True)
emb, output, hidden, context = init_states
preds = []
batch_size = targets.size(1)
num_eos = targets[0].data.byte().new(batch_size).zero_()
for i in range(max_length):
output, hidden = self.decoder.step(emb, output, hidden, context)
logit = self.generator(output)
pred = logit.max(1)[1].view(-1).data
preds.append(pred)
# Stop if all sentences reach EOS.
num_eos |= (pred == lib.Constants.EOS)
if num_eos.sum() == batch_size: break
emb = self.decoder.word_lut(Variable(pred))
preds = torch.stack(preds)
return preds
def sample(self, inputs, max_length):
targets, init_states = self.initialize(inputs, eval=False)
emb, output, hidden, context = init_states
outputs = []
samples = []
batch_size = targets.size(1)
num_eos = targets[0].data.byte().new(batch_size).zero_()
for i in range(max_length):
output, hidden = self.decoder.step(emb, output, hidden, context)
outputs.append(output)
dist = F.softmax(self.generator(output))
sample = dist.multinomial(1, replacement=False).view(-1).data
samples.append(sample)
# Stop if all sentences reach EOS.
num_eos |= (sample == lib.Constants.EOS)
if num_eos.sum() == batch_size: break
emb = self.decoder.word_lut(Variable(sample))
outputs = torch.stack(outputs)
samples = torch.stack(samples)
return samples, outputs
def __getitem__(self, index):
assert index < self.numBatches, "%d > %d" % (index, self.numBatches)
srcBatch, lengths = self._batchify(self.src[index*self.batchSize:(index+1)*self.batchSize],
include_lengths=True)
tgtBatch = self._batchify(self.tgt[index*self.batchSize:(index+1)*self.batchSize])
# within batch sort by decreasing length.
indices = range(len(srcBatch))
batch = zip(indices, srcBatch, tgtBatch)
batch, lengths = zip(*sorted(zip(batch, lengths), key=lambda x: -x[1]))
indices, srcBatch, tgtBatch = zip(*batch)
def wrap(b):
b = torch.stack(b, 0).t().contiguous()
if self.cuda:
b = b.cuda()
b = Variable(b, volatile=self.eval)
return b
return (wrap(srcBatch), lengths), wrap(tgtBatch), indices
def detection_collate(batch):
"""Custom collate fn for dealing with batches of images that have a different
number of associated object annotations (bounding boxes).
Arguments:
batch: (tuple) A tuple of tensor images and lists of annotations
Return:
A tuple containing:
1) (tensor) batch of images stacked on their 0 dim
2) (list of tensors) annotations for a given image are stacked on 0 dim
"""
targets = []
imgs = []
for sample in batch:
imgs.append(sample[0])
targets.append(torch.FloatTensor(sample[1]))
return torch.stack(imgs, 0), targets
def fetch_batch(self, part, batch_size: int = None):
if batch_size is None:
batch_size = self.batch_size
X, Y = self._fetch_batch(part, batch_size)
X = Variable(torch.from_numpy(X)).view(2*batch_size, self.image_size, self.image_size)
X1 = X[:batch_size] # (B, h, w)
X2 = X[batch_size:] # (B, h, w)
X = torch.stack([X1, X2], dim=1) # (B, 2, h, w)
Y = Variable(torch.from_numpy(Y))
if use_cuda:
X, Y = X.cuda(), Y.cuda()
return X, Y
def detection_collate(batch):
"""Custom collate fn for dealing with batches of images that have a different
number of associated object annotations (bounding boxes).
Arguments:
batch: (tuple) A tuple of tensor images and lists of annotations
Return:
A tuple containing:
1) (tensor) batch of images stacked on their 0 dim
2) (list of tensors) annotations for a given image are stacked on 0 dim
"""
targets = []
imgs = []
image_ids = []
for sample in batch:
imgs.append(sample[0])
targets.append(torch.FloatTensor(sample[1]))
image_ids.append(sample[2])
return torch.stack(imgs, 0), targets, image_ids
def _forward_rnn(cell, input_, grads_, length, hx):
max_time = input_.size(0)
output = []
for time in range(max_time):
hx = cell(input_=input_[time],grads_=grads_[time], hx=hx)
#mask = (time < length).float().unsqueeze(1).expand_as(h_next[0])
#fS_next = h_next[0] * mask + hx[0] * (1 - mask)
#iS_next = h_next[1] * mask + hx[1] * (1 - mask)
#cS_next = h_next[2] * mask + hx[2] * (1 - mask)
#deltaS_next = h_next[3] * mask + hx[3] * (1 - mask)
#hx_next = (fS_next, iS_next, cS_next, deltaS_next)
#output.append(h_next)
#hx = hx_next
#output = torch.stack(output, 0)
#return output,hx
#return hx[2],hx
return hx
def _forward_rnn(cell, input_, length, hx):
max_time = input_.size(0)
output = []
for time in range(max_time):
if isinstance(cell, BNLSTMCell):
h_next, c_next = cell(input_=input_[time], hx=hx, time=time)
else:
h_next, c_next = cell(input_=input_[time], hx=hx)
mask = (time < length).float().unsqueeze(1).expand_as(h_next)
h_next = h_next*mask + hx[0]*(1 - mask)
c_next = c_next*mask + hx[1]*(1 - mask)
hx_next = (h_next, c_next)
output.append(h_next)
hx = hx_next
output = torch.stack(output, 0)
return output, hx
def forward(self, input_, length=None, hx=None):
if self.batch_first:
input_ = input_.transpose(0, 1)
max_time, batch_size, _ = input_.size()
if length is None:
length = Variable(torch.LongTensor([max_time] * batch_size))
if input_.is_cuda:
length = length.cuda()
if hx is None:
hx = Variable(input_.data.new(batch_size, self.hidden_size).zero_())
hx = (hx, hx)
h_n = []
c_n = []
layer_output = None
for layer in range(self.num_layers):
layer_output, (layer_h_n, layer_c_n) = LSTM._forward_rnn(
cell=self.cells[layer], input_=input_, length=length, hx=hx)
input_ = self.dropout_layer(layer_output)
h_n.append(layer_h_n)
c_n.append(layer_c_n)
output = layer_output
h_n = torch.stack(h_n, 0)
c_n = torch.stack(c_n, 0)
return output, (h_n, c_n)
def forward(self, pretrained_word_tokens, word_tokens, pos_tokens):
lengths = np.array([len(tokens) for tokens in word_tokens])
X = self.forward_embed(
pretrained_word_tokens, word_tokens, pos_tokens, lengths)
indices = np.argsort(-np.array(lengths)).astype(np.int64)
lengths = lengths[indices]
X = torch.stack([X[idx] for idx in indices])
X = nn.utils.rnn.pack_padded_sequence(X, lengths, batch_first=True)
R = self.blstm(X)[0]
R = nn.utils.rnn.pad_packed_sequence(R, batch_first=True)[0]
R = R.index_select(dim=0, index=_model_var(
self, torch.from_numpy(np.argsort(indices).astype(np.int64))))
H_arc_head = self.mlp_arc_head(R)
H_arc_dep = self.mlp_arc_dep(R)
arc_logits = self.arc_biaffine(H_arc_dep, H_arc_head)
arc_logits = torch.squeeze(arc_logits, dim=3)
H_label_dep = self.mlp_label_dep(R)
H_label_head = self.mlp_label_head(R)
label_logits = self.label_biaffine(H_label_dep, H_label_head)
return arc_logits, label_logits
def forward(self, inputs, hidden):
def select_layer(h_state, i): # To work on both LSTM / GRU, RNN
if isinstance(h_state, tuple):
return tuple([select_layer(s, i) for s in h_state])
else:
return h_state[i]
next_hidden = []
for i, layer in enumerate(self.layers):
hidden_i = select_layer(hidden, i)
next_hidden_i = layer(inputs, hidden_i)
output = next_hidden_i[0] if isinstance(next_hidden_i, tuple) \
else next_hidden_i
if i + 1 != self.num_layers:
output = self.dropout(output)
if i > 0 and self.residual:
inputs = output + inputs
else:
inputs = output
next_hidden.append(next_hidden_i)
if isinstance(hidden, tuple):
next_hidden = tuple([torch.stack(h) for h in zip(*next_hidden)])
else:
next_hidden = torch.stack(next_hidden)
return inputs, next_hidden
def eval(self):
self.model.eval()
pred_result = {}
for _, batch in enumerate(self.dataloader_dev):
question_ids, questions, passages, passage_tokenized = batch
questions.variable(volatile=True)
passages.variable(volatile=True)
begin_, end_ = self.model(questions, passages) # batch x seq
_, pred_begin = torch.max(begin_, 1)
_, pred_end = torch.max(end_, 1)
pred = torch.stack([pred_begin, pred_end], dim=1)
for i, (begin, end) in enumerate(pred.cpu().data.numpy()):
ans = passage_tokenized[i][begin:end + 1]
qid = question_ids[i]
pred_result[qid] = " ".join(ans)
self.model.train()
return evaluate(self.dev_dataset, pred_result)
def prepare_message(self, target_features, source_features, select_mat, gate_module):
feature_data = []
transfer_list = np.where(select_mat > 0)
source_indices = Variable(torch.from_numpy(transfer_list[1]).type(torch.LongTensor)).cuda()
target_indices = Variable(torch.from_numpy(transfer_list[0]).type(torch.LongTensor)).cuda()
source_f = torch.index_select(source_features, 0, source_indices)
target_f = torch.index_select(target_features, 0, target_indices)
transferred_features = gate_module(target_f, source_f)
for f_id in range(target_features.size()[0]):
if len(np.where(select_mat[f_id, :] > 0)[0]) > 0:
feature_indices = np.where(transfer_list[0] == f_id)[0]
indices = Variable(torch.from_numpy(feature_indices).type(torch.LongTensor)).cuda()
features = torch.index_select(transferred_features, 0, indices).mean(0).view(-1)
feature_data.append(features)
else:
temp = Variable(torch.zeros(target_features.size()[1:]), requires_grad=True).type(torch.FloatTensor).cuda()
feature_data.append(temp)
return torch.stack(feature_data, 0)
def baseline_search(self, input, beam_size=None):
# This is the simple greedy search
batch_size = input.size(0)
hidden_feat = self.lstm_im(input.view(1, input.size()[0], input.size()[1]))[1]
x = Variable(torch.ones(1, batch_size,).type(torch.LongTensor) * self.start, requires_grad=False).cuda() # <start>
output = []
flag = torch.ones(batch_size)
for i in range(self.nseq):
input_x = self.encoder(x.view(1, -1))
output_feature, hidden_feat = self.lstm_word(input_x, hidden_feat)
output_t = self.decoder(output_feature.view(-1, output_feature.size(2)))
output_t = F.log_softmax(output_t)
logprob, x = output_t.max(1)
output.append(x)
flag[x.cpu().eq(self.end).data] = 0
if flag.sum() == 0:
break
output = torch.stack(output, 0).squeeze().transpose(0, 1).cpu().data
return output
def getHyp(self, k):
"""
Walk back to construct the full hypothesis.
Parameters.
* `k` - the position in the beam to construct.
Returns.
1. The hypothesis
2. The attention at each time step.
"""
hyp, attn = [], []
for j in range(len(self.prevKs) - 1, -1, -1):
hyp.append(self.nextYs[j+1][k])
attn.append(self.attn[j][k])
k = self.prevKs[j][k]
return hyp[::-1], torch.stack(attn[::-1])
def forward(self, input, hidden, context, init_output):
emb = self.word_lut(input)
# n.b. you can increase performance if you compute W_ih * x for all
# iterations in parallel, but that's only possible if
# self.input_feed=False
outputs = []
output = init_output
for emb_t in emb.split(1):
emb_t = emb_t.squeeze(0)
if self.input_feed:
emb_t = torch.cat([emb_t, output], 1)
output, hidden = self.rnn(emb_t, hidden)
output, attn = self.attn(output, context.t())
output = self.dropout(output)
outputs += [output]
outputs = torch.stack(outputs)
return outputs, hidden, attn
def safeCoalesce(self, t):
tc = t.coalesce()
value_map = {}
for idx, val in zip(t._indices().t(), t._values()):
idx_tup = tuple(idx)
if idx_tup in value_map:
value_map[idx_tup] += val
else:
value_map[idx_tup] = val.clone() if torch.is_tensor(val) else val
new_indices = sorted(list(value_map.keys()))
new_values = [value_map[idx] for idx in new_indices]
if t._values().ndimension() < 2:
new_values = t._values().new(new_values)
else:
new_values = torch.stack(new_values)
new_indices = t._indices().new(new_indices).t()
tg = t.new(new_indices, new_values, t.size())
self.assertEqual(tc._indices(), tg._indices())
self.assertEqual(tc._values(), tg._values())
return tg
def _forward_rnn(cell, input_, length, hx):
max_time = input_.size(0)
output = []
for time in range(max_time):
if isinstance(cell, BNLSTMCell):
h_next, c_next = cell(input_=input_[time], hx=hx, time=time)
else:
h_next, c_next = cell(input_=input_[time], hx=hx)
mask = (time < length).float().unsqueeze(1).expand_as(h_next)
h_next = h_next*mask + hx[0]*(1 - mask)
c_next = c_next*mask + hx[1]*(1 - mask)
hx_next = (h_next, c_next)
output.append(h_next)
hx = hx_next
output = torch.stack(output, 0)
return output, hx