PyTorch로 배우는 이미지 딥러닝
Michal Oleszak
Machine Learning Engineer
처음부터 모델 학습:
사전 학습 모델 - 이미 특정 작업에 학습된 모델
사전 학습 모델 활용 단계:
torchvision 모델 다운로드torch.save().pt 또는 .pth.state_dict()로 가중치 저장torch.save(model.state_dict(), "BinaryCNN.pth")
새 모델 인스턴스화
new_model = BinaryCNN()
저장한 파라미터 로드
new_model.load_state_dict(torch.load('BinaryCNN.pth'))
from torchvision.models import ( resnet18, ResNet18_Weights )weights = ResNet18_Weights.DEFAULTmodel = resnet18(weights=weights)transforms = weights.transforms()
resnet 구조와 가중치 임포트from PIL import Image image = Image.open("cat013.jpg")image_tensor = transform(image)image_reshaped = image_tensors.unsqueeze(0)

model.eval()with torch.no_grad():pred = model(image_reshaped).squeeze(0)pred_cls = pred.softmax(0)cls_id = pred_cls.argmax().item()cls_name = weights.meta["categories"][cls_id]print(cls_name)
Egyptian cat
PyTorch로 배우는 이미지 딥러닝