본문 바로가기

pytorch

Pytorch에서 meshgrid 만들기 ''' x : forward direction y : side (left) direction ''' s, b, c, h, w = seq_z.size() h_meters = self.cfg['bev']['h_meters'] w_meters = self.cfg['bev']['w_meters'] h_grid = torch.linspace(0, h_meters, h) - (h_meters / 2) w_grid = torch.linspace(0, w_meters, w) - (w_meters / 2) grid_x, grid_y = torch.meshgrid(torch.flip(h_grid, dims=(0,)), torch.flip(w_grid, dims=(0,)), indexing='ij') grid = torch.. 더보기
einops으로 pytorch의 차원관리 예제 1. rearrange # suppose we have a set of 32 images in "h w c" format (height-width-channel) >>> images = [np.random.randn(30, 40, 3) for _ in range(32)] # stack along first (batch) axis, output is a single array >>> rearrange(images, 'b h w c -> b h w c').shape (32, 30, 40, 3) # concatenate images along height (vertical axis), 960 = 32 * 30 >>> rearrange(images, 'b h w c -> (b h) w c').shape (960.. 더보기
RTX 3090 을 지원하는 pytorch anaconda로 설치하기 anaconda에서 바로 pytorch를 설치하면 deep learning 모델 학습 시 다음과 같은 error message가 나올 수 있다. $ CUDA error: no kernel image is available for execution on the device 이때 아래와 같은 방식으로 설치를 하자. 1. anaconda 환경을 만든다. (python 3.8이상) $ conda create -y -n example python=3.8 2. pytorch를 pip로 설치한다. $ pip install torch==1.11+cu113 torchvision==0.12.0+cu113 -f https://download.pytorch.org/whl/torch_stable.html 더보기
Gaussian, LogGaussian PDF in Pytorch def GaussianPDF(mean, logvar, z): r""" Return the PDF value of z in N(mean,exp(logvar)) mean, logvar : [*, dim_z] z : [*, N, dim_z] return: [*, N, dim_z] """ if type(mean) is torch.Tensor: mean, logvar = mean.unsqueeze(-2), logvar.unsqueeze(-2) return 1/(np.sqrt(2*np.pi)*torch.exp(logvar*0.5)) * torch.exp(-((z-mean)**2) / (2*torch.exp(logvar))) elif type(mean) is np.ndarray: mean, logvar = np.ex.. 더보기
Implement KL Divergence using Pytorch def GaussianPDF(mean, logvar, z): r""" Return the PDF value of z in N(mean,exp(logvar)) mean, logvar : [*, dim_z] z : [*, N, dim_z] return: [*, N, dim_z] """ if type(mean) is torch.Tensor: mean, logvar = mean.unsqueeze(-2), logvar.unsqueeze(-2) return 1/(np.sqrt(2*np.pi)*torch.exp(logvar*0.5)) * torch.exp(-((z-mean)**2) / (2*torch.exp(logvar))) elif type(mean) is np.ndarray: mean, logvar = np.ex.. 더보기