PyTorch skills
Torch skills
- create patches and transform patches back to the original one
x = torch.rand(1,1,1000,480,480)
c = x.unfold(0,1,1).unfold(1,1,1).unfold(2,250,250).unfold(3,240,240).unfold(4,240,240)
d = c.reshape(-1,1,250,240,240)
re = c.permute(0,5,1,6,2,7,3,8,4,9).contiguous().view_as(x)
- Create one-hot-encoding vector
target=target.long()
target=torch.nn.functional.one_hot(target,num_classes=3)
target=target.permute(0,5,1,2,3,4).squeeze(2)
https://www.programcreek.com/python/example/126481/torch.nn.functional.one_hot
- Reshape tensor by change dimension
permuted = aa.permute(1, 2, 0)
https://stackoverflow.com/questions/50710037/how-reshape-3d-tensor-of-shape-3-1-2-to-1-2-3
- Delete tensor dimension as 1
Torch.sequeeze
x = torch.zeros(2, 1, 2, 1, 2) >>> x.size() torch.Size([2, 1, 2, 1, 2]) >>> y = torch.squeeze(x) >>> y.size() torch.Size([2, 2, 2]) >>> y = torch.squeeze(x, 0) >>> y.size() torch.Size([2, 1, 2, 1, 2]) >>> y = torch.squeeze(x, 1) >>> y.size() torch.Size([2, 2, 1, 2])
https://pytorch.org/docs/stable/generated/torch.squeeze.html