라이브러리/PyTorch
-
Multi-gpu에서 학습된 모델의 weights를 Single-gpu (or CPU)에서 불러오기라이브러리/PyTorch 2022. 9. 12. 03:38
model = torchvision.models.segmentation.__dict__['deeplabv3_resnet50'](pretrained=False, aux_loss=False) device = torch.device("cpu") model.classifier[-1] = torch.nn.Conv2d(model.classifier[-1].in_channels, 1, kernel_size=model.classifier[-1].kernel_size) # change number of outputs to 1 model.to(device) # checkpoint = torch.load("output/segmentation/deeplabv3_resnet50_random/best.pt", map_locati..
-
Don't forget to accumulate the Gradient라이브러리/PyTorch 2022. 4. 26. 11:24
ViT와 같은 self-Attention based model 또는 CNN을 Group Normalization + Weight Standardization (Batch Normalization 대신)와 사용중이라면 gradient를 accumulate하는 것을 잊지마세요! Batch Normalization이 사용되고 있을 땐 gradient accumulation이 효과적이지 않을 지도 모르지만, 위와 같은 모델을 사용중이고 GPU VRAM이 한정적이여서 배치 사이즈를 작게 세팅하는 경우 효과적일 거야. AMP+Gradient accumulation은 더 효과적! from torch.cuda import amp scaler = amp.GradScaler() n_accumulate = 16 # n_ac..
-
gc.collect, torch.cuda.empty_cache()라이브러리/PyTorch 2022. 4. 25. 23:53
gc.collect: Forces an immediate garbage collection of all generations torch.cuda.empty_cache(): Releases all unoccupied cached memory currently held by the caching allocator so that those can be used in other GPU application and visible in nvidia-smi
-
Tensor moduels라이브러리/PyTorch 2022. 3. 7. 14:18
Tensor moduels tensor는 NumPy arrays와 컨셉적으로 유사하다. tensorsms n-차원 배열로 수학적 함수를 적용하고, GPU를 이용해 계산을 가속화 할 수 있으며, computational graph와 gradients 를 추적하는데 사용 될 수 있다. PyTorch에서 tneosrs는 연속적인 메모리 덩어리(contiguous chunks of memory)에 저장된 수치 데이터의 1차원 배열에 대한 views로 구현된다. 이런 배열은 stroage instances라 불린다. 모든 PyTorch tensor는 storage 속성을 갖고 있는데, 이를 호출함으로써 기본 storage 인스턴스(the underlying storage instance)를 출력할 수 있다. po..
-
torch.utils.data라이브러리/PyTorch 2022. 3. 7. 14:04
torch.utils.data torch.utils.data module하에는 torch가 제공하는 여러 데이터셋과 DataLoader class를 제공하는데, 이는 추상화와 유연하게 구현하는데 도움을 준다. torch.utils.data.DataLoader 예시는 다음과 같다. form torch.utils.data import TensorDataset, DataLoader train_ds = TensorDataset(X_train, y_train) train_dl = DataLoader(train_ds, batch_size=bs) for x_batch, y_batch in train_dl: pred = model(x_batch) ###### # 다음과 같이 batch를 iterate할 필요 없다. f..
-
torch.optim라이브러리/PyTorch 2022. 3. 7. 14:00
torch.optim back-progate 함으로써 NN의 weight를 업데이트 하는 과정을 optimization이라 한다. torch.optim module은 optimization schedule 에 대한 다양한 툴과 기능을 포함한다. 다음과 같이 torch.optim module을 이용해 optimizer를 정의할 수 있다. import torch opt = torch.optim.SGD(model.parameters(), lr=lr) 그리고나서 optimization 수행을 다음과 같이 하면 된다. opt.step() opt.zero_grad() ##### 다음과 같이 할 필요 없다. with torch.no_grad(): # applying the parameter updates uisng ..