Every line of 'pytorch tensor to numpy' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
26 def torch2numpy(X): 27 """Convert a torch float32 tensor to a numpy array, sharing the same memory. 28 """ 29 return X.detach().numpy()
29 @staticmethod 30 def to_numpy(tensor): 31 if isinstance(tensor, cp.ndarray): 32 return cp.asnumpy(tensor) 33 return tensor
396 def tensor_to_ndarray(tensor): 397 """ 398 Convert float tensor into numpy image 399 400 :param tensor: input tensor 401 :type tensor: torch.Tensor 402 :return: numpy image 403 :rtype: np.ndarray 404 """ 405 tensor_np = tensor.permute(1, 2, 0).cpu().numpy() 406 tensor_np = tensor_np.astype(np.float32) 407 tensor_np = (tensor_np * 255).astype(np.uint8) 408 return tensor_np
142 @staticmethod 143 def videos_to_numpy(tensor): 144 generated = tensor.transpose(0, 1, 2, 3, 4) 145 generated[generated < -1] = -1 146 generated[generated > 1] = 1 147 generated = (generated + 1) / 2 * 255 148 return generated.astype('uint8')
20 def to_torch(ndarray): 21 if type(ndarray).__module__ == 'numpy': 22 return torch.from_numpy(ndarray) 23 elif not torch.is_tensor(ndarray): 24 raise ValueError("Cannot convert {} to torch tensor".format(type(ndarray))) 25 return ndarray
45 def as_numpy(tensor): 46 if isinstance(tensor, list): 47 return [as_numpy(t) for t in tensor] 48 assert isinstance(tensor, core.LoDTensor) 49 lod = tensor.lod() 50 tensor_data = np.array(tensor) 51 if len(lod) == 0: 52 ans = tensor_data 53 else: 54 #raise RuntimeError("LoD Calculate lacks unit tests and buggy") 55 ans = tensor_data 56 # elif len(lod) == 1: 57 # ans = [] 58 # idx = 0 59 # while idx < len(lod) - 1: 60 # ans.append(tensor_data[lod[idx]:lod[idx + 1]]) 61 # idx += 1 62 # else: 63 # for l in reversed(lod): 64 # ans = [] 65 # idx = 0 66 # while idx < len(l) - 1: 67 # ans.append(tensor_data[l[idx]:l[idx + 1]]) 68 # idx += 1 69 # tensor_data = ans 70 # ans = tensor_data 71 return ans
32 def to_torch(array): 33 if len(array.shape) == 4: 34 array = np.transpose(array, (0, 3, 1, 2)) 35 return torch.from_numpy(array).to(device).float()
49 def transform_to_numpy_image(tensor_image): 50 image = tensor_image.cpu().detach().numpy() 51 image = np.transpose(image, (0, 2, 3, 1)) 52 if image.shape[3] != 3: 53 image = np.repeat(image, 3, axis=3) 54 else: 55 image = (image / 2 + 0.5) 56 return image
40 def from_numpy(np_data): 41 return th.from_numpy(np_data)
36 def to_torch(nparray): 37 tensor = torch.from_numpy(nparray).float().cuda() 38 return torch.autograd.Variable(tensor, requires_grad=False)