def encode(self, x, no_grad=True, use_eval=False):
     '''
     Encode the images in x, with or without grads detached.
     '''
     if use_eval:
         self.eval()
     x = maybe_half(x)
     if no_grad:
         with torch.no_grad():
             rkhs_1, rkhs_5, rkhs_7 = self.encoder(x)
     else:
         rkhs_1, rkhs_5, rkhs_7 = self.encoder(x)
     if use_eval:
         self.train()
     return maybe_half(rkhs_1), maybe_half(rkhs_5), maybe_half(rkhs_7)
示例#2
0
 def encode(self, x, no_grad=True, use_eval=False):
     '''
     Encode the images in x, with or without grads detached.
     '''
     if use_eval:
         self.eval()
     x = maybe_half(x)
     if no_grad:
         with torch.no_grad():
             rkhs_1, rkhs_5, rkhs_7 = self.encoder(x) # Nawid- Obtain the embeddings, rkhs_1 (normalised feature vector), rkhs_5 (an index of the 5x5 feature vector which is transformed to the global _feature vector version) and rkhs_7 which is an part of the 7x7 feature vector which is transformed to a global feature vector version
     else:
         rkhs_1, rkhs_5, rkhs_7 = self.encoder(x) #Nawid -Encodes the image
     if use_eval:
         self.train()
     return maybe_half(rkhs_1), maybe_half(rkhs_5), maybe_half(rkhs_7)
示例#3
0
def random_locs_2d(x, k_hot=1):
    '''
    Sample a k-hot mask over spatial locations for each set of conv features
    in x, where x.shape is like (n_batch, n_feat, n_x, n_y).
    '''
    # assume x is (n_batch, n_feat, n_x, n_y)
    x_size = x.size()
    n_batch = x_size[0]
    n_locs = x_size[2] * x_size[3] # Nawid - Number of positions is the height * width
    idx_topk = torch.topk(torch.rand((n_batch, n_locs)), k=k_hot, dim=1)[1] # Nawid- In this case, k is equal to 1, top k gives the k largest elements and indices of an index tensor along a given dimension. The dimension in this case 1 which is the dimension of the different n locations. since [1] is chosen it obtains the indices whilst [0] would give the actual elements
    khot_mask = torch.zeros((n_batch, n_locs)).scatter_(1, idx_topk, 1.)  # Nawid - This puts (along dimension 1 which is dimension related to the location), it places a value of 1 at the indices specified by idx_topk
    rand_locs = khot_mask.reshape((n_batch, 1, x_size[2], x_size[3]))
    rand_locs = maybe_half(rand_locs) # Nawid - Change the prevision
    return rand_locs
def random_locs_2d(x, k_hot=1):
    '''
    Sample a k-hot mask over spatial locations for each set of conv features
    in x, where x.shape is like (n_batch, n_feat, n_x, n_y).
    '''
    # assume x is (n_batch, n_feat, n_x, n_y)
    x_size = x.size()
    n_batch = x_size[0]
    n_locs = x_size[2] * x_size[3]
    idx_topk = torch.topk(torch.rand((n_batch, n_locs)), k=k_hot, dim=1)[1]
    khot_mask = torch.zeros((n_batch, n_locs)).scatter_(1, idx_topk, 1.)
    rand_locs = khot_mask.reshape((n_batch, 1, x_size[2], x_size[3]))
    rand_locs = maybe_half(rand_locs)
    return rand_locs