PyTorch で学ぶ画像向け Deep Learning
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 で学ぶ画像向け Deep Learning