일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- CellPin
- docker attach
- IVI
- 티스토리챌린지
- Multi-Resolution Networks for Semantic Segmentation in Whole Slide Images
- Jupyter notebook
- 사회조사분석사2급
- cocre
- logistic regression
- aiffel exploration
- numpy
- GIT
- 코크리
- Pull Request
- airflow
- cs231n
- AIFFEL
- 백신후원
- 도커
- Decision Boundary
- HookNet
- docker
- docker exec
- 프로그래머스
- WSSS
- 오블완
- vscode
- ssh
- 기초확률론
- 히비스서커스
- Today
- Total
히비스서커스의 블로그
[error] RuntimeError: Expected object of scalar type Long but got scalar type Float when using CrossEntropyLoss (feat. RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size) 본문
[error] RuntimeError: Expected object of scalar type Long but got scalar type Float when using CrossEntropyLoss (feat. RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size)
HibisCircus 2022. 6. 14. 16:39
torch로 multi class semantic segmentation model을 학습 중이다. 원래는 DiceLoss를 사용하여 Train을 하다가 CrossEntropyLoss에 Class Weight을 주어 다시 Train하려는데 다음과 같은 에러가 발생하였다.
RuntimeError: Expected object of scalar type Long but got scalar type Float when using CrossEntropyLoss
이를 해결방법을 찾던 중 loss를 구하는 부분에서 y부분 (ground truth 부분)에
y.to(dtype=torch.long)
을 해주어 해결이 가능하다고 하여 적용하였으나 바로 다음과 같은 에러를 마주쳤다.
RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size)
이는 예전에 내가 마주쳤던 에러 중 하나인데 model의 클래스 설정과 class weight의 클래스 설정이 다를 경우 발생하는 에러다. 그런데 클래스를 같게 해주었음에도 에러가 계속 발생하였다.
https://biology-statistics-programming.tistory.com/173
다른 서버에서는 맨 처음 에러 발생도 없이 잘 Train되는 것을 보고서 이제서야 torch의 버전이 다름을 깨달았다.
shell에서 torch 버전확인 코드
$ python
>>> import torch
>>> torch.__version__
잘 돌아가는 서버의 torch 버전은 1.10.0+cu113 이었고, 잘 돌아가지 않는 서버의 torch 버전은
1.8.2+cu111 버전이었다.
간단하게 torch, torchvision, torchaudio를 밀고 다시 재설치 하였다.
$ pip uninstall torch torchvision torchaudio
torch의 cuda와 맞게 설치하려면 이쪽 페이지를 참고하길 바란다.
https://pytorch.org/get-started/locally/
$ pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
설치 후 맨 처음 코드로 실행을 하니 잘 돌아간다!
- 히비스서커스 -