本文整理汇总了Python中torch.autograd.Variable.permute方法的典型用法代码示例。如果您正苦于以下问题:Python Variable.permute方法的具体用法?Python Variable.permute怎么用?Python Variable.permute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.autograd.Variable
的用法示例。
在下文中一共展示了Variable.permute方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: initialize_input
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import permute [as 别名]
def initialize_input(self):
##Variables input transformed for cuda
self.batch_x=standardize(self.sample['input'])
X = Variable(self.batch_x.float())
X=X.permute(0,3,1,2).cuda() #for the model to fwd/back in pytorch always batch size x channels x height x width
return X
示例2: forward
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import permute [as 别名]
def forward(self, blobs, mode='TRAIN', killing_inds=None):
self._scene_info = blobs['data'].shape[2:]
self._id = blobs['id'][0]
self.cuda()
self.batch_size = blobs['data'].shape[0]
if mode == 'TRAIN':
self.train()
if cfg.USE_IMAGES and not cfg.USE_IMAGES_GT:
# eval of enet
self.image_enet_fixed.eval()
self.image_enet_trainable.eval()
self._mode = 'TRAIN'
self._scene = Variable(blobs['data'].cuda())
self._gt_bbox = blobs['gt_box']
self._gt_mask = blobs['gt_mask'] if cfg.USE_MASK else None
if cfg.USE_IMAGES:
grid_shape = blobs['data'].shape[-3:]
self._imageft = []
for i in range(self.batch_size):
num_images = blobs['nearest_images']['images'][i].shape[0]
if cfg.USE_IMAGES_GT:
imageft = Variable(blobs['nearest_images']['images'][i].cuda())
#imageft = imageft.expand(imageft.shape[0], 128, imageft.shape[2], imageft.shape[3]).contiguous()
else:
imageft = self.image_enet_fixed(Variable(blobs['nearest_images']['images'][i].cuda()))
imageft = self.image_enet_trainable(imageft)
proj3d = Variable(blobs['proj_ind_3d'][i].cuda())
proj2d = Variable(blobs['proj_ind_2d'][i].cuda())
# project 2d to 3d
imageft = [Projection.apply(ft, ind3d, ind2d, grid_shape) for ft, ind3d, ind2d in zip(imageft, proj3d, proj2d)]
imageft = torch.stack(imageft, dim=4)
# reshape to max pool over features
sz = imageft.shape
imageft = imageft.view(sz[0], -1, num_images)
imageft = torch.nn.MaxPool1d(kernel_size=num_images)(imageft)
imageft = imageft.view(sz[0], sz[1], sz[2], sz[3], 1)
self._imageft.append(imageft.permute(4, 0, 3, 2, 1))
self._imageft = torch.cat(self._imageft, 0)
#--------------------------
# visualization snippets
#-------------------------
#import ipdb
#ipdb.set_trace()
#data = np.where(self._scene[0,0].data.cpu().numpy() <=1.0, 1, 0)
#data = self._imageft[0]
#write_mask(data, 'data.ply')
#data = blobs['gt_box'][0].numpy()
#write_bbox(data, 'bbox.ply')
if cfg.USE_BACKBONE:
net_conv_level1, net_conv_level2, net_conv_level3 = self._backbone()
if cfg.USE_RPN:
# build the anchors for the scene
if cfg.FIRST_TIME_ANCHORS:
cfg.FIRST_TIME_ANCHORS = False
# build the anchors for the scene
if cfg.NUM_ANCHORS_LEVEL1 != 0:
size_level1 = [net_conv_level1.size(2), net_conv_level1.size(3), net_conv_level1.size(4)]
if cfg.NUM_ANCHORS_LEVEL2 != 0:
size_level2 = [net_conv_level2.size(2), net_conv_level2.size(3), net_conv_level2.size(4)]
if cfg.NUM_ANCHORS_LEVEL3 != 0:
size_level3 = [net_conv_level3.size(2), net_conv_level3.size(3), net_conv_level3.size(4)]
self._anchor_component(size_level1 if cfg.NUM_ANCHORS_LEVEL1 !=0 else [],
size_level2 if cfg.NUM_ANCHORS_LEVEL2 !=0 else [],
size_level3 if cfg.NUM_ANCHORS_LEVEL3 !=0 else [])
self._region_proposal(net_conv_level1, net_conv_level2, net_conv_level3)
else:
# only predictions['rois']/['roi_scores']/['mask_pred'] batch is a list, since not even number/dim in each sample
self._predictions['rois'] = [self._gt_bbox[i][:,:6].cuda() for i in range(self.batch_size)]
self._predictions['roi_scores'] = [torch.ones(self._gt_bbox[i].size(0), 1).cuda() for i in range(self.batch_size)]
if cfg.USE_CLASS:
self._proposal_target_layer(self._predictions['rois'], self._predictions['roi_scores'], self._predictions['level_inds'])
pool5 = self._roi_pool_layer(net_conv_level1, net_conv_level2, net_conv_level3,
self._proposal_targets['rois'], self._proposal_targets['levelInds'],
self._feat_stride, cfg.CLASS_POOLING_SIZE)
fc7 = self._classifier(pool5)
self._region_classification(fc7)
else:
self._predictions["cls_pred"] = Variable(self._gt_bbox[0][:,6].long())
self._predictions["cls_prob"] = Variable(torch.zeros((self._predictions['cls_pred'].shape[0], cfg.NUM_CLASSES)))
self._predictions["bbox_pred"] = Variable(torch.zeros((self._predictions['cls_pred'].shape[0], cfg.NUM_CLASSES*6)))
for ind_sample in range(self._predictions['cls_pred'].shape[0]):
self._predictions['cls_prob'][ind_sample, self._predictions['cls_pred'].data[ind_sample]] = 1.0
if cfg.USE_MASK:
self._mask_target_layer(self._predictions['rois'])
mask_pred_batch = []
for i in range(self.batch_size):
#.........这里部分代码省略.........
示例3: test_permute2
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import permute [as 别名]
def test_permute2(self):
x = Variable(torch.Tensor([[[[[[0]]]]]]), requires_grad=True)
self.assertONNX(lambda x: x.permute(0, 1, 4, 2, 5, 3), x)
示例4: forward
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import permute [as 别名]
def forward(self, x, dist):
[N, T, M, C, H, W] = x.shape
base_out = self.base_model(x.view(-1, C, H, W)).view(N*T, M, -1)
# print(np.where(base_out>100))
# base_out_c.detach()
# base_out_c.no_grad()
# print("base_out", base_out[0][0])
# print("base_out", base_out[0][1])
# x = x.view(-1, C, H, W)
# dist = torch.zeros(N*T, M, M)
# for i in np.arange(N*T):
# for j in np.arange(M):
# for k in np.arange(j+1,M):
# # print("base_out", base_out[i][j])
# # print("base_out", base_out[i][k])
# dist[i,j,k] = F.pairwise_distance(base_out[i][j][:].unsqueeze(0), \
# base_out[i][k][:].unsqueeze(0))
# dist[i,k,j] = dist[i,j,k]
#
# dist_des, dist_index = dist[i].sort(1,descending=True)
# for l in np.arange(M):
# dist[i][l][dist_index[:, 3:][l]] = 0
# dist[i][l][dist_index[:, :3][l]] = 0.25
#
# # print("dist",dist[i])
# dist[i] += torch.eye(M)*0.25
# dist[i] = self.normalize_digraph(dist[i].unsqueeze(0).cuda())
# # print("dist",dist[0])
with torch.no_grad():
base_out = Variable(base_out)
node1 = self.conv1da(base_out.permute(0,2,1)).cuda()
node1 = torch.bmm(dist.view(-1,12,12).float(), node1.permute(0,2,1))
node1 = F.relu(node1)
nodelinear = self.convLinear(node1.permute(0,2,1))
pooled_feat = self.pool(nodelinear).squeeze(2)
group_out = self.avg_pool(pooled_feat.view(N, -1, T))
# gcn_out = torch.zeros(N*T, 8).cuda()
# dist_num = dist_num.view(-1)
# dist = dist.view(-1, 12, 12)
# x = x.view(-1, M, C, H, W)
# for i in range(N*T):
# base_out = self.base_model(x[i, :dist_num[i]])
# with torch.no_grad():
# base_out = Variable(base_out)
# node1 = self.conv1da(base_out[:dist_num.view(-1)[i]].unsqueeze(0).permute(0,2,1).contiguous())
# node1 = torch.bmm(dist[i, :dist_num[i], :dist_num[i]].unsqueeze(0).float(), \
# node1.permute(0,2,1).contiguous())
# node1 = F.relu(node1)
# nodeLinear = self.convLinear(node1.permute(0,2,1).contiguous())
# pooled_feat = self.pool(nodeLinear).squeeze(2)
# gcn_out[i] = pooled_feat
#
# group_out = self.avg_pool(gcn_out.view(N, -1, T))
#normalize
# node1 = self.conv1da(base_out.permute(0,2,1).contiguous()).permute(0,2,1).contiguous()
# node1 = torch.bmm(dist.squeeze(0)[::2,:,:].float(), node1).permute(0,2,1).contiguous()
# node1 = F.relu(node1)
# node2 = self.conv1db(node1).permute(0,2,1).contiguous()
# node2 = torch.bmm(dist.squeeze(0)[::2,:,:].float(), node2).permute(0,2,1).contiguous()
# node2 = F.relu(node2)
#
# nodeLinear = self.convLinear(node2)
#
# pooled_feat = self.pool(nodeLinear).squeeze(2).view(N, T, -1)
# group_out = self.avg_pool(pooled_feat.view(N,-1,T))
# print("gcn", gcn_out.shape)
# group_out, _ = self.lstm(gcn_out.view(N, T, -1))
# # print("group_out", group_out.squeeze(2).squeeze(0))
# # group_cls_out = self.linear(group_out[:,-1,:])
# # group_cls_out = self.linear(group_out.squeeze(2))
# print(group_out.shape)
# return group_out[:,-1,:]
return group_out.squeeze(2)
示例5: __init__
# 需要导入模块: from torch.autograd import Variable [as 别名]
# 或者: from torch.autograd.Variable import permute [as 别名]
#.........这里部分代码省略.........
if set_new:
current_idx = 0
for idx, param in enumerate(self.trainable_params):
vals = new_params[current_idx:current_idx + self.param_sizes[idx]]
vals = vals.reshape(self.param_shapes[idx])
param.data = torch.from_numpy(vals).float()
current_idx += self.param_sizes[idx]
# clip std at minimum value
self.trainable_params[-1].data = \
torch.clamp(self.trainable_params[-1], self.min_log_std).data
# update log_std_val for sampling
self.log_std_val = np.float64(self.log_std.cpu().data.numpy().ravel())
if set_old:
current_idx = 0
for idx, param in enumerate(self.old_params):
vals = new_params[current_idx:current_idx + self.param_sizes[idx]]
vals = vals.reshape(self.param_shapes[idx])
param.data = torch.from_numpy(vals).float()
current_idx += self.param_sizes[idx]
# clip std at minimum value
self.old_params[-1].data = \
torch.clamp(self.old_params[-1], self.min_log_std).data
# Main functions
# ============================================
def get_action(self, observation, use_seq=True, use_cuda=False, robot_info=None):
bin = self.num_bins
o = observation.copy()
o = np.expand_dims(o, axis=0)
if robot_info is not None:
robot_info = np.expand_dims(robot_info, 0)
prem_shape = (0, 1, 4, 2, 3) if use_seq else (0, 3, 1, 2)
try:
self.obs_var.data = torch.from_numpy(o).permute(prem_shape).float()
if robot_info is not None:
self.robot_info = torch.from_numpy(robot_info).float()
if use_cuda:
self.obs_var.data = torch.from_numpy(o).permute(prem_shape).float().cuda()
if robot_info is not None:
self.robot_info.data = torch.from_numpy(robot_info).float().cuda()
except:
self.obs_var = Variable(torch.from_numpy(o).float(), requires_grad=False)
if robot_info is not None:
self.robot_info = Variable(torch.from_numpy(robot_info).float(), requires_grad=False)
if use_cuda:
self.obs_var = Variable(torch.from_numpy(o).float().cuda(), requires_grad=False)
if robot_info is not None:
self.robot_info = Variable(torch.from_numpy(robot_info).float().cuda(), requires_grad=False)
self.obs_var = self.obs_var.permute(prem_shape)
if bin == 0:
mean = self.model.forward(self.obs_var, robot_info=self.robot_info).cpu().data.numpy().ravel()
return [mean, {'mean': mean, 'log_std': self.log_std_val, 'evaluation': mean}]
else:
action = self.model(self.obs_var).cpu().data.numpy().ravel()
action = action.reshape((-1, bin))
action_bin = np.zeros(action.shape)
action = multilabel_bin_to_action(np.argmax(action_bin, axis=1), self.num_bins)
return [action, {'mean': action, 'log_std': self.log_std_val, 'evaluation': action}]
def softmax(self, x):
"""Compute softmax values for each sets of scores in x."""
return np.exp(x) / np.sum(np.exp(x), axis=1)
def mean_LL(self, img_pixels, actions, model=None, log_std=None, robot_info=None):
model = self.model if model is None else model